Skip to content

Commit 6c74e23

Browse files
author
Steve Canny
committed
opc: refactor _ContentTypeMap.from_xml()
* extract ._add_default() and ._add_override()
1 parent d815baf commit 6c74e23

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

docx/opc/pkgreader.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,27 @@ def from_xml(content_types_xml):
140140
of *content_types_xml*.
141141
"""
142142
types_elm = oxml_fromstring(content_types_xml)
143-
ctmap = _ContentTypeMap()
144-
ctmap._overrides = dict(
145-
(o.partname, o.content_type) for o in types_elm.overrides
146-
)
147-
ctmap._defaults = dict(
148-
('.%s' % d.extension, d.content_type) for d in types_elm.defaults
149-
)
150-
return ctmap
143+
ct_map = _ContentTypeMap()
144+
for o in types_elm.overrides:
145+
ct_map._add_override(o.partname, o.content_type)
146+
for d in types_elm.defaults:
147+
ct_map._add_default(d.extension, d.content_type)
148+
return ct_map
149+
150+
def _add_default(self, extension, content_type):
151+
"""
152+
Add the default mapping of *extension* to *content_type* to this
153+
content type mapping.
154+
"""
155+
ext = '.%s' % extension
156+
self._defaults[ext] = content_type
157+
158+
def _add_override(self, partname, content_type):
159+
"""
160+
Add the default mapping of *partname* to *content_type* to this
161+
content type mapping.
162+
"""
163+
self._overrides[partname] = content_type
151164

152165

153166
class _SerializedPart(object):

0 commit comments

Comments
 (0)