-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbtx_io.py
40 lines (34 loc) · 1.25 KB
/
btx_io.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#! python3
"""
Simple IO functions to get pybtex.database.Entry objects out of and into
files or strings.
"""
from collections import OrderedDict
from io import open
import os
from pybtex import database
def read_bib_entries(*locations):
"""Yield pybtex.database.Entry objects from each location in turn.
Locations can be file names or strings containing file contents.
"""
for loc in locations:
if os.path.isfile(loc):
with open(loc, encoding='latin1') as f:
loc = f.read()
for item in database.parse_string(
loc.replace('}.' ,'},'), 'bibtex').entries.values():
yield item
def write_bib_entries(entries, fname=None):
"""Take an iterable of pybtex.database.Entry objects, and return a string.
If fname is not None, write the string to a file before returning it.
"""
entries_dict = OrderedDict((e.key, e) for e in entries)
if not entries_dict:
return ''
#assert len(entries) == len(entries_dict), 'Entries must have unique keys'
btex_str = database.BibliographyData(
entries=entries_dict).to_string('bibtex')
if fname is not None:
with open(fname, 'w', encoding='latin1') as f:
f.write(btex_str)
return btex_str