Skip to content

Commit 74c06f9

Browse files
author
Steve Canny
committed
add CT_Types.add_default() and .add_override()
1 parent 9e8315d commit 74c06f9

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

opc/oxml.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,22 @@ class CT_Types(OxmlBaseElement):
180180
``<Types>`` element, the container element for Default and Override
181181
elements in [Content_Types].xml.
182182
"""
183+
def add_default(self, ext, content_type):
184+
"""
185+
Add a child ``<Default>`` element with attributes set to parameter
186+
values.
187+
"""
188+
default = CT_Default.new(ext, content_type)
189+
self.append(default)
190+
191+
def add_override(self, partname, content_type):
192+
"""
193+
Add a child ``<Override>`` element with attributes set to parameter
194+
values.
195+
"""
196+
override = CT_Override.new(partname, content_type)
197+
self.append(override)
198+
183199
@property
184200
def defaults(self):
185201
try:

tests/test_oxml.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,13 @@ def it_can_construct_a_new_types_element(self):
7474
types = CT_Types.new()
7575
expected_xml = a_Types().empty().xml
7676
assert types.xml == expected_xml
77+
78+
def it_can_build_types_element_incrementally(self):
79+
types = CT_Types.new()
80+
types.add_default('.xml', 'application/xml')
81+
types.add_default('.jpeg', 'image/jpeg')
82+
types.add_override('/docProps/core.xml', 'app/vnd.type1')
83+
types.add_override('/ppt/presentation.xml', 'app/vnd.type2')
84+
types.add_override('/docProps/thumbnail.jpeg', 'image/jpeg')
85+
expected_types_xml = a_Types().xml
86+
assert types.xml == expected_types_xml

tests/unitdata.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ def with_extension(self, extension):
5050
self._extension = extension
5151
return self
5252

53+
def without_namespace(self):
54+
"""Don't include an 'xmlns=' attribute"""
55+
self._namespace = ''
56+
return self
57+
5358
@property
5459
def xml(self):
5560
"""Return Default element"""
@@ -81,6 +86,11 @@ def with_partname(self, partname):
8186
self._partname = partname
8287
return self
8388

89+
def without_namespace(self):
90+
"""Don't include an 'xmlns=' attribute"""
91+
self._namespace = ''
92+
return self
93+
8494
@property
8595
def xml(self):
8696
"""Return Override element"""
@@ -152,11 +162,13 @@ def xml(self):
152162
xml += (a_Default().with_extension(extension)
153163
.with_content_type(content_type)
154164
.with_indent(2)
165+
.without_namespace()
155166
.xml)
156167
for partname, content_type in self._overrides:
157168
xml += (an_Override().with_partname(partname)
158169
.with_content_type(content_type)
159170
.with_indent(2)
171+
.without_namespace()
160172
.xml)
161173
xml += '</Types>\n'
162174
return xml

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy