def test_pubyear(self): record = Record() self.assertEqual(record.pubyear(), None) record.add_field( Field( "260", [" ", " "], subfields=[ "a", "Paris :", "b", "Gauthier-Villars ;", "a", "Chicago :", "b", "University of Chicago Press,", "c", "1955.", ], ) ) self.assertEqual(record.pubyear(), "1955.") record = Record() self.assertEqual(record.pubyear(), None) record.add_field(Field("264", [" ", "1"], subfields=["a", "London :", "b", "Penguin,", "c", "1961."])) self.assertEqual(record.pubyear(), "1961.")
def test_add_field(self): record = Record() field = Field(tag='245', indicators=['1', '0'], subfields=['a', 'Python', 'c', 'Guido']) record.add_field(field) self.assertTrue(field in record.fields, msg='found field')
def test_notes(self): record = Record() self.assertEquals(record.notes(), []) record.add_field( Field("500", [" ", " "], subfields=["a", "Recast in bronze from artist's plaster original of 1903."]) ) self.assertEquals(record.notes()[0].format_field(), "Recast in bronze from artist's plaster original of 1903.")
def test_physicaldescription(self): record = Record() pd1 = "=300 \\$a1 photographic print :$bgelatin silver ;$c10 x 56 in." pd2 = "=300 \\$aFOO$bBAR$cBAZ" pdlist = [pd1, pd2] self.assertEqual(record.physicaldescription(), []) record.add_field( Field( "300", ["\\", ""], subfields=[ "a", "1 photographic print :", "b", "gelatin silver ;", "c", "10 x 56 in.", ], )) record.add_field( Field("300", ["\\", ""], subfields=["a", "FOO", "b", "BAR", "c", "BAZ"])) self.assertEqual(len(record.physicaldescription()), 2) self.assertEqual(record.physicaldescription()[0].__str__(), pd1) self.assertEqual(record.physicaldescription()[1].__str__(), pd2) rpdlist = [rpd.__str__() for rpd in record.physicaldescription()] self.assertEqual(pdlist, rpdlist)
def startElementNS(self, name, qname, attrs): if self._strict and name[0] != MARC_XML_NS: return element = name[1] self._text = [] if element == 'record': self._record = Record() elif element == 'controlfield': tag = attrs.getValue((None, u'tag')) # patch 1: if it is not a valid controlfield, omit it if not tag < '010' or not tag.isdigit(): return self._field = Field(tag) elif element == 'datafield': tag = attrs.getValue((None, u'tag')) # patch 3: if somehow datafield has controlfield tag if tag in [ '001', '002', '003', '004', '005', '006', '007', '008', '009', '04 ' ]: return ind1 = attrs.get((None, u'ind1'), u' ') ind2 = attrs.get((None, u'ind2'), u' ') # patch 2: if field lacks indicators or they're not ASCII, force to blank try: self._field = Field(tag, [ind1, ind2]) except (ValueError, UnicodeEncodeError): self._field = Field(tag, [' ', ' ']) elif element == 'subfield': self._subfield_code = attrs[(None, 'code')]
def test_add_field(self): record = Record() field = Field(tag="245", indicators=["1", "0"], subfields=["a", "Python", "c", "Guido"]) record.add_field(field) self.assertTrue(field in record.fields, msg="found field")
def test_quick_access(self): record = Record() title = Field(tag="245", indicators=["1", "0"], subfields=["a", "Python", "c", "Guido"]) record.add_field(title) self.assertEqual(record["245"], title, "short access") self.assertEqual(record["999"], None, "short access with no field")
def test_quick_access(self): record = Record() title = Field(tag='245', indicators=['1', '0'], subfields=['a', 'Python', 'c', 'Guido']) record.add_field(title) self.assertEqual(record['245'], title, 'short access') self.assertEqual(record['999'], None, 'short access with no field')
def test_membership(self): record = Record() title = Field(tag='245', indicators=['1', '0'], subfields=['a', 'Python', 'c', 'Guido']) record.add_field(title) self.assertTrue('245' in record) self.assertFalse('999' in record)
def test_add_field(self): record = Record() field = Field( tag = '245', indicators = ['1', '0'], subfields = ['a', 'Python', 'c', 'Guido']) record.add_field(field) self.assertTrue(field in record.fields, msg='found field')
def test_remove_fields(self): with open('test/testunimarc.dat', 'rb') as fh: record = Record(fh.read(), force_utf8=True) self.assertTrue(len(record.get_fields('899'))!=0) self.assertTrue(len(record.get_fields('702'))!=0) record.remove_fields('899', '702') self.assertTrue(len(record.get_fields('899'))==0) self.assertTrue(len(record.get_fields('702'))==0)
def test_membership(self): record = Record() title = Field(tag="245", indicators=["1", "0"], subfields=["a", "Python", "c", "Guido"]) record.add_field(title) self.assertTrue("245" in record) self.assertFalse("999" in record)
def test_remove_fields(self): with open("test/testunimarc.dat", "rb") as fh: record = Record(fh.read(), force_utf8=True) self.assertTrue(len(record.get_fields("899")) != 0) self.assertTrue(len(record.get_fields("702")) != 0) record.remove_fields("899", "702") self.assertTrue(len(record.get_fields("899")) == 0) self.assertTrue(len(record.get_fields("702")) == 0)
def test_init_with_no_leader(self): """ Test creating a Record object with no leader argument. """ record = Record() record.add_field(Field(tag="245", indicators=["0", "1"], subfields=["a", "The pragmatic programmer"])) transmission_format = record.as_marc() transmission_format_leader = transmission_format[0:24] self.assertEqual(transmission_format_leader, b"00067 2200037 4500")
def test_alphatag(self): record = Record() record.add_field(Field('CAT', [' ', ' '], subfields=['a', 'foo'])) record.add_field(Field('CAT', [' ', ' '], subfields=['b', 'bar'])) fields = record.get_fields('CAT') self.assertEqual(len(fields), 2) self.assertEqual(fields[0]['a'], 'foo') self.assertEqual(fields[1]['b'], 'bar') self.assertEqual(record['CAT']['a'], 'foo')
def test_quick_access(self): record = Record() title = Field( tag = '245', indicators = ['1', '0'], subfields = ['a', 'Python', 'c', 'Guido']) record.add_field(title) self.assertEqual(record['245'], title, 'short access') self.assertEqual(record['999'], None, 'short access with no field')
def test_membership(self): record = Record() title = Field( tag = '245', indicators = ['1', '0'], subfields = ['a', 'Python', 'c', 'Guido']) record.add_field(title) self.assertTrue('245' in record) self.assertFalse('999' in record)
def test_init_with_leader(self): """ Test creating a Record with a leader argument. """ record = Record(leader="abcdefghijklmnopqrstuvwx") record.add_field(Field(tag="245", indicators=["0", "1"], subfields=["a", "The pragmatic programmer"])) transmission_format = record.as_marc() transmission_format_leader = transmission_format[0:24] self.assertEqual(transmission_format_leader, b"00067fghij2200037rst4500")
def test_alphatag(self): record = Record() record.add_field(Field("CAT", [" ", " "], subfields=["a", "foo"])) record.add_field(Field("CAT", [" ", " "], subfields=["b", "bar"])) fields = record.get_fields("CAT") self.assertEqual(len(fields), 2) self.assertEqual(fields[0]["a"], "foo") self.assertEqual(fields[1]["b"], "bar") self.assertEqual(record["CAT"]["a"], "foo")
def test_electronic_location(self): record = Record() record.add_field( Field( '856', ['4', ' '], subfields=[ 's', '287086', 'u', 'http://digitallibrary.un.org/record/1317932/files/A_C-4_72_L-24-EN.pdf' ])) record.add_field( Field( '856', ['4', ' '], subfields=[ 's', '289890', 'u', 'http://digitallibrary.un.org/record/1317932/files/A_C-4_72_L-24-ES.pdf' ])) self.assertEqual(len(record.electronic_location()), 2) self.assertEqual( record.electronic_location()[0].format_field(), '287086 http://digitallibrary.un.org/record/1317932/files/A_C-4_72_L-24-EN.pdf' ) self.assertEqual( record.electronic_location()[1].format_field(), '289890 http://digitallibrary.un.org/record/1317932/files/A_C-4_72_L-24-ES.pdf' )
def test_pubyear(self): record = Record() self.assertEqual(record.pubyear(), None) record.add_field(Field('260', [' ', ' '], subfields=['a', 'Paris :', 'b', 'Gauthier-Villars ;', 'a', 'Chicago :', 'b', 'University of Chicago Press,', 'c', '1955.'])) self.assertEqual(record.pubyear(), '1955.') record = Record() self.assertEqual(record.pubyear(), None) record.add_field(Field('264', [' ', '1'], subfields=['a', 'London :', 'b', 'Penguin,', 'c', '1961.'])) self.assertEqual(record.pubyear(), '1961.')
def test_as_marc_leader(self): record = Record() record.add_field( Field( tag = '245', indicators = ['0','1'], subfields = ['a', 'The pragmatic programmer'])) record.leader = '00067 2200037 4500' leader_not_touched = record.leader transmission_format = record.as_marc() leader_touched = record.leader self.assertTrue(leader_not_touched==leader_touched)
def standardize_maemarc(fid, mae_marc, fname_suffix): marc_file = MARC_FILE % fname_suffix mae_marc = mae_marc.replace("\r", "") mae_marc = mae_marc.strip() mae_marc = mae_marc.rstrip("&") mae_marc = mae_marc.rstrip("%") parts = mae_marc.split('\n') header = parts[:2] tags_values = parts[2:] ret = [] writer = MARCWriter(file(marc_file, 'a+')) record = Record() control = parts[1:2][0][:-1] field = Field(tag = '001', data = fid) record.add_field(field) #field = Field(tag = '008', data = control) #record.add_field(field) for dat in tags_values: dat = dat.strip('\n') tag = dat[0:3] values = [] subfields = [] for v in dat[6:-1].split('$'): subfield = v[:1] value = v[1:] if (tag == '260') and (subfield == 'c'): value = value.lstrip('c') value = value.rstrip(".") subfields.append(subfield) subfields.append(value) if tag == '245': indicator = ['1', '0'] elif tag == '041': indicator = ['1', '0'] else: indicator = ['', ''] field = Field(tag, indicator, subfields) record.add_field(field) if tag: ret.append({tag:subfields}) print "final output:" print record writer.write(record) writer.close()
def test_init_with_leader_and_force_utf8(self): record = Record(leader="abcdefghijklmnopqrstuvwx", force_utf8=True) record.add_field( Field( tag="245", indicators=["0", "1"], subfields=["a", "The pragmatic programmer"], )) transmission_format = record.as_marc() transmission_format_leader = transmission_format[0:24] self.assertEqual(transmission_format_leader, b"00067fghia2200037rst4500")
def test_init_with_no_leader_but_with_force_utf8(self): record = Record(force_utf8=True) record.add_field( Field( tag="245", indicators=["0", "1"], subfields=["a", "The pragmatic programmer"], )) transmission_format = record.as_marc() transmission_format_leader = transmission_format[0:24] self.assertEqual(transmission_format_leader, b"00067 a2200037 4500")
def test_as_marc_with_explicit_leader(self): """ Test setting an explicit leader. as_marc() should use the whole leader as set. """ record = Record() record.add_field(Field(tag="245", indicators=["0", "1"], subfields=["a", "The pragmatic programmer"])) record.leader = "00067 2200037 4500" leader_not_touched = record.leader transmission_format = record.as_marc() leader_touched = record.leader self.assertTrue(leader_not_touched == leader_touched)
def test_author(self): record = Record() self.assertEquals(record.author(), None) record.add_field(Field("100", [1, 0], subfields=["a", "Bletch, Foobie,", "d", "1979-1981."])) self.assertEquals(record.author(), "Bletch, Foobie, 1979-1981.") record = Record() record.add_field(Field("130", [0, " "], subfields=["a", "Bible.", "l", "Python."])) self.assertEquals(record.author(), None)
def test_uniformtitle(self): record = Record() self.assertEquals(record.uniformtitle(), None) record.add_field(Field("130", [0, " "], subfields=["a", "Tosefta.", "l", "English.", "f", "1977."])) self.assertEquals(record.uniformtitle(), "Tosefta. English. 1977.") record = Record() record.add_field(Field("240", [1, 4], subfields=["a", "The Pickwick papers.", "l", "French."])) self.assertEquals(record.uniformtitle(), "The Pickwick papers. French.")
def test_title(self): record = Record() self.assertEquals(record.title(), None) record.add_field(Field("245", [0, 1], subfields=["a", "Foo :", "b", "bar"])) self.assertEquals(record.title(), "Foo :bar") record = Record() record.add_field(Field("245", [0, 1], subfields=["a", "Farghin"])) self.assertEquals(record.title(), "Farghin")
def test_init_with_no_leader(self): """ Test creating a Record object with no leader argument. """ record = Record() record.add_field( Field(tag='245', indicators=['0', '1'], subfields=['a', 'The pragmatic programmer'])) transmission_format = record.as_marc() transmission_format_leader = transmission_format[0:24] self.assertEqual(transmission_format_leader, b'00067 2200037 4500')
def test_init_with_leader(self): """ Test creating a Record with a leader argument. """ record = Record(leader='abcdefghijklmnopqrstuvwx') record.add_field( Field(tag='245', indicators=['0', '1'], subfields=['a', 'The pragmatic programmer'])) transmission_format = record.as_marc() transmission_format_leader = transmission_format[0:24] self.assertEqual(transmission_format_leader, b'00067fghij2200037rst4500')
def test_init_with_no_leader(self): """Test creating a Record object with no leader argument.""" record = Record() record.add_field( Field( tag="245", indicators=["0", "1"], subfields=["a", "The pragmatic programmer"], )) transmission_format = record.as_marc() transmission_format_leader = transmission_format[0:24] self.assertEqual(transmission_format_leader, b"00067 2200037 4500")
def test_remove_field(self): record = Record() field = Field(tag="245", indicators=["1", "0"], subfields=["a", "Python", "c", "Guido"]) record.add_field(field) self.assertEqual(record["245"]["a"], "Python") # try removing a field that exists record.remove_field(field) self.assertEqual(record["245"], None) # try removing a field that doesn't exist field = Field("001", data="abcd1234") self.assertRaises(FieldNotFound, record.remove_field, field)
def test_author(self): record = Record() self.assertEqual(record.author(), None) record.add_field(Field('100', [1, 0], subfields=['a', 'Bletch, Foobie,', 'd', '1979-1981.'])) self.assertEqual(record.author(), 'Bletch, Foobie, 1979-1981.') record = Record() record.add_field(Field('130', [0, ' '], subfields=['a', 'Bible.', 'l', 'Python.'])) self.assertEqual(record.author(), None)
def test_title(self): record = Record() self.assertEqual(record.title(), None) record.add_field(Field('245', [0, 1], subfields=['a', 'Foo :', 'b', 'bar'])) self.assertEqual(record.title(), 'Foo :bar') record = Record() record.add_field(Field('245', [0, 1], subfields=['a', "Farghin"])) self.assertEqual(record.title(), "Farghin")
def test_uniformtitle(self): record = Record() self.assertEqual(record.uniformtitle(), None) record.add_field(Field('130', [0, ' '], subfields=['a', "Tosefta.", 'l', "English.", 'f', "1977."])) self.assertEqual(record.uniformtitle(), "Tosefta. English. 1977.") record = Record() record.add_field(Field('240', [1, 4], subfields=['a', "The Pickwick papers.", 'l', "French."])) self.assertEqual(record.uniformtitle(), "The Pickwick papers. French.")
def test_remove_field(self): record = Record() field = Field( tag = '245', indicators = ['1', '0'], subfields = ['a', 'Python', 'c', 'Guido']) record.add_field(field) self.assertEqual(record['245']['a'], 'Python') # try removing a field that exists record.remove_field(field) self.assertEqual(record['245'], None) # try removing a field that doesn't exist field = Field('001', data='abcd1234') self.assertRaises(FieldNotFound, record.remove_field, field)
def test_subjects(self): record = Record() subject1 = "=630 0\\$aTosefta.$lEnglish.$f1977." subject2 = "=600 10$aLe Peu, Pepe." shlist = [subject1, subject2] self.assertEquals(record.subjects(), []) record.add_field(Field("630", [0, " "], subfields=["a", "Tosefta.", "l", "English.", "f", "1977."])) record.add_field(Field("730", [0, " "], subfields=["a", "Tosefta.", "l", "English.", "f", "1977."])) record.add_field(Field("600", [1, 0], subfields=["a", "Le Peu, Pepe."])) self.assertEquals(len(record.subjects()), 2) self.assertEquals(record.subjects()[0].__str__(), subject1) self.assertEquals(record.subjects()[1].__str__(), subject2) rshlist = [rsh.__str__() for rsh in record.subjects()] self.assertEquals(shlist, rshlist)
def test_physicaldescription(self): record = Record() pd1 = '=300 \\$a1 photographic print :$bgelatin silver ;$c10 x 56 in.' pd2 = '=300 \\$aFOO$bBAR$cBAZ' pdlist = [pd1, pd2] self.assertEqual(record.physicaldescription(), []) record.add_field(Field('300', ['\\', ''], subfields=['a', '1 photographic print :', 'b', 'gelatin silver ;', 'c', '10 x 56 in.'])) record.add_field(Field('300', ['\\', ''], subfields=['a', 'FOO', 'b', 'BAR', 'c', 'BAZ'])) self.assertEqual(len(record.physicaldescription()), 2) self.assertEqual(record.physicaldescription()[0].__str__(), pd1) self.assertEqual(record.physicaldescription()[1].__str__(), pd2) rpdlist = [rpd.__str__() for rpd in record.physicaldescription()] self.assertEqual(pdlist, rpdlist)
def test_authority_authors(self): record = Record() record.add_field( Field('710', ['2', ' '], subfields=[ 'a', "UN. General Assembly (60th sess. : 2005-2006)" ])) record.add_field(Field('710', ['2', ' '], subfields=['a', "Andorra"])) self.assertEqual(len(record.authority_authors()), 2)
def test_find(self): record = Record() subject1 = Field(tag="650", indicators=["", "0"], subfields=["a", "Programming Language"]) record.add_field(subject1) subject2 = Field(tag="650", indicators=["", "0"], subfields=["a", "Object Oriented"]) record.add_field(subject2) found = record.get_fields("650") self.assertEqual(found[0], subject1, "get_fields() item 1") self.assertEqual(found[0], subject1, "get_fields() item 2") found = record.get_fields() self.assertEqual(len(found), 2, "get_fields() with no tag")
def test_publisher(self): record = Record() self.assertEquals(record.publisher(), None) record.add_field( Field( "260", [" ", " "], subfields=[ "a", "Paris :", "b", "Gauthier-Villars ;", "a", "Chicago :", "b", "University of Chicago Press,", "c", "1955.", ], ) ) self.assertEquals(record.publisher(), "Gauthier-Villars ;")
def test_issn(self): record = Record() self.assertEqual(record.issn(), None) record.add_field( Field(tag="022", indicators=["0", ""], subfields=["a", "0395-2037"])) self.assertEqual(record.issn(), "0395-2037")
def test_multi_find(self): record = Record() subject1 = Field(tag="650", indicators=["", "0"], subfields=["a", "Programming Language"]) record.add_field(subject1) subject2 = Field(tag="651", indicators=["", "0"], subfields=["a", "Object Oriented"]) record.add_field(subject2) found = record.get_fields("650", "651") self.assertEquals(len(found), 2)
def test_find(self): record = Record() subject1 = Field(tag='650', indicators=['', '0'], subfields=['a', 'Programming Language']) record.add_field(subject1) subject2 = Field(tag='650', indicators=['', '0'], subfields=['a', 'Object Oriented']) record.add_field(subject2) found = record.get_fields('650') self.assertEqual(found[0], subject1, 'get_fields() item 1') self.assertEqual(found[0], subject1, 'get_fields() item 2') found = record.get_fields() self.assertEqual(len(found), 2, 'get_fields() with no tag')
def test_multi_find(self): record = Record() subject1 = Field(tag="650", indicators=["", "0"], subfields=["a", "Programming Language"]) record.add_field(subject1) subject2 = Field(tag="651", indicators=["", "0"], subfields=["a", "Object Oriented"]) record.add_field(subject2) found = record.get_fields("650", "651") self.assertEqual(len(found), 2)
def test_physicaldescription(self): record = Record() pd1 = "=300 \\$a1 photographic print :$bgelatin silver ;$c10 x 56 in." pd2 = "=300 \\$aFOO$bBAR$cBAZ" pdlist = [pd1, pd2] self.assertEquals(record.physicaldescription(), []) record.add_field( Field( "300", ["\\", ""], subfields=["a", "1 photographic print :", "b", "gelatin silver ;", "c", "10 x 56 in."], ) ) record.add_field(Field("300", ["\\", ""], subfields=["a", "FOO", "b", "BAR", "c", "BAZ"])) self.assertEquals(len(record.physicaldescription()), 2) self.assertEquals(record.physicaldescription()[0].__str__(), pd1) self.assertEquals(record.physicaldescription()[1].__str__(), pd2) rpdlist = [rpd.__str__() for rpd in record.physicaldescription()] self.assertEquals(pdlist, rpdlist)
def test_multi_find(self): record = Record() subject1 = Field(tag='650', indicators=['', '0'], subfields=['a', 'Programming Language']) record.add_field(subject1) subject2 = Field(tag='651', indicators=['', '0'], subfields=['a', 'Object Oriented']) record.add_field(subject2) found = record.get_fields('650', '651') self.assertEqual(len(found), 2)
def test_pubyear(self): record = Record() self.assertEquals(record.pubyear(), None) record.add_field( Field('260', [' ', ' '], subfields=[ 'a', 'Paris :', 'b', 'Gauthier-Villars ;', 'a', 'Chicago :', 'b', 'University of Chicago Press,', 'c', '1955.' ])) self.assertEquals(record.pubyear(), '1955.')
def test_as_marc_leader(self): record = Record() record.add_field( Field(tag='245', indicators=['0', '1'], subfields=['a', 'The pragmatic programmer'])) record.leader = '00067 2200037 4500' leader_not_touched = record.leader transmission_format = record.as_marc() leader_touched = record.leader self.assertTrue(leader_not_touched == leader_touched)
def test_notes(self): record = Record() self.assertEqual(record.notes(), []) record.add_field( Field( '500', [' ', ' '], subfields=[ 'a', "Recast in bronze from artist's plaster original of 1903." ])) self.assertEqual( record.notes()[0].format_field(), "Recast in bronze from artist's plaster original of 1903.")
def test_as_marc_with_explicit_leader(self): """Test setting an explicit leader. as_marc() should use the whole leader as set. """ record = Record() record.add_field( Field( tag="245", indicators=["0", "1"], subfields=["a", "The pragmatic programmer"], )) record.leader = "00067 2200037 4500" leader_not_touched = record.leader record.as_marc() leader_touched = record.leader self.assertTrue(leader_not_touched == leader_touched)
def test_find(self): record = Record() subject1 = Field( tag = '650', indicators = ['', '0'], subfields = ['a', 'Programming Language']) record.add_field(subject1) subject2 = Field( tag = '650', indicators = ['', '0'], subfields = ['a', 'Object Oriented']) record.add_field(subject2) found = record.get_fields('650') self.assertEqual(found[0], subject1, 'get_fields() item 1') self.assertEqual(found[0], subject1, 'get_fields() item 2') found = record.get_fields() self.assertEqual(len(found), 2, 'get_fields() with no tag')
def test_remove_field(self): record = Record() field = Field(tag='245', indicators=['1', '0'], subfields=['a', 'Python', 'c', 'Guido']) record.add_field(field) self.assertEqual(record['245']['a'], 'Python') # try removing a field that exists record.remove_field(field) self.assertEqual(record['245'], None) # try removing a field that doesn't exist field = Field('001', data='abcd1234') self.assertRaises(FieldNotFound, record.remove_field, field)