def format_record(recid, record=None):
    # print recid
    if not record:
        record = ALL[str(recid)]
    if len(record):
        mapping = reg_mapping
        if is_staff_paper(record):
            mapping = sp_mapping
        (collections, locations, formats) = index_record(record)
        tags = []
        for (field, value) in record.items():
            if field in mapping:
                try:
                    if field in ['TI', ]:
                        subfields = mapping[field](value, record.get('LANG'))
                    else:
                        subfields = mapping[field](value)
                except Exception as error:
                    # print "%s\t%s\t%s" % (recid, field, value)
                    # f = open('log.txt', 'a', "utf-8-sig")
                    with codecs.open(log_dir + 'log_bib.txt', 'a', encoding='utf8') as f:
                        f.write(("==================\n%s\nTI:\t%s\n%s:\t%s\n%s\n\n" % (recid, record['TI'], field, value, error)).replace('\n', '\r\n'))
                        f.close()

                    subfields = []
                for subfield in subfields:
                    tags.append(format_tag(subfield))
        tags.append(format_tag(mapping['RECID'](recid)[0]))
        tags.sort()
        res = Record(force_utf8=True, utf8_handling='ignore')
        # res = Record()
        for tag in tags:
            res.add_ordered_field(tag)
        res = merge_subfields(res, ['999', '998', '080', '773',])
        return res
Exemple #2
0
def main():
    global d
    if not d:
        d = get_items('BARCD')
    total = float(len(d))
    i = 1
    fb = open(export_dir+'ITEMS.marc.dat', 'wb')
    ft = open(export_dir+'ITEMS.marc.txt', 'w')
    print 'Exporting items...'
    item_count = 0
    for (recid, copies) in d.items():
        if not is_staff_paper(recid):
            record = Record()
            id_field = Field(tag='999', indicators=[' ', ' '], subfields=['a', recid, 'b', ALL[recid].get('ID', '')])
            record.add_ordered_field(id_field)
            for c in copies.items():
                aux = [(e[0], items_fix[e[0]](e[1])) for e in c[1].items() if e[0] in items_fix]
                item_field = Field(tag='945', indicators=[' ', ' '], subfields= ['b', c[0]]+flatten_list(aux))
                record.add_ordered_field(item_field)
                item_count = item_count + 1
            fb.write(record.as_marc())
            ft.write(str(record) + '\n==================\n')
        update_progress(i*100/total)
        i = i + 1
    print "\nRecords:\t" + str(int(total))
    print "Items:  \t" + str(item_count)
    fb.close()
    ft.close()