Commit 62b8eb84 authored by Vincent Michel's avatar Vincent Michel

Run "black --fast ." to blackify the whole project

parent 1d76be44
Pipeline #4958 passed with stages
in 25 minutes and 7 seconds
......@@ -5,7 +5,7 @@
# Copyright (c) 2016 Beamline Control Unit, ESRF
# Distributed under the GNU LGPLv3. See LICENSE for more info.
'''Bliss main package
"""Bliss main package
For your convenience, configuration motion and scan APIs have been made available
directly at this level.
......@@ -24,9 +24,9 @@ Here are the main bliss sub-systems:
scanning
shell
tango
'''
"""
from .import release
from . import release
__version__ = release.version
__author__ = release.author
......@@ -34,4 +34,5 @@ __license__ = release.license
version_info = release.version_info
from gevent import monkey
monkey.patch_all(thread=False)
......@@ -14,53 +14,57 @@ import gevent.queue
exporter_clients = {}
def start_exporter(address, port, timeout=3, retries=1):
global exporter_clients
if not (address, port) in exporter_clients:
client = Exporter(address, port, timeout)
exporter_clients[(address, port)] = client
client.start()
return client
else:
return exporter_clients[(address, port)]
global exporter_clients
if not (address, port) in exporter_clients:
client = Exporter(address, port, timeout)
exporter_clients[(address, port)] = client
client.start()
return client
else:
return exporter_clients[(address, port)]
class Exporter(ExporterClient.ExporterClient):
STATE_EVENT = "State"
STATUS_EVENT = "Status"
VALUE_EVENT = "Value"
POSITION_EVENT = "Position"
MOTOR_STATES_EVENT = "MotorStates"
STATE_READY = "Ready"
STATE_INITIALIZING = "Initializing"
STATE_STARTING = "Starting"
STATE_RUNNING = "Running"
STATE_MOVING = "Moving"
STATE_CLOSING = "Closing"
STATE_REMOTE = "Remote"
STATE_STOPPED = "Stopped"
STATE_EVENT = "State"
STATUS_EVENT = "Status"
VALUE_EVENT = "Value"
POSITION_EVENT = "Position"
MOTOR_STATES_EVENT = "MotorStates"
STATE_READY = "Ready"
STATE_INITIALIZING = "Initializing"
STATE_STARTING = "Starting"
STATE_RUNNING = "Running"
STATE_MOVING = "Moving"
STATE_CLOSING = "Closing"
STATE_REMOTE = "Remote"
STATE_STOPPED = "Stopped"
STATE_COMMUNICATION_ERROR = "Communication Error"
STATE_INVALID = "Invalid"
STATE_OFFLINE = "Offline"
STATE_ALARM = "Alarm"
STATE_FAULT = "Fault"
STATE_UNKNOWN = "Unknown"
STATE_INVALID = "Invalid"
STATE_OFFLINE = "Offline"
STATE_ALARM = "Alarm"
STATE_FAULT = "Fault"
STATE_UNKNOWN = "Unknown"
def __init__(self, address, port, timeout=3, retries=1):
ExporterClient.ExporterClient.__init__(self, address, port, ExporterClient.PROTOCOL.STREAM, timeout, retries)
ExporterClient.ExporterClient.__init__(
self, address, port, ExporterClient.PROTOCOL.STREAM, timeout, retries
)
self.started = False
self.callbacks = {}
self.events_queue = gevent.queue.Queue()
self.events_processing_task = None
self.started = False
self.callbacks = {}
self.events_queue = gevent.queue.Queue()
self.events_processing_task = None
def start(self):
pass
#self.started=True
#self.reconnect()
# self.started=True
# self.reconnect()
def stop(self):
#self.started=False
# self.started=False
self.disconnect()
def execute(self, *args, **kwargs):
......@@ -68,7 +72,7 @@ class Exporter(ExporterClient.ExporterClient):
return self._to_python_value(ret)
def get_state(self):
return self.execute("getState")
return self.execute("getState")
def readProperty(self, *args, **kwargs):
ret = ExporterClient.ExporterClient.readProperty(self, *args, **kwargs)
......@@ -85,118 +89,112 @@ class Exporter(ExporterClient.ExporterClient):
self.reconnect()
def onDisconnected(self):
pass #self.reconnect()
pass # self.reconnect()
def register(self, name, cb):
if callable(cb):
self.callbacks.setdefault(name, []).append(cb)
if not self.events_processing_task:
self.events_processing_task = gevent.spawn(self.processEventsFromQueue)
if callable(cb):
self.callbacks.setdefault(name, []).append(cb)
if not self.events_processing_task:
self.events_processing_task = gevent.spawn(self.processEventsFromQueue)
def _to_python_value(self, value):
if value is None:
return
return
if '\x1f' in value:
value = self.parseArray(value)
try:
value = map(int, value)
except:
if "\x1f" in value:
value = self.parseArray(value)
try:
value = map(float, value)
value = map(int, value)
except:
pass
try:
value = map(float, value)
except:
pass
else:
try:
value = int(value)
except:
try:
value = float(value)
value = int(value)
except:
pass
try:
value = float(value)
except:
pass
return value
def onEvent(self, name, value, timestamp):
self.events_queue.put((name, value))
def processEventsFromQueue(self):
while True:
try:
name, value = self.events_queue.get()
except:
return
for cb in self.callbacks.get(name, []):
try:
cb(self._to_python_value(value))
name, value = self.events_queue.get()
except:
logging.exception("Exception while executing callback %s for event %s", cb, name)
continue
return
for cb in self.callbacks.get(name, []):
try:
cb(self._to_python_value(value))
except:
logging.exception(
"Exception while executing callback %s for event %s", cb, name
)
continue
class ExporterChannel:
def __init__(self, attribute_name, address = None, port = None, timeout=3, **kwargs):
def __init__(self, attribute_name, address=None, port=None, timeout=3, **kwargs):
self.__exporter = start_exporter(address, port, timeout)
self.attributeName = attribute_name
self.value = None
self.__exporter.register(attribute_name, self.update)
self.__exporter.register(attribute_name, self.update)
self.update()
def update(self, value = None):
def update(self, value=None):
if value is None:
value = self.getValue()
if type(value) == types.TupleType:
value = list(value)
value = list(value)
self.value = value
self.emit('update', value)
self.emit("update", value)
def getValue(self):
value = self.__exporter.readProperty(self.attributeName)
value = self.__exporter.readProperty(self.attributeName)
return value
def setValue(self, newValue):
self.__exporter.writeProperty(self.attributeName, newValue)
def isConnected(self):
return self.__exporter.isConnected()
class ExporterCommand:
def __init__(self, name, command, address = None, port = None, timeout=3, **kwargs):
def __init__(self, name, command, address=None, port=None, timeout=3, **kwargs):
self.command = command
self.__exporter = start_exporter(address, port, timeout)
self.__exporter = start_exporter(address, port, timeout)
def __call__(self, *args, **kwargs):
self.emit('commandBeginWaitReply', (str(self.name()), ))
self.emit("commandBeginWaitReply", (str(self.name()),))
try:
ret = self.__exporter.execute(self.command, args, kwargs.get("timeout", -1))
except:
self.emit('commandFailed', (-1, self.name()))
self.emit("commandFailed", (-1, self.name()))
raise
else:
self.emit('commandReplyArrived', (ret, str(self.name())))
self.emit("commandReplyArrived", (ret, str(self.name())))
return ret
def abort(self):
# TODO: implement async commands
pass
def get_state(self):
return self.__exporter.get_state()
......
......@@ -5,7 +5,7 @@
# Copyright (c) 2016 Beamline Control Unit, ESRF
# Distributed under the GNU LGPLv3. See LICENSE for more info.
'''Communication (:class:`~bliss.comm.gpib.Gpib`, :class:`~bliss.comm.tcp.Tcp`, \
"""Communication (:class:`~bliss.comm.gpib.Gpib`, :class:`~bliss.comm.tcp.Tcp`, \
:class:`~bliss.comm.serial.Serial`, etc)
This module gathers different communication interfaces
......@@ -20,4 +20,4 @@ This module gathers different communication interfaces
serial
tcp
util
'''
"""
......@@ -12,100 +12,101 @@ from StandardClient import *
CMD_SYNC_CALL = "EXEC"
CMD_ASNC_CALL = "ASNC"
CMD_METHOD_LIST = "LIST"
CMD_PROPERTY_READ = "READ";
CMD_PROPERTY_WRITE = "WRTE";
CMD_PROPERTY_LIST = "PLST";
CMD_PROPERTY_READ = "READ"
CMD_PROPERTY_WRITE = "WRTE"
CMD_PROPERTY_LIST = "PLST"
CMD_NAME = "NAME"
RET_ERR = "ERR:"
RET_OK = "RET:"
RET_NULL = "NULL"
EVENT = "EVT:"
PARAMETER_SEPARATOR = "\t";
ARRAY_SEPARATOR = ""; # 0x001F
PARAMETER_SEPARATOR = "\t"
ARRAY_SEPARATOR = ""
# 0x001F
class ExporterClient(StandardClient):
def onMessageReceived(self,msg):
if msg[:4]=="EVT:":
class ExporterClient(StandardClient):
def onMessageReceived(self, msg):
if msg[:4] == "EVT:":
try:
evtstr=msg[4:]
tokens=evtstr.split(PARAMETER_SEPARATOR)
self.onEvent(tokens[0],tokens[1],long(tokens[2]))
evtstr = msg[4:]
tokens = evtstr.split(PARAMETER_SEPARATOR)
self.onEvent(tokens[0], tokens[1], long(tokens[2]))
except:
#print "Error processing event: " + str(sys.exc_info()[1])
# print "Error processing event: " + str(sys.exc_info()[1])
pass
else:
StandardClient.onMessageReceived(self,msg)
StandardClient.onMessageReceived(self, msg)
def getMethodList(self):
cmd=CMD_METHOD_LIST
cmd = CMD_METHOD_LIST
ret = self.sendReceive(cmd)
ret = self.__processReturn(ret)
if ret is None:
return None
ret = ret.split(PARAMETER_SEPARATOR);
if len(ret)>1:
if (ret[-1]==""):
ret=ret[0:-1]
ret = ret.split(PARAMETER_SEPARATOR)
if len(ret) > 1:
if ret[-1] == "":
ret = ret[0:-1]
return ret
def getPropertyList(self):
cmd=CMD_PROPERTY_LIST
cmd = CMD_PROPERTY_LIST
ret = self.sendReceive(cmd)
ret = self.__processReturn(ret)
if ret is None:
return None
ret = ret.split(PARAMETER_SEPARATOR);
if len(ret)>1:
if (ret[-1]==""):
ret=ret[0:-1]
ret = ret.split(PARAMETER_SEPARATOR)
if len(ret) > 1:
if ret[-1] == "":
ret = ret[0:-1]
return ret
def getServerObjectName(self):
cmd=CMD_NAME
cmd = CMD_NAME
ret = self.sendReceive(cmd)
return self.__processReturn(ret)
def execute(self,method,pars=None,timeout=-1):
cmd=CMD_SYNC_CALL + " " + method + " "
def execute(self, method, pars=None, timeout=-1):
cmd = CMD_SYNC_CALL + " " + method + " "
if pars is not None:
if type(pars) is list or type(pars) is tuple:
if type(pars) is list or type(pars) is tuple:
for par in pars:
par=self.createArrayParameter(par)
cmd += (str(par) + PARAMETER_SEPARATOR)
par = self.createArrayParameter(par)
cmd += str(par) + PARAMETER_SEPARATOR
else:
cmd += str(pars)
ret = self.sendReceive(cmd,timeout)
ret = self.sendReceive(cmd, timeout)
return self.__processReturn(ret)
def __processReturn(self,ret):
if ret[:4]==RET_ERR:
raise Exception,ret[4:]
elif ret==RET_NULL:
def __processReturn(self, ret):
if ret[:4] == RET_ERR:
raise Exception, ret[4:]
elif ret == RET_NULL:
return None
elif ret[:4]==RET_OK:
elif ret[:4] == RET_OK:
return ret[4:]
else:
raise ProtocolError
def executeAsync(self,method,pars=None):
cmd=CMD_ASNC_CALL + " " + method + " "
def executeAsync(self, method, pars=None):
cmd = CMD_ASNC_CALL + " " + method + " "
if pars is not None:
for par in pars:
cmd += (str(par) + PARAMETER_SEPARATOR)
cmd += str(par) + PARAMETER_SEPARATOR
return self.send(cmd)
def writeProperty(self,property,value,timeout=-1):
if type(value) is list or type(value) is tuple:
value=self.createArrayParameter(value)
cmd=CMD_PROPERTY_WRITE + " " + property + " " + str(value)
ret = self.sendReceive(cmd,timeout)
def writeProperty(self, property, value, timeout=-1):
if type(value) is list or type(value) is tuple:
value = self.createArrayParameter(value)
cmd = CMD_PROPERTY_WRITE + " " + property + " " + str(value)
ret = self.sendReceive(cmd, timeout)
return self.__processReturn(ret)
def readProperty(self,property,timeout=-1):
cmd=CMD_PROPERTY_READ + " " + property
ret = self.sendReceive(cmd,timeout)
def readProperty(self, property, timeout=-1):
cmd = CMD_PROPERTY_READ + " " + property
ret = self.sendReceive(cmd, timeout)
return self.__processReturn(ret)
def readPropertyAsString(self, property):
......@@ -126,26 +127,25 @@ class ExporterClient(StandardClient):
ret = self.readProperty(property)
return self.parseArray(ret)
def parseArray(self,value):
value=str(value)
if value.startswith(ARRAY_SEPARATOR)==False:
def parseArray(self, value):
value = str(value)
if value.startswith(ARRAY_SEPARATOR) == False:
return None
if value==ARRAY_SEPARATOR:
if value == ARRAY_SEPARATOR:
return []
value=value.lstrip(ARRAY_SEPARATOR).rstrip(ARRAY_SEPARATOR)
value = value.lstrip(ARRAY_SEPARATOR).rstrip(ARRAY_SEPARATOR)
return value.split(ARRAY_SEPARATOR)
def createArrayParameter(self,value):
ret=""+ARRAY_SEPARATOR
def createArrayParameter(self, value):
ret = "" + ARRAY_SEPARATOR
if not value is None:
if type(value) is list or type(value) is tuple:
if type(value) is list or type(value) is tuple:
for item in value:
ret=ret+str(item)
ret=ret+ARRAY_SEPARATOR
ret = ret + str(item)
ret = ret + ARRAY_SEPARATOR
else:
ret=ret+str(value)
return ret
ret = ret + str(value)
return ret
def onEvent(self, name, value, timestamp):
pass
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -5,13 +5,12 @@
# Copyright (c) 2016 Beamline Control Unit, ESRF
# Distributed under the GNU LGPLv3. See LICENSE for more info.
'''Exception classes for communication'''
"""Exception classes for communication"""
class CommunicationError(RuntimeError):
'''Base communication error'''
"""Base communication error"""