Skip to content

Commit 7cf579b

Browse files
author
Steve Canny
committed
add custom part class registration
opc.package.PartFactory allows a custom part class to be registered for a content type and uses that class when called with a matching content type.
1 parent 2af7e11 commit 7cf579b

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

opc/package.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,12 @@ class PartFactory(object):
167167
Provides a way for client code to specify a subclass of |Part| to be
168168
constructed by |Unmarshaller| based on its content type.
169169
"""
170+
part_type_for = {}
171+
170172
def __new__(cls, partname, content_type, blob):
173+
if content_type in PartFactory.part_type_for:
174+
CustomPartClass = PartFactory.part_type_for[content_type]
175+
return CustomPartClass(partname, content_type, blob)
171176
return Part(partname, content_type, blob)
172177

173178

tests/test_package.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from mock import call, Mock, patch, PropertyMock
1515

16+
from opc.constants import CONTENT_TYPE as CT
1617
from opc.oxml import CT_Relationships
1718
from opc.package import (
1819
OpcPackage, Part, PartFactory, _Relationship, RelationshipCollection,
@@ -192,6 +193,18 @@ def it_constructs_a_part_instance(self, Part_):
192193
Part_.assert_called_once_with(partname, content_type, blob)
193194
assert part == Part_.return_value
194195

196+
def it_constructs_custom_part_type_for_registered_content_types(self):
197+
# mockery ----------------------
198+
CustomPartClass = Mock(name='CustomPartClass')
199+
partname, blob = (Mock(name='partname'), Mock(name='blob'))
200+
# exercise ---------------------
201+
PartFactory.part_type_for[CT.WML_DOCUMENT_MAIN] = CustomPartClass
202+
part = PartFactory(partname, CT.WML_DOCUMENT_MAIN, blob)
203+
# verify -----------------------
204+
CustomPartClass.assert_called_once_with(partname,
205+
CT.WML_DOCUMENT_MAIN, blob)
206+
assert part is CustomPartClass.return_value
207+
195208

196209
class Describe_Relationship(object):
197210

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