prompt.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. def flush_data(data):
  13. if data:
  14. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": username}))).read()
  15. return []
  16. var_list = {}
  17. data = []
  18. for p in v:
  19. if isinstance(p, int):
  20. if p not in var_list:
  21. data = flush_data(data)
  22. val = urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "username": username}))).read()
  23. val = val.split("=", 2)[1].split("&", 1)[0]
  24. var_list[p] = val
  25. continue
  26. else:
  27. val = var_list[p]
  28. t = "R"
  29. else:
  30. val = p
  31. t = "V"
  32. data.append([t, val])
  33. flush_data(data)
  34. def local_print(string):
  35. if os.name == "posix":
  36. # Nicer colour output when using posix (and thus supporting colour)
  37. string = "\033[92m%s\033[0m" % string
  38. print(string)
  39. def remote_print(string):
  40. if os.name == "posix":
  41. # Nicer colour output when using posix (and thus supporting colour)
  42. string = "\033[94m%s\033[0m" % string
  43. print(string)
  44. local_print("Welcome to the local shell!")
  45. local_print("Please specify Modelverse location (default: 127.0.0.1:8001)")
  46. location = raw_input()
  47. if location == "":
  48. address = "http://127.0.0.1:8001/"
  49. local_print("Username (default: test)")
  50. username = raw_input()
  51. if username == "":
  52. username = "test"
  53. else:
  54. username = username.strip()
  55. # If user shouldn't exist yet, we create it
  56. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % username, "username": "user_manager"}))).read()
  57. local_print("Switching context to Modelverse: all data is piped.")
  58. local_print("Use command $ to switch to HUTN parsing mode.")
  59. local_print("Use \\ before the value to use a primitive value different from a string, which is already JSON serialized.")
  60. local_print("To quit: execute command 'quit'")
  61. def print_output():
  62. while 1:
  63. output = urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "username": username}))).read()
  64. l, r = output.split("&", 1)
  65. if "value" in l:
  66. output = l
  67. else:
  68. output = r
  69. _, output = output.split("=", 1)
  70. remote_print("%s" % str(output))
  71. thrd = threading.Thread(target=print_output)
  72. thrd.daemon = True
  73. thrd.start()
  74. while 1:
  75. command = raw_input()
  76. if command == "quit":
  77. local_print("Received quit: breaking connection to Modelverse immediately!")
  78. break
  79. if command.startswith("$"):
  80. # Invoke the HUTN parser
  81. tmp_file = "__action.alc"
  82. with open(tmp_file, 'w') as f:
  83. f.write("")
  84. local_print("Entering HUTN coding environment.")
  85. local_print("There is no nice editor right now, so please just modify the file '__action.alc' in this folder.")
  86. while 1:
  87. local_print("When you are done, press <return> to continue.")
  88. raw_input()
  89. local_print("File contents:")
  90. with open(tmp_file, 'r') as f:
  91. local_print(f.read())
  92. local_print("Sending file through parser!")
  93. try:
  94. commands = hutn_compile(tmp_file, "interface/HUTN/grammars/actionlanguage.g", "CS")
  95. local_print("Compilation succesfully terminated!")
  96. local_print("Upload this file? [Y/n]")
  97. i = raw_input()
  98. if i == "n\n":
  99. continue
  100. send_data(commands, address)
  101. except:
  102. import traceback
  103. traceback.print_exc()
  104. local_print("Error during compilation!")
  105. local_print("Do you want to try again? [Y/n]")
  106. i = raw_input()
  107. if i[0] == "n":
  108. break
  109. local_print("HUTN input finished, going back to regular prompt!")
  110. else:
  111. # Just send a normal request
  112. if command.startswith("\\"):
  113. command = command[1:]
  114. else:
  115. command = '"%s"' % command
  116. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": command, "username": username}))).read()