Exemplo n.º 1
0
 def test_layer_nodes(self):
     idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "4-pages.idml"),
                             mode="r")
     designmap = idml_file.designmap
     self.assertEqual(len(designmap.layer_nodes), 1)
     self.assertEqual(designmap.layer_nodes[0].get("Name"), 'Layer 1')
     idml_file.close()
Exemplo n.º 2
0
 def test_spread_nodes(self):
     idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "4-pages-layers-with-guides.idml"), mode="r")
     designmap = idml_file.designmap
     self.assertEqual([etree_dom_to_tree(n, True) for n in designmap.spread_nodes], [
         {
             'attrs': {'src': 'Spreads/Spread_ud8.xml'},
             'content': [],
             'tag': '{http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging}Spread',
             'tail': '',
             'text': None
         },
         {
             'attrs': {'src': 'Spreads/Spread_u13b.xml'},
             'content': [],
             'tag': '{http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging}Spread',
             'tail': '',
             'text': None
         },
         {
             'attrs': {'src': 'Spreads/Spread_u142.xml'},
             'content': [],
             'tag': '{http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging}Spread',
             'tail': '',
             'text': None
         }
     ])
     idml_file.close()
Exemplo n.º 3
0
 def test_suffix_layers(self):
     idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "4-pages.idml"), mode="r")
     designmap = idml_file.designmap
     self.assertEqual(designmap.layer_nodes[0].get("Name"), 'Layer 1')
     designmap.suffix_layers(" #66")
     self.assertEqual(designmap.layer_nodes[0].get("Name"), 'Layer 1 #66')
     idml_file.close()
Exemplo n.º 4
0
 def test_repr(self):
     idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "4-pages.idml"),
                             mode="r")
     designmap = idml_file.designmap
     self.assertEqual(
         repr(designmap),
         '<Designmap object designmap.xml at %s>' % hex(id(designmap)))
     idml_file.close()
Exemplo n.º 5
0
    def test_active_layer(self):
        idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "4-pages-layers-with-guides.idml"), mode="r")
        designmap = idml_file.designmap
        self.assertEqual(designmap.active_layer, "u2db")

        designmap.active_layer = "ua4"
        self.assertEqual(designmap.active_layer, "ua4")

        del designmap.active_layer
        self.assertEqual(designmap.active_layer, None)
        idml_file.close()
Exemplo n.º 6
0
def create_idml_package_from_dir(dir_path, package_path=None):
    if os.path.exists(package_path):
        print "%s already exists." % package_path
        return None

    package = IDMLPackage(package_path, mode="w")

    for root, dirs, filenames in os.walk(dir_path):
        for filename in filenames:
            package.write(os.path.join(root, filename),
                          os.path.join(root.replace(dir_path, "."), filename))
    return package
Exemplo n.º 7
0
 def test_character_style_mapping(self):
     idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "article-1photo_import-xml.idml"), mode="r")
     style_mapping = StyleMapping(idml_file)
     self.assertEqual(style_mapping.character_style_mapping,
                      {'italique': 'CharacterStyle/italique',
                       'bold': 'CharacterStyle/bold',
                       'sup': 'CharacterStyle/sup'})
Exemplo n.º 8
0
    def test_styles(self):
        idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "article-1photo_import-xml.idml"), mode="r")
        style_mapping = StyleMapping(idml_file)
        self.assertEqual([line.strip() for line in style_mapping.tostring().split(b"\n")], [
            b"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>",
            b'<idPkg:Mapping xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging" DOMVersion="10.0">',
            b'<XMLImportMap Self="did2" MarkupTag="XMLTag/bold" MappedStyle="CharacterStyle/bold"/>',
            b'<XMLImportMap Self="di13f" MarkupTag="XMLTag/italique" MappedStyle="CharacterStyle/italique"/>',
            b'<XMLImportMap Self="di141" MarkupTag="XMLTag/sup" MappedStyle="CharacterStyle/sup"/>',
            b'</idPkg:Mapping>',
            b''
        ])

        # The XML/Mapping.xml may not be present.
        idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "4-pages.idml"), mode="r")
        style_mapping = StyleMapping(idml_file)
Exemplo n.º 9
0
    def test_page_items(self):
        idml_file = IDMLPackage(os.path.join(
            IDMLFILES_DIR, "magazineA-courrier-des-lecteurs-3pages.idml"),
                                mode="r")
        spread = Spread(idml_file, idml_file.spreads[1])

        page1 = spread.pages[0]
        self.assertEqual([i.tag for i in page1.page_items], ["Rectangle"])

        page2 = spread.pages[1]
        self.assertEqual([i.tag for i in page2.page_items], [
            'Rectangle',
            'TextFrame',
            'Polygon',
            'Polygon',
            'Polygon',
            'GraphicLine',
            'Polygon',
            'Polygon',
            'Oval',
            'Rectangle',
        ])

        # test the setter
        page2.page_items = ["foo", "bar"]
        self.assertEqual(page2.page_items, ["foo", "bar"])
Exemplo n.º 10
0
    def test_geometric_bounds(self):
        idml_file = IDMLPackage(os.path.join(
            IDMLFILES_DIR, "magazineA-courrier-des-lecteurs-3pages.idml"),
                                mode="r")
        spread = Spread(idml_file, idml_file.spreads[1])

        page3 = spread.pages[1]
        self.assertEqual(page3.geometric_bounds, [
            Decimal('0'),
            Decimal('0'),
            Decimal('759.6850393700788'),
            Decimal('566.9291338582677')
        ])

        page3.geometric_bounds = [
            Decimal('210'),
            Decimal('297'),
            Decimal('10.51'),
            Decimal('7.23')
        ]
        self.assertEqual(page3.geometric_bounds, [
            Decimal('210'),
            Decimal('297'),
            Decimal('10.51'),
            Decimal('7.23')
        ])
Exemplo n.º 11
0
    def test_set_face(self):
        idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "magazineA-courrier-des-lecteurs.idml"), mode="r")
        spread2 = Spread(idml_file, idml_file.spreads[1])
        page2 = spread2.pages[0]
        self.assertEqual(page2.face, VERSO)

        page2.set_face(RECTO)
        self.assertEqual(page2.face, RECTO)
Exemplo n.º 12
0
    def test_has_any_item_on_layer(self):
        idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "4-pages-layers-with-guides.idml"), mode="r")
        spreads = idml_file.spreads

        # Spread_ud8.xml
        spread1 = Spread(idml_file, spreads[0])
        self.assertFalse(spread1.has_any_item_on_layer("unknown_layer"))
        self.assertTrue(spread1.has_any_item_on_layer("u2db"))
Exemplo n.º 13
0
    def test_get_element_by_id(self):
        idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "4-pages.idml"), mode="r")
        stories = idml_file.stories
        story = Story(idml_file, stories[1])  # u11b
        elem = story.get_element_by_id("di2i3i2")
        self.assertEqual(elem.get("MarkupTag"), "XMLTag/content")

        elem = story.get_element_by_id("di2i3i2", tag="*")
        self.assertEqual(elem.get("MarkupTag"), "XMLTag/content")
Exemplo n.º 14
0
    def test_has_any_guide_on_layer(self):
        # Package with 2 layers, each one having guides.
        idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "4-pages-layers-with-guides.idml"), mode="r")
        spreads = idml_file.spreads

        # Spread_ud8.xml
        spread1 = Spread(idml_file, spreads[0])
        self.assertFalse(spread1.has_any_guide_on_layer("unknown_layer"))
        self.assertTrue(spread1.has_any_guide_on_layer("u2db"))
        self.assertTrue(spread1.has_any_guide_on_layer("ua4"))

        # Package with one layer and no guides.
        idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "4-pages.idml"), mode="r")
        spreads = idml_file.spreads

        # Spread_ub6.xml
        spread1 = Spread(idml_file, spreads[0])
        self.assertFalse(spread1.has_any_guide_on_layer("ub3"))
Exemplo n.º 15
0
def create_idml_package_from_dir(src_dir, destination):
    if not os.path.exists(src_dir):
        raise IOError("%s does not exist." % src_dir)
    if os.path.exists(destination):
        raise IOError("%s already exist." % destination)

    with IDMLPackage(destination, mode="w") as package:
        for root, dirs, filenames in os.walk(src_dir):
            for filename in filenames:
                package.write(os.path.join(root, filename),
                              os.path.join(root.replace(src_dir, "."), filename))
Exemplo n.º 16
0
    def test_is_recto(self):
        idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "magazineA-courrier-des-lecteurs-3pages.idml"), mode="r")
        spread1 = Spread(idml_file, idml_file.spreads[0])
        page1 = spread1.pages[0]
        self.assertTrue(page1.is_recto)

        spread2 = Spread(idml_file, idml_file.spreads[1])
        page2 = spread2.pages[0]
        page3 = spread2.pages[1]
        self.assertFalse(page2.is_recto)
        self.assertTrue(page3.is_recto)
Exemplo n.º 17
0
def use_working_copy(view_func):
    def new_func(idml_package, *args, **kwargs):
        if idml_package.working_copy_path is not None:
            return view_func(idml_package, *args, **kwargs)

        tmp_filename = NamedTemporaryFile().name
        idml_package.extractall(tmp_filename)
        idml_package.working_copy_path = tmp_filename
        idml_package.init_lazy_references()

        if idml_package.debug:
            # In debug it is useful to have the original trace.
            idml_package = view_func(idml_package, *args, **kwargs)
        else:
            # Catch any exception to reset working_copy_path.
            try:
                idml_package = view_func(idml_package, *args, **kwargs)
            except BaseException, err:
                idml_package.working_copy_path = None
                raise err

        from simple_idml.idml import IDMLPackage

        # Create a new archive from the extracted one.
        tmp_package = IDMLPackage("%s.idml" % tmp_filename, mode="w")
        for root, dirs, filenames in os.walk(tmp_filename):
            for filename in filenames:
                filename = os.path.join(root, filename)
                arcname = filename.replace(tmp_filename, "")
                tmp_package.write(filename, arcname)
        tmp_package.close()

        # swap working_copy with initial IDML Package.
        new_filename = idml_package.filename
        idml_package.close()
        os.unlink(idml_package.filename)
        os.rename(tmp_package.filename, new_filename)
        shutil.rmtree(tmp_filename)
        idml_package.working_copy_path = None

        return IDMLPackage(new_filename)
Exemplo n.º 18
0
 def test_get_style_node_by_name(self):
     idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR,
                                          "article-1photo_import-xml.idml"),
                             mode="r")
     style = Style(idml_file)
     style_node = style.get_style_node_by_name("CharacterStyle/bold")
     self.assertEqual(
         style_node.nsmap,
         {'idPkg': 'http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging'})
     self.assertEqual(
         etree_dom_to_tree(style_node, True), {
             'attrs': {
                 'FontStyle': 'Bold',
                 'Imported': 'false',
                 'KeyboardShortcut': '0 0',
                 'Name': 'bold',
                 'Self': 'CharacterStyle/bold'
             },
             'content': [{
                 'attrs': {},
                 'content': [{
                     'attrs': {
                         'type': 'string'
                     },
                     'content': [],
                     'tag': 'BasedOn',
                     'tail': '',
                     'text': '$ID/[No character style]'
                 }, {
                     'attrs': {
                         'type': 'enumeration'
                     },
                     'content': [],
                     'tag': 'PreviewColor',
                     'tail': '',
                     'text': 'Nothing'
                 }],
                 'tag':
                 'Properties',
                 'tail':
                 '',
                 'text':
                 ''
             }],
             'tag':
             'CharacterStyle',
             'tail':
             '',
             'text':
             ''
         })
Exemplo n.º 19
0
    def test_pages(self):
        idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "4-pages.idml"), mode="r")
        spreads = idml_file.spreads

        spread1 = Spread(idml_file, spreads[0])
        spread1_pages = spread1.pages
        self.assertEqual(len(spread1_pages), 1)
        self.assertEqual(spread1_pages[0].node.tag, "Page")

        spread2 = Spread(idml_file, spreads[1])
        spread2_pages = spread2.pages
        self.assertEqual(len(spread2_pages), 2)
        self.assertEqual(spread2_pages[0].node.tag, "Page")
        self.assertEqual(spread2_pages[1].node.tag, "Page")
Exemplo n.º 20
0
    def test_add_layer_nodes(self):
        idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "4-pages.idml"),
                                mode="r")
        designmap = idml_file.designmap

        designmap.add_layer_nodes([
            etree.fromstring(
                """<Layer Self="toto" Name="Layer 2" Visible="true" Locked="false" IgnoreWrap="false" ShowGuides="true" LockGuides="false" UI="true" Expendable="true" Printable="true">
                    <Properties>
                        <LayerColor type="enumeration">Red</LayerColor>
                    </Properties>
                </Layer>"""),
            etree.fromstring(
                """<Layer Self="titi" Name="Layer 3" Visible="true" Locked="false" IgnoreWrap="false" ShowGuides="true" LockGuides="false" UI="true" Expendable="true" Printable="true">
                    <Properties>
                        <LayerColor type="enumeration">Blue</LayerColor>
                    </Properties>
                </Layer>"""),
        ])
        self.assertEqual(len(designmap.layer_nodes), 3)
        self.assertEqual([n.get("Name") for n in designmap.layer_nodes],
                         ['Layer 1', 'Layer 2', 'Layer 3'])
        idml_file.close()
Exemplo n.º 21
0
    def test_add_layer_nodes(self):
        idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "4-pages.idml"), mode="r")
        designmap = idml_file.designmap

        designmap.add_layer_nodes([
            etree.fromstring(
                """<Layer Self="toto" Name="Layer 2" Visible="true" Locked="false" IgnoreWrap="false" ShowGuides="true" LockGuides="false" UI="true" Expendable="true" Printable="true">
                    <Properties>
                        <LayerColor type="enumeration">Red</LayerColor>
                    </Properties>
                </Layer>"""
            ),
            etree.fromstring(
                """<Layer Self="titi" Name="Layer 3" Visible="true" Locked="false" IgnoreWrap="false" ShowGuides="true" LockGuides="false" UI="true" Expendable="true" Printable="true">
                    <Properties>
                        <LayerColor type="enumeration">Blue</LayerColor>
                    </Properties>
                </Layer>"""
            ),
        ])
        self.assertEqual(len(designmap.layer_nodes), 3)
        self.assertEqual([n.get("Name") for n in designmap.layer_nodes],
                         ['Layer 1', 'Layer 2', 'Layer 3'])
        idml_file.close()
Exemplo n.º 22
0
    def test_remove_layer(self):
        # Remove active layer.
        idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "4-pages-layers-with-guides.idml"), mode="r")
        designmap = idml_file.designmap
        self.assertEqual(designmap.active_layer, "u2db")

        designmap.remove_layer("u2db")
        self.assertEqual(designmap.active_layer, "ua4")
        idml_file.close()

        # Remove inactive layer.
        idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "4-pages-layers-with-guides.idml"), mode="r")
        designmap = idml_file.designmap
        self.assertEqual(designmap.active_layer, "u2db")

        designmap.remove_layer("ua4")
        self.assertEqual(designmap.active_layer, "u2db")
        designmap.remove_layer("u2db")
        self.assertEqual(designmap.active_layer, None)
        idml_file.close()
Exemplo n.º 23
0
    def test_coordinates(self):
        idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "magazineA-courrier-des-lecteurs-3pages.idml"), mode="r")
        spread = Spread(idml_file, idml_file.spreads[1])

        page2 = spread.pages[0]
        self.assertEqual(page2.coordinates, {
            'x1': Decimal('-566.9291338582677'),
            'y1': Decimal('-379.8425196850394'),
            'x2': Decimal('0E-13'),
            'y2': Decimal('379.8425196850394')
        })

        page3 = spread.pages[1]
        self.assertEqual(page3.coordinates, {
            'x1': Decimal('0'),
            'y1': Decimal('-379.8425196850394'),
            'x2': Decimal('566.9291338582677'),
            'y2': Decimal('379.8425196850394'),
        })
Exemplo n.º 24
0
def use_working_copy(view_func):
    def new_func(idml_package, *args, **kwargs):
        if idml_package.working_copy_path is not None:
            return view_func(idml_package, *args, **kwargs)

        tmp_filename = NamedTemporaryFile().name
        idml_package.extractall(tmp_filename)
        idml_package.working_copy_path = tmp_filename
        idml_package.init_lazy_references()

        if idml_package.debug:
            # In debug it is useful to have the original trace.
            idml_package = view_func(idml_package, *args, **kwargs)
        else:
            # Catch any exception to reset working_copy_path.
            try:
                idml_package = view_func(idml_package, *args, **kwargs)
            except BaseException, err:
                idml_package.working_copy_path = None
                raise err

        from simple_idml.idml import IDMLPackage
        # Create a new archive from the extracted one.
        tmp_package = IDMLPackage("%s.idml" % tmp_filename, mode="w")
        for root, dirs, filenames in os.walk(tmp_filename):
            for filename in filenames:
                filename = os.path.join(root, filename)
                arcname = filename.replace(tmp_filename, "")
                tmp_package.write(filename, arcname)
        tmp_package.close()

        # swap working_copy with initial IDML Package.
        new_filename = idml_package.filename
        idml_package.close()
        os.unlink(idml_package.filename)
        os.rename(tmp_package.filename, new_filename)
        shutil.rmtree(tmp_filename)
        idml_package.working_copy_path = None

        return IDMLPackage(new_filename)
Exemplo n.º 25
0
 def test_repr(self):
     idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "4-pages.idml"), mode="r")
     designmap = idml_file.designmap
     self.assertEqual(repr(designmap), '<Designmap object designmap.xml at %s>' % hex(id(designmap)))
     idml_file.close()
Exemplo n.º 26
0
 def test_pages(self):
     idml_file = IDMLPackage(os.path.join(IDMLFILES_DIR, "4-pages.idml"),
                             mode="r")
     stories = idml_file.stories
     story = Story(idml_file, stories[0])
     self.assertEqual(story.node.tag, "Story")