-
Notifications
You must be signed in to change notification settings - Fork 2
/
planimation_api.py
74 lines (66 loc) · 3.03 KB
/
planimation_api.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
import requests
from datetime import datetime
import re
import json
# To be changed once deployed on actural server
pddl_url = "http://127.0.0.1:8000/upload/(?P<filename>[^/]+)$"
vfg_url = "http://127.0.0.1:8000/downloadVisualisation"
# check if the input file is in the correct format using regex
def check_input(filename, format):
x = re.search("^.+\."+format+"$", filename)
return bool(x)
# helper function to read file
def read_file(filename):
file = open(filename, "rb")
content = file.read()
file.close()
return content
# send 3 PDDL files and get back either vfg, png, gif, webm or mp4 file
def pddl_visualise(domain_file, problem_file, animation_profile, output_format):
if check_input(domain_file, "pddl") is False or check_input(problem_file, "pddl") is False \
or check_input(animation_profile, "pddl") is False:
print("Error: incorrect input format; all input files should be in PDDL format")
return None
if output_format != "png" and output_format != "webm" and output_format != "gif" and output_format != "mp4" \
and output_format != "vfg":
print("Error: desired output format is not supported; only vfg, png, gif, webm and mp4 are supported")
return None
domain = read_file(domain_file)
problem = read_file(problem_file)
animation = read_file(animation_profile)
files = ("domain", (None, domain)), ("problem", (None, problem)), ("animation", (None, animation)), \
("fileType", (None, output_format))
print("Waiting for response...")
r = requests.post(pddl_url, files=files)
if "message" in r.text:
message = json.loads(r.text)
return message['message']
if output_format == "png":
output_format = "zip"
output_name = "planimation " + datetime.now().strftime("%Y-%m-%d_%X.") + output_format
with open(output_name, "wb") as output:
for chunk in r.iter_content(chunk_size=None):
output.write(chunk)
return output_name
# send a vfg file and get back either png, gif, webm or mp4 file
def vfg_visualise(vfg_file, output_format):
if check_input(vfg_file, "vfg") is False:
print("Error: incorrect input format; input should be in vfg format")
return None
if output_format != "png" and output_format != "webm" and output_format != "gif" and output_format != "mp4":
print("Error: desired output format is not supported; only png, gif, webm and mp4 are supported")
return None
vfg = read_file(vfg_file)
files = ('vfg', (None, vfg)), ('fileType', (None, output_format))
print("Waiting for response...")
r = requests.post(vfg_url, files=files)
if "message" in r.text:
message = json.loads(r.text)
return message['message']
if output_format == "png":
output_format = "zip"
output_name = "planimation " + datetime.now().strftime("%Y-%m-%d_%X.") + output_format
with open(output_name, "wb") as output:
for chunk in r.iter_content(chunk_size=None):
output.write(chunk)
return output_name