def process_file(path, args): """Process a single bib file.""" # Iterate the files given on the command line results = bibpy.read_file(path, format='relaxed') if args.inherit_crossreferences: bibpy.inherit_crossrefs(results.entries) if args.inherit_xdata: bibpy.inherit_xdata(results.entries) if args.expand_string_vars: # Expand string variables after crossref and xdata inheritance bibpy.expand_strings(results.entries, results.strings) if args.order: if args.order.lower() == 'true': args.order = True else: args.order = [ order.strip() for order in [e.strip() for e in args.order.split(',')] ] return results
def test_inheritance_no_override_no_exceptions(test_entries): bibpy.inherit_crossrefs(test_entries, inherit=True, override=False, exceptions={}) entry1 = test_entries[0] assert entry1.crossref == 'key2' assert entry1.title == 'Title' assert entry1.author == 'Author' assert entry1.booktitle == 'Booktitle' assert entry1.booksubtitle == 'Booksubtitle'
def test_crossref_inheritance_with_exceptions(test_entries): exceptions = {('inbook', 'book'): {'inherit': False}} bibpy.inherit_crossrefs(test_entries, inherit=True, override=False, exceptions=exceptions) entry1 = test_entries[0] assert entry1.crossref == 'key2' assert entry1.title == 'Title' assert entry1.author == 'Author' assert entry1.pages == '5--25'
def test_inheritance_with_override_no_exceptions(test_entries): # Set the target entry's booktitle field test_entries[0].booktitle = 'Cool Booktitle' bibpy.inherit_crossrefs(test_entries, inherit=True, override=True, exceptions={}) entry1 = test_entries[0] assert entry1.crossref == 'key2' assert entry1.title == 'Title' assert entry1.author == 'Author' assert entry1.booktitle == 'Booktitle' assert entry1.booksubtitle == 'Booksubtitle'
def test_uninheritance_no_override(test_entries): bibpy.inherit_crossrefs(test_entries, inherit=True, override=False, exceptions={}) bibpy.uninherit_crossrefs(test_entries, inherit=True, override=False, exceptions={}) entry = test_entries[0] assert set(entry.fields) == set(['crossref', 'title', 'author', 'pages']) assert entry.crossref == 'key2' assert entry.title == 'Title' assert entry.booktitle is None assert entry.booksubtitle is None
def test_uninheritance_no_override_with_exceptions(test_entries): entry1, entry2 = test_entries bibpy.inherit_crossrefs(test_entries, inherit=True, override=False) # Do not uninherit fields in @books crossreferenceing @inbooks bibpy.uninherit_crossrefs(test_entries, exceptions={ ('inbook', 'book'): { 'inherit': False } }) assert entry1.crossref == 'key2' assert entry1.title == 'Title' assert entry1.author == 'Author' assert entry1.booktitle == 'Booktitle' assert entry1.booksubtitle == 'Booksubtitle'
def test_no_inheritance(test_entries): entry1, entry2 = test_entries bibpy.inherit_crossrefs(test_entries, inherit=False) assert set(entry1.fields) == set(['crossref', 'title', 'author', 'pages']) assert entry1.crossref == 'key2' assert entry1.title == 'Title' assert entry1.author == 'Author' assert entry1.pages == '5--25' assert set(entry2.fields) == set( ['subtitle', 'title', 'author', 'date', 'publisher', 'location']) assert entry2.subtitle == 'Booksubtitle' assert entry2.title == 'Booktitle' assert entry2.author == 'Author2' assert entry2.date == '1995' assert entry2.publisher == 'Publisher' assert entry2.location == 'Location'
def test_uninheritance_with_override(test_entries): bibpy.inherit_crossrefs(test_entries, inherit=True, override=False, exceptions={}) # NOTE: Does override even make sense here? bibpy.uninherit_crossrefs(test_entries, inherit=False, override=True, exceptions={}) # Should not change the entries as inherit is still False in # bibpy.uninherit_crossrefs entry1 = test_entries[0] assert entry1.crossref == 'key2' assert entry1.title == 'Title' assert entry1.author == 'Author' assert entry1.booktitle == 'Booktitle' assert entry1.booksubtitle == 'Booksubtitle'
def print_entries(entries): print(os.linesep.join(map(str, entries))) print() if __name__ == '__main__': # Load just the entries of the file entries = bibpy.read_file( get_abspath_for(__file__, '../tests/data/crossreferences.bib'), format='relaxed' ).entries print("Before inheriting crossreferences") print_entries(entries) # Inherit crossreferences in-place bibpy.inherit_crossrefs(entries, inherit=True, override=False, exceptions={}) print("After inheriting crossreferences") print_entries(entries) # Uninherit crossreferences again bibpy.uninherit_crossrefs(entries, inherit=True, override=False, exceptions={}) # The entries should now be the same as before they inherited # crossreferences print("After uninheriting crossreferences") print_entries(entries)