Skip to content

Commit ddd5c30

Browse files
author
Steve Canny
committed
oxml: mv BaseOxmlElement to docx.oxml.xmlchemy
was OxmlBaseElement
1 parent df464f4 commit ddd5c30

12 files changed

Lines changed: 94 additions & 92 deletions

File tree

docx/opc/oxml.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def serialize_for_reading(element):
6969
# Custom element classes
7070
# ===========================================================================
7171

72-
class OxmlBaseElement(etree.ElementBase):
72+
class BaseOxmlElement(etree.ElementBase):
7373
"""
7474
Base class for all custom element classes, to add standardized behavior
7575
to all classes in one place.
@@ -84,7 +84,7 @@ def xml(self):
8484
return serialize_for_reading(self)
8585

8686

87-
class CT_Default(OxmlBaseElement):
87+
class CT_Default(BaseOxmlElement):
8888
"""
8989
``<Default>`` element, specifying the default content type to be applied
9090
to a part with the specified extension.
@@ -118,7 +118,7 @@ def new(ext, content_type):
118118
return default
119119

120120

121-
class CT_Override(OxmlBaseElement):
121+
class CT_Override(BaseOxmlElement):
122122
"""
123123
``<Override>`` element, specifying the content type to be applied for a
124124
part with the specified partname.
@@ -152,7 +152,7 @@ def partname(self):
152152
return self.get('PartName')
153153

154154

155-
class CT_Relationship(OxmlBaseElement):
155+
class CT_Relationship(BaseOxmlElement):
156156
"""
157157
``<Relationship>`` element, representing a single relationship from a
158158
source to a target part.
@@ -205,7 +205,7 @@ def target_mode(self):
205205
return self.get('TargetMode', RTM.INTERNAL)
206206

207207

208-
class CT_Relationships(OxmlBaseElement):
208+
class CT_Relationships(BaseOxmlElement):
209209
"""
210210
``<Relationships>`` element, the root element in a .rels file.
211211
"""
@@ -243,7 +243,7 @@ def xml(self):
243243
return serialize_part_xml(self)
244244

245245

246-
class CT_Types(OxmlBaseElement):
246+
class CT_Types(BaseOxmlElement):
247247
"""
248248
``<Types>`` element, the container element for Default and Override
249249
elements in [Content_Types].xml.

docx/oxml/parts/document.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
"""
77

88
from ..ns import qn
9-
from ..shared import OxmlBaseElement
109
from ..table import CT_Tbl
1110
from ..text import CT_P
11+
from ..xmlchemy import BaseOxmlElement
1212

1313

14-
class CT_Document(OxmlBaseElement):
14+
class CT_Document(BaseOxmlElement):
1515
"""
1616
``<w:document>`` element, the root element of a document.xml file.
1717
"""
@@ -20,7 +20,7 @@ def body(self):
2020
return self.find(qn('w:body'))
2121

2222

23-
class CT_Body(OxmlBaseElement):
23+
class CT_Body(BaseOxmlElement):
2424
"""
2525
``<w:body>``, the container element for the main document story in
2626
``document.xml``.

docx/oxml/parts/numbering.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
"""
66

77
from .. import OxmlElement
8-
from ..shared import CT_DecimalNumber, OxmlBaseElement
8+
from ..shared import CT_DecimalNumber
99
from ..ns import nsmap, qn
10+
from ..xmlchemy import BaseOxmlElement
1011

1112

12-
class CT_Num(OxmlBaseElement):
13+
class CT_Num(BaseOxmlElement):
1314
"""
1415
``<w:num>`` element, which represents a concrete list definition
1516
instance, having a required child <w:abstractNumId> that references an
@@ -48,7 +49,7 @@ def numId(self):
4849
return int(numId_str)
4950

5051

51-
class CT_NumLvl(OxmlBaseElement):
52+
class CT_NumLvl(BaseOxmlElement):
5253
"""
5354
``<w:lvlOverride>`` element, which identifies a level in a list
5455
definition to override with settings it contains.
@@ -71,7 +72,7 @@ def new(cls, ilvl):
7172
return OxmlElement('w:lvlOverride', {qn('w:ilvl'): str(ilvl)})
7273

7374

74-
class CT_NumPr(OxmlBaseElement):
75+
class CT_NumPr(BaseOxmlElement):
7576
"""
7677
A ``<w:numPr>`` element, a container for numbering properties applied to
7778
a paragraph.
@@ -152,7 +153,7 @@ def _insert_numId(self, numId):
152153
)
153154

154155

155-
class CT_Numbering(OxmlBaseElement):
156+
class CT_Numbering(BaseOxmlElement):
156157
"""
157158
``<w:numbering>`` element, the root element of a numbering part, i.e.
158159
numbering.xml

docx/oxml/parts/styles.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
Custom element classes related to the styles part
55
"""
66

7-
from ..shared import OxmlBaseElement
87
from ..ns import nsmap, qn
8+
from ..xmlchemy import BaseOxmlElement
99

1010

11-
class CT_Style(OxmlBaseElement):
11+
class CT_Style(BaseOxmlElement):
1212
"""
1313
A ``<w:style>`` element, representing a style definition
1414
"""
@@ -17,7 +17,7 @@ def pPr(self):
1717
return self.find(qn('w:pPr'))
1818

1919

20-
class CT_Styles(OxmlBaseElement):
20+
class CT_Styles(BaseOxmlElement):
2121
"""
2222
``<w:styles>`` element, the root element of a styles part, i.e.
2323
styles.xml

docx/oxml/shape.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
"""
66

77
from . import OxmlElement
8-
from .shared import OxmlBaseElement
98
from ..shared import Emu
109
from .ns import nsmap, nspfxmap, qn
10+
from .xmlchemy import BaseOxmlElement
1111

1212

13-
class CT_Blip(OxmlBaseElement):
13+
class CT_Blip(BaseOxmlElement):
1414
"""
1515
``<a:blip>`` element, specifies image source and adjustments such as
1616
alpha and tint.
@@ -30,7 +30,7 @@ def new(cls, rId):
3030
return blip
3131

3232

33-
class CT_BlipFillProperties(OxmlBaseElement):
33+
class CT_BlipFillProperties(BaseOxmlElement):
3434
"""
3535
``<pic:blipFill>`` element, specifies picture properties
3636
"""
@@ -46,7 +46,7 @@ def new(cls, rId):
4646
return blipFill
4747

4848

49-
class CT_GraphicalObject(OxmlBaseElement):
49+
class CT_GraphicalObject(BaseOxmlElement):
5050
"""
5151
``<a:graphic>`` element, container for a DrawingML object
5252
"""
@@ -61,7 +61,7 @@ def new(cls, uri, pic):
6161
return graphic
6262

6363

64-
class CT_GraphicalObjectData(OxmlBaseElement):
64+
class CT_GraphicalObjectData(BaseOxmlElement):
6565
"""
6666
``<a:graphicData>`` element, container for the XML of a DrawingML object
6767
"""
@@ -81,7 +81,7 @@ def uri(self):
8181
return self.get('uri')
8282

8383

84-
class CT_Inline(OxmlBaseElement):
84+
class CT_Inline(BaseOxmlElement):
8585
"""
8686
``<w:inline>`` element, container for an inline shape.
8787
"""
@@ -111,7 +111,7 @@ def new(cls, cx, cy, shape_id, pic):
111111
return inline
112112

113113

114-
class CT_NonVisualDrawingProps(OxmlBaseElement):
114+
class CT_NonVisualDrawingProps(BaseOxmlElement):
115115
"""
116116
Used for ``<wp:docPr>`` element, and perhaps others. Specifies the id and
117117
name of a DrawingML drawing.
@@ -124,7 +124,7 @@ def new(cls, nsptagname_str, shape_id, name):
124124
return elt
125125

126126

127-
class CT_NonVisualPictureProperties(OxmlBaseElement):
127+
class CT_NonVisualPictureProperties(BaseOxmlElement):
128128
"""
129129
``<pic:cNvPicPr>`` element, specifies picture locking and resize
130130
behaviors.
@@ -134,7 +134,7 @@ def new(cls):
134134
return OxmlElement('pic:cNvPicPr')
135135

136136

137-
class CT_Picture(OxmlBaseElement):
137+
class CT_Picture(BaseOxmlElement):
138138
"""
139139
``<pic:pic>`` element, a DrawingML picture
140140
"""
@@ -156,7 +156,7 @@ def new(cls, pic_id, filename, rId, cx, cy):
156156
return pic
157157

158158

159-
class CT_PictureNonVisual(OxmlBaseElement):
159+
class CT_PictureNonVisual(BaseOxmlElement):
160160
"""
161161
``<pic:nvPicPr>`` element, non-visual picture properties
162162
"""
@@ -170,7 +170,7 @@ def new(cls, pic_id, image_filename):
170170
return nvPicPr
171171

172172

173-
class CT_Point2D(OxmlBaseElement):
173+
class CT_Point2D(BaseOxmlElement):
174174
"""
175175
Used for ``<a:off>`` element, and perhaps others. Specifies an x, y
176176
coordinate (point).
@@ -183,7 +183,7 @@ def new(cls, nsptagname_str, x, y):
183183
return elm
184184

185185

186-
class CT_PositiveSize2D(OxmlBaseElement):
186+
class CT_PositiveSize2D(BaseOxmlElement):
187187
"""
188188
Used for ``<wp:extent>`` element, and perhaps others later. Specifies the
189189
size of a DrawingML drawing.
@@ -218,7 +218,7 @@ def new(cls, nsptagname_str, cx, cy):
218218
return elm
219219

220220

221-
class CT_PresetGeometry2D(OxmlBaseElement):
221+
class CT_PresetGeometry2D(BaseOxmlElement):
222222
"""
223223
``<a:prstGeom>`` element, specifies an preset autoshape geometry, such
224224
as ``rect``.
@@ -230,7 +230,7 @@ def new(cls, prst):
230230
return prstGeom
231231

232232

233-
class CT_RelativeRect(OxmlBaseElement):
233+
class CT_RelativeRect(BaseOxmlElement):
234234
"""
235235
``<a:fillRect>`` element, specifying picture should fill containing
236236
rectangle shape.
@@ -240,7 +240,7 @@ def new(cls):
240240
return OxmlElement('a:fillRect')
241241

242242

243-
class CT_ShapeProperties(OxmlBaseElement):
243+
class CT_ShapeProperties(BaseOxmlElement):
244244
"""
245245
``<pic:spPr>`` element, specifies size and shape of picture container.
246246
"""
@@ -252,7 +252,7 @@ def new(cls, cx, cy):
252252
return spPr
253253

254254

255-
class CT_StretchInfoProperties(OxmlBaseElement):
255+
class CT_StretchInfoProperties(BaseOxmlElement):
256256
"""
257257
``<a:stretch>`` element, specifies how picture should fill its containing
258258
shape.
@@ -264,7 +264,7 @@ def new(cls):
264264
return stretch
265265

266266

267-
class CT_Transform2D(OxmlBaseElement):
267+
class CT_Transform2D(BaseOxmlElement):
268268
"""
269269
``<a:xfrm>`` element, specifies size and shape of picture container.
270270
"""

docx/oxml/shared.py

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,13 @@
66

77
from __future__ import absolute_import
88

9-
from lxml import etree
10-
119
from . import OxmlElement
1210
from .exceptions import ValidationError
1311
from .ns import qn
14-
from .xmlchemy import serialize_for_reading
15-
16-
17-
class OxmlBaseElement(etree.ElementBase):
18-
"""
19-
Base class for all custom element classes, to add standardized behavior
20-
to all classes in one place.
21-
"""
22-
def first_child_found_in(self, *tagnames):
23-
"""
24-
Return the first child found with tag in *tagnames*, or None if
25-
not found.
26-
"""
27-
for tagname in tagnames:
28-
child = self.find(qn(tagname))
29-
if child is not None:
30-
return child
31-
return None
32-
33-
def insert_element_before(self, elm, *tagnames):
34-
successor = self.first_child_found_in(*tagnames)
35-
if successor is not None:
36-
successor.addprevious(elm)
37-
else:
38-
self.append(elm)
39-
return elm
40-
41-
@property
42-
def xml(self):
43-
"""
44-
Return XML string for this element, suitable for testing purposes.
45-
Pretty printed for readability and without an XML declaration at the
46-
top.
47-
"""
48-
return serialize_for_reading(self)
12+
from .xmlchemy import BaseOxmlElement
4913

5014

51-
class CT_DecimalNumber(OxmlBaseElement):
15+
class CT_DecimalNumber(BaseOxmlElement):
5216
"""
5317
Used for ``<w:numId>``, ``<w:ilvl>``, ``<w:abstractNumId>`` and several
5418
others, containing a text representation of a decimal number (e.g. 42) in
@@ -76,7 +40,7 @@ def val(self, val):
7640
self.set(qn('w:val'), decimal_number_str)
7741

7842

79-
class CT_OnOff(OxmlBaseElement):
43+
class CT_OnOff(BaseOxmlElement):
8044
"""
8145
Used for ``<w:b>``, ``<w:i>`` elements and others, containing a bool-ish
8246
string in its ``val`` attribute, xsd:boolean plus 'on' and 'off'.
@@ -102,7 +66,7 @@ def val(self, value):
10266
self.set(val, '0')
10367

10468

105-
class CT_String(OxmlBaseElement):
69+
class CT_String(BaseOxmlElement):
10670
"""
10771
Used for ``<w:pStyle>`` and ``<w:tblStyle>`` elements and others,
10872
containing a style name in its ``val`` attribute.

0 commit comments

Comments
 (0)