-
Notifications
You must be signed in to change notification settings - Fork 2
/
bibrmfield
executable file
·144 lines (125 loc) · 4.87 KB
/
bibrmfield
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
#
# Author: Gokberk Cinbis
# November 2019
import sys
DEL_DEFAULTS = ['abstract','keywords','file','note','url','urldate','isbn','doi','publisher','annote','editor','copyright']
opts = {}
opts['unique'] = False
opts['nocomment'] = False
opts['printcmd'] = False
opts['nobak'] = False
opts['stderr'] = False
def help_and_exit():
print('bibrmfield [?field1] ... [?fieldn] <additional_arguments> [<file>]')
print('Remove unwanted fields from a bibtex file.')
print('This script will create the backup <file>.bak (any existing file will be overwritten) and alter the original file using bibtool.')
print('Each [?fieldj] is optional. ? has to be - (delete) or + (keep). "field" refers to a bibtex field.')
print('By default, only the following fields are deleted:')
for x in set(DEL_DEFAULTS):
print(' - ' + x)
print('To keep a field "f" that is deleted by default, pass "+f". e.g., bibrmfield +abstract <file>')
print('If a field is passed multiple times in the options, the final one determines the actions.')
print('')
print('ADDITIONAL ARGUMENTS')
print('--help Print help & exit')
print('--unique Comment-out repeated entries, according to Finding Double Entries poliy of bibtool (see manual). Does not always work well. (Off by default)')
print('--nocomment Delete commented-out entries. (Off by default)')
print('--printcmd Print-out the bibtool command before execution. (Off by default)')
print('--stderr Do not suppress stderr (suppressed by default)')
print('--nobak Delete the generated bak field at the end of the process. (Ignored if input is stdin)')
print('')
print('FILE ARGUMENT')
print('If <file> is -- or not provided, stdin is used.')
print('')
print('RESOURCES')
print('* http://www.gerd-neugebauer.de/software/TeX/BibTool/bibtool.pdf')
print('* http://www.gerd-neugebauer.de')
exit(1)
def parse_args(argv,opts):
#if len(argv) < 2:
# help_and_exit()
path=None
# NO ! path=argv[-1] # file
del_flds = set(DEL_DEFAULTS)
arg_index = 0
for opt in argv[1:]:
arg_index += 1
if opt == '--unique':
opts['unique'] = True
if opt == '--nobak':
opts['nobak'] = True
if opt == '--stderr':
opts['stderr'] = True
elif opt == '--help':
help_and_exit()
elif opt == '--nocomment':
opts['nocomment'] = True
elif opt == '--printcmd':
opts['printcmd'] = True
elif opt == '--': # stdin, handle before "-field"
if arg_index < (len(argv)-1):
print('-- option can only be the last argument')
exit(2)
else:
path = '--'
elif opt[0] == '-':
del_flds.add(opt[1:]) # set operation
elif opt[0] == '+':
if opt[1:] in del_flds:
del_flds.remove(opt[1:]) # set operation
elif arg_index == (len(argv)-1):
path = opt
else:
print('Unrecognized option: ' + opt)
exit(2)
if path is None:
path = '--' # stdin by default
if path != '--':
print('These fields will be deleted: ',del_flds) # dont print in stdin
return path,del_flds
path,del_flds = parse_args(sys.argv,opts)
# first create the backup
if path != '--':
from shutil import copyfile
path_bak = path + '.bak'
copyfile(path, path_bak)
stdin=False
else:
stdin=True
# Example: bibtool --delete.field='{ abstract }' --delete.field='{ keywords }' meta-learning.bib
cmd = 'bibtool'
for f in del_flds:
cmd += " --delete.field='{ " + f + " }'"
if opts['nocomment']:
cmd += " --pass.comments='{off}'"
else:
cmd += " --pass.comments='{on}'"
if opts['unique']:
cmd += " -- 'check.double = on'" # (on)
cmd += " -- 'check.double.delete=off'" # (off)
cmd += " -- 'print.deleted.entries=on'" # (on = just comment out, do not delete)
else:
cmd += " -- 'check.double=off'" # in case default options of bibtool change over time
if not stdin:
#cmd += ' "' + path + '.bak" -o "' + path + '"' -- .bib.bak extension fails in linux, for some reasons I don't know
cmd += ' "' + path + '" -o "' + path + '"'
# bibtool lowers key cases for historic reasons, better not manipulate them.
cmd += " --preserve.key.case='{on}'"
if opts['printcmd']:
print(cmd)
import subprocess
if opts['stderr']:
output = subprocess.check_output(cmd,shell=True)
else:
output = subprocess.check_output(cmd,shell=True,stderr=subprocess.DEVNULL)
if stdin:
print(output.decode("utf-8"))
else:
print('Done. Check the output file carefully.')
if opts['nobak']:
from os import remove
try:
remove(path_bak)
except OSError as e: ## if failed, report it back to the user ##
print ("Error: %s - %s." % (e.filename, e.strerror))