Beispiel #1
0
 def open(cls, pkg, is_from_file=True):
     """
     Return an |OpcPackage| instance loaded with the contents of
     *pkg_file*.
     """
     if is_from_file:
         pkg_reader = PackageReader.from_file(pkg)
     else:
         pkg_reader = PackageReader.from_str(pkg)
     package = cls()
     Unmarshaller.unmarshal(pkg_reader, package, PartFactory)
     return package
Beispiel #2
0
 def it_can_load_serialized_parts(self, _SerializedPart_, _walk_phys_parts):
     # test data --------------------
     test_data = (
         ('/part/name1.xml', 'app/vnd.type_1', 'reltype1', '<Part_1/>',
          'srels_1'),
         ('/part/name2.xml', 'app/vnd.type_2', 'reltype2', '<Part_2/>',
          'srels_2'),
     )
     iter_vals = [(t[0], t[2], t[3], t[4]) for t in test_data]
     content_types = dict((t[0], t[1]) for t in test_data)
     # mockery ----------------------
     phys_reader = Mock(name='phys_reader')
     pkg_srels = Mock(name='pkg_srels')
     _walk_phys_parts.return_value = iter_vals
     _SerializedPart_.side_effect = expected_sparts = (Mock(name='spart_1'),
                                                       Mock(name='spart_2'))
     # exercise ---------------------
     retval = PackageReader._load_serialized_parts(phys_reader, pkg_srels,
                                                   content_types)
     # verify -----------------------
     expected_calls = [
         call('/part/name1.xml', 'app/vnd.type_1', '<Part_1/>', 'reltype1',
              'srels_1'),
         call('/part/name2.xml', 'app/vnd.type_2', '<Part_2/>', 'reltype2',
              'srels_2'),
     ]
     assert _SerializedPart_.call_args_list == expected_calls
     assert retval == expected_sparts
 def it_can_load_serialized_parts(self, _SerializedPart_, _walk_phys_parts):
     # test data --------------------
     test_data = (
         ('/part/name1.xml', 'app/vnd.type_1', 'reltype1', '<Part_1/>',
          'srels_1'),
         ('/part/name2.xml', 'app/vnd.type_2', 'reltype2', '<Part_2/>',
          'srels_2'),
     )
     iter_vals = [(t[0], t[2], t[3], t[4]) for t in test_data]
     content_types = dict((t[0], t[1]) for t in test_data)
     # mockery ----------------------
     phys_reader = Mock(name='phys_reader')
     pkg_srels = Mock(name='pkg_srels')
     _walk_phys_parts.return_value = iter_vals
     _SerializedPart_.side_effect = expected_sparts = (
         Mock(name='spart_1'), Mock(name='spart_2')
     )
     # exercise ---------------------
     retval = PackageReader._load_serialized_parts(
         phys_reader, pkg_srels, content_types
     )
     # verify -----------------------
     expected_calls = [
         call('/part/name1.xml', 'app/vnd.type_1', '<Part_1/>',
              'reltype1', 'srels_1'),
         call('/part/name2.xml', 'app/vnd.type_2', '<Part_2/>',
              'reltype2', 'srels_2'),
     ]
     assert _SerializedPart_.call_args_list == expected_calls
     assert retval == expected_sparts
Beispiel #4
0
 def iter_sparts_fixture(self, sparts_, partnames_, content_types_,
                         reltypes_, blobs_):
     pkg_reader = PackageReader(None, None, sparts_)
     expected_iter_spart_items = [
         (partnames_[0], content_types_[0], reltypes_[0], blobs_[0]),
         (partnames_[1], content_types_[1], reltypes_[1], blobs_[1]),
     ]
     return pkg_reader, expected_iter_spart_items
Beispiel #5
0
 def open(cls, pkg_file):
     """
     Return an |OpcPackage| instance loaded with the contents of
     *pkg_file*.
     """
     pkg_reader = PackageReader.from_file(pkg_file)
     package = cls()
     Unmarshaller.unmarshal(pkg_reader, package, PartFactory)
     return package
Beispiel #6
0
 def it_can_walk_phys_pkg_parts(self, _srels_for):
     # test data --------------------
     # +----------+       +--------+
     # | pkg_rels |-----> | part_1 |
     # +----------+       +--------+
     #      |               |    ^
     #      v               v    |
     #   external         +--------+     +--------+
     #                    | part_2 |---> | part_3 |
     #                    +--------+     +--------+
     partname_1, partname_2, partname_3 = ('/part/name1.xml',
                                           '/part/name2.xml',
                                           '/part/name3.xml')
     part_1_blob, part_2_blob, part_3_blob = ('<Part_1/>', '<Part_2/>',
                                              '<Part_3/>')
     reltype1, reltype2, reltype3 = ('reltype1', 'reltype2', 'reltype3')
     srels = [
         Mock(name='rId1', is_external=True),
         Mock(name='rId2',
              is_external=False,
              reltype=reltype1,
              target_partname=partname_1),
         Mock(name='rId3',
              is_external=False,
              reltype=reltype2,
              target_partname=partname_2),
         Mock(name='rId4',
              is_external=False,
              reltype=reltype1,
              target_partname=partname_1),
         Mock(name='rId5',
              is_external=False,
              reltype=reltype3,
              target_partname=partname_3),
     ]
     pkg_srels = srels[:2]
     part_1_srels = srels[2:3]
     part_2_srels = srels[3:5]
     part_3_srels = []
     # mockery ----------------------
     phys_reader = Mock(name='phys_reader')
     _srels_for.side_effect = [part_1_srels, part_2_srels, part_3_srels]
     phys_reader.blob_for.side_effect = [
         part_1_blob, part_2_blob, part_3_blob
     ]
     # exercise ---------------------
     generated_tuples = list(
         PackageReader._walk_phys_parts(phys_reader, pkg_srels))
     # verify -----------------------
     expected_tuples = [
         (partname_1, part_1_blob, reltype1, part_1_srels),
         (partname_2, part_2_blob, reltype2, part_2_srels),
         (partname_3, part_3_blob, reltype3, part_3_srels),
     ]
     assert generated_tuples == expected_tuples
Beispiel #7
0
 def it_can_iterate_over_all_the_srels(self):
     # mockery ----------------------
     pkg_srels = ['srel1', 'srel2']
     sparts = [
         Mock(name='spart1', partname='pn1', srels=['srel3', 'srel4']),
         Mock(name='spart2', partname='pn2', srels=['srel5', 'srel6']),
     ]
     pkg_reader = PackageReader(None, pkg_srels, sparts)
     # exercise ---------------------
     generated_tuples = [t for t in pkg_reader.iter_srels()]
     # verify -----------------------
     expected_tuples = [
         ('/', 'srel1'),
         ('/', 'srel2'),
         ('pn1', 'srel3'),
         ('pn1', 'srel4'),
         ('pn2', 'srel5'),
         ('pn2', 'srel6'),
     ]
     assert generated_tuples == expected_tuples
 def it_can_iterate_over_all_the_srels(self):
     # mockery ----------------------
     pkg_srels = ['srel1', 'srel2']
     sparts = [
         Mock(name='spart1', partname='pn1', srels=['srel3', 'srel4']),
         Mock(name='spart2', partname='pn2', srels=['srel5', 'srel6']),
     ]
     pkg_reader = PackageReader(None, pkg_srels, sparts)
     # exercise ---------------------
     generated_tuples = [t for t in pkg_reader.iter_srels()]
     # verify -----------------------
     expected_tuples = [
         ('/',   'srel1'),
         ('/',   'srel2'),
         ('pn1', 'srel3'),
         ('pn1', 'srel4'),
         ('pn2', 'srel5'),
         ('pn2', 'srel6'),
     ]
     assert generated_tuples == expected_tuples
Beispiel #9
0
 def it_can_retrieve_srels_for_a_source_uri(self,
                                            _SerializedRelationships_):
     # mockery ----------------------
     phys_reader = Mock(name='phys_reader')
     source_uri = Mock(name='source_uri')
     rels_xml = phys_reader.rels_xml_for.return_value
     load_from_xml = _SerializedRelationships_.load_from_xml
     srels = load_from_xml.return_value
     # exercise ---------------------
     retval = PackageReader._srels_for(phys_reader, source_uri)
     # verify -----------------------
     phys_reader.rels_xml_for.assert_called_once_with(source_uri)
     load_from_xml.assert_called_once_with(source_uri.baseURI, rels_xml)
     assert retval == srels
 def it_can_retrieve_srels_for_a_source_uri(
         self, _SerializedRelationships_):
     # mockery ----------------------
     phys_reader = Mock(name='phys_reader')
     source_uri = Mock(name='source_uri')
     rels_xml = phys_reader.rels_xml_for.return_value
     load_from_xml = _SerializedRelationships_.load_from_xml
     srels = load_from_xml.return_value
     # exercise ---------------------
     retval = PackageReader._srels_for(phys_reader, source_uri)
     # verify -----------------------
     phys_reader.rels_xml_for.assert_called_once_with(source_uri)
     load_from_xml.assert_called_once_with(source_uri.baseURI, rels_xml)
     assert retval == srels
 def it_can_walk_phys_pkg_parts(self, _srels_for):
     # test data --------------------
     # +----------+       +--------+
     # | pkg_rels |-----> | part_1 |
     # +----------+       +--------+
     #      |               |    ^
     #      v               v    |
     #   external         +--------+     +--------+
     #                    | part_2 |---> | part_3 |
     #                    +--------+     +--------+
     partname_1, partname_2, partname_3 = (
         '/part/name1.xml', '/part/name2.xml', '/part/name3.xml'
     )
     part_1_blob, part_2_blob, part_3_blob = (
         '<Part_1/>', '<Part_2/>', '<Part_3/>'
     )
     reltype1, reltype2, reltype3 = ('reltype1', 'reltype2', 'reltype3')
     srels = [
         Mock(name='rId1', is_external=True),
         Mock(name='rId2', is_external=False, reltype=reltype1,
              target_partname=partname_1),
         Mock(name='rId3', is_external=False, reltype=reltype2,
              target_partname=partname_2),
         Mock(name='rId4', is_external=False, reltype=reltype1,
              target_partname=partname_1),
         Mock(name='rId5', is_external=False, reltype=reltype3,
              target_partname=partname_3),
     ]
     pkg_srels = srels[:2]
     part_1_srels = srels[2:3]
     part_2_srels = srels[3:5]
     part_3_srels = []
     # mockery ----------------------
     phys_reader = Mock(name='phys_reader')
     _srels_for.side_effect = [part_1_srels, part_2_srels, part_3_srels]
     phys_reader.blob_for.side_effect = [
         part_1_blob, part_2_blob, part_3_blob
     ]
     # exercise ---------------------
     generated_tuples = list(
         PackageReader._walk_phys_parts(phys_reader, pkg_srels)
     )
     # verify -----------------------
     expected_tuples = [
         (partname_1, part_1_blob, reltype1, part_1_srels),
         (partname_2, part_2_blob, reltype2, part_2_srels),
         (partname_3, part_3_blob, reltype3, part_3_srels),
     ]
     assert generated_tuples == expected_tuples
    def it_can_construct_from_pkg_file(self, _init_, PhysPkgReader_, from_xml,
                                       _srels_for, _load_serialized_parts):
        phys_reader = PhysPkgReader_.return_value
        content_types = from_xml.return_value
        pkg_srels = _srels_for.return_value
        sparts = _load_serialized_parts.return_value
        pkg_file = Mock(name='pkg_file')

        pkg_reader = PackageReader.from_file(pkg_file)

        PhysPkgReader_.assert_called_once_with(pkg_file)
        from_xml.assert_called_once_with(phys_reader.content_types_xml)
        _srels_for.assert_called_once_with(phys_reader, '/')
        _load_serialized_parts.assert_called_once_with(phys_reader, pkg_srels,
                                                       content_types)
        phys_reader.close.assert_called_once_with()
        _init_.assert_called_once_with(ANY, content_types, pkg_srels, sparts)
        assert isinstance(pkg_reader, PackageReader)
 def it_can_construct_from_pkg_file(
         self, init, PhysPkgReader_, from_xml, _srels_for,
         _load_serialized_parts):
     # mockery ----------------------
     phys_reader = PhysPkgReader_.return_value
     content_types = from_xml.return_value
     pkg_srels = _srels_for.return_value
     sparts = _load_serialized_parts.return_value
     pkg_file = Mock(name='pkg_file')
     # exercise ---------------------
     pkg_reader = PackageReader.from_file(pkg_file)
     # verify -----------------------
     PhysPkgReader_.assert_called_once_with(pkg_file)
     from_xml.assert_called_once_with(phys_reader.content_types_xml)
     _srels_for.assert_called_once_with(phys_reader, '/')
     _load_serialized_parts.assert_called_once_with(phys_reader, pkg_srels,
                                                    content_types)
     phys_reader.close.assert_called_once_with()
     init.assert_called_once_with(content_types, pkg_srels, sparts)
     assert isinstance(pkg_reader, PackageReader)