def it_can_save_itself_to_a_zip(self, pkg_item_dict_, PhysPkg_, blob_collection_): # fixture ---------------------- package = Package(pkg_item_dict_) # exercise --------------------- package.save(PACKAGE_PATH) # verify ----------------------- PhysPkg_.write_to_zip.assert_called_once_with(blob_collection_, PACKAGE_PATH)
def it_can_save_an_expanded_version_of_itself_to_a_directory(self, pkg_item_dict_, PhysPkg_, blob_collection_): # fixture ---------------------- package = Package(pkg_item_dict_) # exercise --------------------- package.save_to_dir(DIRPATH) # verify ----------------------- PhysPkg_.write_to_dir.assert_called_once_with(blob_collection_, DIRPATH)
def it_can_save_an_expanded_version_of_itself_to_a_directory( self, pkg_item_dict_, PhysPkg_, blob_collection_): # fixture ---------------------- package = Package(pkg_item_dict_) # exercise --------------------- package.save_to_dir(DIRPATH) # verify ----------------------- PhysPkg_.write_to_dir.assert_called_once_with(blob_collection_, DIRPATH)
def it_can_change_one_of_its_items_to_another(self, pkg_item_, pkg_item_2_): # fixture ---------------------- pkg_items = {"uri": pkg_item_} package = Package(pkg_items) pkg_item_2_.uri = "uri" pkg_item_2_.blob = "new blob" # exercise --------------------- package.substitute_item(pkg_item_2_) # verify ----------------------- assert pkg_item_.blob == "new blob"
def it_can_find_one_of_its_items_by_uri_tail(self, pkg_item_): # fixture ---------------------- pkg_items_ = {"head/tail": pkg_item_} package = Package(pkg_items_) # exercise --------------------- pkg_item = package.find_item_by_uri_tail("tail") # verify ----------------------- assert pkg_item == pkg_item_ with pytest.raises(KeyError): package.find_item_by_uri_tail("head")
def it_can_find_one_of_its_items_by_uri_tail(self, pkg_item_): # fixture ---------------------- pkg_items_ = {'head/tail': pkg_item_} package = Package(pkg_items_) # exercise --------------------- pkg_item = package.find_item_by_uri_tail('tail') # verify ----------------------- assert pkg_item == pkg_item_ with pytest.raises(KeyError): package.find_item_by_uri_tail('head')
def it_can_change_one_of_its_items_to_another(self, pkg_item_, pkg_item_2_): # fixture ---------------------- pkg_items = {'uri': pkg_item_} package = Package(pkg_items) pkg_item_2_.uri = 'uri' pkg_item_2_.blob = 'new blob' # exercise --------------------- package.substitute_item(pkg_item_2_) # verify ----------------------- assert pkg_item_.blob == 'new blob'
def diff_item(self, package_1_path, package_2_path, uri_tail): """ Display the meaningful differences between the item identified by *uri_tail* in the package at *package_1_path* and its counterpart in the package at *package_2_path*. Each path can be either a standard zip package (e.g. a .pptx file) or a directory containing an extracted package. """ package_1 = Package.read(package_1_path) package_2 = Package.read(package_2_path) diff = DiffPresenter.named_item_diff(package_1, package_2, uri_tail) OpcView.item_diff(diff)
def diff_pkg(self, package_1_path, package_2_path): """ Display the meaningful differences between the packages at *package_1_path* and *package_2_path*. Each path can be either a standard zip package (e.g. .pptx file) or a directory containing an extracted package. """ package_1 = Package.read(package_1_path) package_2 = Package.read(package_2_path) content_types_diff = DiffPresenter.named_item_diff( package_1, package_2, _CONTENT_TYPES_URI) rels_diffs = DiffPresenter.rels_diffs(package_1, package_2) xml_part_diffs = DiffPresenter.xml_part_diffs(package_1, package_2) OpcView.package_diff(content_types_diff, rels_diffs, xml_part_diffs)
def substitute(self, uri_tail, src_pkg_path, tgt_pkg_path, new_pkg_path): """ Substitute the package item identified by *uri_tail* from the package at *src_pkg_path* into the package at *tgt_pkg_path* and save the resulting package at *new_pkg_path*. This can be handy for identifying which package item(s) in the source package are causing a "repair needed" error when loading the target package in MS Office. """ package_1 = Package.read(src_pkg_path) package_2 = Package.read(tgt_pkg_path) pkg_item = package_1.find_item_by_uri_tail(uri_tail) package_2.substitute_item(pkg_item) package_2.save(new_pkg_path) OpcView.substitute(pkg_item.uri, src_pkg_path, tgt_pkg_path, new_pkg_path)
def repackage(self, package_path, new_package_path): """ Write the contents of the package found at *package_path* to a new zip package at *new_package_path*. """ package = Package.read(package_path) package.save(new_package_path)
def extract_package(self, package_path, extract_dirpath): """ Extract the contents of the package at *package_path* to individual files in a directory at *extract_dirpath*. """ package = Package.read(package_path) package.prettify_xml() package.save_to_dir(extract_dirpath)
def browse(self, pkg_path, uri_tail): """ Display pretty-printed XML contained in package item with URI ending with *uri_tail* in package at *pkg_path*. """ pkg = Package.read(pkg_path) pkg_item = pkg.find_item_by_uri_tail(uri_tail) item_presenter = ItemPresenter(pkg_item) OpcView.pkg_item(item_presenter)
def it_can_provide_a_list_of_rels_items_in_the_package( self, pkg_item_, pkg_item_2_, pkg_item_3_): # fixture ---------------------- pkg_items = { 'foo/bar.rels': pkg_item_, # should be sorted second 'joe/bob.xml': pkg_item_2_, # should be skipped 'bar/foo.rels': pkg_item_3_, # should be sorted first } package = Package(pkg_items) # exercise --------------------- rels_items = package.rels_items # verify ----------------------- assert rels_items == [pkg_item_3_, pkg_item_]
def it_can_provide_a_list_of_xml_parts_in_the_package( self, pkg_item_, pkg_item_2_, pkg_item_3_): # fixture ---------------------- pkg_items = { 'foobar': pkg_item_, # should be sorted second 'joebob': pkg_item_2_, # should be skipped 'barfoo': pkg_item_3_, # should be sorted first } package = Package(pkg_items) # exercise --------------------- xml_parts = package.xml_parts # verify ----------------------- assert xml_parts == [pkg_item_3_, pkg_item_]
def it_can_construct_from_a_filesystem_package( self, path_, root_uri_, uri_, uri_2_, blob_, blob_2_, PhysPkg_, PkgItem_, pkg_item_, pkg_item_2_, Package_ ): # exercise --------------------- pkg = Package.read(path_) # expected values -------------- expected_PkgItem_calls = [call(root_uri_, uri_, blob_), call(root_uri_, uri_2_, blob_2_)] expected_items = {uri_: pkg_item_, uri_2_: pkg_item_2_} # verify ----------------------- PhysPkg_.read.assert_called_once_with(path_) assert PkgItem_.call_count == 2 PkgItem_.assert_has_calls(expected_PkgItem_calls, any_order=True) Package_.assert_called_once_with(expected_items) assert isinstance(pkg, Package)
def it_can_construct_from_a_filesystem_package(self, path_, root_uri_, uri_, uri_2_, blob_, blob_2_, PhysPkg_, PkgItem_, pkg_item_, pkg_item_2_, Package_): # exercise --------------------- pkg = Package.read(path_) # expected values -------------- expected_PkgItem_calls = [ call(root_uri_, uri_, blob_), call(root_uri_, uri_2_, blob_2_) ] expected_items = {uri_: pkg_item_, uri_2_: pkg_item_2_} # verify ----------------------- PhysPkg_.read.assert_called_once_with(path_) assert PkgItem_.call_count == 2 PkgItem_.assert_has_calls(expected_PkgItem_calls, any_order=True) Package_.assert_called_once_with(expected_items) assert isinstance(pkg, Package)
def prettify_fixture(self, pkg_item_, pkg_item_2_): pkg_items = {1: pkg_item_, 2: pkg_item_2_} package = Package(pkg_items) return package, pkg_item_, pkg_item_2_