forked from python-openxml/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.py
More file actions
342 lines (284 loc) · 10.3 KB
/
shared.py
File metadata and controls
342 lines (284 loc) · 10.3 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# encoding: utf-8
"""
Objects shared by modules in the docx.oxml subpackage.
"""
from lxml import etree
import re
from .exceptions import ValidationError
nsmap = {
'a': ('http://schemas.openxmlformats.org/drawingml/2006/main'),
'c': ('http://schemas.openxmlformats.org/drawingml/2006/chart'),
'dgm': ('http://schemas.openxmlformats.org/drawingml/2006/diagram'),
'pic': ('http://schemas.openxmlformats.org/drawingml/2006/picture'),
'r': ('http://schemas.openxmlformats.org/officeDocument/2006/relations'
'hips'),
'w': ('http://schemas.openxmlformats.org/wordprocessingml/2006/main'),
'wp': ('http://schemas.openxmlformats.org/drawingml/2006/wordprocessing'
'Drawing'),
'xml': ('http://www.w3.org/XML/1998/namespace')
}
# configure XML parser
element_class_lookup = etree.ElementNamespaceClassLookup()
oxml_parser = etree.XMLParser(remove_blank_text=True)
oxml_parser.set_element_class_lookup(element_class_lookup)
# ===========================================================================
# utility functions
# ===========================================================================
class NamespacePrefixedTag(str):
"""
Value object that knows the semantics of an XML tag having a namespace
prefix.
"""
def __new__(cls, nstag, *args):
return super(NamespacePrefixedTag, cls).__new__(cls, nstag)
def __init__(self, nstag):
self._pfx, self._local_part = nstag.split(':')
self._ns_uri = nsmap[self._pfx]
@property
def clark_name(self):
return '{%s}%s' % (self._ns_uri, self._local_part)
@property
def local_part(self):
"""
Return the local part of the tag as a string. E.g. 'foobar' is
returned for tag 'f:foobar'.
"""
return self._local_part
@property
def nsmap(self):
"""
Return a dict having a single member, mapping the namespace prefix of
this tag to it's namespace name (e.g. {'f': 'http://foo/bar'}). This
is handy for passing to xpath calls and other uses.
"""
return {self._pfx: self._ns_uri}
@property
def nspfx(self):
"""
Return the string namespace prefix for the tag, e.g. 'f' is returned
for tag 'f:foobar'.
"""
return self._pfx
@property
def nsuri(self):
"""
Return the namespace URI for the tag, e.g. 'http://foo/bar' would be
returned for tag 'f:foobar' if the 'f' prefix maps to
'http://foo/bar' in nsmap.
"""
return self._ns_uri
def nsdecls(*prefixes):
return ' '.join(['xmlns:%s="%s"' % (pfx, nsmap[pfx]) for pfx in prefixes])
def nspfxmap(*nspfxs):
"""
Return a dict containing the subset namespace prefix mappings specified by
*nspfxs*. Any number of namespace prefixes can be supplied, e.g.
namespaces('a', 'r', 'p').
"""
return dict((pfx, nsmap[pfx]) for pfx in nspfxs)
def OxmlElement(nsptag_str, attrs=None, nsmap=None):
"""
Return a 'loose' lxml element having the tag specified by *nsptag_str*.
*nsptag_str* must contain the standard namespace prefix, e.g. 'a:tbl'.
The resulting element is an instance of the custom element class for this
tag name if one is defined. A dictionary of attribute values may be
provided as *attrs*; they are set if present.
"""
nsptag = NamespacePrefixedTag(nsptag_str)
nsmap = nsmap if nsmap is not None else nsptag.nsmap
return oxml_parser.makeelement(
nsptag.clark_name, attrib=attrs, nsmap=nsmap
)
def oxml_fromstring(text):
"""
``etree.fromstring()`` replacement that uses oxml parser
"""
return etree.fromstring(text, oxml_parser)
def qn(tag):
"""
Stands for "qualified name", a utility function to turn a namespace
prefixed tag name into a Clark-notation qualified tag name for lxml. For
example, ``qn('p:cSld')`` returns ``'{http://schemas.../main}cSld'``.
"""
prefix, tagroot = tag.split(':')
uri = nsmap[prefix]
return '{%s}%s' % (uri, tagroot)
def register_custom_element_class(tag, cls):
"""
Register *cls* to be constructed when the oxml parser encounters an
element with matching *tag*. *tag* is a string of the form
``nspfx:tagroot``, e.g. ``'w:document'``.
"""
nspfx, tagroot = tag.split(':')
namespace = element_class_lookup.get_namespace(nsmap[nspfx])
namespace[tagroot] = cls
def serialize_for_reading(element):
"""
Serialize *element* to human-readable XML suitable for tests. No XML
declaration.
"""
xml = etree.tostring(element, encoding='unicode', pretty_print=True)
return XmlString(xml)
def _SubElement(parent, tag):
return etree.SubElement(parent, qn(tag), nsmap=nsmap)
class XmlString(str):
"""
Provides string comparison override suitable for serialized XML that is
useful for tests.
"""
# ' <w:xyz xmlns:a="http://ns/decl/a" attr_name="val">text</w:xyz>'
# | | || |
# +----------+------------------------------------------++-----------+
# front attrs | text
# close
_xml_elm_line_patt = re.compile(
'( *</?[\w:]+)(.*?)(/?>)([^<]*</\w+>)?'
)
def __eq__(self, other):
lines = self.splitlines()
lines_other = other.splitlines()
if len(lines) != len(lines_other):
return False
for line, line_other in zip(lines, lines_other):
if not self._eq_elm_strs(line, line_other):
return False
return True
def __ne__(self, other):
return not self.__eq__(other)
def _attr_seq(self, attrs):
"""
Return a sequence of attribute strings parsed from *attrs*. Each
attribute string is stripped of whitespace on both ends.
"""
attrs = attrs.strip()
attr_lst = attrs.split()
return sorted(attr_lst)
def _eq_elm_strs(self, line, line_2):
"""
Return True if the element in *line_2* is XML equivalent to the
element in *line*.
"""
front, attrs, close, text = self._parse_line(line)
front_2, attrs_2, close_2, text_2 = self._parse_line(line_2)
if front != front_2:
return False
if self._attr_seq(attrs) != self._attr_seq(attrs_2):
return False
if close != close_2:
return False
if text != text_2:
return False
return True
def _parse_line(self, line):
"""
Return front, attrs, close, text 4-tuple result of parsing XML element
string *line*.
"""
match = self._xml_elm_line_patt.match(line)
front, attrs, close, text = [match.group(n) for n in range(1, 5)]
return front, attrs, close, text
# ===========================================================================
# shared custom element classes
# ===========================================================================
class OxmlBaseElement(etree.ElementBase):
"""
Base class for all custom element classes, to add standardized behavior
to all classes in one place.
"""
def first_child_found_in(self, *tagnames):
"""
Return the first child found with tag in *tagnames*, or None if
not found.
"""
for tagname in tagnames:
child = self.find(qn(tagname))
if child is not None:
return child
return None
def insert_element_before(self, elm, *tagnames):
successor = self.first_child_found_in(*tagnames)
if successor is not None:
successor.addprevious(elm)
else:
self.append(elm)
return elm
@property
def xml(self):
"""
Return XML string for this element, suitable for testing purposes.
Pretty printed for readability and without an XML declaration at the
top.
"""
return serialize_for_reading(self)
class CT_DecimalNumber(OxmlBaseElement):
"""
Used for ``<w:numId>``, ``<w:ilvl>``, ``<w:abstractNumId>`` and several
others, containing a text representation of a decimal number (e.g. 42) in
its ``val`` attribute.
"""
@classmethod
def new(cls, nsptagname, val):
"""
Return a new ``CT_DecimalNumber`` element having tagname *nsptagname*
and ``val`` attribute set to *val*.
"""
return OxmlElement(nsptagname, attrs={qn('w:val'): str(val)})
@property
def val(self):
"""
Required attribute containing a decimal integer
"""
number_str = self.get(qn('w:val'))
return int(number_str)
@val.setter
def val(self, val):
decimal_number_str = '%d' % val
self.set(qn('w:val'), decimal_number_str)
class CT_OnOff(OxmlBaseElement):
"""
Used for ``<w:b>``, ``<w:i>`` elements and others, containing a bool-ish
string in its ``val`` attribute, xsd:boolean plus 'on' and 'off'.
"""
@property
def val(self):
val = self.get(qn('w:val'))
if val is None:
return True
elif val in ('0', 'false', 'off'):
return False
elif val in ('1', 'true', 'on'):
return True
raise ValidationError("expected xsd:boolean, got '%s'" % val)
@val.setter
def val(self, value):
val = qn('w:val')
if bool(value) is True:
if val in self.attrib:
del self.attrib[val]
else:
self.set(val, '0')
class CT_String(OxmlBaseElement):
"""
Used for ``<w:pStyle>`` and ``<w:tblStyle>`` elements and others,
containing a style name in its ``val`` attribute.
"""
@classmethod
def new(cls, nsptagname, val):
"""
Return a new ``CT_String`` element with tagname *nsptagname* and
``val`` attribute set to *val*.
"""
return OxmlElement(nsptagname, attrs={qn('w:val'): val})
@classmethod
def new_pStyle(cls, val):
"""
Return a new ``<w:pStyle>`` element with ``val`` attribute set to
*val*.
"""
return OxmlElement('w:pStyle', attrs={qn('w:val'): val})
@property
def val(self):
return self.get(qn('w:val'))
@val.setter
def val(self, val):
return self.set(qn('w:val'), val)