Skip to content

Commit 91ff987

Browse files
author
Steve Canny
committed
add _ContentTypeMap.__getitem__()
1 parent b3d2544 commit 91ff987

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

opc/pkgreader.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,25 @@ class _ContentTypeMap(object):
101101
Value type providing dictionary semantics for looking up content type by
102102
part name, e.g. ``content_type = cti['/ppt/presentation.xml']``.
103103
"""
104+
def __init__(self):
105+
super(_ContentTypeMap, self).__init__()
106+
self._overrides = dict()
107+
self._defaults = dict()
108+
109+
def __getitem__(self, partname):
110+
"""
111+
Return content type for part identified by *partname*.
112+
"""
113+
if not isinstance(partname, PackURI):
114+
tmpl = "_ContentTypeMap key must be <type 'PackURI'>, got %s"
115+
raise KeyError(tmpl % type(partname))
116+
if partname in self._overrides:
117+
return self._overrides[partname]
118+
if partname.ext in self._defaults:
119+
return self._defaults[partname.ext]
120+
tmpl = "no content type for partname '%s' in [Content_Types].xml"
121+
raise KeyError(tmpl % partname)
122+
104123
@staticmethod
105124
def from_xml(content_types_xml):
106125
"""

tests/test_pkgreader.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from mock import call, Mock, patch
1515

1616
from opc.constants import RELATIONSHIP_TARGET_MODE as RTM
17+
from opc.packuri import PackURI
1718
from opc.phys_pkg import ZipPkgReader
1819
from opc.pkgreader import (
1920
_ContentTypeMap, PackageReader, _SerializedRelationship,
@@ -230,6 +231,33 @@ def it_can_construct_from_types_xml(self, oxml_fromstring):
230231
assert ct_map._overrides == expected_overrides
231232
assert ct_map._defaults == expected_defaults
232233

234+
def it_matches_overrides(self):
235+
# test data --------------------
236+
partname = PackURI('/part/name1.xml')
237+
content_type = 'app/vnd.type1'
238+
# fixture ----------------------
239+
ct_map = _ContentTypeMap()
240+
ct_map._overrides = {partname: content_type}
241+
# verify -----------------------
242+
assert ct_map[partname] == content_type
243+
244+
def it_falls_back_to_defaults(self):
245+
ct_map = _ContentTypeMap()
246+
ct_map._overrides = {PackURI('/part/name1.xml'): 'app/vnd.type1'}
247+
ct_map._defaults = {'.xml': 'application/xml'}
248+
assert ct_map[PackURI('/part/name2.xml')] == 'application/xml'
249+
250+
def it_should_raise_on_partname_not_found(self):
251+
ct_map = _ContentTypeMap()
252+
with pytest.raises(KeyError):
253+
ct_map[PackURI('/!blat/rhumba.1x&')]
254+
255+
def it_should_raise_on_key_not_instance_of_PackURI(self):
256+
ct_map = _ContentTypeMap()
257+
ct_map._overrides = {PackURI('/part/name1.xml'): 'app/vnd.type1'}
258+
with pytest.raises(KeyError):
259+
ct_map['/part/name1.xml']
260+
233261

234262
class Describe_SerializedRelationship(object):
235263

0 commit comments

Comments
 (0)