Skip to content

Commit

Permalink
Added compatibility with Python 3; everything should still work on Py…
Browse files Browse the repository at this point in the history
…thon 2; partial -- enough to run OpenRTiST
  • Loading branch information
pspillai committed Oct 10, 2019
1 parent 70c9ee8 commit 68a4aeb
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 51 deletions.
6 changes: 5 additions & 1 deletion server/bin/gabriel-control
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@



#from __future__ import absolute_import
import json
from optparse import OptionParser
import os
import Queue
try:
import Queue
except ImportError:
import queue as Queue
import re
import sys
import threading
Expand Down
5 changes: 4 additions & 1 deletion server/bin/gabriel-ucomm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import json
from optparse import OptionParser
import pprint
import Queue
try:
import Queue
except ImportError:
import queue as Queue
import os
import socket
import sys
Expand Down
22 changes: 16 additions & 6 deletions server/gabriel/common/network/HTTP.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,20 @@
# limitations under the License.
#

import httplib
import json
import urllib2
import urlparse
try:
import urllib2
import httplib
import urlparse
def mystr(b):
return str(b)
except ImportError:
import urllib.request as urllib2
import http.client as httplib
import urllib.parse as urlparse
def mystr(b):
return str(b, 'ascii')



class HttpConnectionError(Exception):
Expand All @@ -32,7 +42,7 @@ def http_get(url, rtn_format = "json"):
conn = urllib2.urlopen("%s" % (url))
rtn_data = conn.read()
if rtn_format == "json":
return json.loads(rtn_data)
return json.loads(mystr(rtn_data))
elif rtn_format == "raw":
return rtn_data

Expand All @@ -48,7 +58,7 @@ def http_post(url, json_string, rtn_format = "json"):
rtn_data = response.read()
conn.close()
if rtn_format == "json":
return json.loads(rtn_data)
return json.loads(mystr(rtn_data))
elif rtn_format == "raw":
return rtn_data

Expand All @@ -64,6 +74,6 @@ def http_put(url, json_string, rtn_format = "json"):
rtn_data = response.read()
conn.close()
if rtn_format == "json":
return json.loads(rtn_data)
return json.loads(mystr(rtn_data))
elif rtn_format == "raw":
return rtn_data
24 changes: 19 additions & 5 deletions server/gabriel/common/network/REST_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,29 @@
import sys
from flask import Flask
from flask import request
from flask.ext import restful
from flask.ext.restful import abort
from flask.ext.restful import reqparse
from flask.ext.restful import Resource
try:
from flask.ext import restful
from flask.ext.restful import abort
from flask.ext.restful import reqparse
from flask.ext.restful import Resource
except ImportError:
import flask_restful as restful
from flask_restful import abort, reqparse, Resource

try:
str(b'0x1','ascii')
def mystr(b):
return str(b, 'ascii')
except:
def mystr(b):
return str(b)


dir_file = os.path.dirname(os.path.realpath(__file__))

sys.path.insert(0, os.path.join(dir_file, "../../.."))
import gabriel
LOG = gabriel.logging.getLogger(__name__)


KEY_SERVICE_NAME = "service_name"
Expand Down Expand Up @@ -183,7 +197,7 @@ def put(self):
'''
Registers new ucomm address
'''
data = dict(json.loads(request.data))
data = dict(json.loads(mystr(request.data)))
ucomm_ip = data.get(gabriel.ServiceMeta.UCOMM_SERVER_IP, None)
ucomm_port = data.get(gabriel.ServiceMeta.UCOMM_SERVER_PORT, None)
# check if address exists
Expand Down
4 changes: 2 additions & 2 deletions server/gabriel/common/network/REST_server_binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(self):
threading.Thread.__init__(self, target = self.run_exec)

def run_exec(self):
cmd = ["python", "%s" % (self.REST_bin), '-n', self.net_interface, '-s', self.ip_addr]
cmd = ["python", "%s" % (self.REST_bin), '-n', self.net_interface, '-s', "%s" % (self.ip_addr)]
#_PIPE = subprocess.PIPE
_PIPE = None
self.proc = subprocess.Popen(cmd, close_fds = True,
Expand Down Expand Up @@ -97,5 +97,5 @@ def terminate(self):
REST_server = RESTServer()
REST_server.start()
except RESTServerError as e:
print str(e)
print(str(e))
REST_server = None
11 changes: 9 additions & 2 deletions server/gabriel/common/network/TCP.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@
import multiprocessing
import select
import socket
import SocketServer
try:
import SocketServer
def emptydata():
return ''
except ImportError:
import socketserver as SocketServer
def emptydata():
return b''
import threading
import traceback

Expand Down Expand Up @@ -50,7 +57,7 @@ def _recv_all(self, recv_size):
'''
Received data till a specified size.
'''
data = ''
data = emptydata()
while len(data) < recv_size:
tmp_data = self.request.recv(recv_size - len(data))
if tmp_data is None:
Expand Down
12 changes: 10 additions & 2 deletions server/gabriel/common/network/UPnP_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@
import subprocess
import sys
import threading
import urllib2
#import urllib2

try:
str(b'0x1','ascii')
def mystr(b):
return str(b, 'ascii')
except:
def mystr(b):
return str(b)

import gabriel
LOG = gabriel.logging.getLogger(__name__)
Expand Down Expand Up @@ -62,7 +70,7 @@ def run_exec(self):
continue

if self.proc.returncode == 0:
out = self.proc.stdout.read()
out = mystr(self.proc.stdout.read())
out_list = out.strip().split("\n")
for outline in out_list:
if len(outline.strip()) == 0:
Expand Down
2 changes: 1 addition & 1 deletion server/gabriel/common/network/UPnP_server_binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ def terminate(self):
UPnP_server.terminate()
sys.exit(1)
except UPnPServerError as e:
print str(e)
print(str(e))
UPnP_server = None
10 changes: 9 additions & 1 deletion server/gabriel/common/network/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,22 @@
import struct

import gabriel
try:
bytes("qw",'ascii')
def bts(s):
return bytes(s, 'ascii')
except:
def bts(s):
return bytes(s)



def get_ip(iface = 'eth0'):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockfd = sock.fileno()
SIOCGIFADDR = 0x8915

ifreq = struct.pack('16sH14s', iface, socket.AF_INET, '\x00' * 14)
ifreq = struct.pack('16sH14s', bts(iface), socket.AF_INET, bts('\x00' * 14))
try:
res = fcntl.ioctl(sockfd, SIOCGIFADDR, ifreq)
except:
Expand Down
36 changes: 24 additions & 12 deletions server/gabriel/control/debug_display_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,29 @@
# limitations under the License.
#

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import json
import multiprocessing
import os
import pprint
import Queue
import re
from SocketServer import ThreadingMixIn
try:
import Queue
from SocketServer import ThreadingMixIn
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
except ImportError:
import queue as Queue
from socketserver import ThreadingMixIn
from http.server import BaseHTTPRequestHandler, HTTPServer
import sys
import threading
import time
try:
bts("qw",'ascii')
def bts(s):
return bts(s, 'ascii')
except:
def bts(s):
return bts(s)

import gabriel
import gabriel.control
Expand All @@ -43,7 +55,7 @@ def do_POST(self):
def do_GET(self):
try:
self.path = self.path.split('?')[0]
print self.path
print(self.path)

if self.path.endswith(".mjpeg"):
if self.path.endswith("camera.mjpeg"):
Expand All @@ -53,20 +65,20 @@ def do_GET(self):
elif self.path.endswith("debug.mjpeg"):
data_queue = gabriel.control.output_display_queue_dict['debug']
self.send_response(200)
self.wfile.write("Content-Type: multipart/x-mixed-replace; boundary=--aaboundary")
self.wfile.write("\r\n\r\n")
self.wfile.write(bts("Content-Type: multipart/x-mixed-replace; boundary=--aaboundary"))
self.wfile.write(bts("\r\n\r\n"))
while 1:
if self.server.stopped:
break

try:
image_data = data_queue.get_nowait()

self.wfile.write("--aaboundary\r\n")
self.wfile.write("Content-Type: image/jpeg\r\n")
self.wfile.write("Content-length: " + str(len(image_data)) + "\r\n\r\n")
self.wfile.write(bts("--aaboundary\r\n"))
self.wfile.write(bts("Content-Type: image/jpeg\r\n"))
self.wfile.write(bts("Content-length: " + str(len(image_data)) + "\r\n\r\n"))
self.wfile.write(image_data)
self.wfile.write("\r\n\r\n\r\n")
self.wfile.write(bts("\r\n\r\n\r\n"))
time.sleep(0.001)

except Queue.Empty as e:
Expand Down Expand Up @@ -98,7 +110,7 @@ def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write("")
self.wfile.write(bts(""))

elif self.path.endswith("video"):
data_queue = gabriel.control.output_display_queue_dict['video']
Expand All @@ -113,7 +125,7 @@ def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write("")
self.wfile.write(bts(""))

else:
f = open(dir_file + os.sep + self.path)
Expand Down
26 changes: 19 additions & 7 deletions server/gabriel/control/mobile_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,22 @@
import json
import multiprocessing
import os
import Queue
import select
import socket
import SocketServer
try:
import Queue
import SocketServer
def mystr(b):
return str(b)
def bts(s):
return bytes(s)
except ImportError:
import queue as Queue
import socketserver as SocketServer
def mystr(b):
return str(b,'ascii')
def bts(s):
return bytes(s,'ascii')
import struct
import sys
import threading
Expand Down Expand Up @@ -226,7 +238,7 @@ def _handle_input_data(self):
import numpy as np
img_array = np.asarray(bytearray(image_data), dtype = np.int8)
cv_image = cv2.imdecode(img_array, -1)
print cv_image.shape
print(cv_image.shape)
if not self.log_video_writer_created:
self.log_video_writer_created = True
self.log_video_writer = cv2.VideoWriter(gabriel.Const.LOG_VIDEO_PATH, cv2.cv.CV_FOURCC('X','V','I','D'), 10, (cv_image.shape[1], cv_image.shape[0]))
Expand Down Expand Up @@ -417,7 +429,7 @@ def _remove_debug_header_fields(rtn_header_json):
def _handle_queue_data(self):
try:
(rtn_header, rtn_data) = self.data_queue.get(timeout = 0.0001)
rtn_header_json = json.loads(rtn_header)
rtn_header_json = json.loads(mystr(rtn_header))
## log measured time
if gabriel.Debug.TIME_MEASUREMENT:
frame_id = rtn_header_json[gabriel.Protocol_client.JSON_KEY_FRAME_ID]
Expand All @@ -444,7 +456,7 @@ def _handle_queue_data(self):

if gabriel.Debug.WEB_SERVER:
if gabriel.Const.LEGACY_JSON_ONLY_RESULT:
rtn_data_json = json.loads(rtn_data)
rtn_data_json = json.loads(mystr(rtn_data))
self._add_data_to_debug_server(rtn_header_json, rtn_data_json)
else:
raise NotImplementedError("Debug server only support legacy mode!")
Expand All @@ -460,10 +472,10 @@ def _handle_queue_data(self):
if gabriel.Const.LEGACY_JSON_ONLY_RESULT:
rtn_header_json[gabriel.Protocol_client.JSON_KEY_RESULT_MESSAGE]=rtn_data
rtn_header=json.dumps(rtn_header_json)
packet = struct.pack("!I{}s".format(len(rtn_header)), len(rtn_header), rtn_header)
packet = struct.pack("!I{}s".format(len(rtn_header)), len(rtn_header), bts(rtn_header))
LOG.info("message sent to the Glass: %s", gabriel.util.print_rtn(rtn_header_json))
else:
packet = struct.pack("!I{}s{}s".format(len(rtn_header),len(rtn_data)), len(rtn_header), rtn_header, rtn_data)
packet = struct.pack("!I{}s{}s".format(len(rtn_header),len(rtn_data)), len(rtn_header), bts(rtn_header), rtn_data)
LOG.info("message sent to the Glass: %s", gabriel.util.print_rtn(rtn_header_json))
self.request.send(packet)
self.wfile.flush()
Expand Down
Loading

0 comments on commit 68a4aeb

Please sign in to comment.