prompt.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. import random
  39. taskname = str(random.random())
  40. # If task doesn't exist yet, we create it
  41. data = urlencode({"op": "set_input", "value": '"%s"' % taskname, "taskname": "task_manager"})
  42. urlopen(Request(address, data.encode())).read()
  43. local_print("Switching context to Modelverse: all data is piped.")
  44. local_print("Use command $ to switch to HUTN parsing mode.")
  45. local_print("Use \\ before the value to use a primitive value different from a string, which is already JSON serialized.")
  46. local_print("To quit: execute command 'quit'")
  47. def print_output():
  48. while 1:
  49. data = urlencode({"op": "get_output", "taskname": taskname})
  50. output = urlopen(Request(address, data.encode())).read()
  51. remote_print("%s" % str(json.loads(output)))
  52. thrd = threading.Thread(target=print_output)
  53. thrd.daemon = True
  54. thrd.start()
  55. while 1:
  56. if sys.version_info[0] < 3:
  57. command = raw_input()
  58. else:
  59. command = input()
  60. if command == "quit":
  61. local_print("Received quit: breaking connection to Modelverse immediately!")
  62. break
  63. if command.startswith("$"):
  64. # Invoke the HUTN parser
  65. tmp_file = "__action.alc"
  66. with open(tmp_file, 'w') as f:
  67. f.write("")
  68. local_print("Entering HUTN coding environment.")
  69. local_print("There is no nice editor right now, so please just modify the file '__action.alc' in this folder.")
  70. while 1:
  71. local_print("When you are done, press <return> to continue.")
  72. if sys.version_info[0] < 3:
  73. raw_input()
  74. else:
  75. input()
  76. local_print("File contents:")
  77. with open(tmp_file, 'r') as f:
  78. local_print(f.read())
  79. local_print("Sending file through parser!")
  80. try:
  81. commands = hutn_compile(tmp_file, "interface/HUTN/grammars/actionlanguage.g", "CS")
  82. local_print("Compilation succesfully terminated!")
  83. local_print("Upload this file? [Y/n]")
  84. if sys.version_info[0] < 3:
  85. i = raw_input()
  86. else:
  87. i = input()
  88. if i == "n\n":
  89. continue
  90. send_data(commands, address)
  91. except:
  92. import traceback
  93. traceback.print_exc()
  94. local_print("Error during compilation!")
  95. local_print("Do you want to try again? [Y/n]")
  96. if sys.version_info[0] < 3:
  97. i = raw_input()
  98. else:
  99. i = input()
  100. if i[0] == "n":
  101. break
  102. local_print("HUTN input finished, going back to regular prompt!")
  103. else:
  104. # Just send a normal request
  105. if command.startswith("\\"):
  106. command = command[1:]
  107. else:
  108. command = '"%s"' % command
  109. data = urlencode({"op": "set_input", "value": command, "taskname": taskname})
  110. urlopen(Request(address, data.encode())).read()