18from __future__
import print_function
25from requests.packages.urllib3.exceptions
import InsecureRequestWarning
26requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
30base_url =
"http://35.153.64.44:8081"
35vehicles_url =
"/rest/vehicles"
36experiments_url =
"/rest/experiments"
37algorithms_url =
"/rest/algorithms"
42 print(resp.status_code)
46 print(
"Unable to decode the rest of the response.")
49 print(cmd +
" is not a recognized command. Try \"help\" for help.")
52 exec(
"global " + parts[1] +
";" + parts[1] +
" = " + parts[2])
58 full_url = urlparse.urljoin(full_url, parts[1])
60 r = requests.get(full_url, verify=
False)
66 full_url = urlparse.urljoin(full_url, parts[1])
68 ack = raw_input(
"Are you sure you want to delete " + full_url +
"?\n(y/N): ")
69 if ack.lower() ==
"y":
70 r = requests.delete(full_url, verify=
False)
75 rel_url = urlparse.urljoin(rel_url, new_url)
87 data = eval(
" ".join(parts[data_start:]))
89 headers = {
"Content-Type" :
"application/json" }
91 r = requests.post(full_url, data=json.dumps(data), headers=headers, verify=
False)
95 uniqVehId = raw_input(
"Unique Vehicle ID: ")
96 description = raw_input(
"Vehicle Description: ")
97 full_url = urlparse.urljoin(base_url, vehicles_url)
98 headers = {
"Content-Type" :
"application/json" }
99 r = requests.post(full_url, data=json.dumps({
"uniqVehId": uniqVehId,
"description": description}), headers=headers, verify=
False)
103 description = raw_input(
"Experiment Description: ")
104 location = raw_input(
"Experiment Location: ")
105 full_url = urlparse.urljoin(base_url, experiments_url)
106 headers = {
"Content-Type" :
"application/json" }
107 r = requests.post(full_url, data=json.dumps({
"description": description,
"location": location}), headers=headers, verify=
False)
112 r = requests.get(urlparse.urljoin(base_url,
"/rest/"), verify=
False)
113 algos = r.json()[
"availableAlgorithms"]
114 print(
"Select Algorithm: ")
115 for a
in zip(algos, range(len(algos))):
116 print(
"{}. {}".format(a[1] + 1, a[0]))
118 idx = input(
"Choice #? ") - 1
120 full_url = urlparse.urljoin(base_url, algorithms_url)
121 headers = {
"Content-Type" :
"application/json" }
122 data = json.dumps({
"className": algos[idx]})
124 r = requests.post(full_url, data=data, headers=headers, verify=
False)
128 if parts[1] ==
"vehicle":
130 elif parts[1] ==
"experiment":
132 elif parts[1] ==
"algorithm":
135 print(
"Unknown entity to create: " + parts[1])
138 r = requests.get(urlparse.urljoin(base_url, vehicles_url), verify=
False).json()
140 print(
"Select Vehicle:")
142 print(
"{}. Description: {}; Unique ID: {}".format(veh[
"id"], veh[
"description"], veh[
"uniqVehId"]))
144 return raw_input(
"Vehicle ID#? ")
149 r = requests.get(urlparse.urljoin(base_url, experiments_url), verify=
False).json()
151 print(
"Select Experiment: ")
153 print(
"{}. Description: {}; Location: {}".format(exp[
"id"], exp[
"description"], exp[
"location"]))
154 exp_id = raw_input(
"Experiment ID#? ")
156 headers = {
"Content-Type" :
"application/json" }
157 data = json.dumps({
"id": int(veh_id)})
159 r = requests.post(urlparse.urljoin(base_url, experiments_url +
"/" + exp_id +
"/vehicles"), data=data, headers=headers, verify=
False)
165 r = requests.get(urlparse.urljoin(base_url, algorithms_url), verify=
False).json()
167 print(
"Select Algorithm: ")
169 print(
"{}. Class: {}".format(exp[
"id"], exp[
"className"]))
170 algo_id = raw_input(
"Algorithm ID#? ")
172 headers = {
"Content-Type" :
"application/json" }
173 data = json.dumps({
"id": int(veh_id)})
175 r = requests.post(urlparse.urljoin(base_url, algorithms_url +
"/" + algo_id +
"/vehicles"), data=data, headers=headers, verify=
False)
179 if parts[1] ==
"experiment":
181 elif parts[1] ==
"algorithm":
184 print(
"Unknown relationship to assign: " + parts[1])
188 r = requests.get(urlparse.urljoin(base_url, vehicles_url), verify=
False).json()
192 print(
"{}. Description: {}; Unique ID: {}".format(veh[
"id"], veh[
"description"], veh[
"uniqVehId"]))
195 r = requests.get(urlparse.urljoin(base_url, experiments_url), verify=
False).json()
197 print(
"Experiments:")
199 print(
"{}. Description: {}; Location: {}".format(veh[
"id"], veh[
"description"], veh[
"location"]))
202 r = requests.get(urlparse.urljoin(base_url, algorithms_url), verify=
False).json()
206 print(
"{}. ClassName: {}".format(veh[
"id"], veh[
"className"]))
209 if parts[1] ==
"vehicles":
211 elif parts[1] ==
"experiments":
213 elif parts[1] ==
"algorithms":
216 print(
"Unknown entity to list: " + parts[1])
219 if parts[1] ==
"vehicle":
220 full_url = urlparse.urljoin(base_url, vehicles_url)
221 elif parts[1] ==
"experiment":
222 full_url = urlparse.urljoin(base_url, experiments_url)
223 elif parts[1] ==
"algorithm":
224 full_url = urlparse.urljoin(base_url, algorithms_url)
226 print(
"Unknown entity to fetch: " + parts[1])
229 r = requests.get(full_url +
"/" + parts[2], verify=
False)
234 Parse the command input and execute the appropriate action
237 parts = cmd.split(' ')
239 args = {
"set" :
"variable value",
"get" :
"[url]",
"cd" :
"rel_url",
240 "post" :
"data",
"delete" :
"[url]",
"create" :
"(vehicle | experiment | algorithm)",
241 "assign" :
"(experiment | algorithm)",
"list" :
"(vehicle | experiment | algorithm)",
242 "fetch" :
"(vehicle | experiment | algorithm) id",
"help":
"",
"exit" :
""}
245 if parts[0] ==
"exit":
249 elif parts[0] ==
"set":
251 elif parts[0] ==
"get":
253 elif parts[0] ==
"cd":
255 elif parts[0] ==
"post":
257 elif parts[0] ==
"delete":
259 elif parts[0] ==
"create":
261 elif parts[0] ==
"assign":
263 elif parts[0] ==
"list":
265 elif parts[0] ==
"fetch":
267 elif parts[0] ==
"help":
269 for cmd, usage
in args.items():
270 print(
"{} {}".format(cmd, usage))
274 print(
"Incorrect usage of: " + parts[0])
275 print(
"Usage: " + parts[0] +
" " + args[parts[0]])
276 except requests.exceptions.ConnectionError:
277 print(
"Unable to connect to " + base_url +
".")
280 return urlparse.urljoin(base_url, rel_url)
283 print(
"Speed Harmonization Command Line Configurator " + version_id +
".")
287if __name__ ==
"__main__":
def process_create_vehicle()
def process_delete_command(parts)
def process_get_command(parts)
def process_assign_command(parts)
def process_list_vehicles()
def process_create_algorithm()
def process_create_experiment()
def process_fetch_command(parts)
def process_create_command(parts)
def process_set_command(parts)
def process_assign_algorithm()
def process_list_experiments()
def process_list_command(parts)
def process_list_algorithms()
def process_cd_command(parts)
def process_assign_experiment()
def process_post_command(parts)