Browse Source

Fix up urllib for Python 3.

Bentley James Oakes 7 years ago
parent
commit
b35da8b505
1 changed files with 35 additions and 10 deletions
  1. 35 10
      scripts/prompt.py

+ 35 - 10
scripts/prompt.py

@@ -1,11 +1,18 @@
-import urllib
-import urllib2
 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
+
 sys.path.append("../interface/HUTN")
 sys.path.append("interface/HUTN")
 from hutn_compiler.compiler import main as hutn_compile
@@ -13,7 +20,7 @@ from hutn_compiler.compiler import main as hutn_compile
 memory = {}
 
 def send_data(commands, address, taskname):
-    urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "data": json.dumps(commands), "taskname": taskname}))).read()
+    urlopen(Request(address, urlencode({"op": "set_input", "data": json.dumps(commands), "taskname": taskname}))).read()
 
 def local_print(string):
     if os.name == "posix":
@@ -39,7 +46,12 @@ except IndexError:
     taskname = str(random.random())
 
 # If task doesn't exist yet, we create it
-urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": '"%s"' % taskname, "taskname": "task_manager"}))).read()
+
+url = urlencode({"op": "set_input", "value": '"%s"' % taskname, "taskname": "task_manager"})
+urlopen(Request(address, url.encode())).read()
+
+
+
 
 local_print("Switching context to Modelverse: all data is piped.")
 local_print("Use command $ to switch to HUTN parsing mode.")
@@ -48,7 +60,7 @@ local_print("To quit: execute command 'quit'")
 
 def print_output():
     while 1:
-        output = urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "taskname": taskname}))).read()
+        output = urlopen(Request(address, urlencode({"op": "get_output", "taskname": taskname}))).read()
         remote_print("%s" % str(json.loads(output)))
 
 thrd = threading.Thread(target=print_output)
@@ -56,7 +68,11 @@ thrd.daemon = True
 thrd.start()
 
 while 1:
-    command = raw_input()
+    if sys.version_info[0] < 3:
+        command = raw_input()
+    else:
+        command = input()
+
     if command == "quit":
         local_print("Received quit: breaking connection to Modelverse immediately!")
         break
@@ -70,7 +86,10 @@ while 1:
         local_print("There is no nice editor right now, so please just modify the file '__action.alc' in this folder.")
         while 1:
             local_print("When you are done, press <return> to continue.")
-            raw_input()
+            if sys.version_info[0] < 3:
+                raw_input()
+            else:
+                input()
             local_print("File contents:")
             with open(tmp_file, 'r') as f:
                 local_print(f.read())
@@ -80,7 +99,10 @@ while 1:
                 commands = hutn_compile(tmp_file, "interface/HUTN/grammars/actionlanguage.g", "CS")
                 local_print("Compilation succesfully terminated!")
                 local_print("Upload this file? [Y/n]")
-                i = raw_input()
+                if sys.version_info[0] < 3:
+                    i = raw_input()
+                else:
+                    i = input()
                 if i == "n\n":
                     continue
                 send_data(commands, address)
@@ -89,7 +111,10 @@ while 1:
                 traceback.print_exc()
                 local_print("Error during compilation!")
                 local_print("Do you want to try again? [Y/n]")
-                i = raw_input()
+                if sys.version_info[0] < 3:
+                    i = raw_input()
+                else:
+                    i = input()
                 if i[0] == "n":
                     break
 
@@ -101,4 +126,4 @@ while 1:
         else:
             command = '"%s"' % command
 
-        urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": command, "taskname": taskname}))).read()
+        urlopen(Request(address, urlencode({"op": "set_input", "value": command, "taskname": taskname}))).read()