prompt.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import urllib
  2. import urllib2
  3. import threading
  4. import subprocess
  5. import os
  6. import sys
  7. sys.path.append("../interface/HUTN")
  8. sys.path.append("interface/HUTN")
  9. from hutn_compiler.compiler import main as hutn_compile
  10. memory = {}
  11. def send_data(commands, address, username):
  12. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "data": json.dumps(commands), "username": username}))).read()
  13. def local_print(string):
  14. if os.name == "posix":
  15. # Nicer colour output when using posix (and thus supporting colour)
  16. string = "\033[92m%s\033[0m" % string
  17. print(string)
  18. def remote_print(string):
  19. if os.name == "posix":
  20. # Nicer colour output when using posix (and thus supporting colour)
  21. string = "\033[94m%s\033[0m" % string
  22. print(string)
  23. local_print("Welcome to the local shell!")
  24. local_print("Please specify Modelverse location (default: 127.0.0.1:8001)")
  25. location = raw_input()
  26. if location == "":
  27. address = "http://127.0.0.1:8001/"
  28. local_print("Username (default: test)")
  29. username = raw_input()
  30. if username == "":
  31. username = "test"
  32. else:
  33. username = username.strip()
  34. # If user doesn't exist yet, we create it
  35. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % username, "username": "user_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", "username": username}))).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, "username": username}))).read()