|
| 1 | +import requests |
| 2 | +from datetime import datetime |
| 3 | +import re |
| 4 | + |
| 5 | +# To be changed once deployed on actural server |
| 6 | +pddl_url = "http://127.0.0.1:8000/upload/(?P<filename>[^/]+)$" |
| 7 | +vfg_url = "http://127.0.0.1:8000/downloadVisualisation" |
| 8 | + |
| 9 | +# check if the input file is in the correct format using regex |
| 10 | +def check_input(filename, format): |
| 11 | + x = re.search("^.+\."+format+"$", filename) |
| 12 | + return bool(x) |
| 13 | + |
| 14 | + |
| 15 | +# helper function to read file |
| 16 | +def read_file(filename): |
| 17 | + file = open(filename, "rb") |
| 18 | + content = file.read() |
| 19 | + file.close() |
| 20 | + return content |
| 21 | + |
| 22 | + |
| 23 | +# send 3 PDDL files and get back either vfg, png, gif, webm or mp4 file |
| 24 | +def pddl_visualise(domain_file, problem_file, animation_profile, output_format): |
| 25 | + if check_input(domain_file, "pddl") is False or check_input(problem_file, "pddl") is False \ |
| 26 | + or check_input(animation_profile, "pddl") is False: |
| 27 | + print("Error: incorrect input format; all input files should be in PDDL format") |
| 28 | + return None |
| 29 | + if output_format != "png" and output_format != "webm" and output_format != "gif" and output_format != "mp4" \ |
| 30 | + and output_format != "vfg": |
| 31 | + print("Error: desired output format is not supported; only vfg, png, gif, webm and mp4 are supported") |
| 32 | + return None |
| 33 | + domain = read_file(domain_file) |
| 34 | + problem = read_file(problem_file) |
| 35 | + animation = read_file(animation_profile) |
| 36 | + files = ("domain", (None, domain)), ("problem", (None, problem)), ("animation", (None, animation)), \ |
| 37 | + ("fileType", (None, output_format)) |
| 38 | + r = requests.post(pddl_url, files=files) |
| 39 | + if output_format == "png": |
| 40 | + output_format = "zip" |
| 41 | + output_name = "planimation " + datetime.now().strftime("%Y-%m-%d_%X.") + output_format |
| 42 | + with open(output_name, "wb") as output: |
| 43 | + for chunk in r.iter_content(chunk_size=None): |
| 44 | + output.write(chunk) |
| 45 | + return output_name |
| 46 | + |
| 47 | + |
| 48 | +# send a vfg file and get back either png, gif, webm or mp4 file |
| 49 | +def vfg_visualise(vfg_file, output_format): |
| 50 | + if check_input(vfg_file, "vfg") is False: |
| 51 | + print("Error: incorrect input format; input should be in vfg format") |
| 52 | + return None |
| 53 | + if output_format != "png" and output_format != "webm" and output_format != "gif" and output_format != "mp4": |
| 54 | + print("Error: desired output format is not supported; only png, gif, webm and mp4 are supported") |
| 55 | + return None |
| 56 | + vfg = read_file(vfg_file) |
| 57 | + files = ('vfg', (None, vfg)), ('fileType', (None, output_format)) |
| 58 | + r = requests.post(vfg_url, files=files) |
| 59 | + if output_format == "png": |
| 60 | + output_format = "zip" |
| 61 | + output_name = "planimation " + datetime.now().strftime("%Y-%m-%d_%X.") + output_format |
| 62 | + with open(output_name, "wb") as output: |
| 63 | + for chunk in r.iter_content(chunk_size=None): |
| 64 | + output.write(chunk) |
| 65 | + return output_name |
0 commit comments