prompt.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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, username):
  13. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "data": json.dumps(commands), "username": username}))).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. local_print("Please specify Modelverse location (default: 127.0.0.1:8001)")
  26. location = raw_input()
  27. if location == "":
  28. address = "http://127.0.0.1:8001/"
  29. local_print("Username (default: test)")
  30. username = raw_input()
  31. if username == "":
  32. username = "test"
  33. else:
  34. username = username.strip()
  35. # If user doesn't exist yet, we create it
  36. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % username, "username": "user_manager"}))).read()
  37. local_print("Switching context to Modelverse: all data is piped.")
  38. local_print("Use command $ to switch to HUTN parsing mode.")
  39. local_print("Use \\ before the value to use a primitive value different from a string, which is already JSON serialized.")
  40. local_print("To quit: execute command 'quit'")
  41. def print_output():
  42. while 1:
  43. output = urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "username": username}))).read()
  44. remote_print("%s" % str(json.loads(output)))
  45. thrd = threading.Thread(target=print_output)
  46. thrd.daemon = True
  47. thrd.start()
  48. while 1:
  49. command = raw_input()
  50. if command == "quit":
  51. local_print("Received quit: breaking connection to Modelverse immediately!")
  52. break
  53. if command.startswith("$"):
  54. # Invoke the HUTN parser
  55. tmp_file = "__action.alc"
  56. with open(tmp_file, 'w') as f:
  57. f.write("")
  58. local_print("Entering HUTN coding environment.")
  59. local_print("There is no nice editor right now, so please just modify the file '__action.alc' in this folder.")
  60. while 1:
  61. local_print("When you are done, press <return> to continue.")
  62. raw_input()
  63. local_print("File contents:")
  64. with open(tmp_file, 'r') as f:
  65. local_print(f.read())
  66. local_print("Sending file through parser!")
  67. try:
  68. commands = hutn_compile(tmp_file, "interface/HUTN/grammars/actionlanguage.g", "CS")
  69. local_print("Compilation succesfully terminated!")
  70. local_print("Upload this file? [Y/n]")
  71. i = raw_input()
  72. if i == "n\n":
  73. continue
  74. send_data(commands, address)
  75. except:
  76. import traceback
  77. traceback.print_exc()
  78. local_print("Error during compilation!")
  79. local_print("Do you want to try again? [Y/n]")
  80. i = raw_input()
  81. if i[0] == "n":
  82. break
  83. local_print("HUTN input finished, going back to regular prompt!")
  84. else:
  85. # Just send a normal request
  86. if command.startswith("\\"):
  87. command = command[1:]
  88. else:
  89. command = '"%s"' % command
  90. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": command, "username": username}))).read()