prompt.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import threading
  2. import subprocess
  3. import os
  4. import sys
  5. import json
  6. if sys.version_info[0] < 3:
  7. from urllib2 import urlopen as urlopen
  8. from urllib2 import Request as Request
  9. from urllib import urlencode as urlencode
  10. else:
  11. from urllib.request import urlopen as urlopen
  12. from urllib.request import Request as Request
  13. from urllib.parse import urlencode as urlencode
  14. sys.path.append("../interface/HUTN")
  15. sys.path.append("interface/HUTN")
  16. from hutn_compiler.compiler import main as hutn_compile
  17. memory = {}
  18. def send_data(commands, address, taskname):
  19. urlopen(Request(address, urlencode({"op": "set_input", "data": json.dumps(commands), "taskname": taskname}))).read()
  20. def local_print(string):
  21. if os.name == "posix":
  22. # Nicer colour output when using posix (and thus supporting colour)
  23. string = "\033[92m%s\033[0m" % string
  24. print(string)
  25. def remote_print(string):
  26. if os.name == "posix":
  27. # Nicer colour output when using posix (and thus supporting colour)
  28. string = "\033[94m%s\033[0m" % string
  29. print(string)
  30. local_print("Welcome to the local shell!")
  31. try:
  32. address = sys.argv[1]
  33. except IndexError:
  34. address = "http://127.0.0.1:8001"
  35. try:
  36. taskname = sys.argv[2]
  37. except IndexError:
  38. data = urlencode({"op": "get_output", "taskname": "task_manager"})
  39. taskname = json.loads(urlopen(Request(address, data.encode())).read())
  40. local_print("Switching context to Modelverse: all data is piped.")
  41. local_print("Use command $ to switch to HUTN parsing mode.")
  42. local_print("Use \\ before the value to use a primitive value different from a string, which is already JSON serialized.")
  43. local_print("To quit: execute command 'quit'")
  44. def print_output():
  45. while 1:
  46. data = urlencode({"op": "get_output", "taskname": taskname})
  47. output = urlopen(Request(address, data.encode())).read()
  48. remote_print("%s" % str(json.loads(output)))
  49. thrd = threading.Thread(target=print_output)
  50. thrd.daemon = True
  51. thrd.start()
  52. while 1:
  53. if sys.version_info[0] < 3:
  54. command = raw_input()
  55. else:
  56. command = input()
  57. if command == "quit":
  58. local_print("Received quit: breaking connection to Modelverse immediately!")
  59. break
  60. if command.startswith("$"):
  61. # Invoke the HUTN parser
  62. tmp_file = "__action.alc"
  63. with open(tmp_file, 'w') as f:
  64. f.write("")
  65. local_print("Entering HUTN coding environment.")
  66. local_print("There is no nice editor right now, so please just modify the file '__action.alc' in this folder.")
  67. while 1:
  68. local_print("When you are done, press <return> to continue.")
  69. if sys.version_info[0] < 3:
  70. raw_input()
  71. else:
  72. input()
  73. local_print("File contents:")
  74. with open(tmp_file, 'r') as f:
  75. local_print(f.read())
  76. local_print("Sending file through parser!")
  77. try:
  78. commands = hutn_compile(tmp_file, "interface/HUTN/grammars/actionlanguage.g", "CS")
  79. local_print("Compilation succesfully terminated!")
  80. local_print("Upload this file? [Y/n]")
  81. if sys.version_info[0] < 3:
  82. i = raw_input()
  83. else:
  84. i = input()
  85. if i == "n\n":
  86. continue
  87. send_data(commands, address)
  88. except:
  89. import traceback
  90. traceback.print_exc()
  91. local_print("Error during compilation!")
  92. local_print("Do you want to try again? [Y/n]")
  93. if sys.version_info[0] < 3:
  94. i = raw_input()
  95. else:
  96. i = input()
  97. if i[0] == "n":
  98. break
  99. local_print("HUTN input finished, going back to regular prompt!")
  100. else:
  101. # Just send a normal request
  102. if command.startswith("\\"):
  103. command = command[1:]
  104. else:
  105. command = '"%s"' % command
  106. data = urlencode({"op": "set_input", "value": command, "taskname": taskname})
  107. urlopen(Request(address, data.encode())).read()