forked from G-Node/GCA-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gca-client
executable file
·139 lines (119 loc) · 5.86 KB
/
gca-client
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
#!/usr/bin/env python
from __future__ import print_function
import argparse
import sys
from gca.core import Session
from gca.auth import NetRCAuth
def write_json(json_data):
data = Session.to_json(json_data)
sys.stdout.write(data.encode('utf-8'))
return data
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='GCA Client')
parser.add_argument('url', type=str)
parser.add_argument('-a', '--auth', dest='auth', action='store_true', default=False, help='force authentication')
subparsers = parser.add_subparsers(help='commands')
parser_abs = subparsers.add_parser("abstracts", help='list abstracts')
parser_abs.add_argument('conference', type=str, help='for which conference')
parser_abs.add_argument('-f', '--full', help='fetch all data', action='store_true', default=False)
parser_abs.set_defaults(cmd='abstracts')
parser_abs = subparsers.add_parser("image", help='get figure images')
parser_abs.add_argument('uuid', type=str, nargs='+', help='figure uuid')
parser_abs.add_argument('--path', type=str, default=None, help='path to store the figures in')
parser_abs.set_defaults(cmd='image')
parser_abs = subparsers.add_parser("owners", help='get owner of an object')
parser_abs.add_argument('-t', '--type', type=str, help='type to fetch [abstract]', default='abstracts', dest='otype')
parser_abs.add_argument('uuid', type=str, nargs='+', help='object uuid')
parser_abs.set_defaults(cmd='owners')
parser_abs = subparsers.add_parser("log", help='get state log of an abstract')
parser_abs.add_argument('uuid', type=str, help='abstract uuid to fetch log for')
parser_abs.set_defaults(cmd='state_log')
parser_abs = subparsers.add_parser("put", help='put abstracts')
parser_abs.add_argument('conference', type=str, help='for which conference')
parser_abs.add_argument('abstract', type=str, help='abstract to upload')
parser_abs.set_defaults(cmd='put')
parser_abs = subparsers.add_parser("conference", help='fetch conference info')
parser_abs.add_argument('conference', type=str, help='conf to fetch')
parser_abs.set_defaults(cmd='conf')
parser_abs = subparsers.add_parser("patch", help='patch abstracts')
parser_abs.add_argument('file', type=str, default='-', help="json file to read abstracts from")
parser_abs.add_argument('fields', type=str, nargs='+', help='fields to patch online in the abstract')
parser_abs.set_defaults(cmd='patch')
parser_abs = subparsers.add_parser("set-state", help='set the state if abstracts')
parser_abs.add_argument('state', type=str, help='new state for the abstract')
parser_abs.add_argument('uuid', type=str, nargs='+', help='abstracts to set the state')
parser_abs.add_argument('--note', type=str, dest='note', default='', help='note to attach to the state change log')
parser_abs.set_defaults(cmd='set-state')
args = parser.parse_args()
auth = NetRCAuth()
session = Session(args.url, auth)
if args.auth:
session.authenticate()
if args.cmd == 'abstracts':
abstracts = session.get_all_abstracts(args.conference, raw=True, full=args.full)
write_json(abstracts)
elif args.cmd == 'image':
paths = [session.get_figure_image(uuid, path=args.path) for uuid in args.uuid]
for p in paths:
print(p)
elif args.cmd == 'owners':
import itertools
owners = itertools.chain.from_iterable(session.get_owners(uuid, args.otype, raw=True) for uuid in args.uuid)
write_json(list(owners))
elif args.cmd == 'state_log':
log = session.get_state_log(args.uuid, raw=True)
write_json(log)
elif args.cmd == 'put':
import json
import codecs
conference = args.conference
fd = codecs.open(args.abstract, 'r', encoding='utf-8') if args.abstract != '-' else sys.stdin
data = json.load(fd)
if isinstance(data, list):
res = [session.upload_abstract(a, conference, raw=True) for a in data]
else:
res = session.upload_abstract(data, conference, raw=True)
write_json(res)
elif args.cmd == 'conf':
conf = session.get_conference(args.conference, raw=True)
write_json(conf)
elif args.cmd == 'patch':
import codecs
import json
import urllib2
fd = codecs.open(args.file, 'r', encoding='utf-8') if args.file != '-' else sys.stdin
data = json.load(fd)
abstracts = []
failed = []
for abstract in data:
uuid = abstract['uuid']
try:
sys.stderr.write('[P] %s\n' % uuid)
patched = session.patch_abstract(abstract, args.fields, raw=True)
abstracts.append(patched)
except urllib2.HTTPError, error:
sys.stderr.write('[E] Failed: %s \n' % str(error.code))
failed.append((uuid, str(error.code)))
if len(failed):
sys.stderr.write('Following abstracts could not be patched [N = %d of %d]:\n' %
(len(failed), len(data)))
for f in failed:
sys.stderr.write('\t%s [%s]\n' % (f[0], f[1]))
write_json(abstracts)
elif args.cmd == 'set-state':
import urllib2
failed = []
logs = []
for uuid in args.uuid:
try:
log = session.set_state(uuid, args.state, args.note, raw=True)
logs.append(log)
except urllib2.HTTPError, error:
sys.stderr.write('[E] Failed: %s \n' % str(error.code))
failed.append((uuid, str(error.code)))
if len(failed):
sys.stderr.write('Following abstracts had an error while setting the state [N = %d of %d]:\n' %
(len(failed), len(args.uuid)))
for f in failed:
sys.stderr.write('\t%s [%s]\n' % (f[0], f[1]))
write_json(logs)