Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_xml(self):
# read in xml to a record
record1 = pymarc.parse_xml_to_array("test/batch.xml")[0]
# generate xml
xml = pymarc.record_to_xml(record1)
# parse generated xml
record2 = pymarc.parse_xml_to_array(BytesIO(xml))[0]
# compare original and resulting record
self.assertEqual(record1.leader, record2.leader)
field1 = record1.get_fields()
field2 = record2.get_fields()
self.assertEqual(len(field1), len(field2))
pos = 0
while pos < len(field1):
self.assertEqual(field1[pos].tag, field2[pos].tag)
if field1[pos].is_control_field():
self.assertEqual(field1[pos].data, field2[pos].data)
else:
def test_xml_namespaces(self):
"""Tests the 'namespace' parameter of the record_to_xml() method."""
# get a test record
fh = open("test/test.dat", "rb")
record = next(pymarc.reader.MARCReader(fh))
# record_to_xml() with quiet set to False should generate errors
# and write them to sys.stderr
xml = pymarc.record_to_xml(record, namespace=False)
# look for the xmlns in the written xml, should be -1
self.assertFalse(b'xmlns="http://www.loc.gov/MARC21/slim"' in xml)
# record_to_xml() with quiet set to True should not generate errors
xml = pymarc.record_to_xml(record, namespace=True)
# look for the xmlns in the written xml, should be >= 0
self.assertTrue(b'xmlns="http://www.loc.gov/MARC21/slim"' in xml)
fh.close()
def test_xml_namespaces(self):
"""Tests the 'namespace' parameter of the record_to_xml() method."""
# get a test record
fh = open("test/test.dat", "rb")
record = next(pymarc.reader.MARCReader(fh))
# record_to_xml() with quiet set to False should generate errors
# and write them to sys.stderr
xml = pymarc.record_to_xml(record, namespace=False)
# look for the xmlns in the written xml, should be -1
self.assertFalse(b'xmlns="http://www.loc.gov/MARC21/slim"' in xml)
# record_to_xml() with quiet set to True should not generate errors
xml = pymarc.record_to_xml(record, namespace=True)
# look for the xmlns in the written xml, should be >= 0
self.assertTrue(b'xmlns="http://www.loc.gov/MARC21/slim"' in xml)
fh.close()
# update fk relationships with new values
self._extract_languages(record, title)
self._extract_places(record, title)
self._extract_publication_dates(record, title)
self._extract_subjects(record, title)
self._extract_notes(record, title)
self._extract_preceeding_titles(record, title)
self._extract_succeeding_titles(record, title)
self._extract_related_titles(record, title)
self._extract_alt_titles(record, title)
self._extract_urls(record, title)
title.save()
marc, marc_created = models.MARC.objects.get_or_create(title=title)
marc.xml = record_to_xml(record)
marc.save()
if _is_openoni_electronic_resource(title, record):
_logger.info("deleting title record for openoni electronic resource: %s" % title)
title.delete()
# this is for long running processes so the query cache
# doesn't bloat memory
reset_queries()
return title
title.save()
self._extract_languages(record, title)
self._extract_places(record, title)
self._extract_publication_dates(record, title)
self._extract_subjects(record, title)
self._extract_notes(record, title)
self._extract_preceeding_titles(record, title)
self._extract_succeeding_titles(record, title)
self._extract_related_titles(record, title)
self._extract_alt_titles(record, title)
self._extract_urls(record, title)
title.save()
marc = models.MARC()
marc.xml = record_to_xml(record)
marc.title = title
marc.save()
# for context see: https://rdc.lctl.gov/trac/ndnp/ticket/375
if _is_chronam_electronic_resource(title, record):
_logger.info("deleting title record for chronicling america electronic resource: %s" % title)
title.delete()
# this is for long running processes so the query cache
# doesn't bloat memory
reset_queries()
return title
['a', '%s, %s' % (book.series.editor.last_name,
book.series.editor.first_name),
'e', 'Series editor']
))
title = book.title # Add record to file.
if not xml:
filename = 'book_' + str(book.id) + '_' + re.sub(
'[^a-zA-Z0-9\n\.]', '', title.lower()
) + '_marc21.dat'
_file = handle_marc21_file(record.as_marc(), filename, book, owner)
else:
filename = 'book_' + str(book.id) + '_' + re.sub(
'[^a-zA-Z0-9\n\.]', '', title.lower()
) + '_marc21.xml'
content = record_to_xml(record, quiet=False, namespace=False)
_file = handle_marc21_file(content, filename, book, owner)
return _file.pk
# add handle_file ?
]
))
title = book.title # Add record to file.
if not xml:
filename = 'book_' + str(book.id) + '_' + re.sub(
'[^a-zA-Z0-9\n\.]', '', title.lower()
) + '_marc21.dat'
handle_marc21_file(record.as_marc(), filename, book, owner)
content = record.as_marc()
else:
filename = 'book_' + str(book.id) + '_' + re.sub(
'[^a-zA-Z0-9\n\.]', '', title.lower()
) + '_marc21.xml'
content = record_to_xml(record, quiet=False, namespace=False)
handle_marc21_file(content, filename, book, owner)
return content or None