import urllib import urllib2 import threading import subprocess import os import sys sys.path.append("../interface/HUTN") sys.path.append("interface/HUTN") from hutn_compiler.compiler import main as hutn_compile memory = {} def send_data(commands, address, username): def flush_data(data): if data: urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": username}))).read() return [] var_list = {} data = [] for p in v: if isinstance(p, int): if p not in var_list: data = flush_data(data) val = urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "username": username}))).read() val = val.split("=", 2)[1].split("&", 1)[0] var_list[p] = val continue else: val = var_list[p] t = "R" else: val = p t = "V" data.append([t, val]) flush_data(data) def local_print(string): if os.name == "posix": # Nicer colour output when using posix (and thus supporting colour) string = "\033[92m%s\033[0m" % string print(string) def remote_print(string): if os.name == "posix": # Nicer colour output when using posix (and thus supporting colour) string = "\033[94m%s\033[0m" % string print(string) local_print("Welcome to the local shell!") local_print("Please specify Modelverse location (default: 127.0.0.1:8001)") location = raw_input() if location == "": address = "http://127.0.0.1:8001/" local_print("Username (default: test)") username = raw_input() if username == "": username = "test" else: username = username.strip() # If user shouldn't exist yet, we create it urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % username, "username": "user_manager"}))).read() local_print("Switching context to Modelverse: all data is piped.") local_print("Use command $ to switch to HUTN parsing mode.") local_print("Use \\ before the value to use a primitive value different from a string, which is already JSON serialized.") local_print("To quit: execute command 'quit'") def print_output(): while 1: output = urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "username": username}))).read() l, r = output.split("&", 1) if "value" in l: output = l else: output = r _, output = output.split("=", 1) remote_print("%s" % str(output)) thrd = threading.Thread(target=print_output) thrd.daemon = True thrd.start() while 1: command = raw_input() if command == "quit": local_print("Received quit: breaking connection to Modelverse immediately!") break if command.startswith("$"): # Invoke the HUTN parser with open("__constraint.al", 'w') as f: f.write("Element function constraint(model : Element, element_name : String):\n") commands = hutn_compile(tmp_file, grammar_file, "CS") send_data(commands, address) else: # Just send a normal request if command.startswith("\\"): command = command else: command = '"%s"' % command urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": command, "username": username}))).read()