prompt.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. with open("__constraint.al", 'w') as f:
  82. f.write("Element function constraint(model : Element, element_name : String):\n")
  83. commands = hutn_compile(tmp_file, grammar_file, "CS")
  84. send_data(commands, address)
  85. else:
  86. # Just send a normal request
  87. if command.startswith("\\"):
  88. command = command
  89. else:
  90. command = '"%s"' % command
  91. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": command, "username": username}))).read()