prompt.py 3.6 KB

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