prompt.py 4.2 KB

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