Skip to content

Commit c6a48fb

Browse files
author
Steve Canny
committed
opc: refactor _ContentTypesItem to instantiate
Keep defaults and overrides as instance variables and change methods from static to instance methods. It's still a one-shot object, but the call structure is cleaner without having to pass defaults and overrides around everywhere.
1 parent abc2073 commit c6a48fb

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

docx/opc/pkgwriter.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,50 +66,50 @@ def _write_pkg_rels(phys_writer, pkg_rels):
6666
class _ContentTypesItem(object):
6767
"""
6868
Service class that composes a content types item ([Content_Types].xml)
69-
based on a list of parts. Not meant to be instantiated, its single
70-
interface method is xml_for(), e.g. ``_ContentTypesItem.xml_for(parts)``.
69+
based on a list of parts. Not meant to be instantiated directly, its
70+
single interface method is xml_for(), e.g.
71+
``_ContentTypesItem.xml_for(parts)``.
7172
"""
72-
@staticmethod
73-
def xml_for(parts):
73+
def __init__(self):
74+
self._defaults = CaseInsensitiveDict()
75+
self._overrides = dict()
76+
77+
@classmethod
78+
def xml_for(cls, parts):
7479
"""
7580
Return content types XML mapping each part in *parts* to the
7681
appropriate content type and suitable for storage as
7782
``[Content_Types].xml`` in an OPC package.
7883
"""
79-
defaults = CaseInsensitiveDict()
80-
defaults['.rels'] = CT.OPC_RELATIONSHIPS
81-
defaults['.xml'] = CT.XML
82-
overrides = dict()
84+
cti = cls()
85+
cti._defaults['.rels'] = CT.OPC_RELATIONSHIPS
86+
cti._defaults['.xml'] = CT.XML
8387
for part in parts:
84-
_ContentTypesItem._add_content_type(
85-
defaults, overrides, part.partname, part.content_type
86-
)
87-
return _ContentTypesItem._xml(defaults, overrides)
88+
cti._add_content_type(part.partname, part.content_type)
89+
return cti._xml()
8890

89-
@staticmethod
90-
def _add_content_type(defaults, overrides, partname, content_type):
91+
def _add_content_type(self, partname, content_type):
9192
"""
9293
Add a content type for the part with *partname* and *content_type*,
9394
using a default or override as appropriate.
9495
"""
9596
ext = partname.ext
9697
if (ext.lower(), content_type) in default_content_types:
97-
defaults[ext] = content_type
98+
self._defaults[ext] = content_type
9899
else:
99-
overrides[partname] = content_type
100+
self._overrides[partname] = content_type
100101

101-
@staticmethod
102-
def _xml(defaults, overrides):
102+
def _xml(self):
103103
"""
104-
XML form of this content types item, suitable for storage as
104+
Return XML form of this content types item, suitable for storage as
105105
``[Content_Types].xml`` in an OPC package. Although the sequence of
106106
elements is not strictly significant, as an aid to testing and
107107
readability Default elements are sorted by extension and Override
108108
elements are sorted by partname.
109109
"""
110110
_types_elm = CT_Types.new()
111-
for ext in sorted(defaults.keys()):
112-
_types_elm.add_default(ext, defaults[ext])
113-
for partname in sorted(overrides.keys()):
114-
_types_elm.add_override(partname, overrides[partname])
111+
for ext in sorted(self._defaults.keys()):
112+
_types_elm.add_default(ext, self._defaults[ext])
113+
for partname in sorted(self._overrides.keys()):
114+
_types_elm.add_override(partname, self._overrides[partname])
115115
return serialize_part_xml(_types_elm)

0 commit comments

Comments
 (0)