def test_reader(): reader = pymarc.MARCReader(file('rusmarc_ebsco.mrc', 'r'), to_unicode=True, encoding='utf-8') for i, record in enumerate(reader): if i == 100: break print record_to_xml(record)
def handle(self, bibid, **options): record = voyager.get_marc_blob(bibid) if options['format'] == 'json': print record.as_json(indent=2) elif options['format'] == 'xml': print record_to_xml(record) else: print record.as_marc21()
def get(self): record = '' if self.request.get('marc') != '': record = pymarc.Record(cgi.escape(self.request.get('marc'))) else: key = cgi.escape(self.request.get('key')) savedrecord = db.get(key) record = pymarc.Record(savedrecord.iso2709) self.response.headers["Content-Type"] = "text/xml" self.response.out.write(marcxml.record_to_xml(record))
def produce_output_xml(self): processed_records_in_xml = [] for rcd in self.marc_processed_objects_chunk: xmlized_rcd = marcxml.record_to_xml(rcd, namespace=True) processed_records_in_xml.append(bytearray(xmlized_rcd)) joined = bytearray().join(processed_records_in_xml) out_xml_beginning = f'<resp><nextPage>{self.next_page_for_user}</nextPage><collection>'.encode('utf-8') out_xml_end = '</collection></resp>'.encode('utf-8') out_xml = bytearray().join([out_xml_beginning, joined, out_xml_end]) return bytes(out_xml)
except (ckanapi.SearchError,ckanapi.NotFound) as e: try: created_dataset = ckanapiutils.create_package(dataset_metadata) dataset_metadata['id'] = created_dataset['id'] print("Dataset created ",created_dataset['id'],created_dataset['title']) except (ckan.lib.search.common.SearchIndexError) as e: continue try: temp_file_path = script_utils._generate_temp_filename('xml') with open(temp_file_path, 'w') as fw: xml = marcxml.record_to_xml(record) fw.write(xml) fw.close() resource_dict = script_utils._create_metadata_dictionary_for_upload(dataset_metadata['id'],"N/A",temp_file_path,dataset_metadata['title'],'Alternative representation [MARCXML]','xml',None) ckanapiutils.create_resource_with_file_upload(resource_dict) if os.path.exists(temp_file_path): os.remove(temp_file_path) except (ValueError, OSError) as e: if (config.DEBUG): traceback.print_exc() try: temp_file_path = script_utils._generate_temp_filename('json')
def get(self): template_values = {} if self.request.get('isbn'): isbn = unicode(self.request.get('isbn')) isbn = isbn.replace("-", "") items = db.GqlQuery("SELECT * FROM BokhyllaItem WHERE isbn = :isbn", isbn=isbn) template_values["isbn"] = isbn elif self.request.get('no'): no = int(self.request.get('no')) items = db.GqlQuery("SELECT * FROM BokhyllaItem WHERE no = :no", no=no) # Search elif self.request.get('q'): q = unicode(self.request.get('q')) items = db.GqlQuery("SELECT * FROM BokhyllaItem WHERE ticr = :q", q=q) elif self.request.get('au'): au = unicode(self.request.get('au')) items = db.GqlQuery("SELECT * FROM BokhyllaItem WHERE creator = :creator", creator=au) elif self.request.get('ti'): ti = unicode(self.request.get('ti')) items = db.GqlQuery("SELECT * FROM BokhyllaItem WHERE creator = :title", title=ti) # By type elif self.request.get('coll'): coll = unicode(self.request.get('coll')) if coll == 'public': items = db.GqlQuery("SELECT * FROM BokhyllaItem WHERE public = True ORDER BY no DESC LIMIT 25") elif coll == 'bokhylla': items = db.GqlQuery("SELECT * FROM BokhyllaItem WHERE bokhylla = True ORDER BY no DESC LIMIT 25") else: if self.request.get('page'): offset = items_per_page * int(self.request.get('page')) offset_str = str(offset) items = db.GqlQuery("SELECT * FROM BokhyllaItem ORDER BY no DESC LIMIT " + str(items_per_page) + " OFFSET " + offset_str) else: items = db.GqlQuery("SELECT * FROM BokhyllaItem ORDER BY no DESC LIMIT " + str(items_per_page)) if items: template_values["items"] = items # Output if self.request.get('format') == 'json': jsonitems = [] for item in items: jsonitems.append(item2dict(item)) self.response.out.write(simplejson.dumps(jsonitems)) elif self.request.get('format') == 'marc' or self.request.get('format') == 'marcxml': marcout = ""; for item in items: # Turn item into MARC marcitem = item2marc(item) # Format MARC if self.request.get('format') == 'marc': marcout = marcout + "\n" + marcitem.as_marc() elif self.request.get('format') == 'marcxml': marcout = marcout + "\n" + marcxml.record_to_xml(marcitem) # Output headers and add any extra formatting if self.request.get('format') == 'marcxml': self.response.headers["Content-Type"] = "text/xml" marcout = "<records>" + marcout + "</records>" # Output self.response.out.write(marcout) else: template_values["query_string"] = self.request.query_string template_values["page"] = self.request.get('page') template_values["no"] = self.request.get('no') template_values["isbn"] = self.request.get('isbn') path = os.path.join(os.path.dirname(__file__), 'tmpl/index.tmpl') self.response.out.write(template.render(path, template_values))
def __call__(self, element): handler = marcxml.XmlHandler() marcxml.parse_xml(StringIO(tostring(element[0])), handler) return marcxml.record_to_xml(handler.records[0], namespace=True)