Skip to content

Commit b3d2544

Browse files
author
Steve Canny
committed
add CT_Types.defaults
1 parent 9936afa commit b3d2544

3 files changed

Lines changed: 51 additions & 3 deletions

File tree

opc/oxml.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ class CT_Types(OxmlBaseElement):
154154
``<Types>`` element, the container element for Default and Override
155155
elements in [Content_Types].xml.
156156
"""
157+
@property
158+
def defaults(self):
159+
try:
160+
return self.Default[:]
161+
except AttributeError:
162+
return []
163+
157164
@property
158165
def overrides(self):
159166
try:

tests/test_oxml.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"""Test suite for opc.oxml module."""
1111

1212
from opc.constants import RELATIONSHIP_TARGET_MODE as RTM
13-
from opc.oxml import CT_Override
13+
from opc.oxml import CT_Default, CT_Override
1414

1515
from .unitdata import a_Default, an_Override, a_Relationship, a_Types
1616

@@ -43,8 +43,19 @@ def it_provides_read_access_to_xml_values(self):
4343

4444
class DescribeCT_Types(object):
4545

46+
def it_provides_access_to_default_child_elements(self):
47+
types = a_Types().element
48+
assert len(types.defaults) == 2
49+
for default in types.defaults:
50+
assert isinstance(default, CT_Default)
51+
4652
def it_provides_access_to_override_child_elements(self):
4753
types = a_Types().element
4854
assert len(types.overrides) == 3
4955
for override in types.overrides:
5056
assert isinstance(override, CT_Override)
57+
58+
def it_should_have_empty_list_on_no_matching_elements(self):
59+
types = a_Types().empty().element
60+
assert types.defaults == []
61+
assert types.overrides == []

tests/unitdata.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,26 @@ def __init__(self):
3737
"""Establish instance variables with default values"""
3838
self._content_type = 'application/xml'
3939
self._extension = 'xml'
40+
self._indent = 0
4041
self._namespace = ' xmlns="%s"' % NS.OPC_CONTENT_TYPES
4142

43+
def with_content_type(self, content_type):
44+
"""Set ContentType attribute to *content_type*"""
45+
self._content_type = content_type
46+
return self
47+
48+
def with_extension(self, extension):
49+
"""Set Extension attribute to *extension*"""
50+
self._extension = extension
51+
return self
52+
4253
@property
4354
def xml(self):
4455
"""Return Default element"""
45-
tmpl = '<Default%s Extension="%s" ContentType="%s"/>\n'
46-
return tmpl % (self._namespace, self._extension, self._content_type)
56+
tmpl = '%s<Default%s Extension="%s" ContentType="%s"/>\n'
57+
indent = ' ' * self._indent
58+
return tmpl % (indent, self._namespace, self._extension,
59+
self._content_type)
4760

4861

4962
class CT_OverrideBuilder(BaseBuilder):
@@ -111,18 +124,35 @@ class CT_TypesBuilder(BaseBuilder):
111124
"""
112125
def __init__(self):
113126
"""Establish instance variables with default values"""
127+
self._defaults = (
128+
('xml', 'application/xml'),
129+
('jpeg', 'image/jpeg'),
130+
)
131+
self._empty = False
114132
self._overrides = (
115133
('/docProps/core.xml', 'app/vnd.type1'),
116134
('/ppt/presentation.xml', 'app/vnd.type2'),
117135
('/docProps/thumbnail.jpeg', 'image/jpeg'),
118136
)
119137

138+
def empty(self):
139+
self._empty = True
140+
return self
141+
120142
@property
121143
def xml(self):
122144
"""
123145
Return XML string based on settings accumulated via method calls
124146
"""
147+
if self._empty:
148+
return '<Types xmlns="%s"/>\n' % NS.OPC_CONTENT_TYPES
149+
125150
xml = '<Types xmlns="%s">\n' % NS.OPC_CONTENT_TYPES
151+
for extension, content_type in self._defaults:
152+
xml += (a_Default().with_extension(extension)
153+
.with_content_type(content_type)
154+
.with_indent(2)
155+
.xml)
126156
for partname, content_type in self._overrides:
127157
xml += (an_Override().with_partname(partname)
128158
.with_content_type(content_type)

0 commit comments

Comments
 (0)