def get_web_bib(params_obj, create_bib_snippets=False): """ Return the parsed paper list, customized for web formatting. If create_bib_snippet is True, generate a subsetted bibtex snippet. """ weblist = [] # web formatted biblist = [] # bib formatted # open and parse all bibfiles (web format) for bib_filetail in params_obj.BIB_FILES: with open(os.path.join(params_obj.BIB_FLDR, bib_filetail), "r") as bibfile: bibfile_str = bibfile.read() # parse bib file for web webparse = BibTexParser(bibfile_str, customization=web_customizations) weblist += webparse.get_entry_list() # parse bib file for bib snippets bibfile.seek(0) # rewind bibparse = BibTexParser(bibfile_str, customization=bib_customizations) biblist += bibparse.get_entry_list() # if required, create bib snippets for each record if create_bib_snippets: for record in biblist: create_bibtex_snippet(params_obj, record) # return web-formatted version sorted_by_year = sorted(weblist, key=sort_key, reverse=True) return sorted_by_year
def test_article_protection_braces(self): with open('bibtexparser/tests/data/article_with_protection_braces.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [ { 'ENTRYTYPE': 'article', 'journal': '{Nice Journal}', 'volume': '12', 'pages': '12-23', 'ID': 'Cesar2013', 'year': '2013', 'month': 'jan', 'author': 'Jean César', 'comments': 'A comment', 'keyword': 'keyword1, keyword2', 'title': '{An amazing title}', 'abstract': "This is an abstract. This line should be long enough to test\nmultilines... and with a french érudit word", }, ] self.assertEqual(res, expected)
def test_article_comma_first(self): with open('bibtexparser/tests/data/article_comma_first.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [{'ENTRYTYPE': 'article', 'journal': 'Nice Journal', 'volume': '12', 'ID': 'Cesar2013', 'year': '2013', 'author': 'Jean Cesar', 'comments': 'A comment', 'keyword': 'keyword1, keyword2', 'title': 'An amazing title' }, {'ENTRYTYPE': 'article', 'journal': 'Nice Journal', 'volume': '12', 'ID': 'Baltazar2013', 'year': '2013', 'author': 'Jean Baltazar', 'comments': 'A comment', 'keyword': 'keyword1, keyword2', 'title': 'An amazing title' }] self.assertEqual(res, expected)
def save(self): bibfile = self.cleaned_data['file'].file bp = BibTexParser(bibfile, customization=convert_to_unicode) good = 0 bad = 0 results = [] for item in bp.get_entry_list(): # find the common keys keys = set(Reference._meta.get_all_field_names()).intersection( item.keys()) # populate the common fields r = Reference() for k in keys: if k == 'id': setattr(r, 'slug', item[k]) else: setattr(r, k, item[k]) try: r.save() except Exception as e: bad += 1 results.append("![{0}]: {1}".format(r.slug, e.message)) else: good += 1 results.append("[{0}]: {1}".format(r.slug, r.title)) return good, bad, results
def test_article_comma_first(self): with open('bibtexparser/tests/data/article_comma_first.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [{ 'ENTRYTYPE': 'article', 'journal': 'Nice Journal', 'volume': '12', 'ID': 'Cesar2013', 'year': '2013', 'author': 'Jean Cesar', 'comments': 'A comment', 'keyword': 'keyword1, keyword2', 'title': 'An amazing title' }, { 'ENTRYTYPE': 'article', 'journal': 'Nice Journal', 'volume': '12', 'ID': 'Baltazar2013', 'year': '2013', 'author': 'Jean Baltazar', 'comments': 'A comment', 'keyword': 'keyword1, keyword2', 'title': 'An amazing title' }] self.assertEqual(res, expected)
def test_article_cust_latex(self): with codecs.open('bibtexparser/tests/data/article.bib', 'r', 'utf-8') as bibfile: bib = BibTexParser(bibfile.read(), customization=customizations_latex) res = bib.get_entry_list() expected = [{ 'abstract': 'This is an abstract. This line should be long enough to test\nmultilines... and with a french {\\\'e}rudit word', 'ENTRYTYPE': 'article', 'pages': '12--23', 'volume': '12', 'ID': 'Cesar2013', 'year': '2013', 'author': ['C{\\\'e}sar, Jean'], 'journal': { 'ID': 'NiceJournal', 'name': 'Nice Journal' }, 'comments': 'A comment', 'month': 'jan', 'keyword': ['keyword1', 'keyword2'], 'title': '{A}n amazing title' }] self.assertEqual(res, expected)
def save(self): bibfile = self.cleaned_data['file'].file bp = BibTexParser(bibfile, customization=convert_to_unicode) good = 0 bad = 0 results = [] for item in bp.get_entry_list(): # find the common keys keys = set(Reference._meta.get_all_field_names()).intersection(item.keys()) # populate the common fields r = Reference() for k in keys: if k == 'id': setattr(r, 'slug', item[k]) else: setattr(r, k, item[k]) try: r.save() except Exception as e: bad += 1 results.append("![{0}]: {1}".format(r.slug, e.message)) else: good += 1 results.append("[{0}]: {1}".format(r.slug, r.title)) return good, bad, results
def test_article_comma_first(self): with open("bibtexparser/tests/data/article_comma_first.bib", "r") as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [ { "ENTRYTYPE": "article", "journal": "Nice Journal", "volume": "12", "ID": "Cesar2013", "year": "2013", "author": "Jean Cesar", "comments": "A comment", "keyword": "keyword1, keyword2", "title": "An amazing title", }, { "ENTRYTYPE": "article", "journal": "Nice Journal", "volume": "12", "ID": "Baltazar2013", "year": "2013", "author": "Jean Baltazar", "comments": "A comment", "keyword": "keyword1, keyword2", "title": "An amazing title", }, ] self.assertEqual(res, expected)
def test_article(self): with codecs.open('bibtexparser/tests/data/article.bib', 'r', 'utf-8') as bibfile: bib = BibTexParser(bibfile.read()) res_list = bib.get_entry_list() res_dict = bib.get_entry_dict() expected_list = [{'keyword': 'keyword1, keyword2', 'ENTRYTYPE': 'article', 'abstract': 'This is an abstract. This line should be long enough to test\nmultilines... and with a french érudit word', 'year': '2013', 'journal': 'Nice Journal', 'ID': 'Cesar2013', 'pages': '12-23', 'title': 'An amazing title', 'comments': 'A comment', 'author': 'Jean César', 'volume': '12', 'month': 'jan' }] expected_dict = {'Cesar2013': {'keyword': 'keyword1, keyword2', 'ENTRYTYPE': 'article', 'abstract': 'This is an abstract. This line should be long enough to test\nmultilines... and with a french érudit word', 'year': '2013', 'journal': 'Nice Journal', 'ID': 'Cesar2013', 'pages': '12-23', 'title': 'An amazing title', 'comments': 'A comment', 'author': 'Jean César', 'volume': '12', 'month': 'jan' }} self.assertEqual(res_list, expected_list) self.assertEqual(res_dict, expected_dict)
def test_article_annotation(self): with codecs.open('bibtexparser/tests/data/article_with_annotation.bib', 'r', 'utf-8') as bibfile: bib = BibTexParser(bibfile.read()) res_list = bib.get_entry_list() res_dict = bib.get_entry_dict() expected_list = [{'keyword': 'keyword1, keyword2', 'ENTRYTYPE': 'article', 'abstract': 'This is an abstract. This line should be long enough to test\nmultilines... and with a french érudit word', 'year': '2013', 'journal': 'Nice Journal', 'ID': 'Cesar2013', 'pages': '12-23', 'title': 'An amazing title', 'comments': 'A comment', 'author': 'Jean César', 'author+an': '1=highlight', 'volume': '12', 'month': 'jan' }] expected_dict = {'Cesar2013': {'keyword': 'keyword1, keyword2', 'ENTRYTYPE': 'article', 'abstract': 'This is an abstract. This line should be long enough to test\nmultilines... and with a french érudit word', 'year': '2013', 'journal': 'Nice Journal', 'ID': 'Cesar2013', 'pages': '12-23', 'title': 'An amazing title', 'comments': 'A comment', 'author': 'Jean César', 'author+an': '1=highlight', 'volume': '12', 'month': 'jan' }} self.assertEqual(res_list, expected_list) self.assertEqual(res_dict, expected_dict)
def test_nonstandard_ignored(self): with open('bibtexparser/tests/data/wrong.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [{'author': 'correct', 'ID': 'bar', 'ENTRYTYPE': 'article'}] self.assertEqual(res, expected)
def test_wrong(self): with open('bibtexparser/tests/data/wrong.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [{'author': 'correct', 'id': 'bar', 'type': 'article'}] self.assertEqual(res, expected)
def test_nonstandard_ignored(self): with open('bibtexparser/tests/data/wrong.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [{'author': 'correct', 'ID': 'bar', 'ENTRYTYPE': 'article'}] self.assertEqual(res, expected)
def test_wrong(self): with open('bibtexparser/tests/data/wrong.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [{'author': 'correct', 'id': 'bar', 'type': 'article'}] self.assertEqual(res, expected)
def load_bibtex_file(filepath): """Parse BibTeX file and return entry list""" with open(filepath, 'rU') as bibfile: bp = BibTexParser(bibfile) entries = bp.get_entry_list() entries = list(map(_capitalize_entry_title, entries)) entries = list(map(_format_entry_authors, entries)) return entries
def load_bibfile(bib_path): if bib_path is None: sublime.status_message("WARNING: No BibTex file configured for Citer") return {} bib_path = bib_path.strip() with open(bib_path, 'r', encoding="utf-8") as bibfile: bp = BibTexParser(bibfile.read(), customization=convert_to_unicode) return list(bp.get_entry_list())
def __init__(self, bibtex_filename): ''' Constructor ''' self.filename = bibtex_filename self.entry_list = [] with open(self.filename) as bibfile: bp = BibTexParser(bibfile) self.entry_list = bp.get_entry_list()
def __init__(self, bib_file_location=None): if bib_file_location is None: bib_file_location = gflags.FLAGS.bib_file bib_file_location = re.sub('~', os.environ['HOME'], bib_file_location) with open(bib_file_location) as bibfile: content = bibfile.read() bp = BibTexParser(content) self.entry_map = {} for ent in bp.get_entry_list(): self.entry_map[ent['id']] = ent
def ReadFromFile(self, file_name): with open(file_name, 'r') as bibfile: bp = BibTexParser(bibfile) self.bib_list = bp.get_entry_list() self.bib_num = len(self.bib_list) for each in self.bib_list: pd = Paper(each) #pd.printPlainRef() self.papers.append(pd)
def __init__(self, bib_file_location=None): if bib_file_location is None: bib_file_location = gflags.FLAGS.bib_file bib_file_location = re.sub('~', os.environ['HOME'], bib_file_location) with open(bib_file_location) as bibfile: content = bibfile.read() bp = BibTexParser(content) self.entry_map = {} for ent in bp.get_entry_list(): self.entry_map[ent['id']] = ent
def load_bibfile(bib_path): if bib_path is None: sublime.status_message("WARNING: No BibTex file configured for Citer") return {} bib_path = bib_path.strip() with open(bib_path, 'r', encoding="utf-8") as bibfile: bp = BibTexParser(bibfile.read(), customization=convert_to_unicode, ignore_nonstandard_types=False) return list(bp.get_entry_list())
def test_article_cust_order(self): def cust(record): record = customization.page_double_hyphen(record) record = customization.homogenize_latex_encoding(record) record = customization.author(record) return record def cust2(record): record = customization.author(record) record = customization.page_double_hyphen(record) record = customization.homogenize_latex_encoding(record) return record with open('bibtexparser/tests/data/multiple_entries.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read(), customization=cust) res = bib.get_entry_list() with open('bibtexparser/tests/data/multiple_entries.bib', 'r') as bibfile: bib2 = BibTexParser(bibfile.read(), customization=cust2) res2 = bib.get_entry_list() self.assertEqual(res, res2)
def test_article_cust_order(self): def cust(record): record = customization.page_double_hyphen(record) record = customization.homogenize_latex_encoding(record) record = customization.author(record) return record def cust2(record): record = customization.author(record) record = customization.page_double_hyphen(record) record = customization.homogenize_latex_encoding(record) return record with open('bibtexparser/tests/data/multiple_entries.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read(), customization=cust) res = bib.get_entry_list() with open('bibtexparser/tests/data/multiple_entries.bib', 'r') as bibfile: bib2 = BibTexParser(bibfile.read(), customization=cust2) res2 = bib.get_entry_list() self.assertEqual(res, res2)
def test_features(self): with open('bibtexparser/tests/data/features.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [{'ENTRYTYPE': 'inproceedings', 'year': '2014', 'title': 'Cool Stuff', 'author': 'John', 'ID': 'mykey', 'booktitle': 'My International Conference', }] self.assertEqual(res, expected)
def test_features(self): with open('bibtexparser/tests/data/features.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [{'ENTRYTYPE': 'inproceedings', 'year': '2014', 'title': 'Cool Stuff', 'author': 'John', 'ID': 'mykey', 'booktitle': 'My International Conference', }] self.assertEqual(res, expected)
def test_string_braces(self): with codecs.open('bibtexparser/tests/data/string.bib', 'r', 'utf-8') as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [{'author': 'Sang Kil Cha and Maverick Woo and David Brumley', 'ID': 'cha:oakland15', 'year': '2015', 'booktitle': 'Proceedings of the {IEEE} Symposium on Security and Privacy', 'title': '{Program-Adaptive Mutational Fuzzing}', 'ENTRYTYPE': 'inproceedings', 'pages': '725--741' }] self.assertEqual(res, expected)
def test_string_braces(self): with codecs.open('bibtexparser/tests/data/string.bib', 'r', 'utf-8') as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [{'author': 'Sang Kil Cha and Maverick Woo and David Brumley', 'ID': 'cha:oakland15', 'year': '2015', 'booktitle': 'Proceedings of the {IEEE} Symposium on Security and Privacy', 'title': 'Program-Adaptive Mutational Fuzzing', 'ENTRYTYPE': 'inproceedings', 'pages': '725--741' }] self.assertEqual(res, expected)
def test_string_is_not_interpolated(self): with open('bibtexparser/tests/data/article_with_strings.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read(), common_strings=True, interpolate_strings=False) res = bib.get_entry_list()[0] self.assertIsInstance(res['month'], BibDataStringExpression) self.assertEqual(len(res['month'].expr), 1) self.assertEqual(res['month'].get_value(), 'January') self.assertIsInstance(res['author'], BibDataStringExpression) self.assertEqual(len(res['author'].expr), 3) self.assertEqual(res['author'].get_value(), 'Jean César') self.assertIsInstance(res['journal'], BibDataStringExpression) self.assertEqual(len(res['journal'].expr), 1) self.assertEqual(res['journal'].get_value(), 'Nice Journal')
def test_string_is_not_interpolated(self): with open('bibtexparser/tests/data/article_with_strings.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read(), common_strings=True, interpolate_strings=False) res = bib.get_entry_list()[0] self.assertIsInstance(res['month'], BibDataStringExpression) self.assertEqual(len(res['month'].expr), 1) self.assertEqual(res['month'].get_value(), 'January') self.assertIsInstance(res['author'], BibDataStringExpression) self.assertEqual(len(res['author'].expr), 3) self.assertEqual(res['author'].get_value(), 'Jean César') self.assertIsInstance(res['journal'], BibDataStringExpression) self.assertEqual(len(res['journal'].expr), 1) self.assertEqual(res['journal'].get_value(), 'Nice Journal')
def test_features2(self): with open('bibtexparser/tests/data/features2.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [{'type': 'inproceedings', 'year': '2014', 'title': 'Cool Stuff', 'author': 'John Doe', 'id': 'mykey', 'booktitle': 'My International Conference', 'note': 'Email: [email protected]', 'pages': '1--10', }] self.assertEqual(res, expected)
def test_features2(self): with open('bibtexparser/tests/data/features2.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [{'type': 'inproceedings', 'year': '2014', 'title': 'Cool Stuff', 'author': 'John Doe', 'id': 'mykey', 'booktitle': 'My International Conference', 'note': 'Email: [email protected]', 'pages': '1--10', }] self.assertEqual(res, expected)
def test_book_cust_latex(self): with open('bibtexparser/tests/data/book.bib', 'r') as bibfile: bib = BibTexParser(bibfile, customization=customizations_latex) res = bib.get_entry_list() expected = [{'type': 'book', 'year': '1987', 'edition': '2', 'publisher': 'Wiley Edition', 'id': 'Bird1987', 'volume': '1', 'title': '{D}ynamics of {P}olymeric {L}iquid', 'author': ['Bird, R.B.', 'Armstrong, R.C.', 'Hassager, O.'] }] self.assertEqual(res, expected)
def test_book_cust_unicode(self): with open('bibtexparser/tests/data/book.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read(), customization=customizations_unicode) res = bib.get_entry_list() expected = [{'ENTRYTYPE': 'book', 'year': '1987', 'edition': '2', 'publisher': 'Wiley Edition', 'ID': 'Bird1987', 'volume': '1', 'title': 'Dynamics of Polymeric Liquid', 'author': ['Bird, R.B.', 'Armstrong, R.C.', 'Hassager, O.'] }] self.assertEqual(res, expected)
def test_book(self): with open('bibtexparser/tests/data/book.bib', 'r') as bibfile: bib = BibTexParser(bibfile) res = bib.get_entry_list() expected = [{'type': 'book', 'year': '1987', 'edition': '2', 'publisher': 'Wiley Edition', 'id': 'Bird1987', 'volume': '1', 'title': 'Dynamics of Polymeric Liquid', 'author': 'Bird, R.B. and Armstrong, R.C. and Hassager, O.' }] self.assertEqual(res, expected)
def test_features(self): with open("bibtexparser/tests/data/features.bib", "r") as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [ { "ENTRYTYPE": "inproceedings", "year": "2014", "title": "Cool Stuff", "author": "John", "ID": "mykey", "booktitle": "My International Conference", } ] self.assertEqual(res, expected)
def test_book_cust_latex(self): with open('bibtexparser/tests/data/book.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read(), customization=customizations_latex) res = bib.get_entry_list() expected = [{'ENTRYTYPE': 'book', 'year': '1987', 'edition': '2', 'publisher': 'Wiley Edition', 'ID': 'Bird1987', 'volume': '1', 'title': '{D}ynamics of {P}olymeric {L}iquid', 'author': ['Bird, R.B.', 'Armstrong, R.C.', 'Hassager, O.'] }] self.assertEqual(res, expected)
def test_book(self): with open('bibtexparser/tests/data/book.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [{'ENTRYTYPE': 'book', 'year': '1987', 'edition': '2', 'publisher': 'Wiley Edition', 'ID': 'Bird1987', 'volume': '1', 'title': 'Dynamics of Polymeric Liquid', 'author': 'Bird, R.B. and Armstrong, R.C. and Hassager, O.' }] self.assertEqual(res, expected)
def test_encoding_with_homogenize(self): with codecs.open('bibtexparser/tests/data/encoding.bib', 'r', 'utf-8') as bibfile: bib = BibTexParser(bibfile.read(), customization=homogenize_latex_encoding) res = bib.get_entry_list() expected = [{'keywords': 'keyword1, keyword2', 'ENTRYTYPE': 'article', 'abstract': 'This is an abstract. This line should be long enough to test\nmultilines... and with a french {\\\'e}rudit word', 'year': '2013', 'journal': 'El{\\\'e}mentaire', 'ID': 'Cesar_2013', 'pages': '12-23', 'title': '{A}n amazing title: {\\`a}', 'comments': 'A comment', 'author': 'Jean C{\\\'e}sar', 'volume': '12', 'month': 'jan' }] self.assertEqual(res, expected)
def get_bibtex_entries(filename, unicode=True): """ Parse a bibtex file and return the content :param filename: bibtex filepath :param unicode: If True, unicode style, if False, latex style :returns: a dictionnary; key=ID, content=entry """ if unicode: _customizations = _customizations_unicode else: _customizations = _customizations_latex with open(filename, 'r') as bibfile: biblio = BibTexParser(bibfile.read(), customization=_customizations) entries = biblio.get_entry_list() return biblio.get_entry_dict()
def test_article_cust_unicode(self): with codecs.open('bibtexparser/tests/data/article.bib', 'r', 'utf-8') as bibfile: bib = BibTexParser(bibfile.read(), customization=customizations_unicode) res = bib.get_entry_list() expected = [{'abstract': 'This is an abstract. This line should be long enough to test\nmultilines... and with a french érudit word', 'ENTRYTYPE': 'article', 'pages': '12--23', 'volume': '12', 'ID': 'Cesar2013', 'year': '2013', 'author': ['César, Jean'], 'journal': {'ID': 'NiceJournal', 'name': 'Nice Journal'}, 'comments': 'A comment', 'month': 'jan', 'keyword': ['keyword1', 'keyword2'], 'title': 'An amazing title' }] self.assertEqual(res, expected)
def test_traps(self): with codecs.open('bibtexparser/tests/data/traps.bib', 'r', 'utf-8') as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [{'keywords': 'keyword1, keyword2', 'ENTRYTYPE': 'article', 'abstract': 'This is an abstract. This line should be long enough to test\nmultilines... and with a french érudit word', 'year': '2013', 'journal': 'Nice Journal', 'ID': 'Laide2013', 'pages': '12-23', 'title': '{An} amazing {title}', 'comments': 'A comment', 'author': 'Jean Laid{\\\'e},\nBen Loaeb', 'volume': 'n.s.~2', 'month': 'jan' }] self.assertEqual(res, expected)
def test_encoding_with_homogenize(self): with codecs.open('bibtexparser/tests/data/encoding.bib', 'r', 'utf-8') as bibfile: bib = BibTexParser(bibfile.read(), customization=homogenize_latex_encoding) res = bib.get_entry_list() expected = [{'keywords': 'keyword1, keyword2', 'ENTRYTYPE': 'article', 'abstract': 'This is an abstract. This line should be long enough to test\nmultilines... and with a french {\\\'e}rudit word', 'year': '2013', 'journal': 'El{\\\'e}mentaire', 'ID': 'Cesar_2013', 'pages': '12-23', 'title': '{A}n amazing title: {\\`a}', 'comments': 'A comment', 'author': 'Jean C{\\\'e}sar', 'volume': '12', 'month': 'jan' }] self.assertEqual(res, expected)
def test_book_cust_latex(self): with open("bibtexparser/tests/data/book.bib", "r") as bibfile: bib = BibTexParser(bibfile.read(), customization=customizations_latex) res = bib.get_entry_list() expected = [ { "ENTRYTYPE": "book", "year": "1987", "edition": "2", "publisher": "Wiley Edition", "ID": "Bird1987", "volume": "1", "title": "{D}ynamics of {P}olymeric {L}iquid", "author": ["Bird, R.B.", "Armstrong, R.C.", "Hassager, O."], } ] self.assertEqual(res, expected)
def test_book(self): with open("bibtexparser/tests/data/book.bib", "r") as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [ { "ENTRYTYPE": "book", "year": "1987", "edition": "2", "publisher": "Wiley Edition", "ID": "Bird1987", "volume": "1", "title": "Dynamics of Polymeric Liquid", "author": "Bird, R.B. and Armstrong, R.C. and Hassager, O.", } ] self.assertEqual(res, expected)
def test_article_cust_latex(self): with open('bibtexparser/tests/data/article.bib', 'r') as bibfile: bib = BibTexParser(bibfile, customization=customizations_latex) res = bib.get_entry_list() expected = [{'abstract': 'This is an abstract. This line should be long enough to test\nmultilines... and with a french {\\\'e}rudit word', 'type': 'article', 'pages': '12--23', 'volume': '12', 'id': 'Cesar2013', 'year': '2013', 'author': ['C{\\\'e}sar, Jean'], 'journal': {'id': 'NiceJournal', 'name': 'Nice Journal'}, 'comments': 'A comment', 'month': 'jan', 'keyword': ['keyword1', 'keyword2'], 'title': '{A}n amazing title' }] self.assertEqual(res, expected)
def test_article(self): with open('bibtexparser/tests/data/article.bib', 'r') as bibfile: bib = BibTexParser(bibfile) res = bib.get_entry_list() expected = [{'keyword': 'keyword1, keyword2', 'type': 'article', 'abstract': 'This is an abstract. This line should be long enough to test\nmultilines... and with a french érudit word', 'year': '2013', 'journal': 'Nice Journal', 'id': 'Cesar2013', 'pages': '12-23', 'title': 'An amazing title', 'comments': 'A comment', 'author': 'Jean César', 'volume': '12', 'month': 'jan' }] self.assertEqual(res, expected)
def test_traps(self): with codecs.open('bibtexparser/tests/data/traps.bib', 'r', 'utf-8') as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [{'keywords': 'keyword1, keyword2', 'ENTRYTYPE': 'article', 'abstract': 'This is an abstract. This line should be long enough to test\nmultilines... and with a french érudit word', 'year': '2013', 'journal': 'Nice Journal', 'ID': 'Laide2013', 'pages': '12-23', 'title': '{An} amazing {title}', 'comments': 'A comment', 'author': 'Jean Laid{\\\'e},\nBen Loaeb', 'volume': 'n.s.~2', 'month': 'jan' }] self.assertEqual(res, expected)
def get_bibtex_entries(filename, unicode=True): """ Parse a bibtex file and return the content :param filename: bibtex filepath :param unicode: If True, unicode style, if False, latex style :returns: a dictionnary; key=ID, content=entry """ if unicode: _customizations = _customizations_unicode else: _customizations = _customizations_latex with open(filename, 'r') as bibfile: biblio = BibTexParser(bibfile.read(), customization=_customizations) entries = biblio.get_entry_list() return biblio.get_entry_dict()
def test_encoding(self): with open('bibtexparser/tests/data/encoding.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [{'keyword': 'keyword1, keyword2', 'type': 'article', 'abstract': 'This is an abstract. This line should be long enough to test\nmultilines... and with a french érudit word', 'year': '2013', 'journal': 'Elémentaire', 'id': 'Cesar_2013', 'pages': '12-23', 'title': 'An amazing title: à', 'comments': 'A comment', 'author': 'Jean César', 'volume': '12', 'month': 'jan' }] self.assertEqual(res, expected)
def test_string_is_interpolated(self): with open('bibtexparser/tests/data/article_with_strings.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read(), common_strings=True, interpolate_strings=True) res = bib.get_entry_list() expected = [{ 'keyword': 'keyword1, keyword2', 'ENTRYTYPE': 'article', 'year': '2013', 'month': 'January', 'journal': 'Nice Journal', 'ID': 'Cesar2013', 'pages': '12-23', 'title': 'An amazing title', 'comments': 'A comment', 'author': 'Jean César', 'volume': '12', }] self.assertEqual(res, expected)
def test_field_name_with_dash_underscore(self): with open('bibtexparser/tests/data/article_field_name_with_underscore.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [{ 'keyword': 'keyword1, keyword2', 'ENTRYTYPE': 'article', 'year': '2013', 'journal': 'Nice Journal', 'ID': 'Cesar2013', 'pages': '12-23', 'title': 'An amazing title', 'comments': 'A comment', 'author': 'Jean César', 'volume': '12', 'strange_field_name': 'val', 'strange-field-name2': 'val2', }] self.assertEqual(res, expected)
def test_string_is_interpolated(self): with open('bibtexparser/tests/data/article_with_strings.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read(), common_strings=True, interpolate_strings=True) res = bib.get_entry_list() expected = [{ 'keyword': 'keyword1, keyword2', 'ENTRYTYPE': 'article', 'year': '2013', 'month': 'January', 'journal': 'Nice Journal', 'ID': 'Cesar2013', 'pages': '12-23', 'title': 'An amazing title', 'comments': 'A comment', 'author': 'Jean César', 'volume': '12', }] self.assertEqual(res, expected)
def test_field_name_with_dash_underscore(self): with open('bibtexparser/tests/data/article_field_name_with_underscore.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) res = bib.get_entry_list() expected = [{ 'keyword': 'keyword1, keyword2', 'ENTRYTYPE': 'article', 'year': '2013', 'journal': 'Nice Journal', 'ID': 'Cesar2013', 'pages': '12-23', 'title': 'An amazing title', 'comments': 'A comment', 'author': 'Jean César', 'volume': '12', 'strange_field_name': 'val', 'strange-field-name2': 'val2', }] self.assertEqual(res, expected)
def test_book_cust_unicode(self): with open('bibtexparser/tests/data/book.bib', 'r') as bibfile: bib = BibTexParser(bibfile, customization=customizations_unicode) res = bib.get_entry_list() expected = [{ 'type': 'book', 'year': '1987', 'edition': '2', 'publisher': 'Wiley Edition', 'id': 'Bird1987', 'volume': '1', 'title': 'Dynamics of Polymeric Liquid', 'author': ['Bird, R.B.', 'Armstrong, R.C.', 'Hassager, O.'] }] self.assertEqual(res, expected)
def test_article_cust_unicode(self): with open('bibtexparser/tests/data/article.bib', 'r') as bibfile: bib = BibTexParser(bibfile, customization=customizations_unicode) res = bib.get_entry_list() expected = [{ 'abstract': 'This is an abstract. This line should be long enough to test\nmultilines... and with a french érudit word', 'type': 'article', 'pages': '12--23', 'volume': '12', 'id': 'Cesar2013', 'year': '2013', 'author': ['César, Jean'], 'journal': { 'id': 'NiceJournal', 'name': 'Nice Journal' }, 'comments': 'A comment', 'month': 'jan', 'keyword': ['keyword1', 'keyword2'], 'title': 'An amazing title' }] self.assertEqual(res, expected)
def bibtex_to_article_object(filename, review_id, source_id): filehandler = open(filename, 'r') parser = BibTexParser(filehandler) record_list = parser.get_entry_list() articles = [] for record in record_list: article = Article() try: if 'id' in record: article.bibtex_key = record['id'][:100] if 'title' in record: article.title = record['title'][:1000] if 'journal' in record: article.journal = record['journal'][:1000] if 'year' in record: article.year = record['year'][:10] if 'author' in record: article.author = record['author'][:1000] if 'abstract' in record: article.abstract = record['abstract'][:4000] if 'pages' in record: article.pages = record['pages'][:20] if 'volume' in record: article.volume = record['volume'][:100] if 'document_type' in record: article.document_type = record['document_type'][:100] article.review = Review(id=review_id) article.source = Source(id=source_id) except: continue articles.append(article) return articles
def test_nonstandard_not_ignored(self): with open('bibtexparser/tests/data/wrong.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read(), ignore_nonstandard_types=False) res = bib.get_entry_list() self.assertEqual(len(res), 2)
'': { 'handlers': ['default'], 'level': 'DEBUG', 'formatter': 'standard', 'propagate': True } } }) if __name__ == '__main__': bibtex_source = """@ARTICLE{Cesar2013, author = {Jean César}, title = {An amazing title}, year = {2013}, month = {1}, volume = {12}, pages = {12--23}, journal = {Nice Journal}, abstract = {This is an abstract. This line should be long enough to test multilines...}, comments = {A comment}, keywords = {keyword1, keyword2}, } """ from bibtexparser.bparser import BibTexParser with io.StringIO(bibtex_source) as f: bp = BibTexParser(f.read()) print(bp.get_entry_list())
def test_article_start_with_whitespace(self): with open('bibtexparser/tests/data/article_start_with_whitespace.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) self.assertEqual(len(bib.get_entry_list()), 2)