def check_record(record, texkey_field="035__a", extra_subfields=()): """ Add a tex key to a record, checking that it doesn't have one already. """ tag = texkey_field[:3] ind1, ind2, subfield = texkey_field[3:] provenances = list(record.iterfield(texkey_field[:5] + "9")) if len(provenances) and provenances[0][1] in ("SPIRESTeX", "INSPIRETeX"): for _, val in record.iterfield(texkey_field[:5] + "z"): if val: return # Record already has a texkey if len(list(record.iterfield(texkey_field))) == 0: try: texkey = TexkeySeq().next_value(bibrecord=record) except TexkeyNoAuthorError: record.warn("No first author or collaboration") return subfields_to_add = [(subfield, texkey)] + map(tuple, extra_subfields) record_add_field(record, tag=tag, ind1=ind1, ind2=ind2, subfields=subfields_to_add) record.set_amended("Added Tex key '%s' to field %s" % (texkey, texkey_field))
def test_get_next_texkey_no_author(self): """ Generate an error while getting a texkey with no author """ texkey_seq = TexkeySeq() self.assertRaises(TexkeyNoAuthorError, texkey_seq.next_value, xml_record=get_sample_texkey(3))
def check_record(record, texkey_field="035__a", provenances=None, extra_subfields=None): """ Add a texkey to a record, checking that it doesn't have one already. """ if provenances is None: provenances = ["SPIRESTeX", "INSPIRETeX"] for provenance in provenances: for _, val in record.iterfield(texkey_field, subfield_filter=('9', provenance)): if len(val) > 6: # texkey composition <name>:\d{4}<randchars> return try: texkey = TexkeySeq().next_value(bibrecord=record) except TexkeyNoAuthorError: record.warn("No first author or collaboration") return except TexkeyNoYearError: record.warn("No suitable year/date info") return if extra_subfields is None: extra_subfields = [('9', 'INSPIRETeX')] tag = texkey_field[:3] ind1, ind2, subfield = texkey_field[3:] subfields_to_add = [(subfield, texkey)] + map(tuple, extra_subfields) record_add_field(record, tag=tag, ind1=ind1, ind2=ind2, subfields=subfields_to_add) record.set_amended("Added Tex key '%s' to field %s" % (texkey, texkey_field))
def test_get_next_texkey3(self): """ Generate the third texkey """ texkey_seq = TexkeySeq() self.texkey3 = texkey_seq.next_value(xml_record=get_sample_texkey(2)) self.assertEqual(self.texkey3[:-3], 'ATLAS:2012')
def test_get_next_texkey2(self): """ Generate the second texkey """ texkey_seq = TexkeySeq() self.texkey2 = texkey_seq.next_value(xml_record=get_sample_texkey(1)) self.assertEqual(self.texkey2[:-3], 'Broekhoven-Fiene:2012')
def test_get_next_texkey1(self): """ Generate the first texkey """ texkey_seq = TexkeySeq() self.texkey1 = texkey_seq.next_value(xml_record=get_sample_texkey(0)) self.assertEqual(self.texkey1[:-3], 'Boyle:2012')