improve Py2/3 compat
Change-Id: I1dfdcb88a492401851d5157c734cd708496bf004 Reviewed-on: https://forge.frm2.tum.de/review/17734 Tested-by: JenkinsCodeReview <bjoern_pedersen@frm2.tum.de> Reviewed-by: Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
This commit is contained in:
@ -26,6 +26,19 @@
|
||||
from __future__ import print_function
|
||||
|
||||
import code
|
||||
from os import path
|
||||
import socket
|
||||
import threading
|
||||
from collections import deque
|
||||
|
||||
try:
|
||||
import configparser
|
||||
except ImportError:
|
||||
import ConfigParser as configparser
|
||||
|
||||
import mlzlog
|
||||
from secop.protocol.interface.tcp import decode_msg, get_msg, encode_msg_frame
|
||||
from secop.protocol.messages import EVENTREPLY, DESCRIPTIONREQUEST, Message
|
||||
|
||||
|
||||
class NameSpace(dict):
|
||||
@ -49,14 +62,9 @@ class NameSpace(dict):
|
||||
dict.__delitem__(self, name)
|
||||
|
||||
|
||||
try:
|
||||
import ConfigParser
|
||||
except ImportError:
|
||||
import configparser as ConfigParser
|
||||
|
||||
|
||||
def getClientOpts(cfgfile):
|
||||
parser = ConfigParser.SafeConfigParser()
|
||||
parser = configparser.SafeConfigParser()
|
||||
if not parser.read([cfgfile + '.cfg']):
|
||||
print("Error reading cfg file %r" % cfgfile)
|
||||
return {}
|
||||
@ -65,9 +73,6 @@ def getClientOpts(cfgfile):
|
||||
return dict(item for item in parser.items('client'))
|
||||
|
||||
|
||||
from os import path
|
||||
|
||||
|
||||
class ClientConsole(object):
|
||||
|
||||
def __init__(self, cfgname, basepath):
|
||||
@ -93,23 +98,10 @@ class ClientConsole(object):
|
||||
help(arg)
|
||||
|
||||
|
||||
import socket
|
||||
import threading
|
||||
from collections import deque
|
||||
|
||||
import mlzlog
|
||||
|
||||
from secop.protocol.encoding import ENCODERS
|
||||
from secop.protocol.framing import FRAMERS
|
||||
from secop.protocol.messages import EventMessage, DescribeRequest
|
||||
|
||||
|
||||
class TCPConnection(object):
|
||||
|
||||
def __init__(self, connect, port, encoding, framing, **kwds):
|
||||
def __init__(self, connect, port, **kwds):
|
||||
self.log = mlzlog.log.getChild('connection', False)
|
||||
self.encoder = ENCODERS[encoding]()
|
||||
self.framer = FRAMERS[framing]()
|
||||
self.connection = socket.create_connection((connect, port), 3)
|
||||
self.queue = deque()
|
||||
self._rcvdata = ''
|
||||
@ -120,8 +112,7 @@ class TCPConnection(object):
|
||||
|
||||
def send(self, msg):
|
||||
self.log.debug("Sending msg %r" % msg)
|
||||
frame = self.encoder.encode(msg)
|
||||
data = self.framer.encode(frame)
|
||||
data = encode_msg_frame(*msg.serialize())
|
||||
self.log.debug("raw data: %r" % data)
|
||||
self.connection.sendall(data)
|
||||
|
||||
@ -133,20 +124,29 @@ class TCPConnection(object):
|
||||
self.log.exception("Exception in RCV thread: %r" % e)
|
||||
|
||||
def thread_step(self):
|
||||
data = b''
|
||||
while True:
|
||||
data = self.connection.recv(1024)
|
||||
self.log.debug("RCV: got raw data %r" % data)
|
||||
if data:
|
||||
frames = self.framer.decode(data)
|
||||
self.log.debug("RCV: frames %r" % frames)
|
||||
for frame in frames:
|
||||
msgs = self.encoder.decode(frame)
|
||||
self.log.debug("RCV: msgs %r" % msgs)
|
||||
for msg in msgs:
|
||||
self.handle(msg)
|
||||
newdata = self.connection.recv(1024)
|
||||
self.log.debug("RCV: got raw data %r" % newdata)
|
||||
data = data + newdata
|
||||
while True:
|
||||
origin, data = get_msg(data)
|
||||
if origin is None:
|
||||
break # no more messages to process
|
||||
if not origin: # empty string
|
||||
continue # ???
|
||||
msg = decode_msg(origin)
|
||||
# construct msgObj from msg
|
||||
try:
|
||||
msgObj = Message(*msg)
|
||||
msgObj.origin = origin.decode('latin-1')
|
||||
self.handle(msgObj)
|
||||
except Exception:
|
||||
# ??? what to do here?
|
||||
pass
|
||||
|
||||
def handle(self, msg):
|
||||
if isinstance(msg, EventMessage):
|
||||
if msg.action == EVENTREPLY:
|
||||
self.log.info("got Async: %r" % msg)
|
||||
for cb in self.callbacks:
|
||||
try:
|
||||
@ -188,7 +188,7 @@ class Client(object):
|
||||
# XXX: further notification-callbacks needed ???
|
||||
|
||||
def populateNamespace(self, namespace):
|
||||
self.connection.send(DescribeRequest())
|
||||
self.connection.send(Message(DESCRIPTIONREQUEST))
|
||||
# reply = self.connection.read()
|
||||
# self.log.info("found modules %r" % reply)
|
||||
# create proxies, populate cache....
|
||||
|
@ -32,11 +32,12 @@ from collections import OrderedDict
|
||||
import time
|
||||
import serial
|
||||
|
||||
# Py2/3
|
||||
try:
|
||||
import Queue
|
||||
# py3
|
||||
import queue
|
||||
except ImportError:
|
||||
import queue as Queue
|
||||
# py2
|
||||
import Queue as queue
|
||||
|
||||
import mlzlog
|
||||
|
||||
@ -61,7 +62,7 @@ class TCPConnection(object):
|
||||
self.connect()
|
||||
|
||||
def connect(self):
|
||||
self._readbuffer = Queue.Queue(100)
|
||||
self._readbuffer = queue.Queue(100)
|
||||
io = socket.create_connection((self._host, self._port))
|
||||
io.setblocking(False)
|
||||
io.settimeout(0.3)
|
||||
@ -75,7 +76,7 @@ class TCPConnection(object):
|
||||
data = u''
|
||||
while True:
|
||||
try:
|
||||
newdata = u''
|
||||
newdata = b''
|
||||
dlist = [self._io.fileno()]
|
||||
rlist, wlist, xlist = select(dlist, dlist, dlist, 1)
|
||||
if dlist[0] in rlist + wlist:
|
||||
@ -92,14 +93,14 @@ class TCPConnection(object):
|
||||
for cb, arg in self.callbacks:
|
||||
cb(arg)
|
||||
return
|
||||
data += newdata
|
||||
data += newdata.decode('latin-1')
|
||||
while '\n' in data:
|
||||
line, data = data.split('\n', 1)
|
||||
try:
|
||||
self._readbuffer.put(line.strip('\r'),
|
||||
block=True,
|
||||
timeout=1)
|
||||
except Queue.Full:
|
||||
except queue.Full:
|
||||
self.log.debug('rcv queue full! dropping line: %r' %
|
||||
line)
|
||||
finally:
|
||||
@ -111,7 +112,7 @@ class TCPConnection(object):
|
||||
while i:
|
||||
try:
|
||||
return self._readbuffer.get(block=True, timeout=1)
|
||||
except Queue.Empty:
|
||||
except queue.Empty:
|
||||
continue
|
||||
if not block:
|
||||
i -= 1
|
||||
@ -120,7 +121,7 @@ class TCPConnection(object):
|
||||
return not self._readbuffer.empty()
|
||||
|
||||
def write(self, data):
|
||||
self._io.sendall(data)
|
||||
self._io.sendall(data.encode('latin-1'))
|
||||
|
||||
def writeline(self, line):
|
||||
self.write(line + '\n')
|
||||
@ -370,7 +371,7 @@ class Client(object):
|
||||
try:
|
||||
describing_data = self._decode_substruct(
|
||||
['modules'], describing_data)
|
||||
for modname, module in describing_data['modules'].items():
|
||||
for modname, module in list(describing_data['modules'].items()):
|
||||
describing_data['modules'][modname] = self._decode_substruct(
|
||||
['parameters', 'commands'], module)
|
||||
|
||||
@ -383,14 +384,12 @@ class Client(object):
|
||||
# pprint.pprint(r(describing_data))
|
||||
|
||||
for module, moduleData in self.describing_data['modules'].items():
|
||||
for parameter, parameterData in moduleData[
|
||||
'parameters'].items():
|
||||
for parameter, parameterData in moduleData['parameters'].items():
|
||||
datatype = get_datatype(parameterData['datatype'])
|
||||
self.describing_data['modules'][module]['parameters'] \
|
||||
[parameter]['datatype'] = datatype
|
||||
for _cmdname, cmdData in moduleData[
|
||||
'commands'].items():
|
||||
cmdData['arguments'] = map(get_datatype, cmdData['arguments'])
|
||||
for _cmdname, cmdData in moduleData['commands'].items():
|
||||
cmdData['arguments'] = list(map(get_datatype, cmdData['arguments']))
|
||||
cmdData['resulttype'] = get_datatype(cmdData['resulttype'])
|
||||
except Exception as _exc:
|
||||
print(formatException(verbose=True))
|
||||
@ -544,10 +543,10 @@ class Client(object):
|
||||
|
||||
@property
|
||||
def modules(self):
|
||||
return self.describing_data['modules'].keys()
|
||||
return list(self.describing_data['modules'].keys())
|
||||
|
||||
def getParameters(self, module):
|
||||
return self.describing_data['modules'][module]['parameters'].keys()
|
||||
return list(self.describing_data['modules'][module]['parameters'].keys())
|
||||
|
||||
def getModuleProperties(self, module):
|
||||
return self.describing_data['modules'][module]['properties']
|
||||
|
Reference in New Issue
Block a user