def test_reading_encodings(): bibpy.read_string('@article{key,author={James Grönroos}}', format='bibtex') bibpy.read_file( 'tests/data/simple_1.bib', format='relaxed', encoding='ascii' ) bibpy.read_file( 'tests/data/simple_1.bib', format='relaxed', encoding='utf-8' ) bibpy.read_file( 'tests/data/simple_1.bib', format='relaxed', encoding='iso-8859-1' ) with pytest.raises(UnicodeDecodeError): bibpy.read_file( 'tests/data/iso-8859-1.bib', format='bibtex', encoding='utf-8' )
def benchmark_file(path, args): """Benchmark a single file.""" filename = os.path.basename(path) byte_size = os.stat(path).st_size benchmark = Benchmark(filename, human_readable_size(byte_size)) try: for r in range(args.runs): encoding = 'utf-8' if filename in _LATIN1_ENCODED_FILES: encoding = 'latin1' start = time_stamp() result = bibpy.read_file(path, format='relaxed', encoding=encoding, postprocess=args.postprocess) end = time_stamp() benchmark.num_entries = sum([len(r) for r in result]) benchmark.data.append(end - start) except (LexerException, ParseException) as ex: error = color_string(_RED, 'ERROR') if args.color else 'ERROR' error += ' ({0})'.format(ex) benchmark.data.append(0.) benchmark.status = error return benchmark, False else: ok = color_string(_GREEN, 'OK') if args.color else 'OK' benchmark.status = ok return benchmark, True
def test_bibtex_requirements(): entries = bibpy.read_file( 'tests/data/bibtex_missing_requirements.bib', 'bibtex' ).entries expected_fields = [ # article (set(), []), (set(['author', 'year']), []), (set(['title']), []), (set(['year']), []), (set(['journal']), []), # book (set(), [set(['author', 'editor'])]), (set(['title']), [set(['author', 'editor'])]), (set(['publisher', 'year']), [set(['author', 'editor'])]), (set(['publisher']), [set(['author', 'editor'])]), # booklet (set([]), []), (set(['title']), []) ] for entry, [required, either] in zip(entries, expected_fields): assert bibpy.requirements.check(entry, 'bibtex') ==\ (required, either) assert bibpy.requirements.check(entry, 'relaxed') == (set(), [])
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 process_file(source, unique, predicates): """Process a single bibliographic file.""" entries = bibpy.read_file(source).entries if unique: entries = unique_entries(entries) return filter_entries(entries, predicates)
def test_braces(): entries = bibpy.read_file('tests/data/braces.bib').entries assert len(entries) == 3 assert entries[0].editor == 'value' assert entries[1].editor == '{THIS IS ALL UPPERCASE}' assert entries[2].editor == 'Communications {and} Data' entries = bibpy.read_file( 'tests/data/braces.bib', postprocess=True, remove_braces=True ).entries assert len(entries) == 3 assert entries[0].editor == ['value'] assert entries[1].editor == ['THIS IS ALL UPPERCASE'] assert entries[2].editor == ['Communications and Data']
def test_preprocess(): entry = bibpy.read_file('tests/data/preprocess.bib', 'relaxed').entries[0] expected = { 'address': 'Arthur Cunnings and Michelle Toulouse', 'afterword': 'Arthur Cunnings and Michelle Toulouse', 'author': 'Arthur Cunnings and Michelle Toulouse', 'bookauthor': 'Arthur Cunnings and Michelle Toulouse', 'chapter': '97', 'commentator': 'Arthur Cunnings and Michelle Toulouse', 'date': '1999-03-21', 'editor': 'Arthur Cunnings and Michelle Toulouse', 'editora': 'Arthur Cunnings and Michelle Toulouse', 'editorb': 'Arthur Cunnings and Michelle Toulouse', 'editorc': 'Arthur Cunnings and Michelle Toulouse', 'edition': '2', 'eventdate': '2008-12-07/', 'foreword': 'Arthur Cunnings and Michelle Toulouse', 'holder': 'Arthur Cunnings and Michelle Toulouse', 'institution': 'Arthur Cunnings and Michelle Toulouse', 'introduction': 'Arthur Cunnings and Michelle Toulouse', 'keywords': 'information;data;communications', 'location': 'Arthur Cunnings and Michelle Toulouse', 'language': 'Arthur Cunnings and Michelle Toulouse', 'month': '10', 'number': '28', 'organization': 'Arthur Cunnings and Michelle Toulouse', 'origdate': '2016-10-19', 'origlocation': 'Arthur Cunnings and Michelle Toulouse', 'origpublisher': 'Arthur Cunnings and Michelle Toulouse', 'pages': '11-20', 'pagetotal': '205', 'part': '24', 'publisher': 'Arthur Cunnings and Michelle Toulouse', 'related': 'key1, key2, key3', 'school': 'Arthur Cunnings and Michelle Toulouse', 'series': '23', 'shortauthor': 'Arthur Cunnings and Michelle Toulouse', 'shorteditor': 'Arthur Cunnings and Michelle Toulouse', 'translator': 'Arthur Cunnings and Michelle Toulouse', 'organization': 'Arthur Cunnings and Michelle Toulouse', 'urldate': '2016-10-19/2016-10-27', 'xdata': ', key1, key2, key3, ', 'volume': '83', 'year': '2016', } # Ensure that there are no duplicates assert len(expected.keys()) == len(set(expected.keys())) assert len(preprocess_functions.keys()) ==\ len(set(preprocess_functions.keys())) assert set(expected.keys()) == set(preprocess_functions.keys()) for field, value in preprocess(entry, expected.keys()): assert value == expected.get(field, value)
def test_reading_relaxed(): results = bibpy.read_file( 'tests/data/bibtex_missing_requirements.bib', format='relaxed', ignore_comments=False ) assert len(results.entries) == 12 assert len(results.strings) == 0 assert len(results.comment_entries) == 0 assert len(results.preambles) == 0 assert len(results.comments) == 12
def test_reading_empty(): bibpy.read_string('', 'bibtex') bibpy.read_string('', 'biblatex') bibpy.read_string('', 'mixed') bibpy.read_string('', 'relaxed') with pytest.raises(IOError): bibpy.read_file('', 'bibtex') with pytest.raises(IOError): bibpy.read_file('', 'biblatex') with pytest.raises(IOError): bibpy.read_file('', 'mixed') with pytest.raises(IOError): bibpy.read_file('', 'relaxed')
def test_all_entry_types_bibtex(): """Ensure that all bitex entry types can be parsed.""" entries = bibpy.read_file(filename, 'bibtex').entries entry1 = entries[0] entry2 = entries[1] assert entry1.bibtype == 'article' assert entry1.bibkey == 'test' assert entry1.author == 'James Conway and Archer Sterling' assert entry1.title == '1337 Hacker' assert entry1.year == '2010' assert entry1.month == '4' assert entry1.institution == 'Office of Information Management {and} ' +\ 'Communications' assert entry2.bibtype == 'conference' assert entry2.bibkey == 'anything' assert entry2.author == 'k'
"""Example of expanding and unexpanding crossreferences.""" import bibpy from bibpy.tools import get_abspath_for import os 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={})
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Example of name splitting specific fields and filtering entries.""" import bibpy from bibpy.tools import get_abspath_for if __name__ == '__main__': # Load just the entries of the file and split names only for 'author' # fields entries = bibpy.read_file(get_abspath_for(__file__, '../tests/data/graphs.bib'), postprocess=True, split_names=True).entries # Print the key of each entry that has an author whose last name is # 'Fujisawa' (2: KashiwabaraF79 and OhtsukiMKF81) for entry in entries: if entry.author: if any(name.last == 'Fujisawa' for name in entry.author if name): print(entry.bibkey)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Example of formatting entries for output in different ways.""" import bibpy from bibpy.tools import get_abspath_for if __name__ == '__main__': filename = get_abspath_for(__file__, '../tests/data/small1.bib') entries = [bibpy.read_file(filename, format='relaxed').entries[0]] print("* Default formatting") print( bibpy.write_string(entries, align=True, indent=' ', order=[], surround='{}')) print() print("* Use a tab to indent") print( bibpy.write_string(entries, align=True, indent='\t', order=[], surround='{}')) print() print("* Do not align '=' in entries and enclose field values in double " "quotes")
def test_entries(): return bibpy.read_file('tests/data/simple_1.bib', 'biblatex').entries
if optional: if required: s += " and " temp = ["/".join(map(str, opt)) for opt in optional] s += "optional field(s) " + ", ".join(temp) return s if __name__ == '__main__': filename = get_abspath_for( __file__, '../tests/data/biblatex_missing_requirements.bib') entries = bibpy.read_file(filename, format='biblatex').entries # Collect all results for which a requirements check failed into a list of # pairs. There is also bibpy.requirements.check for checking individual # entries checked = bibpy.requirements.collect(entries, format='biblatex') print("* Using bibpy.requirements.collect:") for (entry, (required, optional)) in checked: if required or optional: # Either a missing required or optional field for this entry print("{0}:{1} is missing {2}".format( entry.bibtype, entry.bibkey, format_requirements_check(required, optional))) # Requirements checks can also be performed on individual entries.
def test_entries(): return bibpy.read_file( 'tests/data/all_bibpy_entry_types.bib', ignore_comments=False )
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Example of formatting bibliographic entries.""" import bibpy from bibpy.tools import get_abspath_for if __name__ == '__main__': bibdata = get_abspath_for(__file__, '../tests/data/small1.bib') entries = bibpy.read_file(bibdata, format='relaxed').entries entry = entries[0] print("Unaligned:") print(entry.format(align=False)) print("Aligned:") print(entry.format(align=True)) print("Aligned with 2 space indent:") print(entry.format(align=True, indent=' ')) print("Aligned with double quotes surrounding values:") print(entry.format(align=True, surround='""')) print("Prefer year and title fields before remaining fields") print(entry.format(order=['year', 'title']))
def test_unicode_file(): bibpy.read_file('tests/data/unicode.bib')
def test_collecting(): entries = bibpy.read_file('tests/data/valid_bibtex.bib', 'bibtex').entries assert bibpy.requirements.collect(entries, 'bibtex') ==\ [(entries[11], (set(['title', 'year']), []))]
from bibpy.tools import get_abspath_for def print_entry_fields(entries): for entry in entries: for field, value in entry: print(" {0} = {1} ({2})".format(field, value, type(value))) print() if __name__ == '__main__': testfile_path = get_abspath_for(__file__, '../tests/data/field_processing.bib') entries = bibpy.read_file(testfile_path, format='relaxed').entries print("Before postprocessing:") print_entry_fields(entries) entries = bibpy.read_file(testfile_path, format='relaxed', postprocess=True).entries print("After postprocessing:") print_entry_fields(entries) # We can also choose to postprocess only a subset of fields print("Postprocess a subset of fields ('xdata' and 'month'):") entries = bibpy.read_file(testfile_path, format='relaxed',
def process_file(path, args): """Process a single bib file.""" return bibpy.read_file(path).entries
"""Example of expanding and unexpanding string variables in entry fields.""" import bibpy from bibpy.tools import get_abspath_for import os def print_entries(entries): print(os.linesep.join(map(str, entries))) print() if __name__ == '__main__': filename = get_abspath_for(__file__, '../tests/data/string_variables.bib') result = bibpy.read_file(filename, format='relaxed') entries, strings = result.entries, result.strings print("* String entries:") print_entries(strings) print("* Without string expansion:") print_entries(entries) # Expand string variables in-place bibpy.expand_strings(entries, strings, ignore_duplicates=False) print("* With string expansion:") print_entries(entries) # Unexpand string variables in-place