-
Notifications
You must be signed in to change notification settings - Fork 10
/
protocol_response.py
71 lines (60 loc) · 2.24 KB
/
protocol_response.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
from __future__ import print_function
import json
import os
import requests
def protocol_response(method, protocol_path=None, response_path=None, **kwargs):
"""
Helper function for getting protocol response and dumping json to response path
Caveat Emptor: Does not do any additional checks on the response object, just dumps to json if possible
"""
from transcriptic import api
if not api:
from transcriptic.config import Connection
api = Connection.from_file("~/.transcriptic")
protocol = json.loads(open(protocol_path).read())
response = requests.post(
api.get_route(method),
headers=api.session.headers,
data=json.dumps({"protocol": protocol}),
)
with open(response_path, "w") as out_file:
json.dump(response.json(), out_file, indent=2)
if __name__ == "__main__":
# Using optparse, to support Python 2
from optparse import OptionParser
usage = "usage: %prog [options] method -i 'myProtocol.json'"
parser = OptionParser(usage=usage)
parser.add_option(
"-i",
"--input",
action="store",
type="str",
dest="protocol_path",
help="Path to protocol.json (required)",
default=None,
)
parser.add_option(
"-o",
"--output",
action="store",
type="str",
dest="response_path",
help="output path for response json. If not specified, goes to protocol_response.json",
default=None,
)
(options, args) = parser.parse_args()
if len(args) != 1:
raise RuntimeError("Please provide a method argument. Example: analyze_run")
if not options.protocol_path:
raise RuntimeError("The input path to the protocol is required")
if not os.path.isfile(options.protocol_path):
raise RuntimeError(f"{options.protocol_path} is an invalid protocol path")
if not options.response_path:
options.response_path = (
options.protocol_path.split(".json")[0] + "_response.json"
)
try:
protocol_response(args[0], options.protocol_path, options.response_path)
print(f"File succesfully generated: {options.response_path}")
except Exception as e:
print(f"Ran into {e} when generating file.")