def test_xslt_dump(): """Test xslt dump.""" path = os.path.dirname(__file__) with open("{0}/demo_marc21_to_dc.converted.xml".format(path)) as myfile: expect = myfile.read() data = list(load("{0}/demo_marc21_to_dc.xml".format(path))) output = dumps(data, xslt_filename="{0}/demo_marc21_to_dc.xslt".format(path)) assert output.decode("utf-8") == expect
def serialize(self, pid, record, links_factory=None): """Serialize a single record and persistent identifier. :param pid: Persistent identifier instance. :param record: Record instance. :param links_factory: Factory function for the link generation, which are added to the response. """ return dumps(self.transform_record(pid, record, links_factory), **self.dumps_kwargs)
def serialize(self, pid, record, links=None): """Serialize a single record and persistent identifier. :param pid: Persistent identifier instance. :param record: Record instance. :param links: Dictionary of links to add to response. """ return dumps( self.transform_record(pid, record), xslt_filename=self.xslt_filename)
def test_xslt_dump(): """Test xslt dump.""" path = os.path.dirname(__file__) with open('{0}/demo_marc21_to_dc.converted.xml'.format(path)) as myfile: expect = myfile.read() data = list(load('{0}/demo_marc21_to_dc.xml'.format(path))) output = dumps( data, xslt_filename='{0}/demo_marc21_to_dc.xslt'.format(path) ) assert output.decode('utf-8') == expect
def serialize(self, pid, record, links_factory=None): """Serialize a single record and persistent identifier. :param pid: The :class:`invenio_pidstore.models.PersistentIdentifier` instance. :param record: The :class:`invenio_records.api.Record` instance. :param links_factory: Factory function for the link generation, which are added to the response. :returns: The object serialized. """ return dumps(self.transform_record(pid, record, links_factory), **self.dumps_kwargs)
def serialize_search(self, pid_fetcher, search_result, links=None): """Serialize a search result. :param pid_fetcher: Persistent identifier fetcher. :param search_result: Elasticsearch search result. :param links: Dictionary of links to add to response. """ return dumps([ self.transform_search_hit( pid_fetcher(hit['_id'], hit['_source']), hit) for hit in search_result['hits']['hits'] ], xslt_filename=self.xslt_filename)
def serialize_search(self, pid_fetcher, search_result, item_links_factory=None, **kwargs): """Serialize a search result. :param pid_fetcher: Persistent identifier fetcher. :param search_result: Elasticsearch search result. :param item_links_factory: Factory function for the items in result. """ ret = [self.transform_search_hit(pid_fetcher(hit['_id'], hit['_source']), hit, links_factory=item_links_factory) for hit in search_result['hits']['hits']] return dumps(ret, **self.dumps_kwargs)
def serialize_search(self, pid_fetcher, search_result, item_links_factory=None, **kwargs): """Serialize a search result. :param pid_fetcher: Persistent identifier fetcher. :param search_result: Elasticsearch search result. :param item_links_factory: Factory function for the items in result. """ ret = [self.transform_search_hit(pid_fetcher(hit['_id'], hit['_source']), hit, links_factory=item_links_factory) for hit in search_result['hits']['hits']] dumps_kwargs = dict(xslt_filename=self.xslt_filename) if \ self.xslt_filename else dict() return dumps(ret, **dumps_kwargs)
def serialize_search( self, pid_fetcher, search_result, item_links_factory=None, **kwargs ): """Serialize a search result. :param pid_fetcher: Persistent identifier fetcher. :param search_result: Elasticsearch search result. :param item_links_factory: Factory function for the items in result. (Default: ``None``) :returns: The objects serialized. """ ret = [ self.transform_search_hit( pid_fetcher(hit["_id"], hit["_source"]), hit, links_factory=item_links_factory, ) for hit in search_result["hits"]["hits"] ] return dumps(ret, **self.dumps_kwargs)
def test_toxml_from_xml(): """Test MARC21 loading from XML and recreating to XML.""" from dojson.contrib.marc21.utils import create_record from dojson.contrib.to_marc21.utils import dumps from lxml import etree, objectify for name, record in RECORDS.items(): blob = create_record(record) xml = dumps(blob) options = {'xml_declaration': True, 'encoding': 'utf8', 'pretty_print': True} recordxml = ('<collection xmlns="http://www.loc.gov/MARC21/slim">' + record + '</collection>') expected = etree.tostring(objectify.fromstring(recordxml), **options) actual = etree.tostring(objectify.fromstring(xml), **options) assert expected == actual
def test_toxml_from_xml(): """Test MARC21 loading from XML and recreating to XML.""" for name, record in RECORDS.items(): blob = create_record(record) rec = marc21.do(blob) back_blob = to_marc21.do(rec) assert blob == back_blob xml = dumps([back_blob]) options = {'xml_declaration': True, 'encoding': 'utf8', 'pretty_print': True} recordxml = ('<collection xmlns="http://www.loc.gov/MARC21/slim">' + record + '</collection>') expected = etree.tostring(objectify.fromstring(recordxml), **options) actual = etree.tostring(objectify.fromstring(xml), **options) assert expected == actual
def convert_xml(self, data, **kwargs): """Convert json into marc21 xml.""" if "json" in data: data["xml"] = dumps(to_marc21.do(data["json"])) return data