

import threading
import subprocess
import os
import sys
import json

if sys.version_info[0] < 3:
    from urllib2 import urlopen as urlopen
    from urllib2 import Request as Request
    from urllib import urlencode as urlencode
else:
    from urllib.request import urlopen as urlopen
    from urllib.request import Request as Request
    from urllib.parse import urlencode as urlencode

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 debugging shell!")
local_print("Please specify Modelverse location (default: 127.0.0.1:8001)")

if sys.version_info[0] < 3:
    location = raw_input()
else:
    location = input()

if location == "":
    address = "http://127.0.0.1:8001/"
    
local_print("Please specify task name (default: test)")

if sys.version_info[0] < 3:
    taskname = raw_input()
else:
    taskname = input()

if location == "":
    taskname = "test"

local_print("Switching context to Modelverse: all data is piped.")
local_print("Available commands: 'pause', 'resume'")
local_print("To quit: execute command 'quit'")

while 1:
    if sys.version_info[0] < 3:
        inp = raw_input().split(" ")
    else:
        inp = input().split(" ")

    action = inp[0]
    if action == "quit":
        local_print("Received quit: breaking connection to Modelverse immediately!")
        break

    data = urlencode({"op": action, "taskname": taskname})
    print(json.loads(urlopen(Request(address, data)).read()))
