def test_multiline_comment_write(self): with open("bibtexparser/tests/data/multiline_comments.bib") as bibfile: expected = bibfile.read() bib = BibTexParser(expected) result = to_bibtex(bib) self.assertEqual(result, expected)
def test_multiple_entries(self): with open('bibtexparser/tests/data/multiple_entries_and_comments.bib') as bibfile: bib = BibTexParser(bibfile.read()) with open('bibtexparser/tests/data/multiple_entries_and_comments_output.bib') as bibfile: expected = bibfile.read() result = to_bibtex(bib) self.assertEqual(result, expected)
def test_multiline_comment_write(self): with open('bibtexparser/tests/data/multiline_comments.bib') as bibfile: expected = bibfile.read() bib = BibTexParser(expected) result = to_bibtex(bib) self.assertEqual(result, expected)
def test_multiple_entries(self): with open("bibtexparser/tests/data/multiple_entries_and_comments.bib") as bibfile: bib = BibTexParser(bibfile.read()) with open("bibtexparser/tests/data/multiple_entries_and_comments_output.bib") as bibfile: expected = bibfile.read() result = to_bibtex(bib) self.assertEqual(result, expected)
def test_comment_write(self): with open("bibtexparser/tests/data/comments_only.bib") as bibfile: bib = BibTexParser(bibfile.read()) with open("bibtexparser/tests/data/comments_only_output.bib") as bibfile: expected = bibfile.read() result = to_bibtex(bib) self.assertEqual(result, expected)
def test_comment_write(self): with open('bibtexparser/tests/data/comments_only.bib') as bibfile: bib = BibTexParser(bibfile.read()) with open('bibtexparser/tests/data/comments_only_output.bib') as bibfile: expected = bibfile.read() result = to_bibtex(bib) self.assertEqual(result, expected)
def test_multiple(self): with open('bibtexparser/tests/data/multiple_entries.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) with open('bibtexparser/tests/data/multiple_entries_output.bib', 'r') as bibfile: expected = bibfile.read() result = to_bibtex(bib) self.maxDiff = None self.assertEqual(expected, result)
def test_book(self): with open('bibtexparser/tests/data/book.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) with open('bibtexparser/tests/data/book_output.bib', 'r') as bibfile: expected = bibfile.read() result = to_bibtex(bib) self.maxDiff = None self.assertEqual(expected, result)
def test_multiple(self): with io.open(_data_path('multiple_entries.bib'), 'r') as bibfile: bib = BibTexParser(bibfile.read()) with io.open(_data_path('multiple_entries_output.bib'), 'r') as bibfile: expected = bibfile.read() result = to_bibtex(bib) self.maxDiff = None self.assertEqual(expected, result)
def test_book(self): with io.open(_data_path('book.bib'), 'r') as bibfile: bib = BibTexParser(bibfile.read()) with io.open(_data_path('book_output.bib'), 'r') as bibfile: expected = bibfile.read() result = to_bibtex(bib) self.maxDiff = None self.assertEqual(expected, result)
def test_article_with_annotation(self): with io.open(_data_path('article_with_annotation.bib'), 'r') as bibfile: bib = BibTexParser(bibfile.read()) with io.open(_data_path('article_with_annotation_output.bib'), 'r') \ as bibfile: expected = bibfile.read() result = to_bibtex(bib) self.maxDiff = None self.assertEqual(expected, result)
def test_with_strings(self): with io.open(_data_path('article_with_strings.bib'), 'r') as bibfile: bib = BibTexParser(bibfile.read(), common_strings=True, interpolate_strings=False) with io.open(_data_path( 'article_with_strings_output.bib'), 'r') as bibfile: expected = bibfile.read() result = to_bibtex(bib) self.maxDiff = None self.assertEqual(expected, result)
def test_article(self): with open('bibtexparser/tests/data/article.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) with open('bibtexparser/tests/data/article_output.bib', 'r') as bibfile: expected = bibfile.read() result = to_bibtex(bib) if python2 and isinstance(result, unicode): result = result.encode('utf-8') self.maxDiff = None self.assertEqual(expected, result)
def test_with_strings(self): with io.open(_data_path('article_with_strings.bib'), 'r') as bibfile: bib = BibTexParser(bibfile.read(), common_strings=True, interpolate_strings=False) with io.open(_data_path('article_with_strings_output.bib'), 'r') as bibfile: expected = bibfile.read() result = to_bibtex(bib) self.maxDiff = None self.assertEqual(expected, result)
def append_bibfile(bib_path, entry): bibtex_db = BibTexParser('') print(entry) bibtex_db.records.append({k: fmt_bibtex(v) for k, v in entry.items() if v}) bibtex_str = to_bibtex(bibtex_db) # append to the output file with open(bib_path, 'a') as bibtex_file: bibtex_file.write(bibtex_str) refresh_caches()
def test_article(self): with open('bibtexparser/tests/data/article.bib', 'r') as bibfile: bib = BibTexParser(bibfile.read()) with open('bibtexparser/tests/data/article_output.bib', 'r') as bibfile: expected = bibfile.read() result = to_bibtex(bib) if not sys.version_info >= (3, 0): if isinstance(result, unicode): result = result.encode('utf-8') self.maxDiff = None self.assertEqual(expected, result)
def main(): parser = ArgumentParser() parser.add_argument("target",help="The bib file to abbreviate.") parser.add_argument("-o","--output",help="The output file name. If missing, output will be sent to stdout.") parser.add_argument("-r","--reverse",help="Reverse the process and unabbreviate journal names.",action="store_true") parser.add_argument("-a","--abbreviations",help="Path to a file of abbreviations in the form (one per line): Journal of Biological Science = J. Sci. Biol.") parser.add_argument("-v","--verbose",action="store_true") args = parser.parse_args() level = logging.WARNING if not args.verbose else logging.INFO logger.setLevel(level) input = open(args.target,"r") output = open(args.output,"w") if args.output else sys.stdout refs_bp = BibTexParser(input.read(),customization=homogeneize_latex_encoding) refs = refs_bp.get_entry_dict() abbrevs = load_abbrevs(determine_path()+"/journal_files/journal_abbreviations_general.txt",reverse=args.reverse) for ref in refs: if 'journal' in refs[ref]: # Assume that if it has a journal key, then it needs abbreviating. I'm doing this # instead of testing for type==article in case I've forgotten about a case where # type != article but there's a journal field. # Also, journal names with one word ('Nature') don't require abbreviation. if len(refs[ref]['journal'].split(' ')) > 1: journal = refs[ref]['journal'].lower() # Handle any difficult characters. TODO: check that this list is complete. journal_clean = re.sub('[{}]','',journal) try: refs[ref]['journal'] = abbrevs[journal_clean] logger.info('%s replaced with %s for key %s' % (journal,abbrevs[journal_clean],ref)) except KeyError: logger.error('%s not found in abbreviations!' % (journal_clean)) output_bib = to_bibtex(refs_bp) output.write(output_bib)
from bibtexparser.bparser import BibTexParser from bibtexparser.customization import convert_to_unicode from bibtexparser.bwriter import to_bibtex def pdf(record): if "file" in record: if record["file"]: f = [i.strip().strip('/') for i in record["file"].split("/")] g = [i.strip().strip(':') for i in f[-1].split(":")] print(g[0]) record["file"] = "../papers/My Collection.Data/PDF/" + g[0] return record def customizations(record): record = convert_to_unicode(record) pdf(record) return record with open('publications.bib', 'r') as bibfile: bp = BibTexParser(bibfile.read(), customization=customizations) output = to_bibtex(bp) file = open("publications_web.bib", "wb") file.write(output.encode('utf8')) file.close()
import bibtexparser import io with io.open('library.bib') as bib: data = bib.read() prs = bibtexparser.bparser.BibTexParser(data) prs from bibtexparser.bwriter import to_bibtex with io.open('library.bib','w') as bib: bib.write(to_bibtex(prs))