Skip to content

Commit 769d2bb

Browse files
author
Steve Canny
committed
oxml: extract parse_xml() to pptx.oxml.__init__
Was `oxml_fromstring()`. Also renamed separate function of same name in pptx.opc.oxml having non-intersecting scope.
1 parent 06ebca3 commit 769d2bb

File tree

14 files changed

+62
-59
lines changed

14 files changed

+62
-59
lines changed

docx/opc/oxml.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
# functions
3131
# ===========================================================================
3232

33-
def oxml_fromstring(text):
33+
def parse_xml(text):
3434
"""
3535
``etree.fromstring()`` replacement that uses oxml parser
3636
"""
@@ -112,7 +112,7 @@ def new(ext, content_type):
112112
values.
113113
"""
114114
xml = '<Default xmlns="%s"/>' % nsmap['ct']
115-
default = oxml_fromstring(xml)
115+
default = parse_xml(xml)
116116
default.set('Extension', ext)
117117
default.set('ContentType', content_type)
118118
return default
@@ -138,7 +138,7 @@ def new(partname, content_type):
138138
values.
139139
"""
140140
xml = '<Override xmlns="%s"/>' % nsmap['ct']
141-
override = oxml_fromstring(xml)
141+
override = parse_xml(xml)
142142
override.set('PartName', partname)
143143
override.set('ContentType', content_type)
144144
return override
@@ -163,7 +163,7 @@ def new(rId, reltype, target, target_mode=RTM.INTERNAL):
163163
Return a new ``<Relationship>`` element.
164164
"""
165165
xml = '<Relationship xmlns="%s"/>' % nsmap['pr']
166-
relationship = oxml_fromstring(xml)
166+
relationship = parse_xml(xml)
167167
relationship.set('Id', rId)
168168
relationship.set('Type', reltype)
169169
relationship.set('Target', target)
@@ -224,7 +224,7 @@ def new():
224224
Return a new ``<Relationships>`` element.
225225
"""
226226
xml = '<Relationships xmlns="%s"/>' % nsmap['pr']
227-
relationships = oxml_fromstring(xml)
227+
relationships = parse_xml(xml)
228228
return relationships
229229

230230
@property
@@ -274,7 +274,7 @@ def new():
274274
Return a new ``<Types>`` element.
275275
"""
276276
xml = '<Types xmlns="%s"/>' % nsmap['ct']
277-
types = oxml_fromstring(xml)
277+
types = parse_xml(xml)
278278
return types
279279

280280
@property

docx/opc/pkgreader.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from __future__ import absolute_import
99

1010
from .constants import RELATIONSHIP_TARGET_MODE as RTM
11-
from .oxml import oxml_fromstring
11+
from .oxml import parse_xml
1212
from .packuri import PACKAGE_URI, PackURI
1313
from .phys_pkg import PhysPkgReader
1414
from .shared import CaseInsensitiveDict
@@ -141,7 +141,7 @@ def from_xml(content_types_xml):
141141
Return a new |_ContentTypeMap| instance populated with the contents
142142
of *content_types_xml*.
143143
"""
144-
types_elm = oxml_fromstring(content_types_xml)
144+
types_elm = parse_xml(content_types_xml)
145145
ct_map = _ContentTypeMap()
146146
for o in types_elm.overrides:
147147
ct_map._add_override(o.partname, o.content_type)
@@ -292,7 +292,7 @@ def load_from_xml(baseURI, rels_item_xml):
292292
"""
293293
srels = _SerializedRelationships()
294294
if rels_item_xml is not None:
295-
rels_elm = oxml_fromstring(rels_item_xml)
295+
rels_elm = parse_xml(rels_item_xml)
296296
for rel_elm in rels_elm.Relationship_lst:
297297
srels._srels.append(_SerializedRelationship(baseURI, rel_elm))
298298
return srels

docx/oxml/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
oxml_parser.set_element_class_lookup(element_class_lookup)
1919

2020

21+
def parse_xml(xml):
22+
"""
23+
Return root lxml element obtained by parsing XML character string in
24+
*xml*, which can be either a Python 2.x string or unicode. The custom
25+
parser is used, so custom element classes are produced for elements in
26+
*xml* that have them.
27+
"""
28+
root_element = etree.fromstring(xml, oxml_parser)
29+
return root_element
30+
31+
2132
def register_custom_element_class(tag, cls):
2233
"""
2334
Register *cls* to be constructed when the oxml parser encounters an

docx/oxml/shared.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@ def OxmlElement(nsptag_str, attrs=None, nsmap=None):
3535
)
3636

3737

38-
def oxml_fromstring(text):
39-
"""
40-
``etree.fromstring()`` replacement that uses oxml parser
41-
"""
42-
return etree.fromstring(text, oxml_parser)
43-
44-
4538
def serialize_for_reading(element):
4639
"""
4740
Serialize *element* to human-readable XML suitable for tests. No XML

docx/oxml/text.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
(CT_R).
66
"""
77

8+
from . import parse_xml
89
from ..enum.text import WD_UNDERLINE
910
from .ns import nsdecls, qn
1011
from .parts.numbering import CT_NumPr
11-
from .shared import CT_String, OxmlBaseElement, OxmlElement, oxml_fromstring
12+
from .shared import CT_String, OxmlBaseElement, OxmlElement
1213

1314

1415
class CT_Br(OxmlBaseElement):
@@ -66,7 +67,7 @@ def new():
6667
Return a new ``<w:p>`` element.
6768
"""
6869
xml = '<w:p %s/>' % nsdecls('w')
69-
p = oxml_fromstring(xml)
70+
p = parse_xml(xml)
7071
return p
7172

7273
@property
@@ -140,7 +141,7 @@ def new():
140141
Return a new ``<w:pPr>`` element.
141142
"""
142143
xml = '<w:pPr %s/>' % nsdecls('w')
143-
pPr = oxml_fromstring(xml)
144+
pPr = parse_xml(xml)
144145
return pPr
145146

146147
@property

docx/parts/document.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from ..opc.constants import RELATIONSHIP_TYPE as RT
1212
from ..opc.oxml import serialize_part_xml
1313
from ..opc.package import Part
14+
from ..oxml import parse_xml
1415
from ..oxml.ns import nsmap
15-
from ..oxml.shared import oxml_fromstring
1616
from ..shape import InlineShape
1717
from ..shared import lazyproperty, Parented
1818
from ..table import Table
@@ -76,7 +76,7 @@ def inline_shapes(self):
7676

7777
@classmethod
7878
def load(cls, partname, content_type, blob, package):
79-
document_elm = oxml_fromstring(blob)
79+
document_elm = parse_xml(blob)
8080
document_part = cls(partname, content_type, document_elm, package)
8181
return document_part
8282

docx/parts/numbering.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
)
1010

1111
from ..opc.package import Part
12-
from ..oxml.shared import oxml_fromstring
12+
from ..oxml import parse_xml
1313
from ..shared import lazyproperty
1414

1515

@@ -29,7 +29,7 @@ def load(cls, partname, content_type, blob, package):
2929
Provides PartFactory interface for loading a numbering part from
3030
a WML package.
3131
"""
32-
numbering_elm = oxml_fromstring(blob)
32+
numbering_elm = parse_xml(blob)
3333
numbering_part = cls(partname, content_type, numbering_elm, package)
3434
return numbering_part
3535

docx/parts/styles.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
)
1010

1111
from ..opc.package import Part
12-
from ..oxml.shared import oxml_fromstring
12+
from ..oxml import parse_xml
1313
from ..shared import lazyproperty
1414

1515

@@ -29,7 +29,7 @@ def load(cls, partname, content_type, blob, package):
2929
Provides PartFactory interface for loading a styles part from a WML
3030
package.
3131
"""
32-
styles_elm = oxml_fromstring(blob)
32+
styles_elm = parse_xml(blob)
3333
styles_part = cls(partname, content_type, styles_elm, package)
3434
return styles_part
3535

tests/opc/test_pkgreader.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,7 @@ def it_raises_on_target_partname_when_external(self):
435435

436436
class Describe_SerializedRelationships(object):
437437

438-
def it_can_load_from_xml(
439-
self, oxml_fromstring_, _SerializedRelationship_):
438+
def it_can_load_from_xml(self, parse_xml_, _SerializedRelationship_):
440439
# mockery ----------------------
441440
baseURI, rels_item_xml, rel_elm_1, rel_elm_2 = (
442441
Mock(name='baseURI'), Mock(name='rels_item_xml'),
@@ -445,7 +444,7 @@ def it_can_load_from_xml(
445444
rels_elm = Mock(
446445
name='rels_elm', Relationship_lst=[rel_elm_1, rel_elm_2]
447446
)
448-
oxml_fromstring_.return_value = rels_elm
447+
parse_xml_.return_value = rels_elm
449448
# exercise ---------------------
450449
srels = _SerializedRelationships.load_from_xml(
451450
baseURI, rels_item_xml)
@@ -454,7 +453,7 @@ def it_can_load_from_xml(
454453
call(baseURI, rel_elm_1),
455454
call(baseURI, rel_elm_2),
456455
]
457-
oxml_fromstring_.assert_called_once_with(rels_item_xml)
456+
parse_xml_.assert_called_once_with(rels_item_xml)
458457
assert _SerializedRelationship_.call_args_list == expected_calls
459458
assert isinstance(srels, _SerializedRelationships)
460459

@@ -470,8 +469,8 @@ def it_should_be_iterable(self):
470469
# fixtures ---------------------------------------------
471470

472471
@pytest.fixture
473-
def oxml_fromstring_(self, request):
474-
return function_mock(request, 'docx.opc.pkgreader.oxml_fromstring')
472+
def parse_xml_(self, request):
473+
return function_mock(request, 'docx.opc.pkgreader.parse_xml')
475474

476475
@pytest.fixture
477476
def _SerializedRelationship_(self, request):

tests/opc/unitdata/rels.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from docx.opc.package import Relationships
1111

1212
from docx.opc.constants import NAMESPACE as NS
13-
from docx.opc.oxml import oxml_fromstring
13+
from docx.opc.oxml import parse_xml
1414

1515

1616
class BaseBuilder(object):
@@ -20,7 +20,7 @@ class BaseBuilder(object):
2020
@property
2121
def element(self):
2222
"""Return element based on XML generated by builder"""
23-
return oxml_fromstring(self.xml)
23+
return parse_xml(self.xml)
2424

2525
def with_indent(self, indent):
2626
"""Add integer *indent* spaces at beginning of element XML"""

0 commit comments

Comments
 (0)