def save_to_dir(self, dirpath): """ Save each of the items in this package as a file in a directory at *dirpath*, using the pack URI as the relative path of each file. If the directory exists, it is deleted (recursively) before being recreated. """ PhysPkg.write_to_dir(self._blobs, dirpath)
def it_can_write_a_blob_collection_to_a_directory(self, uri_, uri_2_, blob_, blob_2_, PhysPkg_): # fixture ---------------------- blobs = BlobCollection(((uri_, blob_), (uri_2_, blob_2_))) # exercise --------------------- PhysPkg.write_to_dir(blobs, DIRPATH) # verify ----------------------- PhysPkg_._clear_or_make_dir.assert_called_once_with(DIRPATH) PhysPkg_._write_blob_to_dir.assert_has_calls( (call(DIRPATH, uri_, blob_), call(DIRPATH, uri_2_, blob_2_)), any_order=True )
def it_can_write_a_blob_collection_to_a_zip(self, tmpdir): # fixture ---------------------- uri, uri_2 = "foo/bar.xml", "foo/_rels/bar.xml.rels" blob, blob_2 = b"blob", b"blob_2" blobs_in = {uri: blob, uri_2: blob_2} zip_path = str(tmpdir.join("foobar.xml")) # exercise --------------------- PhysPkg.write_to_zip(blobs_in, zip_path) # verify ----------------------- assert os.path.isfile(zip_path) zipf = ZipFile(zip_path, "r") blobs_out = dict([(name, zipf.read(name)) for name in zipf.namelist()]) zipf.close() assert blobs_out == blobs_in
def it_can_write_a_blob_to_a_file_in_a_directory(self, tmpdir): """Note: tests integration with filesystem""" # fixture ---------------------- uri = "foo/bar.xml" blob = b"blob" dirpath = str(tmpdir) filepath = os.path.join(dirpath, "foo", "bar.xml") # exercise --------------------- PhysPkg._write_blob_to_dir(str(tmpdir), uri, blob) # verify ----------------------- assert os.path.isfile(filepath) with open(filepath, "rb") as f: actual_blob = f.read() assert actual_blob == blob
def read(path): """ Factory method to construct a new |Package| instance from package at *path*. The package can be either a zip archive (e.g. .docx file) or a directory containing an extracted package. """ phys_pkg = PhysPkg.read(path) pkg_items = {} for uri, blob in phys_pkg: pkg_items[uri] = PkgItem(phys_pkg.root_uri, uri, blob) return Package(pkg_items)
def it_can_create_a_new_empty_directory(self): """Note: tests integration with filesystem""" # case: created if does not exist # ------------------------------ if os.path.exists(DELETEME_DIR): shutil.rmtree(DELETEME_DIR) PhysPkg._clear_or_make_dir(FOOBAR_DIR) assert os.path.exists(FOOBAR_DIR) # case: re-created if exists # ------------------------------ if os.path.exists(DELETEME_DIR): shutil.rmtree(DELETEME_DIR) os.makedirs(FOOBAR_DIR) with open(FOOBAR_FILEPATH, "w") as f: f.write("foobar file") PhysPkg._clear_or_make_dir(FOOBAR_DIR) assert os.path.exists(FOOBAR_DIR) assert not os.path.exists(FOOBAR_FILEPATH) # case: raises if dirpath is file # ------------------------------ shutil.rmtree(DELETEME_DIR) os.makedirs(DELETEME_DIR) with open(FOOBAR_DIR, "w") as f: f.write("foobar file at FOOBAR_DIR path") with pytest.raises(ValueError): PhysPkg._clear_or_make_dir(FOOBAR_DIR)
def save(self, path): """ Save this package to a zip archive at *path*. """ PhysPkg.write_to_zip(self._blobs, path)
def it_should_construct_the_appropriate_subclass(self): pkg = PhysPkg.read(MINI_ZIP_PKG_PATH) assert isinstance(pkg, ZipPhysPkg) pkg = PhysPkg.read(MINI_DIR_PKG_PATH) assert isinstance(pkg, DirPhysPkg)
def it_should_close_zip_file_after_use(self, ZipFile_, zip_file_): # exercise --------------------- PhysPkg.read(MINI_ZIP_PKG_PATH) # verify ----------------------- ZipFile_.assert_called_once_with(MINI_ZIP_PKG_PATH, "r") zip_file_.close.assert_called_with()
def it_should_close_zip_file_after_use(self, ZipFile_, zip_file_): # exercise --------------------- PhysPkg.write_to_zip({}, "foobar") # verify ----------------------- ZipFile_.assert_called_once_with("foobar", "w", ZIP_DEFLATED) zip_file_.close.assert_called_with()