bibfix 4.66 KB
Newer Older
Michael Kuron's avatar
Michael Kuron committed
1
#!/usr/bin/env python
olenz's avatar
olenz committed
2 3 4 5 6 7 8 9 10 11
'''
Created on Aug 5, 2013

@author: Johannes Zeman
'''

DEFAULT_BIB_FILE_NAME = 'icp.bib'
STYLE_SENSITIVE_FIELDS = ["author", "editor", "title", "journal", "school"]

import argparse
olenz's avatar
olenz committed
12
from tools import bibfixtools
olenz's avatar
olenz committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

argparser = argparse.ArgumentParser(description='Checks the ICP bib file for consistency and attempts to fix errors.')
argparser.add_argument('bib_file_name', metavar='bibtex_database_file_name', type=str, help='Name of the ICP bib file (default: %s)' % (DEFAULT_BIB_FILE_NAME), nargs='?')
argparser.add_argument('-u', '--unicodetolatex', dest='unicode_to_latex', action='store_true', default=False, help='Translate non-ASCII characters to latex code (experimental)')
argparser.add_argument('-v', '--verbose', dest='verbose', action='store_true', default=False, help='Enable verbose output')
argparser.add_argument('-o', '--output', dest='output', action='store', type=str, help='Name of output file (default: same as bibtex_database_file_name)')
args = argparser.parse_args()
if args.bib_file_name:
    bib_file_name = args.bib_file_name
else:
    bib_file_name = DEFAULT_BIB_FILE_NAME
if args.output:
    output_file_name = args.output
else:
    output_file_name = bib_file_name
verbose = args.verbose
translate_to_latex = args.unicode_to_latex

print "Loading data from '%s'..." % (bib_file_name),
32
raw_data, header, bib_data, bib_keys, bib_strings, bib_preamble = bibfixtools.load_bib_file(bib_file_name)
olenz's avatar
olenz committed
33 34 35 36 37 38 39
if header == None and bib_data == None and bib_keys == None:
    print "failed."
    print "Fatal error occurred. Exiting..."
    exit(1)
else:
    print "done."

40 41 42 43 44 45 46 47 48 49 50 51
print "Checking encoding...",
encoding_errors = bibfixtools.check_encoding(raw_data)
if not (encoding_errors):
    print "done."
else:
    print "failed."
    print ""
    print encoding_errors
    print "Fatal error: Found non-ASCII characters. Please fix manually."
    print "Fatal error occurred. Exiting..."
    exit(1)

olenz's avatar
olenz committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
need_to_save_changes = False

print "Checking key encoding...",
bib_data, bib_keys, keys_ok, keys_changed = bibfixtools.check_and_fix_key_encoding(bib_data, bib_keys, verbose)
if keys_changed:
    need_to_save_changes = True
if keys_ok:
    print "done."
else:
    print "failed."
    print "Fatal error: correction of key encoding failed."
    print "Fatal error occurred. Exiting..."
    exit(1)

print "Checking DOI fields...",
bib_data, dois_ok, dois_changed = bibfixtools.check_and_fix_doi_fields(bib_data, bib_keys, verbose)
if dois_changed:
    need_to_save_changes = True
if dois_ok:
    print "done."
else:
    print "failed."

print "Checking URL fields...",
bib_data, urls_ok, urls_changed = bibfixtools.check_and_fix_url_fields(bib_data, bib_keys, verbose)
if urls_changed:
    need_to_save_changes = True
if urls_ok:
    print "done."
else:
    print "failed."

print "Checking braces...",
bib_data, braces_ok, braces_changed = bibfixtools.check_and_fix_braces(bib_data, bib_keys, STYLE_SENSITIVE_FIELDS, verbose)
if braces_changed:
    need_to_save_changes = True
if braces_ok:
    print "done."
else:
    print "failed."

print "Checking file links...",
bib_data, file_links_ok, file_links_changed = bibfixtools.check_and_fix_file_links(bib_data, bib_keys, verbose)
if file_links_changed:
    need_to_save_changes = True
if file_links_ok:
    print "done."
else:
    print "failed."

print "Checking superseded entries...",
bib_data, superseded_entries_ok, superseded_entries_changed = bibfixtools.check_and_fix_superseded_entries(bib_data, bib_keys, verbose)
if superseded_entries_changed:
    need_to_save_changes = True
if superseded_entries_ok:
    print "done."
else:
    print "failed."

111 112 113 114 115 116 117 118 119 120 121
#if translate_to_latex and (encoding_errors):
#    need_to_save_changes = True
#    ubib_data, ubib_header, ubib_strings, ubib_preamble = bibfixtools.convert_to_unicode(bib_data, header, bib_strings, bib_preamble)
#    print "Converting non-ASCII characters to latex syntax...",
#    bib_data, header, bib_strings, bib_preamble, encoding_ok = bibfixtools.translate_unicode_to_latex(ubib_data, bib_keys, ubib_header, ubib_strings, ubib_preamble, verbose)
#    if encoding_ok:
#        print "done."
#    else:
#        print "Fatal error: ASCII conversion failed. Please fix manually."
#        print "Fatal error occurred. Exiting..."
#        exit(1)
olenz's avatar
olenz committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

if need_to_save_changes:
    print "Saving data to ASCII-encoded file '%s'..." % (output_file_name),
    if bibfixtools.save_bib_data_to_ascii_file(bib_data, header, bib_strings, bib_preamble, output_file_name):
        print "done."
    else:
        print "Fatal error: Could not save data to ASCII-encoded file.."
        print "Fatal error occurred. Exiting..."
        exit(1)
else:
    print "No correctable errors found. Run icpbibcheck for a complete check."