def match_default_fixture(self, request): partname_str, ext, content_type = request.param partname = PackURI(partname_str) ct_map = _ContentTypeMap() ct_map._add_override(PackURI('/bar/foo.xyz'), 'application/xyz') ct_map._add_default(ext, content_type) return ct_map, partname, content_type
def match_override_fixture(self, request): partname_str, should_match_partname_str = request.param partname = PackURI(partname_str) should_match_partname = PackURI(should_match_partname_str) content_type = 'appl/vnd-foobar' ct_map = _ContentTypeMap() ct_map._add_override(partname, content_type) return ct_map, should_match_partname, content_type
def it_can_calculate_relative_ref_value(self): cases = ( ('/', '/ppt/presentation.xml', 'ppt/presentation.xml'), ('/ppt', '/ppt/slideMasters/slideMaster1.xml', 'slideMasters/slideMaster1.xml'), ('/ppt/slides', '/ppt/slideLayouts/slideLayout1.xml', '../slideLayouts/slideLayout1.xml'), ) for baseURI, uri_str, expected_relative_ref in cases: pack_uri = PackURI(uri_str) assert pack_uri.relative_ref(baseURI) == expected_relative_ref
def _mock_part(self, request, name, partname_str, content_type): partname = PackURI(partname_str) return instance_mock(request, Part, name=name, partname=partname, content_type=content_type)
def default(cls, package): """ Return a newly created styles part, containing a default set of elements. """ partname = PackURI('/word/footnotes.xml') content_type = CT.WML_FOOTNOTES element = parse_xml(cls._default_xml()) return cls(partname, content_type, element, package)
def it_should_have_relative_ref_for_internal_rel(self): """ Internal relationships (TargetMode == 'Internal' in the XML) should have a relative ref, e.g. '../slideLayouts/slideLayout1.xml', for the target_ref attribute. """ part = Mock(name='part', partname=PackURI('/ppt/media/image1.png')) baseURI = '/ppt/slides' rel = _Relationship(None, None, part, baseURI) # external=False assert rel.target_ref == '../media/image1.png'
def filename_fixture(self, request, image_): partname = PackURI('/word/media/image666.png') if request.param == 'loaded': image_part = ImagePart(partname, None, None, None) expected_filename = 'image.png' elif request.param == 'new': image_.filename = 'foobar.PXG' image_part = ImagePart(partname, None, None, image_) expected_filename = image_.filename return image_part, expected_filename
def next_partname(self, template): """Return a |PackURI| instance representing partname matching *template*. The returned part-name has the next available numeric suffix to distinguish it from other parts of its type. *template* is a printf (%)-style template string containing a single replacement item, a '%d' to be used to insert the integer portion of the partname. Example: "/word/header%d.xml" """ partnames = {part.partname for part in self.iter_parts()} for n in range(1, len(partnames) + 2): candidate_partname = template % n if candidate_partname not in partnames: return PackURI(candidate_partname)
def cases(self, expected_values): """ Return list of tuples zipped from uri_str cases and *expected_values*. Raise if lengths don't match. """ uri_str_cases = [ '/', '/ppt/presentation.xml', '/ppt/slides/slide1.xml', ] if len(expected_values) != len(uri_str_cases): msg = "len(expected_values) differs from len(uri_str_cases)" raise AssertionError(msg) pack_uris = [PackURI(uri_str) for uri_str in uri_str_cases] return zip(pack_uris, expected_values)
def target_partname(self): """ |PackURI| instance containing partname targeted by this relationship. Raises ``ValueError`` on reference if target_mode is ``'External'``. Use :attr:`target_mode` to check before referencing. """ if self.is_external: msg = ('target_partname attribute on Relationship is undefined w' 'here TargetMode == "External"') raise ValueError(msg) # lazy-load _target_partname attribute if not hasattr(self, '_target_partname'): self._target_partname = PackURI.from_rel_ref( self._baseURI, self.target_ref) return self._target_partname
def it_can_write_a_blob(self, pkg_file): # setup ------------------------ pack_uri = PackURI('/part/name.xml') blob = '<BlobbityFooBlob/>'.encode('utf-8') # exercise --------------------- pkg_writer = PhysPkgWriter(pkg_file) pkg_writer.write(pack_uri, blob) pkg_writer.close() # verify ----------------------- written_blob_sha1 = hashlib.sha1(blob).hexdigest() zipf = ZipFile(pkg_file, 'r') retrieved_blob = zipf.read(pack_uri.membername) zipf.close() retrieved_blob_sha1 = hashlib.sha1(retrieved_blob).hexdigest() assert retrieved_blob_sha1 == written_blob_sha1
def dimensions_fixture(self, request): image_file_path = test_file('monty-truth.png') image = Image.from_file(image_file_path) expected_cx, expected_cy = 1905000, 2717800 # case 1: image part is loaded by PartFactory w/no Image inst if request.param == 'loaded': partname = PackURI('/word/media/image1.png') content_type = CT.PNG image_part = ImagePart.load(partname, content_type, image.blob, None) # case 2: image part is newly created from image file elif request.param == 'new': image_part = ImagePart.from_image(image, None) return image_part, expected_cx, expected_cy
def it_should_raise_on_partname_not_found(self): ct_map = _ContentTypeMap() with pytest.raises(KeyError): ct_map[PackURI('/!blat/rhumba.1x&')]
def it_should_raise_on_construct_with_bad_pack_uri_str(self): with pytest.raises(ValueError): PackURI('foobar')
def it_can_construct_from_relative_ref(self): baseURI = '/ppt/slides' relative_ref = '../slideLayouts/slideLayout1.xml' pack_uri = PackURI.from_rel_ref(baseURI, relative_ref) assert pack_uri == '/ppt/slideLayouts/slideLayout1.xml'
def it_can_retrieve_the_blob_for_a_pack_uri(self, phys_reader): pack_uri = PackURI('/word/document.xml') blob = phys_reader.blob_for(pack_uri) sha1 = hashlib.sha1(blob).hexdigest() assert sha1 == 'b9b4a98bcac7c5a162825b60c3db7df11e02ac5f'
def new(cls, package, element): """ """ partname = PackURI('/word/styles.xml') content_type = CT.SML_STYLES return cls(partname, content_type, element, package)
def partname_set_fixture(self): old_partname = PackURI('/old/part/name') new_partname = PackURI('/new/part/name') part = Part(old_partname, None, None, None) return part, new_partname
def partname_get_fixture(self): partname = PackURI('/part/name') part = Part(partname, None, None, None) return part, partname
def image_partname(n): return PackURI('/word/media/image%d.png' % n)
def image_partname(n): return PackURI('/word/media/image%d.%s' % (n, ext))
def it_can_retrieve_the_blob_for_a_pack_uri(self, dir_reader): pack_uri = PackURI('/word/document.xml') blob = dir_reader.blob_for(pack_uri) sha1 = hashlib.sha1(blob).hexdigest() assert sha1 == '0e62d87ea74ea2b8088fd11ee97b42da9b4c77b0'
def it_returns_none_when_part_has_no_rels_xml(self, dir_reader): partname = PackURI('/ppt/viewProps.xml') rels_xml = dir_reader.rels_xml_for(partname) assert rels_xml is None
def new(cls, package, element): """ """ partname = PackURI('/xl/sharedStrings.xml') content_type = CT.SML_SHARED_STRINGS return cls(partname, content_type, element, package)
def it_should_raise_on_key_not_instance_of_PackURI(self): ct_map = _ContentTypeMap() ct_map._overrides = {PackURI('/part/name1.xml'): 'app/vnd.type1'} with pytest.raises(KeyError): ct_map['/part/name1.xml']