Skip to content

Commit eb9c314

Browse files
author
Steve Canny
committed
add PackageWriter.write()
1 parent e1927e5 commit eb9c314

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

opc/phys_pkg.py

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ def __new__(cls, pkg_file):
2222
return ZipPkgReader(pkg_file)
2323

2424

25+
class PhysPkgWriter(object):
26+
"""
27+
Factory for physical package writer objects.
28+
"""
29+
30+
2531
class ZipPkgReader(object):
2632
"""
2733
Implements |PhysPkgReader| interface for a zip file OPC package.

opc/pkgwriter.py

+30
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
Convention (OPC) package, essentially an implementation of OpcPackage.save()
1313
"""
1414

15+
from opc.phys_pkg import PhysPkgWriter
16+
1517

1618
class PackageWriter(object):
1719
"""
@@ -27,4 +29,32 @@ def write(pkg_file, pkg_rels, parts):
2729
*pkg_rels* and *parts* and a content types stream based on the
2830
content types of the parts.
2931
"""
32+
phys_writer = PhysPkgWriter(pkg_file)
33+
PackageWriter._write_content_types_stream(phys_writer, parts)
34+
PackageWriter._write_pkg_rels(phys_writer, pkg_rels)
35+
PackageWriter._write_parts(phys_writer, parts)
36+
phys_writer.close()
37+
38+
@staticmethod
39+
def _write_content_types_stream(phys_writer, parts):
40+
"""
41+
Write ``[Content_Types].xml`` part to the physical package with an
42+
appropriate content type lookup target for each part in *parts*.
43+
"""
44+
raise NotImplementedError()
45+
46+
@staticmethod
47+
def _write_parts(phys_writer, parts):
48+
"""
49+
Write the blob of each part in *parts* to the package, along with a
50+
rels item for its relationships if and only if it has any.
51+
"""
52+
raise NotImplementedError()
53+
54+
@staticmethod
55+
def _write_pkg_rels(phys_writer, pkg_rels):
56+
"""
57+
Write the XML rels item for *pkg_rels* ('/_rels/.rels') to the
58+
package.
59+
"""
3060
raise NotImplementedError()

tests/test_pkgwriter.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# test_pkgwriter.py
4+
#
5+
# Copyright (C) 2013 Steve Canny scanny@cisco.com
6+
#
7+
# This module is part of python-pptx and is released under the MIT License:
8+
# http://www.opensource.org/licenses/mit-license.php
9+
10+
"""Test suite for opc.pkgwriter module."""
11+
12+
import pytest
13+
14+
from mock import call, Mock, patch
15+
16+
from opc.pkgwriter import PackageWriter
17+
18+
19+
class DescribePackageWriter(object):
20+
21+
@pytest.fixture
22+
def PhysPkgWriter_(self, request):
23+
_patch = patch('opc.pkgwriter.PhysPkgWriter')
24+
request.addfinalizer(_patch.stop)
25+
return _patch.start()
26+
27+
@pytest.fixture
28+
def _write_methods(self, request):
29+
"""Mock that patches all the _write_* methods of PackageWriter"""
30+
root_mock = Mock(name='PackageWriter')
31+
patch1 = patch.object(PackageWriter, '_write_content_types_stream')
32+
patch2 = patch.object(PackageWriter, '_write_pkg_rels')
33+
patch3 = patch.object(PackageWriter, '_write_parts')
34+
root_mock.attach_mock(patch1.start(), '_write_content_types_stream')
35+
root_mock.attach_mock(patch2.start(), '_write_pkg_rels')
36+
root_mock.attach_mock(patch3.start(), '_write_parts')
37+
38+
def fin():
39+
patch1.stop()
40+
patch2.stop()
41+
patch3.stop()
42+
43+
request.addfinalizer(fin)
44+
return root_mock
45+
46+
def it_can_write_a_package(self, PhysPkgWriter_, _write_methods):
47+
# mockery ----------------------
48+
pkg_file = Mock(name='pkg_file')
49+
pkg_rels = Mock(name='pkg_rels')
50+
parts = Mock(name='parts')
51+
phys_writer = PhysPkgWriter_.return_value
52+
# exercise ---------------------
53+
PackageWriter.write(pkg_file, pkg_rels, parts)
54+
# verify -----------------------
55+
expected_calls = [
56+
call._write_content_types_stream(phys_writer, parts),
57+
call._write_pkg_rels(phys_writer, pkg_rels),
58+
call._write_parts(phys_writer, parts),
59+
]
60+
PhysPkgWriter_.assert_called_once_with(pkg_file)
61+
assert _write_methods.mock_calls == expected_calls
62+
phys_writer.close.assert_called_once_with()

0 commit comments

Comments
 (0)