forked from python-openxml/python-opc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoxml.py
More file actions
278 lines (237 loc) · 8.26 KB
/
oxml.py
File metadata and controls
278 lines (237 loc) · 8.26 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
# -*- coding: utf-8 -*-
#
# oxml.py
#
# Copyright (C) 2012, 2013 Steve Canny scanny@cisco.com
#
# This module is part of python-opc and is released under the MIT License:
# http://www.opensource.org/licenses/mit-license.php
"""
Classes that directly manipulate Open XML and provide direct object-oriented
access to the XML elements.
"""
from lxml import etree, objectify
from opc.constants import NAMESPACE as NS, RELATIONSHIP_TARGET_MODE as RTM
# configure objectified XML parser
fallback_lookup = objectify.ObjectifyElementClassLookup()
element_class_lookup = etree.ElementNamespaceClassLookup(fallback_lookup)
oxml_parser = etree.XMLParser(remove_blank_text=True)
oxml_parser.set_element_class_lookup(element_class_lookup)
nsmap = {
'ct': NS.OPC_CONTENT_TYPES,
'pr': NS.OPC_RELATIONSHIPS,
}
# ===========================================================================
# functions
# ===========================================================================
def oxml_fromstring(text):
"""``etree.fromstring()`` replacement that uses oxml parser"""
return objectify.fromstring(text, oxml_parser)
def oxml_tostring(elm, encoding=None, pretty_print=False, standalone=None):
# if xsi parameter is not set to False, PowerPoint won't load without a
# repair step; deannotate removes some original xsi:type tags in core.xml
# if this parameter is left out (or set to True)
objectify.deannotate(elm, xsi=False, cleanup_namespaces=True)
return etree.tostring(elm, encoding=encoding, pretty_print=pretty_print,
standalone=standalone)
# ===========================================================================
# Custom element classes
# ===========================================================================
class OxmlBaseElement(objectify.ObjectifiedElement):
"""
Base class for all custom element classes, to add standardized behavior
to all classes in one place.
"""
@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 oxml_tostring(self, encoding='unicode', pretty_print=True)
class CT_Default(OxmlBaseElement):
"""
``<Default>`` element, specifying the default content type to be applied
to a part with the specified extension.
"""
@property
def content_type(self):
"""
String held in the ``ContentType`` attribute of this ``<Default>``
element.
"""
return self.get('ContentType')
@property
def extension(self):
"""
String held in the ``Extension`` attribute of this ``<Default>``
element.
"""
return self.get('Extension')
@staticmethod
def new(ext, content_type):
"""
Return a new ``<Default>`` element with attributes set to parameter
values.
"""
xml = '<Default xmlns="%s"/>' % nsmap['ct']
default = oxml_fromstring(xml)
default.set('Extension', ext[1:])
default.set('ContentType', content_type)
objectify.deannotate(default, cleanup_namespaces=True)
return default
class CT_Override(OxmlBaseElement):
"""
``<Override>`` element, specifying the content type to be applied for a
part with the specified partname.
"""
@property
def content_type(self):
"""
String held in the ``ContentType`` attribute of this ``<Override>``
element.
"""
return self.get('ContentType')
@staticmethod
def new(partname, content_type):
"""
Return a new ``<Override>`` element with attributes set to parameter
values.
"""
xml = '<Override xmlns="%s"/>' % nsmap['ct']
override = oxml_fromstring(xml)
override.set('PartName', partname)
override.set('ContentType', content_type)
objectify.deannotate(override, cleanup_namespaces=True)
return override
@property
def partname(self):
"""
String held in the ``PartName`` attribute of this ``<Override>``
element.
"""
return self.get('PartName')
class CT_Relationship(OxmlBaseElement):
"""
``<Relationship>`` element, representing a single relationship from a
source to a target part.
"""
@staticmethod
def new(rId, reltype, target, target_mode=RTM.INTERNAL):
"""
Return a new ``<Relationship>`` element.
"""
xml = '<Relationship xmlns="%s"/>' % nsmap['pr']
relationship = oxml_fromstring(xml)
relationship.set('Id', rId)
relationship.set('Type', reltype)
relationship.set('Target', target)
if target_mode == RTM.EXTERNAL:
relationship.set('TargetMode', RTM.EXTERNAL)
objectify.deannotate(relationship, cleanup_namespaces=True)
return relationship
@property
def rId(self):
"""
String held in the ``Id`` attribute of this ``<Relationship>``
element.
"""
return self.get('Id')
@property
def reltype(self):
"""
String held in the ``Type`` attribute of this ``<Relationship>``
element.
"""
return self.get('Type')
@property
def target_ref(self):
"""
String held in the ``Target`` attribute of this ``<Relationship>``
element.
"""
return self.get('Target')
@property
def target_mode(self):
"""
String held in the ``TargetMode`` attribute of this
``<Relationship>`` element, either ``Internal`` or ``External``.
Defaults to ``Internal``.
"""
return self.get('TargetMode', RTM.INTERNAL)
class CT_Relationships(OxmlBaseElement):
"""
``<Relationships>`` element, the root element in a .rels file.
"""
def add_rel(self, rId, reltype, target, is_external=False):
"""
Add a child ``<Relationship>`` element with attributes set according
to parameter values.
"""
target_mode = RTM.EXTERNAL if is_external else RTM.INTERNAL
relationship = CT_Relationship.new(rId, reltype, target, target_mode)
self.append(relationship)
@staticmethod
def new():
"""
Return a new ``<Relationships>`` element.
"""
xml = '<Relationships xmlns="%s"/>' % nsmap['pr']
relationships = oxml_fromstring(xml)
objectify.deannotate(relationships, cleanup_namespaces=True)
return relationships
@property
def xml(self):
"""
Return XML string for this element, suitable for saving in a .rels
stream, not pretty printed and with an XML declaration at the top.
"""
return oxml_tostring(self, encoding='UTF-8', standalone=True)
class CT_Types(OxmlBaseElement):
"""
``<Types>`` element, the container element for Default and Override
elements in [Content_Types].xml.
"""
def add_default(self, ext, content_type):
"""
Add a child ``<Default>`` element with attributes set to parameter
values.
"""
default = CT_Default.new(ext, content_type)
self.append(default)
def add_override(self, partname, content_type):
"""
Add a child ``<Override>`` element with attributes set to parameter
values.
"""
override = CT_Override.new(partname, content_type)
self.append(override)
@property
def defaults(self):
try:
return self.Default[:]
except AttributeError:
return []
@staticmethod
def new():
"""
Return a new ``<Types>`` element.
"""
xml = '<Types xmlns="%s"/>' % nsmap['ct']
types = oxml_fromstring(xml)
objectify.deannotate(types, cleanup_namespaces=True)
return types
@property
def overrides(self):
try:
return self.Override[:]
except AttributeError:
return []
ct_namespace = element_class_lookup.get_namespace(nsmap['ct'])
ct_namespace['Default'] = CT_Default
ct_namespace['Override'] = CT_Override
ct_namespace['Types'] = CT_Types
pr_namespace = element_class_lookup.get_namespace(nsmap['pr'])
pr_namespace['Relationship'] = CT_Relationship
pr_namespace['Relationships'] = CT_Relationships