-
Notifications
You must be signed in to change notification settings - Fork 34
/
cli.py
executable file
·85 lines (67 loc) · 2.19 KB
/
cli.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
import os
import sys
import optparse
import inspect
# does setup of cfg
from bibserver import dao
def rebuild_db():
'''Rebuild the db'''
conn, db = dao.get_conn()
conn.delete_index(db)
conn.create_index(db)
def fixtures():
import test.base
for dict_ in test.base.fixtures['records']:
dao.Record.upsert(dict_)
def convert(inpath):
'''Convert from bibtex to bibjson. One argument expected: path to bibtext
file.
'''
import bibserver.parsers.BibTexParser
import json
parser = parsers.BibTexParser.BibTexParser()
bibtex = open(inpath).read()
print json.dumps(parser.parse(bibtex), indent=2, sort_keys=True)
def bulk_upload(colls_list):
'''Take a collections list in a JSON file and use the bulk_upload importer.
colls_list described in importer.py
'''
import bibserver.importer
return bibserver.importer.bulk_upload(colls_list)
## ==================================================
## Misc stuff for setting up a command line interface
def _module_functions(functions):
local_functions = dict(functions)
for k,v in local_functions.items():
if not inspect.isfunction(v) or k.startswith('_'):
del local_functions[k]
return local_functions
def _main(functions_or_object):
isobject = inspect.isclass(functions_or_object)
if isobject:
_methods = _object_methods(functions_or_object)
else:
_methods = _module_functions(functions_or_object)
usage = '''%prog {action}
Actions:
'''
usage += '\n '.join(
[ '%s: %s' % (name, m.__doc__.split('\n')[0] if m.__doc__ else '') for (name,m)
in sorted(_methods.items()) ])
parser = optparse.OptionParser(usage)
# Optional: for a config file
# parser.add_option('-c', '--config', dest='config',
# help='Config file to use.')
options, args = parser.parse_args()
if not args or not args[0] in _methods:
parser.print_help()
sys.exit(1)
method = args[0]
if isobject:
getattr(functions_or_object(), method)(*args[1:])
else:
_methods[method](*args[1:])
__all__ = [ '_main' ]
if __name__ == '__main__':
_main(locals())