File: rels.py

package info (click to toggle)
python-docx 1.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,216 kB
  • sloc: xml: 25,323; python: 23,414; makefile: 175
file content (302 lines) | stat: -rw-r--r-- 8,626 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
"""Test data for relationship-related unit tests."""

from docx.opc.constants import NAMESPACE as NS
from docx.opc.constants import RELATIONSHIP_TYPE as RT
from docx.opc.oxml import parse_xml
from docx.opc.rel import Relationships


class BaseBuilder:
    """
    Provides common behavior for all data builders.
    """

    @property
    def element(self):
        """Return element based on XML generated by builder"""
        return parse_xml(self.xml)

    def with_indent(self, indent):
        """Add integer `indent` spaces at beginning of element XML"""
        self._indent = indent
        return self


class RelationshipsBuilder:
    """Builder class for test Relationships"""

    partname_tmpls = {
        RT.SLIDE_MASTER: "/ppt/slideMasters/slideMaster%d.xml",
        RT.SLIDE: "/ppt/slides/slide%d.xml",
    }

    def __init__(self):
        self.relationships = []
        self.next_rel_num = 1
        self.next_partnums = {}

    def _next_partnum(self, reltype):
        if reltype not in self.next_partnums:
            self.next_partnums[reltype] = 1
        partnum = self.next_partnums[reltype]
        self.next_partnums[reltype] = partnum + 1
        return partnum

    @property
    def next_rId(self):
        rId = "rId%d" % self.next_rel_num
        self.next_rel_num += 1
        return rId

    def _next_tuple_partname(self, reltype):
        partname_tmpl = self.partname_tmpls[reltype]
        partnum = self._next_partnum(reltype)
        return partname_tmpl % partnum

    def build(self):
        rels = Relationships()
        for rel in self.relationships:
            rels.add_rel(rel)
        return rels


class CT_DefaultBuilder(BaseBuilder):
    """
    Test data builder for CT_Default (Default) XML element that appears in
    `[Content_Types].xml`.
    """

    def __init__(self):
        """Establish instance variables with default values"""
        self._content_type = "application/xml"
        self._extension = "xml"
        self._indent = 0
        self._namespace = ' xmlns="%s"' % NS.OPC_CONTENT_TYPES

    def with_content_type(self, content_type):
        """Set ContentType attribute to `content_type`"""
        self._content_type = content_type
        return self

    def with_extension(self, extension):
        """Set Extension attribute to `extension`"""
        self._extension = extension
        return self

    def without_namespace(self):
        """Don't include an 'xmlns=' attribute"""
        self._namespace = ""
        return self

    @property
    def xml(self):
        """Return Default element"""
        tmpl = '%s<Default%s Extension="%s" ContentType="%s"/>\n'
        indent = " " * self._indent
        return tmpl % (indent, self._namespace, self._extension, self._content_type)


class CT_OverrideBuilder(BaseBuilder):
    """
    Test data builder for CT_Override (Override) XML element that appears in
    `[Content_Types].xml`.
    """

    def __init__(self):
        """Establish instance variables with default values"""
        self._content_type = "app/vnd.type"
        self._indent = 0
        self._namespace = ' xmlns="%s"' % NS.OPC_CONTENT_TYPES
        self._partname = "/part/name.xml"

    def with_content_type(self, content_type):
        """Set ContentType attribute to `content_type`"""
        self._content_type = content_type
        return self

    def with_partname(self, partname):
        """Set PartName attribute to `partname`"""
        self._partname = partname
        return self

    def without_namespace(self):
        """Don't include an 'xmlns=' attribute"""
        self._namespace = ""
        return self

    @property
    def xml(self):
        """Return Override element"""
        tmpl = '%s<Override%s PartName="%s" ContentType="%s"/>\n'
        indent = " " * self._indent
        return tmpl % (indent, self._namespace, self._partname, self._content_type)


class CT_RelationshipBuilder(BaseBuilder):
    """
    Test data builder for CT_Relationship (Relationship) XML element that
    appears in .rels files
    """

    def __init__(self):
        """Establish instance variables with default values"""
        self._rId = "rId9"
        self._reltype = "ReLtYpE"
        self._target = "docProps/core.xml"
        self._target_mode = None
        self._indent = 0
        self._namespace = ' xmlns="%s"' % NS.OPC_RELATIONSHIPS

    def with_rId(self, rId):
        """Set Id attribute to `rId`"""
        self._rId = rId
        return self

    def with_reltype(self, reltype):
        """Set Type attribute to `reltype`"""
        self._reltype = reltype
        return self

    def with_target(self, target):
        """Set XXX attribute to `target`"""
        self._target = target
        return self

    def with_target_mode(self, target_mode):
        """Set TargetMode attribute to `target_mode`"""
        self._target_mode = None if target_mode == "Internal" else target_mode
        return self

    def without_namespace(self):
        """Don't include an 'xmlns=' attribute"""
        self._namespace = ""
        return self

    @property
    def target_mode(self):
        if self._target_mode is None:
            return ""
        return ' TargetMode="%s"' % self._target_mode

    @property
    def xml(self):
        """Return Relationship element"""
        tmpl = '%s<Relationship%s Id="%s" Type="%s" Target="%s"%s/>\n'
        indent = " " * self._indent
        return tmpl % (
            indent,
            self._namespace,
            self._rId,
            self._reltype,
            self._target,
            self.target_mode,
        )


class CT_RelationshipsBuilder(BaseBuilder):
    """
    Test data builder for CT_Relationships (Relationships) XML element, the
    root element in .rels files.
    """

    def __init__(self):
        """Establish instance variables with default values"""
        self._rels = (
            ("rId1", "http://reltype1", "docProps/core.xml", "Internal"),
            ("rId2", "http://linktype", "http://some/link", "External"),
            ("rId3", "http://reltype2", "../slides/slide1.xml", "Internal"),
        )

    @property
    def xml(self):
        """
        Return XML string based on settings accumulated via method calls.
        """
        xml = '<Relationships xmlns="%s">\n' % NS.OPC_RELATIONSHIPS
        for rId, reltype, target, target_mode in self._rels:
            xml += (
                a_Relationship()
                .with_rId(rId)
                .with_reltype(reltype)
                .with_target(target)
                .with_target_mode(target_mode)
                .with_indent(2)
                .without_namespace()
                .xml
            )
        xml += "</Relationships>\n"
        return xml


class CT_TypesBuilder(BaseBuilder):
    """
    Test data builder for CT_Types (<Types>) XML element, the root element in
    [Content_Types].xml files
    """

    def __init__(self):
        """Establish instance variables with default values"""
        self._defaults = (
            ("xml", "application/xml"),
            ("jpeg", "image/jpeg"),
        )
        self._empty = False
        self._overrides = (
            ("/docProps/core.xml", "app/vnd.type1"),
            ("/ppt/presentation.xml", "app/vnd.type2"),
            ("/docProps/thumbnail.jpeg", "image/jpeg"),
        )

    def empty(self):
        self._empty = True
        return self

    @property
    def xml(self):
        """
        Return XML string based on settings accumulated via method calls
        """
        if self._empty:
            return '<Types xmlns="%s"/>\n' % NS.OPC_CONTENT_TYPES

        xml = '<Types xmlns="%s">\n' % NS.OPC_CONTENT_TYPES
        for extension, content_type in self._defaults:
            xml += (
                a_Default()
                .with_extension(extension)
                .with_content_type(content_type)
                .with_indent(2)
                .without_namespace()
                .xml
            )
        for partname, content_type in self._overrides:
            xml += (
                an_Override()
                .with_partname(partname)
                .with_content_type(content_type)
                .with_indent(2)
                .without_namespace()
                .xml
            )
        xml += "</Types>\n"
        return xml


def a_Default():
    return CT_DefaultBuilder()


def a_Relationship():
    return CT_RelationshipBuilder()


def a_Relationships():
    return CT_RelationshipsBuilder()


def a_Types():
    return CT_TypesBuilder()


def an_Override():
    return CT_OverrideBuilder()