def as_marc(self): """Returns the record serialized as MARC21.""" fields = b"" directory = b"" offset = 0 # build the directory # each element of the directory includes the tag, the byte length of # the field and the offset from the base address where the field data # can be found if self.leader[9] == "a" or self.force_utf8: encoding = "utf-8" else: encoding = "iso8859-1" for field in self.fields: field_data = field.as_marc(encoding=encoding) fields += field_data if field.tag.isdigit(): directory += ("%03d" % int(field.tag)).encode(encoding) else: directory += ("%03s" % field.tag).encode(encoding) directory += ("%04d%05d" % (len(field_data), offset)).encode(encoding) offset += len(field_data) # directory ends with an end of field directory += END_OF_FIELD.encode(encoding) # field data ends with an end of record fields += END_OF_RECORD.encode(encoding) # the base address where the directory ends and the field data begins base_address = LEADER_LEN + len(directory) # figure out the length of the record record_length = base_address + len(fields) # update the leader with the current record length and base address # the lengths are fixed width and zero padded strleader = "%05d%s%05d%s" % ( record_length, self.leader[5:12], base_address, self.leader[17:], ) leader = strleader.encode(encoding) return leader + directory + fields
def as_marc(self): """ returns the record serialized as MARC21 """ fields = b'' directory = b'' offset = 0 # build the directory # each element of the directory includes the tag, the byte length of # the field and the offset from the base address where the field data # can be found if self.leader[9] == 'a' or self.force_utf8: encoding = 'utf-8' else: encoding = 'iso8859-1' for field in self.fields: field_data = field.as_marc(encoding=encoding) fields += field_data if field.tag.isdigit(): directory += ('%03d' % int(field.tag)).encode(encoding) else: directory += ('%03s' % field.tag).encode(encoding) directory += ('%04d%05d' % (len(field_data), offset)).encode(encoding) offset += len(field_data) # directory ends with an end of field directory += END_OF_FIELD.encode(encoding) # field data ends with an end of record fields += END_OF_RECORD.encode(encoding) # the base address where the directory ends and the field data begins base_address = LEADER_LEN + len(directory) # figure out the length of the record record_length = base_address + len(fields) # update the leader with the current record length and base address # the lengths are fixed width and zero padded strleader = '%05d%s%05d%s' % \ (record_length, self.leader[5:12], base_address, self.leader[17:]) self.leader = strleader.encode(encoding) return self.leader + directory + fields