-
Notifications
You must be signed in to change notification settings - Fork 0
/
bibparser.py
62 lines (52 loc) · 1.88 KB
/
bibparser.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
WiSe 15, WebIR, Project-2, Becet, Simmet
BibParser Module
"""
from init_logging import init_logging
import logging
import bibtexparser
from bibtexparser.bparser import BibTexParser
from bibtexparser.customization import convert_to_unicode, type, author
from my_customization import editor
import glob
logger = logging.getLogger(__name__)
def _customizations(record):
"""
Bibtexparser customizations that are applied to every entry found in the .bib files
"""
record = convert_to_unicode(record)
record = type(record) # make the entry types lower-case
record = author(record) # split the authors into a list
record = editor(record) # split the editors into a list
return record
def get_all_entries(locations):
"""
Retrieve all the .bib files and parse their content to a list of simple dictionaries
:param locations: locations of all the .bib files
:return: return a list of dictionaries
"""
result = []
my_parser = BibTexParser()
my_parser.customization = _customizations
for file_location in locations:
if file_location.endswith(".bib"):
with open(file_location) as bib_file:
try:
result.extend(bibtexparser.load(bib_file, parser=my_parser).entries)
except:
logger.error(".bib parsing error: " + file_location)
return result
def main():
"""
This is a main procedure only for testing! It shouldn't normally run!
"""
logger.warning("This is a main procedure only for testing this module! It shouldn't normally run!")
bib_db = get_all_entries(glob.glob("bibs/*.bib"))
for dct in bib_db:
try:
print(str(dct))
except UnicodeEncodeError as e:
logger.error("{}: Entry with id='{}' can't be printed".format(e.reason, dct["ID"]))
if __name__ == "__main__":
init_logging()
main()