-
Notifications
You must be signed in to change notification settings - Fork 13
/
one_ENCODE.py
executable file
·317 lines (279 loc) · 11.2 KB
/
one_ENCODE.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python3
# -*- coding: latin-1 -*-
''' Script to add one ENCODE object from a file or stdin or get one object from an ENCODE server
'''
import json
import sys
import os.path
from base64 import b64encode
#import magic
import mimetypes
import encodedcc
EPILOG = '''Examples:
To get one ENCODE object from the server/keypair called "default" in the default keypair file and print the JSON:
%(prog)s --id ENCBS000AAA
To use a different key from the default keypair file:
%(prog)s --id ENCBS000AAA --key submit
To save the output:
%(prog)s --id ENCBS000AAA --key submit > my_saved_json.json
To PATCH or POST from JSON:
%(prog)s --infile my_new_json.json
Where the file contains a @id property that, if it matches an existing object do a PATCH, else do a POST.
To force a PUT:
%(prog)s --infile my_new_json.json --force-put
To force a GET only (no PATCH, PUT or POST) of the object as it exists in the database:
%(prog)s --infile my_new_json.json --get-only
In case of emergency, break glass:
echo '{"@id": "/biosamples/ENCBS999JSS/", "note": "This is destructive"}' | %(prog)s
'''
'''force return from the server in JSON format'''
HEADERS = {'content-type': 'application/json'}
def main():
import argparse
parser = argparse.ArgumentParser(
description=__doc__, epilog=EPILOG,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument('--infile', '-i',
help="File containing the JSON object as a JSON string.")
parser.add_argument('--server',
help="Full URL of the server.")
parser.add_argument('--key',
default='default',
help="The keypair identifier from the keyfile. \
Default is --key=default")
parser.add_argument('--keyfile',
default=os.path.expanduser("~/keypairs.json"),
help="The keypair file. Default is --keyfile\
=%s" % (os.path.expanduser("~/keypairs.json")))
parser.add_argument('--authid',
help="The HTTP auth ID.")
parser.add_argument('--authpw',
help="The HTTP auth PW.")
parser.add_argument('--force-put',
default=False,
action='store_true',
help="Force the object to be PUT rather than PATCHed. \
Default is False.")
parser.add_argument('--get-only',
default=False,
action='store_true',
help="Do nothing but get the object and print it. \
Default is False.")
parser.add_argument('--id',
help="URI for an object"),
parser.add_argument('--debug',
default=False,
action='store_true',
help="Print debug messages. Default is False.")
parser.add_argument('--frame',
help="define a frame to get back the JSON object, for use with --id. Default is frame=object",
default="object")
parser.add_argument('--type',
help="the object's type")
parser.add_argument('--update',
default=False,
action='store_true',
help="Let the script PATCH/POST the data. Default is False")
args = parser.parse_args()
global DEBUG_ON
DEBUG_ON = args.debug
if args.get_only:
GET_ONLY = True
else:
GET_ONLY = False
key = encodedcc.ENC_Key(args.keyfile, args.key)
if args.server and args.authpw and args.authid:
key.server = args.server
key.authid = args.authid
key.authpw = args.authpw
print("Creating authorization data from command line inputs")
connection = encodedcc.ENC_Connection(key)
print("Running on {}".format(connection.server))
if args.update:
print("This is an UPDATE run! Data will be PATCHed or POSTed accordingly")
else:
print("This is a dry run, no data will be changed")
new_object = False
if args.id:
GET_ONLY = True
print("Taking id to get from --id")
new_json = {}
uuid_response = {}
accession_response = {}
try:
id_response = encodedcc.get_ENCODE(
args.id, connection, frame=args.frame)
except:
id_response = {}
new_object = True
else:
if args.infile:
infile = open(args.infile, 'r')
else:
infile = sys.stdin
new_json_string = infile.read()
new_json = json.loads(new_json_string)
if args.debug:
encodedcc.pprint_ENCODE(new_json)
if '@id' in new_json:
id_response = encodedcc.get_ENCODE(new_json['@id'], connection)
if id_response.get("code") == 404:
id_response = {}
new_object = True
else:
id_response = {}
new_object = True
if 'uuid' in new_json:
uuid_response = encodedcc.get_ENCODE(new_json['uuid'], connection)
if uuid_response.get("code") == 404:
uuid_response = {}
new_object = True
else:
uuid_response = {}
new_object = True
if 'accession' in new_json:
accession_response = encodedcc.get_ENCODE(
new_json['accession'], connection)
if accession_response.get("code") == 404:
accession_response = {}
new_object = True
else:
accession_response = {}
new_object = True
if new_object:
print(
"No identifier in new JSON object. Assuming POST or PUT with auto-accessioning.")
object_exists = False
if id_response:
object_exists = True
print("Found matching @id:")
encodedcc.pprint_ENCODE(id_response)
if uuid_response:
object_exists = True
print("Found matching uuid:")
encodedcc.pprint_ENCODE(uuid_response)
if accession_response:
object_exists = True
print("Found matching accession")
encodedcc.pprint_ENCODE(accession_response)
if id_response and uuid_response and (id_response != uuid_response):
print("Existing id/uuid mismatch")
if id_response and accession_response and (id_response != accession_response):
print("Existing id/accession mismatch")
if uuid_response and accession_response and (uuid_response != accession_response):
print("Existing uuid/accession mismatch")
if new_object and object_exists:
print("Conflict: At least one identifier already exists and at least one does not exist")
profiles = encodedcc.get_ENCODE("/profiles/", connection)
supported_collections = list(profiles.keys())
if "Dataset" not in supported_collections:
supported_collections.append("Dataset")
type_list = new_json.pop('@type', [])
if args.type:
type_list = [args.type]
if any(type_list):
findit = False
for x in supported_collections:
if x.lower() == type_list[0].lower():
type_list = [x]
findit = True
if findit:
if args.debug:
print("Object will have type of", type_list[0])
else:
print("Error! JSON object does not contain one of the supported types")
print("Provided type:", type_list[0])
print(
"Please either change the JSON file or define the type with the --type feature")
sys.exit(1)
else:
print("No type found for JSON object!")
sys.exit(1)
possible_collections = [x for x in type_list if x in supported_collections]
if possible_collections:
# collection = possible_collections[0] + 's/'
collection = possible_collections[0]
else:
collection = []
if '@id' in new_json:
identifier = new_json.pop('@id')
elif 'uuid' in new_json:
if collection:
identifier = '/' + collection + '/' + new_json['uuid'] + '/'
else:
identifier = '/' + new_json['uuid'] + '/'
elif 'accession' in new_json:
if collection:
identifier = '/' + collection + '/' + new_json['accession'] + '/'
else:
identifier = '/' + new_json['accession'] + '/'
if 'attachment' in new_json:
if 'href' in new_json['attachment']:
pass
else:
try:
filename = new_json['attachment']['download']
print("Setting filename to %s" % (filename))
except:
print(
"Must specify either href or filename for attachment", file=sys.stderr)
if new_json['attachment'].get('type'):
mime_type = new_json['attachment'].get('type')
else:
try:
mime_type, encoding = mimetypes.guess_type(filename)
major, minor = mime_type.split('/')
#detected_type = magic.from_file(filename, mime=True)
print("Detected mime type %s" % (mime_type))
except:
print("Failed to detect mime type in file %s" %
(filename), file=sys.stderr)
try:
with open(filename, 'rb') as stream:
print("opened")
newvalue = {
'download': filename, # Just echoes the given filename as the download name
'type': mime_type,
'href': 'data:%s;base64,%s' % (mime_type, b64encode(stream.read()))
}
f = open('tmp', 'w')
print(f, newvalue)
new_json.update({'attachment': newvalue}) # add
except:
print("Cannot open file %s" % (filename), file=sys.stderr)
if object_exists:
if args.force_put:
if not GET_ONLY:
print("Replacing existing object")
if args.update:
e = encodedcc.replace_ENCODE(
identifier, connection, new_json)
print(e)
else:
if not GET_ONLY:
print("PATCHing existing object")
if args.update:
e = encodedcc.patch_ENCODE(
identifier, connection, new_json)
print(e)
elif new_object:
if args.force_put:
if not GET_ONLY:
print("PUT'ing new object")
if args.update:
e = encodedcc.replace_ENCODE(
identifier, connection, new_json)
print(e)
else:
if not GET_ONLY:
print("POST'ing new object")
if not any(collection):
print(
"ERROR: Unable to POST to non-existing collection {}".format(collection))
sys.exit(1)
if args.update:
e = encodedcc.new_ENCODE(connection, collection, new_json)
print(e)
if __name__ == '__main__':
main()