-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubselect.py
51 lines (38 loc) · 1.25 KB
/
subselect.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Subselects a type of publication and re-writes a bib file preserving the
# order found on the original file
import os
import sys
import datetime
# Fix strings output by pybtex which are somehow 'interpreted'
FIX_STRINGS = [
('\\\\', '\\'),
('\\textasciitilde ', '~'),
('\_', '_'),
]
def main():
if len(sys.argv) != 3:
print('Filters BibTeX file preserving order and limiting to last 5 years')
print('usage: %s <original-bib> <output-bib>' % \
os.path.basename(sys.argv[0]))
print('example: %s publications.bib filtered.bib')
sys.exit(1)
original = sys.argv[1]
minyear = datetime.date.today().year - 5
output = sys.argv[2]
from pybtex.database import parse_file, BibliographyData
bib_data = parse_file(original)
filtered = BibliographyData()
for key in bib_data.entries:
entry = bib_data.entries[key]
year = int(entry.fields['year'])
if year > minyear:
print('Selecting @%s[%s] from %s' % (entry.type, key, year))
filtered.entries[key] = entry
print('Saving to %s...' % output)
s = filtered.to_string('bibtex')
for f, t in FIX_STRINGS: s = s = s.replace(f, t)
with open(output, 'wt') as f: f.write(s)
if __name__ == '__main__':
main()