def append_childs(source_node, destination_node): """Recursive function to discover node structure from a story to another. """ for elt in source_node.iterchildren(): if not elt.tag == "XMLElement": append_childs(elt, destination_node) if elt.get("Self") == source_node.get("Self"): continue if not elt.get("MarkupTag"): continue elt = XMLElement(elt) new_destination_node = elt.to_xml_structure_element() destination_node.append(new_destination_node) if elt.get("XMLContent"): xml_content_value = elt.get("XMLContent") story_name = "Stories/Story_%s.xml" % xml_content_value story = Story(self, name=story_name) try: new_source_node = story.get_element_by_id(elt.get("Self")) # The story does not exists. except KeyError: continue else: append_childs(new_source_node, new_destination_node) else: append_childs(elt, new_destination_node)
def append_childs(source_node, destination_node): """Recursive function to discover node structure from a story to another. """ for elt in source_node.iterchildren(): if not elt.tag == "XMLElement": append_childs(elt, destination_node) if elt.get("Self") == source_node.get("Self"): continue if not elt.get("MarkupTag"): continue elt = XMLElement(elt) new_destination_node = elt.to_xml_structure_element() destination_node.append(new_destination_node) if elt.get("XMLContent"): xml_content_value = elt.get("XMLContent") story_name = "Stories/Story_%s.xml" % xml_content_value story = Story(self, name=story_name) try: new_source_node = story.get_element_by_id( elt.get("Self")) # The story does not exists. except KeyError: continue else: append_childs(new_source_node, new_destination_node) else: append_childs(elt, new_destination_node)
def _import_new_node(source_node, at=None, element_id=None, story=None): xml_structure_parent_node = self.xml_structure.find("*//*[@Self='%s']" % element_id) xml_structure_new_node = etree.Element(source_node.tag) # We cannot force the self._xml_structure reset by setting it at None. xml_structure_parent_node.append(xml_structure_new_node) style_range_node, applied_style_node = _get_nested_style_range_node(xml_structure_new_node) story = story or self.get_story_object_by_xpath(at) parent = story.get_element_by_id(element_id) _apply_parent_style_range(style_range_node, applied_style_node, parent) new_xml_element = XMLElement(tag=source_node.tag) new_xml_element.add_content(source_node.text, parent, style_range_node) story.add_element(element_id, new_xml_element.element) xml_structure_new_node.set("Self", new_xml_element.get("Self")) # Source may also contains some children. source_node_children = source_node.getchildren() if len(source_node_children): story.synchronize() for j, source_node_child in enumerate(source_node_children): _import_new_node(source_node_child, element_id=new_xml_element.get("Self"), story=story) if source_node.tail: story.add_content_to_element(element_id, source_node.tail, parent) story.synchronize()
def _import_new_node(source_node, at=None, element_id=None, story=None): xml_structure_parent_node = self.xml_structure.find( "*//*[@Self='%s']" % element_id) xml_structure_new_node = etree.Element(source_node.tag) # We cannot force the self._xml_structure reset by setting it at None. xml_structure_parent_node.append(xml_structure_new_node) style_range_node, applied_style_node = _get_nested_style_range_node( xml_structure_new_node) story = story or self.get_story_object_by_xpath(at) parent = story.get_element_by_id(element_id) _apply_parent_style_range(style_range_node, applied_style_node, parent) new_xml_element = XMLElement(tag=source_node.tag) new_xml_element.add_content(source_node.text, parent, style_range_node) story.add_element(element_id, new_xml_element.element) xml_structure_new_node.set("Self", new_xml_element.get("Self")) # Source may also contains some children. source_node_children = source_node.getchildren() if len(source_node_children): story.synchronize() for j, source_node_child in enumerate(source_node_children): _import_new_node(source_node_child, element_id=new_xml_element.get("Self"), story=story) if source_node.tail: story.add_content_to_element(element_id, source_node.tail, parent) story.synchronize()