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 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 test_repr(self): node = etree.fromstring('<XMLElement Self="di3i4i1" MarkupTag="XMLTag/main_picture" XMLContent="u143" />') elt = XMLElement(node) self.assertEqual( repr(elt), '<Element XMLElement at %s> {Self: di3i4i1, MarkupTag: XMLTag/main_picture, XMLContent: u143}' % hex(id(elt.element)) )
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 test_get_character_style_range(self): elt = XMLElement(etree.fromstring(""" <XMLElement Self="di3i4i1i2i2i2" MarkupTag="XMLTag/texte"> <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/MyFancyStyle" FontStyle="Semibold" PointSize="9" HorizontalScale="90" Tracking="-30"> <Properties> <Leading type="unit">10</Leading> <AppliedFont type="string">Adobe Garamond</AppliedFont> </Properties> <Content>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </Content> </CharacterStyleRange> <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]" FontStyle="Regular" PointSize="9" HorizontalScale="90" Tracking="-30"> <Properties> <Leading type="unit">10</Leading> <AppliedFont type="string">Adobe Garamond</AppliedFont> </Properties> <Content>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum d</Content> </CharacterStyleRange> </XMLElement>""")) self.assertEqual(elt.get_character_style_range().get("AppliedCharacterStyle"), "CharacterStyle/$ID/MyFancyStyle") elt = XMLElement(etree.fromstring(""" <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/MyOtherStyle" FontStyle="Regular" PointSize="9" HorizontalScale="90" Tracking="-30"> <Properties> <Leading type="unit">10</Leading> <AppliedFont type="string">Adobe Garamond</AppliedFont> </Properties> <Content></Content> <XMLElement Self="di3i9i1i2" MarkupTag="XMLTag/texte"> <Content>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</Content> </XMLElement> <Br/> <Content>Prix : </Content> </CharacterStyleRange> """).find("XMLElement")) self.assertEqual(elt.get_character_style_range().get("AppliedCharacterStyle"), "CharacterStyle/$ID/MyOtherStyle")
def test_get_character_style_range(self): elt = XMLElement( etree.fromstring(""" <XMLElement Self="di3i4i1i2i2i2" MarkupTag="XMLTag/texte"> <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/MyFancyStyle" FontStyle="Semibold" PointSize="9" HorizontalScale="90" Tracking="-30"> <Properties> <Leading type="unit">10</Leading> <AppliedFont type="string">Adobe Garamond</AppliedFont> </Properties> <Content>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </Content> </CharacterStyleRange> <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]" FontStyle="Regular" PointSize="9" HorizontalScale="90" Tracking="-30"> <Properties> <Leading type="unit">10</Leading> <AppliedFont type="string">Adobe Garamond</AppliedFont> </Properties> <Content>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum d</Content> </CharacterStyleRange> </XMLElement>""")) self.assertEqual( elt.get_character_style_range().get("AppliedCharacterStyle"), "CharacterStyle/$ID/MyFancyStyle") elt = XMLElement( etree.fromstring(""" <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/MyOtherStyle" FontStyle="Regular" PointSize="9" HorizontalScale="90" Tracking="-30"> <Properties> <Leading type="unit">10</Leading> <AppliedFont type="string">Adobe Garamond</AppliedFont> </Properties> <Content></Content> <XMLElement Self="di3i9i1i2" MarkupTag="XMLTag/texte"> <Content>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</Content> </XMLElement> <Br/> <Content>Prix : </Content> </CharacterStyleRange> """).find("XMLElement")) self.assertEqual( elt.get_character_style_range().get("AppliedCharacterStyle"), "CharacterStyle/$ID/MyOtherStyle")
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 test_attributes(self): dom = etree.fromstring( """<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <idPkg:Story xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging" DOMVersion="7.5"> <Story Self="u10d"> <XMLElement Self="di3i4" MarkupTag="XMLTag/module" XMLContent="u10d"> <ParagraphStyleRange> <CharacterStyleRange> <XMLElement Self="di3i4i1" MarkupTag="XMLTag/main_picture" XMLContent="u143"> <XMLAttribute Self="di3i4i1XMLAttributenhref" Name="href" Value="file:///piscine.jpg"/> <XMLAttribute Self="di3i4i1XMLAttributenbar" Name="bar" Value="baz"/> </XMLElement> <XMLElement Self="di3i4i2" MarkupTag="XMLTag/headline" XMLContent="ue1"/> <XMLElement Self="di3i4i3" MarkupTag="XMLTag/Story" XMLContent="uf7"/> </CharacterStyleRange> </ParagraphStyleRange> </XMLElement> </Story> </idPkg:Story>""") # Getter. module_node = dom.xpath(".//XMLElement[@Self='di3i4']")[0] module_elt = XMLElement(module_node) self.assertEqual(module_elt.get_attribute("foo"), None) self.assertEqual(module_elt.get_attribute("href"), None) self.assertEqual(module_elt.get_attribute("bar"), None) picture_node = dom.xpath(".//XMLElement[@Self='di3i4i1']")[0] picture_elt = XMLElement(picture_node) self.assertEqual(picture_elt.get_attribute("foo"), None) self.assertEqual(picture_elt.get_attribute("href"), "file:///piscine.jpg") self.assertEqual(picture_elt.get_attribute("bar"), "baz") # Get all attributes (similar to Element.items()). self.assertEqual(picture_elt.get_attributes(), { 'href': 'file:///piscine.jpg', 'bar': 'baz' }) # Setter. module_elt.set_attribute("foo", "bar") self.assertEqual(module_elt.get_attribute("foo"), "bar") picture_elt.set_attribute("href", "file:///jardin.jpg") self.assertEqual(picture_elt.get_attribute("href"), "file:///jardin.jpg") picture_elt.set_attribute("bar", "hello") self.assertEqual(picture_elt.get_attribute("bar"), "hello") # Set multiples attributes at once. picture_elt.set_attributes({ "href": "file:///maison.jpg", "style": "fancy" }) self.assertEqual(picture_elt.get_attribute("href"), "file:///maison.jpg") self.assertEqual(picture_elt.get_attribute("style"), "fancy")
def test_attributes(self): dom = etree.fromstring(b"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <idPkg:Story xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging" DOMVersion="7.5"> <Story Self="u10d"> <XMLElement Self="di3i4" MarkupTag="XMLTag/module" XMLContent="u10d"> <ParagraphStyleRange> <CharacterStyleRange> <XMLElement Self="di3i4i1" MarkupTag="XMLTag/main_picture" XMLContent="u143"> <XMLAttribute Self="di3i4i1XMLAttributenhref" Name="href" Value="file:///piscine.jpg"/> <XMLAttribute Self="di3i4i1XMLAttributenbar" Name="bar" Value="baz"/> </XMLElement> <XMLElement Self="di3i4i2" MarkupTag="XMLTag/headline" XMLContent="ue1"/> <XMLElement Self="di3i4i3" MarkupTag="XMLTag/Story" XMLContent="uf7"/> </CharacterStyleRange> </ParagraphStyleRange> </XMLElement> </Story> </idPkg:Story>""") # Getter. module_node = dom.xpath(".//XMLElement[@Self='di3i4']")[0] module_elt = XMLElement(module_node) self.assertEqual(module_elt.get_attribute("foo"), None) self.assertEqual(module_elt.get_attribute("href"), None) self.assertEqual(module_elt.get_attribute("bar"), None) picture_node = dom.xpath(".//XMLElement[@Self='di3i4i1']")[0] picture_elt = XMLElement(picture_node) self.assertEqual(picture_elt.get_attribute("foo"), None) self.assertEqual(picture_elt.get_attribute("href"), "file:///piscine.jpg") self.assertEqual(picture_elt.get_attribute("bar"), "baz") # Get all attributes (similar to Element.items()). self.assertEqual(picture_elt.get_attributes(), {'href': 'file:///piscine.jpg', 'bar': 'baz'}) # Setter. module_elt.set_attribute("foo", "bar") self.assertEqual(module_elt.get_attribute("foo"), "bar") picture_elt.set_attribute("href", "file:///jardin.jpg") self.assertEqual(picture_elt.get_attribute("href"), "file:///jardin.jpg") picture_elt.set_attribute("bar", "hello") self.assertEqual(picture_elt.get_attribute("bar"), "hello") # Set multiples attributes at once. picture_elt.set_attributes({"href": "file:///maison.jpg", "style": "fancy"}) self.assertEqual(picture_elt.get_attribute("href"), "file:///maison.jpg") self.assertEqual(picture_elt.get_attribute("style"), "fancy")