Skip to content

Commit 7423e5c

Browse files
author
Steve Canny
committed
add PackageWriter._write_content_types_stream()
1 parent 44d2f83 commit 7423e5c

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

opc/pkgwriter.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Convention (OPC) package, essentially an implementation of OpcPackage.save()
1313
"""
1414

15+
from opc.packuri import CONTENT_TYPES_URI
1516
from opc.phys_pkg import PhysPkgWriter
1617

1718

@@ -41,7 +42,7 @@ def _write_content_types_stream(phys_writer, parts):
4142
Write ``[Content_Types].xml`` part to the physical package with an
4243
appropriate content type lookup target for each part in *parts*.
4344
"""
44-
raise NotImplementedError()
45+
phys_writer.write(CONTENT_TYPES_URI, _ContentTypesItem.xml_for(parts))
4546

4647
@staticmethod
4748
def _write_parts(phys_writer, parts):
@@ -58,3 +59,18 @@ def _write_pkg_rels(phys_writer, pkg_rels):
5859
package.
5960
"""
6061
raise NotImplementedError()
62+
63+
64+
class _ContentTypesItem(object):
65+
"""
66+
Service class that composes a content types item ([Content_Types].xml)
67+
based on a list of parts. Not meant to be instantiated, its single
68+
interface method is xml_for(), e.g. ``_ContentTypesItem.xml_for(parts)``.
69+
"""
70+
@staticmethod
71+
def xml_for(parts):
72+
"""
73+
Return content types XML mapping each part in *parts* to the
74+
appropriate content type and suitable for storage as
75+
``[Content_Types].xml`` in an OPC package.
76+
"""

tests/test_pkgwriter.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
from mock import call, Mock, patch
1515

16-
from opc.pkgwriter import PackageWriter
16+
from opc.pkgwriter import _ContentTypesItem, PackageWriter
17+
18+
from .unitutil import method_mock
1719

1820

1921
class DescribePackageWriter(object):
@@ -24,6 +26,10 @@ def PhysPkgWriter_(self, request):
2426
request.addfinalizer(_patch.stop)
2527
return _patch.start()
2628

29+
@pytest.fixture
30+
def xml_for(self, request):
31+
return method_mock(_ContentTypesItem, 'xml_for', request)
32+
2733
@pytest.fixture
2834
def _write_methods(self, request):
2935
"""Mock that patches all the _write_* methods of PackageWriter"""
@@ -60,3 +66,14 @@ def it_can_write_a_package(self, PhysPkgWriter_, _write_methods):
6066
PhysPkgWriter_.assert_called_once_with(pkg_file)
6167
assert _write_methods.mock_calls == expected_calls
6268
phys_writer.close.assert_called_once_with()
69+
70+
def it_can_write_a_content_types_stream(self, xml_for):
71+
# mockery ----------------------
72+
phys_writer = Mock(name='phys_writer')
73+
parts = Mock(name='parts')
74+
# exercise ---------------------
75+
PackageWriter._write_content_types_stream(phys_writer, parts)
76+
# verify -----------------------
77+
xml_for.assert_called_once_with(parts)
78+
phys_writer.write.assert_called_once_with('/[Content_Types].xml',
79+
xml_for.return_value)

0 commit comments

Comments
 (0)