Skip to content

Commit c577869

Browse files
author
Steve Canny
committed
opc: match Override on case-insensitive partname
1 parent 152f049 commit c577869

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

docx/opc/pkgreader.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
from __future__ import absolute_import
99

10+
from .constants import RELATIONSHIP_TARGET_MODE as RTM
1011
from .oxml import oxml_fromstring
1112
from .packuri import PACKAGE_URI, PackURI
1213
from .phys_pkg import PhysPkgReader
13-
from .constants import RELATIONSHIP_TARGET_MODE as RTM
14+
from .shared import CaseInsensitiveDict
1415

1516

1617
class PackageReader(object):
@@ -116,7 +117,7 @@ class _ContentTypeMap(object):
116117
"""
117118
def __init__(self):
118119
super(_ContentTypeMap, self).__init__()
119-
self._overrides = dict()
120+
self._overrides = CaseInsensitiveDict()
120121
self._defaults = dict()
121122

122123
def __getitem__(self, partname):

docx/opc/shared.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,28 @@
44
Objects shared by opc modules.
55
"""
66

7+
from __future__ import absolute_import, print_function, unicode_literals
8+
9+
10+
class CaseInsensitiveDict(dict):
11+
"""
12+
Mapping type that behaves like dict except that it matches without respect
13+
to the case of the key. E.g. cid['A'] == cid['a']. Note this is not
14+
general-purpose, just complete enough to satisfy opc package needs. It
15+
assumes str keys, and that it is created empty; keys passed in constructor
16+
are not accounted for
17+
"""
18+
def __contains__(self, key):
19+
return super(CaseInsensitiveDict, self).__contains__(key.lower())
20+
21+
def __getitem__(self, key):
22+
return super(CaseInsensitiveDict, self).__getitem__(key.lower())
23+
24+
def __setitem__(self, key, value):
25+
return super(CaseInsensitiveDict, self).__setitem__(
26+
key.lower(), value
27+
)
28+
729

830
def lazyproperty(f):
931
"""

tests/opc/test_pkgreader.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ def it_can_construct_from_ct_item_xml(self, from_xml_fixture):
271271
assert ct_map._defaults == expected_defaults
272272
assert ct_map._overrides == expected_overrides
273273

274-
def it_matches_an_override_on_partname(self, match_override_fixture):
274+
def it_matches_an_override_on_case_insensitive_partname(
275+
self, match_override_fixture):
275276
ct_map, partname, content_type = match_override_fixture
276277
assert ct_map[partname] == content_type
277278

@@ -316,6 +317,8 @@ def from_xml_fixture(self):
316317

317318
@pytest.fixture(params=[
318319
('/foo/bar.xml', '/foo/bar.xml'),
320+
('/foo/bar.xml', '/FOO/Bar.XML'),
321+
('/FoO/bAr.XmL', '/foo/bar.xml'),
319322
])
320323
def match_override_fixture(self, request):
321324
partname_str, should_match_partname_str = request.param

0 commit comments

Comments
 (0)