Skip to content

Commit 7064640

Browse files
author
Steve Canny
committed
add ZipPkgWriter.write()
1 parent 7423e5c commit 7064640

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

opc/phys_pkg.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,10 @@ def close(self):
8686
releasing any resources it's using.
8787
"""
8888
self._zipf.close()
89+
90+
def write(self, pack_uri, blob):
91+
"""
92+
Write *blob* to this zip package with the membername corresponding to
93+
*pack_uri*.
94+
"""
95+
self._zipf.writestr(pack_uri.membername, blob)

tests/test_phys_pkg.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99

1010
"""Test suite for opc.phys_pkg module."""
1111

12+
try:
13+
from io import BytesIO # Python 3
14+
except ImportError:
15+
from StringIO import StringIO as BytesIO
16+
1217
import hashlib
1318

14-
from zipfile import ZIP_DEFLATED
19+
from zipfile import ZIP_DEFLATED, ZipFile
1520

1621
from opc.packuri import PACKAGE_URI, PackURI
1722
from opc.phys_pkg import (
@@ -110,6 +115,12 @@ def it_returns_none_when_part_has_no_rels_xml(self, phys_reader):
110115

111116
class DescribeZipPkgWriter(object):
112117

118+
@pytest.fixture
119+
def pkg_file(self, request):
120+
pkg_file = BytesIO()
121+
request.addfinalizer(pkg_file.close)
122+
return pkg_file
123+
113124
def it_opens_pkg_file_zip_on_construction(self, ZipFile_):
114125
pkg_file = Mock(name='pkg_file')
115126
ZipPkgWriter(pkg_file)
@@ -124,3 +135,19 @@ def it_can_be_closed(self, ZipFile_):
124135
zip_pkg_writer.close()
125136
# verify -----------------------
126137
zipf.close.assert_called_once_with()
138+
139+
def it_can_write_a_blob(self, pkg_file):
140+
# setup ------------------------
141+
pack_uri = PackURI('/part/name.xml')
142+
blob = '<BlobbityFooBlob/>'.encode('utf-8')
143+
# exercise ---------------------
144+
pkg_writer = PhysPkgWriter(pkg_file)
145+
pkg_writer.write(pack_uri, blob)
146+
pkg_writer.close()
147+
# verify -----------------------
148+
written_blob_sha1 = hashlib.sha1(blob).hexdigest()
149+
zipf = ZipFile(pkg_file, 'r')
150+
retrieved_blob = zipf.read(pack_uri.membername)
151+
zipf.close()
152+
retrieved_blob_sha1 = hashlib.sha1(retrieved_blob).hexdigest()
153+
assert retrieved_blob_sha1 == written_blob_sha1

0 commit comments

Comments
 (0)