-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbibrename.py
executable file
·63 lines (50 loc) · 2.17 KB
/
bibrename.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
63
#!/usr/bin/env python3
import os.path
from biblib import FileBibDB, Entry
# FIXME: this is ugly, algo.py and messages.py should be removed
import algo
import argparse
import shutil
import sys
import re
papers_path = '/Documents/papers/'
def main():
# We want to use the file tag
Entry.processedTags.append('file')
arg_parser = argparse.ArgumentParser(
description='Rename a .pdf based on the content of a .bib first entry')
arg_parser.add_argument('--bib', required=True, help='.bib file(s) to process',
type=open)
arg_parser.add_argument('--pdf', required=True, help='.pdf file to rename',
type=open)
arg_parser.add_argument('--last', help='use last key', action="store_true", default=False)
arg_parser.add_argument('--key', help='cite key to use', action="store", dest="key")
arg_parser.add_argument('--dry-run', help='do not remove any files', action="store_true", default=False)
args = arg_parser.parse_args()
print (args)
# Load databases
db = FileBibDB(args.bib.name, method = 'force', mode = 'r')
entry = list(db.values())[-1]
if rename_file(entry, args.pdf.name, args.bib.name, args.dry_run):
for key, value in entry.items():
print (str(key) + ">" + str(value))
print (db.ckeys)
#db.add_entry(entry, entry.ckey, method = 'force')
def rename_file(entry, pdf, bib, dry_run):
if entry['author']:
authors = entry['author'].split(',')
if len(authors) <= 3:
author = ', '.join(authors[:-1])
else:
author = authors[0] + ' et al.'
if author and 'year' in entry and 'title' in entry:
newname = author + ' - ' + '{}'.format(entry['year']) + ' - ' + algo.tex_to_unicode(algo.title_case(entry['title'])).replace("/", " ") + '.pdf'
if os.path.exists(pdf):
shutil.copy2(pdf, os.path.expanduser("~") + papers_path + newname)
entry.set_tag('file', ':' + pdf + ':PDF' )
if not dry_run:
shutil.move(pdf, os.path.expanduser("~") + '/.local/share/Trash/files/')
return True
return False
if __name__ == '__main__':
main()