make most important classes available from secop
+ consmetic changes to make PyCharm more happy + update authorship Change-Id: I67cb61a04e502b207be74cea4ca07931c88fdafe Reviewed-on: https://forge.frm2.tum.de/review/c/sine2020/secop/playground/+/22070 Tested-by: JenkinsCodeReview <bjoern_pedersen@frm2.tum.de> Reviewed-by: Markus Zolliker <markus.zolliker@psi.ch>
This commit is contained in:
@ -19,9 +19,19 @@
|
||||
# Module authors:
|
||||
# Alexander Lenz <alexander.lenz@frm2.tum.de>
|
||||
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
|
||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
||||
#
|
||||
# *****************************************************************************
|
||||
|
||||
# allow to import the most important classes from 'secop'
|
||||
from secop.datatypes import *
|
||||
from secop.modules import Module, Readable, Writable, Drivable, Communicator, Attached
|
||||
from secop.params import Parameter, Command, Override
|
||||
from secop.metaclass import Done
|
||||
from secop.commandhandler import CmdHandler, CmdHandlerBase
|
||||
from secop.stringio import StringIO, HasIodev
|
||||
|
||||
|
||||
try:
|
||||
import sip
|
||||
sip.setapi('QString', 2)
|
||||
|
@ -58,7 +58,6 @@ from secop.metaclass import Done
|
||||
from secop.errors import ProgrammingError
|
||||
|
||||
|
||||
|
||||
class CmdParser:
|
||||
"""helper for parsing replies
|
||||
|
||||
@ -68,14 +67,13 @@ class CmdParser:
|
||||
|
||||
# make a map of cast functions
|
||||
CAST_MAP = {letter: cast
|
||||
for letters, cast in (
|
||||
('d', int),
|
||||
('s', str), # 'c' is treated separately
|
||||
('o', lambda x:int(x, 8)),
|
||||
('xX', lambda x:int(x, 16)),
|
||||
('eEfFgG', float),
|
||||
) for letter in letters
|
||||
}
|
||||
for letters, cast in (
|
||||
('d', int),
|
||||
('s', str), # 'c' is treated separately
|
||||
('o', lambda x: int(x, 8)),
|
||||
('xX', lambda x: int(x, 16)),
|
||||
('eEfFgG', float),
|
||||
) for letter in letters}
|
||||
# pattern for chacaters to be escaped
|
||||
ESC_PAT = re.compile('([\\%s])' % '\\'.join('|^$-.+*?()[]{}<>'))
|
||||
# format pattern
|
||||
@ -94,13 +92,13 @@ class CmdParser:
|
||||
casts = []
|
||||
# the first item in spl is just plain text
|
||||
pat = [escaped(next(spl_iter))]
|
||||
todofmt = None # format set aside to be treated in next loop
|
||||
todofmt = None # format set aside to be treated in next loop
|
||||
|
||||
# loop over found formats and separators
|
||||
for fmt, sep in zip(spl_iter,spl_iter):
|
||||
for fmt, sep in zip(spl_iter, spl_iter):
|
||||
if fmt == '%%':
|
||||
if todofmt is None:
|
||||
pat.append('%' + escaped(sep)) # plain text
|
||||
pat.append('%' + escaped(sep)) # plain text
|
||||
continue
|
||||
fmt = todofmt
|
||||
todofmt = None
|
||||
@ -108,7 +106,7 @@ class CmdParser:
|
||||
elif todofmt:
|
||||
raise ValueError("a separator must follow '%s'" % todofmt)
|
||||
cast = self.CAST_MAP.get(fmt[-1], None)
|
||||
if cast is None: # special or unknown case
|
||||
if cast is None: # special or unknown case
|
||||
if fmt != '%c':
|
||||
raise ValueError("unsupported format: '%s'" % fmt)
|
||||
# we do not need a separator after %c
|
||||
@ -116,7 +114,7 @@ class CmdParser:
|
||||
casts.append(str)
|
||||
pat.append(escaped(sep))
|
||||
continue
|
||||
if sep == '': # missing separator. postpone handling for '%%' case or end of pattern
|
||||
if sep == '': # missing separator. postpone handling for '%%' case or end of pattern
|
||||
todofmt = fmt
|
||||
continue
|
||||
casts.append(cast)
|
||||
@ -128,7 +126,7 @@ class CmdParser:
|
||||
self.casts = casts
|
||||
self.pat = re.compile(''.join(pat))
|
||||
try:
|
||||
argformat % ((0,) * len(casts)) # validate argformat
|
||||
argformat % ((0,) * len(casts)) # validate argformat
|
||||
except ValueError as e:
|
||||
raise ValueError("%s in %r" % (e, argformat))
|
||||
|
||||
@ -154,7 +152,7 @@ class Change:
|
||||
self._module = module
|
||||
self._valuedict = valuedict
|
||||
self._to_be_changed = set(self._valuedict)
|
||||
self._do_read = True
|
||||
self._reply = None
|
||||
|
||||
def __getattr__(self, key):
|
||||
"""return attribute from module key is not in self._valuedict"""
|
||||
@ -171,8 +169,7 @@ class Change:
|
||||
|
||||
and update our parameter attributes accordingly (i.e. do not touch the new values)
|
||||
"""
|
||||
if self._do_read:
|
||||
self._do_read = False
|
||||
if self._reply is None:
|
||||
self._reply = self._handler.send_command(self._module)
|
||||
result = self._handler.analyze(self._module, *self._reply)
|
||||
result.update(self._valuedict)
|
||||
@ -218,7 +215,7 @@ class CmdHandlerBase:
|
||||
a reply.
|
||||
"""
|
||||
changecmd = self.make_change(module, *values)
|
||||
module.sendRecv(changecmd) # ignore result
|
||||
module.sendRecv(changecmd) # ignore result
|
||||
return self.send_command(module)
|
||||
|
||||
def get_read_func(self, modclass, pname):
|
||||
@ -229,7 +226,7 @@ class CmdHandlerBase:
|
||||
self._module_class = self._module_class or modclass
|
||||
if self._module_class != modclass:
|
||||
raise ProgrammingError("the handler '%s' for '%s.%s' is already used in module '%s'"
|
||||
% (self.group, modclass.__name__, pname, self._module_class.__name__))
|
||||
% (self.group, modclass.__name__, pname, self._module_class.__name__))
|
||||
self.parameters.add(pname)
|
||||
self.analyze = getattr(modclass, 'analyze_' + self.group)
|
||||
return self.read
|
||||
@ -286,7 +283,7 @@ class CmdHandlerBase:
|
||||
assert module.__class__ == self._module_class
|
||||
force_read = False
|
||||
valuedict = {pname: value}
|
||||
if module.writeDict: # collect other parameters to be written
|
||||
if module.writeDict: # collect other parameters to be written
|
||||
for p in self.parameters:
|
||||
if p in module.writeDict:
|
||||
valuedict[p] = module.writeDict.pop(p)
|
||||
@ -296,7 +293,7 @@ class CmdHandlerBase:
|
||||
if force_read:
|
||||
change.readValues()
|
||||
values = self.change(module, change)
|
||||
if values is None: # this indicates that nothing has to be written
|
||||
if values is None: # this indicates that nothing has to be written
|
||||
return
|
||||
# send the change command and a query command
|
||||
reply = self.send_change(module, *values)
|
||||
@ -314,11 +311,8 @@ class CmdHandler(CmdHandlerBase):
|
||||
|
||||
implementing classes have to define/override the following:
|
||||
"""
|
||||
CMDARGS = [] # list of properties or parameters to be used for building
|
||||
# some of the the query and change commands
|
||||
CMDSEPARATOR = ';' # if given, it is valid to join a command a a query with
|
||||
# the given separator
|
||||
|
||||
CMDARGS = [] # list of properties or parameters to be used for building some of the the query and change commands
|
||||
CMDSEPARATOR = ';' # if given, it is valid to join a command a a query with the given separator
|
||||
|
||||
def __init__(self, group, querycmd, replyfmt, changecmd=None):
|
||||
"""initialize the command handler
|
||||
|
@ -17,6 +17,7 @@
|
||||
#
|
||||
# Module authors:
|
||||
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
|
||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
||||
#
|
||||
# *****************************************************************************
|
||||
"""Define validated data types."""
|
||||
|
@ -17,6 +17,7 @@
|
||||
#
|
||||
# Module authors:
|
||||
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
|
||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
||||
#
|
||||
# *****************************************************************************
|
||||
"""Define Metaclass for Modules/Features"""
|
||||
@ -30,14 +31,15 @@ from secop.params import Command, Override, Parameter
|
||||
from secop.datatypes import EnumType
|
||||
from secop.properties import PropertyMeta
|
||||
|
||||
|
||||
EVENT_ONLY_ON_CHANGED_VALUES = False
|
||||
|
||||
|
||||
class Done:
|
||||
"""a special return value for a read/write function
|
||||
|
||||
indicating that the setter is triggered already"""
|
||||
|
||||
|
||||
# warning: MAGIC!
|
||||
|
||||
class ModuleMeta(PropertyMeta):
|
||||
@ -68,7 +70,7 @@ class ModuleMeta(PropertyMeta):
|
||||
accessibles_list.append(base.accessibles)
|
||||
for accessibles in [parameters, commands, overrides]:
|
||||
accessibles_list.append(accessibles)
|
||||
accessibles = {} # unordered dict of accessibles, will be sorted later
|
||||
accessibles = {} # unordered dict of accessibles, will be sorted later
|
||||
for accessibles_dict in accessibles_list:
|
||||
for key, obj in accessibles_dict.items():
|
||||
if isinstance(obj, Override):
|
||||
@ -110,7 +112,7 @@ class ModuleMeta(PropertyMeta):
|
||||
value = pobj.datatype(attrs[pname])
|
||||
except BadValueError:
|
||||
raise ProgrammingError('parameter %s can not be set to %r'
|
||||
% (pname, attrs[pname]))
|
||||
% (pname, attrs[pname]))
|
||||
newtype.accessibles[pname] = Override(default=value).apply(pobj)
|
||||
|
||||
# check validity of Parameter entries
|
||||
@ -142,7 +144,7 @@ class ModuleMeta(PropertyMeta):
|
||||
try:
|
||||
value = rfunc(self)
|
||||
self.log.debug("rfunc(%s) returned %r" % (pname, value))
|
||||
if value is Done: # the setter is already triggered
|
||||
if value is Done: # the setter is already triggered
|
||||
return getattr(self, pname)
|
||||
except Exception as e:
|
||||
self.log.debug("rfunc(%s) failed %r" % (pname, e))
|
||||
|
@ -17,6 +17,7 @@
|
||||
#
|
||||
# Module authors:
|
||||
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
|
||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
||||
#
|
||||
# *****************************************************************************
|
||||
"""Define Baseclasses for real Modules implemented in the server"""
|
||||
@ -187,10 +188,10 @@ class Module(HasProperties, metaclass=ModuleMeta):
|
||||
|
||||
# 4) complain if a Parameter entry has no default value and
|
||||
# is not specified in cfgdict and deal with parameters to be written.
|
||||
self.writeDict = {} # values of parameters to be written
|
||||
self.writeDict = {} # values of parameters to be written
|
||||
for pname, pobj in self.parameters.items():
|
||||
if pname in cfgdict:
|
||||
if not pobj.readonly and not pobj.initwrite is False:
|
||||
if not pobj.readonly and pobj.initwrite is not False:
|
||||
# parameters given in cfgdict have to call write_<pname>
|
||||
try:
|
||||
pobj.value = pobj.datatype(cfgdict[pname])
|
||||
@ -214,7 +215,7 @@ class Module(HasProperties, metaclass=ModuleMeta):
|
||||
value = pobj.datatype(pobj.default)
|
||||
except BadValueError as e:
|
||||
raise ProgrammingError('bad default for %s.%s: %s'
|
||||
% (name, pname, e))
|
||||
% (name, pname, e))
|
||||
if pobj.initwrite:
|
||||
# we will need to call write_<pname>
|
||||
# if this is not desired, the default must not be given
|
||||
@ -264,7 +265,7 @@ class Module(HasProperties, metaclass=ModuleMeta):
|
||||
self.DISPATCHER.announce_update_error(self, pname, pobj, exception)
|
||||
|
||||
def isBusy(self, status=None):
|
||||
'''helper function for treating substates of BUSY correctly'''
|
||||
"""helper function for treating substates of BUSY correctly"""
|
||||
# defined even for non drivable (used for dynamic polling)
|
||||
return False
|
||||
|
||||
@ -276,25 +277,24 @@ class Module(HasProperties, metaclass=ModuleMeta):
|
||||
self.log.debug('empty %s.initModule()' % self.__class__.__name__)
|
||||
|
||||
def startModule(self, started_callback):
|
||||
'''runs after init of all modules
|
||||
"""runs after init of all modules
|
||||
|
||||
started_callback to be called when thread spawned by late_init
|
||||
or, if not implemented, immediately
|
||||
might return a timeout value, if different from default
|
||||
'''
|
||||
|
||||
"""
|
||||
self.log.debug('empty %s.startModule()' % self.__class__.__name__)
|
||||
started_callback()
|
||||
|
||||
def pollOneParam(self, pname):
|
||||
"""poll parameter <pname> with proper error handling"""
|
||||
try:
|
||||
return getattr(self, 'read_'+ pname)()
|
||||
except SilentError as e:
|
||||
return getattr(self, 'read_' + pname)()
|
||||
except SilentError:
|
||||
pass
|
||||
except SECoPError as e:
|
||||
self.log.error(str(e))
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
self.log.error(formatException())
|
||||
|
||||
def writeOrPoll(self, pname):
|
||||
@ -305,14 +305,14 @@ class Module(HasProperties, metaclass=ModuleMeta):
|
||||
try:
|
||||
if pname in self.writeDict:
|
||||
self.log.debug('write parameter %s', pname)
|
||||
getattr(self, 'write_'+ pname)(self.writeDict.pop(pname))
|
||||
getattr(self, 'write_' + pname)(self.writeDict.pop(pname))
|
||||
else:
|
||||
getattr(self, 'read_'+ pname)()
|
||||
except SilentError as e:
|
||||
getattr(self, 'read_' + pname)()
|
||||
except SilentError:
|
||||
pass
|
||||
except SECoPError as e:
|
||||
self.log.error(str(e))
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
self.log.error(formatException())
|
||||
|
||||
|
||||
@ -348,7 +348,7 @@ class Readable(Module):
|
||||
}
|
||||
|
||||
def startModule(self, started_callback):
|
||||
'''start basic polling thread'''
|
||||
"""start basic polling thread"""
|
||||
if issubclass(self.pollerClass, BasicPoller):
|
||||
# use basic poller for legacy code
|
||||
mkthread(self.__pollThread, started_callback)
|
||||
@ -423,15 +423,15 @@ class Drivable(Writable):
|
||||
}
|
||||
|
||||
overrides = {
|
||||
'status' : Override(datatype=TupleOf(EnumType(Status), StringType())),
|
||||
'status': Override(datatype=TupleOf(EnumType(Status), StringType())),
|
||||
}
|
||||
|
||||
def isBusy(self, status=None):
|
||||
'''helper function for treating substates of BUSY correctly'''
|
||||
"""helper function for treating substates of BUSY correctly"""
|
||||
return 300 <= (status or self.status)[0] < 400
|
||||
|
||||
def isDriving(self, status=None):
|
||||
'''helper function (finalize is busy, not driving)'''
|
||||
"""helper function (finalize is busy, not driving)"""
|
||||
return 300 <= (status or self.status)[0] < 390
|
||||
|
||||
# improved polling: may poll faster if module is BUSY
|
||||
@ -472,7 +472,6 @@ class Communicator(Module):
|
||||
}
|
||||
|
||||
|
||||
|
||||
class Attached(Property):
|
||||
# we can not put this to properties.py, as it needs datatypes
|
||||
def __init__(self, attrname=None):
|
||||
|
@ -17,6 +17,7 @@
|
||||
#
|
||||
# Module authors:
|
||||
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
|
||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
||||
#
|
||||
# *****************************************************************************
|
||||
"""Define classes for Parameters/Commands and Overriding them"""
|
||||
|
@ -19,7 +19,7 @@
|
||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
||||
#
|
||||
# *****************************************************************************
|
||||
'''general, advanced frappy poller
|
||||
"""general, advanced frappy poller
|
||||
|
||||
Usage examples:
|
||||
any Module which want to be polled with a specific Poller must define
|
||||
@ -31,7 +31,7 @@ Usage examples:
|
||||
...
|
||||
|
||||
modules having a parameter 'iodev' with the same value will share the same poller
|
||||
'''
|
||||
"""
|
||||
|
||||
import time
|
||||
from threading import Event
|
||||
@ -40,24 +40,25 @@ from secop.lib import mkthread
|
||||
from secop.errors import ProgrammingError
|
||||
|
||||
# poll types:
|
||||
AUTO = 1 # equivalent to True, converted to REGULAR, SLOW or DYNAMIC
|
||||
AUTO = 1 # equivalent to True, converted to REGULAR, SLOW or DYNAMIC
|
||||
SLOW = 2
|
||||
REGULAR = 3
|
||||
DYNAMIC = 4
|
||||
|
||||
|
||||
class PollerBase:
|
||||
|
||||
startup_timeout = 30 # default timeout for startup
|
||||
name = 'unknown' # to be overridden in implementors __init__ method
|
||||
startup_timeout = 30 # default timeout for startup
|
||||
name = 'unknown' # to be overridden in implementors __init__ method
|
||||
|
||||
@classmethod
|
||||
def add_to_table(cls, table, module):
|
||||
'''sort module into poller table
|
||||
"""sort module into poller table
|
||||
|
||||
table is a dict, with (<pollerClass>, <name>) as the key, and the
|
||||
poller as value.
|
||||
<name> is module.iodev or module.name, if iodev is not present
|
||||
'''
|
||||
"""
|
||||
# for modules with the same iodev, a common poller is used,
|
||||
# modules without iodev all get their own poller
|
||||
name = getattr(module, 'iodev', module.name)
|
||||
@ -68,26 +69,26 @@ class PollerBase:
|
||||
poller.add_to_poller(module)
|
||||
|
||||
def start(self, started_callback):
|
||||
'''start poller thread
|
||||
"""start poller thread
|
||||
|
||||
started_callback to be called after all poll items were read at least once
|
||||
'''
|
||||
"""
|
||||
mkthread(self.run, started_callback)
|
||||
return self.startup_timeout
|
||||
|
||||
def run(self, started_callback):
|
||||
'''poller thread function
|
||||
"""poller thread function
|
||||
|
||||
started_callback to be called after all poll items were read at least once
|
||||
'''
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def stop(self):
|
||||
'''stop polling'''
|
||||
"""stop polling"""
|
||||
raise NotImplementedError
|
||||
|
||||
def __bool__(self):
|
||||
'''is there any poll item?'''
|
||||
"""is there any poll item?"""
|
||||
raise NotImplementedError
|
||||
|
||||
def __repr__(self):
|
||||
@ -95,7 +96,7 @@ class PollerBase:
|
||||
|
||||
|
||||
class Poller(PollerBase):
|
||||
'''a standard poller
|
||||
"""a standard poller
|
||||
|
||||
parameters may have the following polltypes:
|
||||
|
||||
@ -106,12 +107,12 @@ class Poller(PollerBase):
|
||||
Scheduled to poll every slowfactor * module.pollinterval
|
||||
- DYNAMIC: by default used for 'value' and 'status'
|
||||
When busy, scheduled to poll every fastfactor * module.pollinterval
|
||||
'''
|
||||
"""
|
||||
|
||||
DEFAULT_FACTORS = {SLOW: 4, DYNAMIC: 0.25, REGULAR: 1}
|
||||
|
||||
def __init__(self, name):
|
||||
'''create a poller'''
|
||||
"""create a poller"""
|
||||
self.queues = {polltype: [] for polltype in self.DEFAULT_FACTORS}
|
||||
self._event = Event()
|
||||
self._stopped = False
|
||||
@ -148,34 +149,34 @@ class Poller(PollerBase):
|
||||
module.registerReconnectCallback(self.name, self.trigger_all)
|
||||
else:
|
||||
module.log.warning("%r has 'is_connected' but no 'registerReconnectCallback'" % module)
|
||||
if polltype == AUTO: # covers also pobj.poll == True
|
||||
if polltype == AUTO: # covers also pobj.poll == True
|
||||
if pname in ('value', 'status'):
|
||||
polltype = DYNAMIC
|
||||
elif pobj.readonly:
|
||||
polltype = REGULAR
|
||||
else:
|
||||
polltype = SLOW
|
||||
if not polltype in factors:
|
||||
if polltype not in factors:
|
||||
raise ProgrammingError("unknown poll type %r for parameter '%s'"
|
||||
% (polltype, pname))
|
||||
if pobj.handler:
|
||||
if pobj.handler in handlers:
|
||||
continue # only one poller per handler
|
||||
continue # only one poller per handler
|
||||
handlers.add(pobj.handler)
|
||||
# placeholders 0 are used for due, lastdue and idx
|
||||
self.queues[polltype].append((0, 0,
|
||||
(0, module, pobj, pname, factors[polltype])))
|
||||
|
||||
def poll_next(self, polltype):
|
||||
'''try to poll next item
|
||||
"""try to poll next item
|
||||
|
||||
advance in queue until
|
||||
- an item is found which is really due to poll. return 0 in this case
|
||||
- or until the next item is not yet due. return next due time in this case
|
||||
'''
|
||||
"""
|
||||
queue = self.queues[polltype]
|
||||
if not queue:
|
||||
return float('inf') # queue is empty
|
||||
return float('inf') # queue is empty
|
||||
now = time.time()
|
||||
done = False
|
||||
while not done:
|
||||
@ -191,7 +192,7 @@ class Poller(PollerBase):
|
||||
interval = module.pollinterval * factor
|
||||
mininterval = interval
|
||||
if due == 0:
|
||||
due = now # do not look at timestamp after trigger_all
|
||||
due = now # do not look at timestamp after trigger_all
|
||||
else:
|
||||
due = max(lastdue + interval, pobj.timestamp + interval * 0.5)
|
||||
if now >= due:
|
||||
@ -211,7 +212,7 @@ class Poller(PollerBase):
|
||||
return True
|
||||
|
||||
def run(self, started_callback):
|
||||
'''start poll loop
|
||||
"""start poll loop
|
||||
|
||||
To be called as a thread. After all parameters are polled once first,
|
||||
started_callback is called. To be called in Module.start_module.
|
||||
@ -221,13 +222,13 @@ class Poller(PollerBase):
|
||||
If more polls are scheduled than time permits, at least every second poll is a
|
||||
dynamic poll. After every n regular polls, one slow poll is done, if due
|
||||
(where n is the number of regular parameters).
|
||||
'''
|
||||
"""
|
||||
if not self:
|
||||
# nothing to do (else we might call time.sleep(float('inf')) below
|
||||
started_callback()
|
||||
return
|
||||
# do all polls once and, at the same time, insert due info
|
||||
for _, queue in sorted(self.queues.items()): # do SLOW polls first
|
||||
for _, queue in sorted(self.queues.items()): # do SLOW polls first
|
||||
for idx, (_, _, (_, module, pobj, pname, factor)) in enumerate(queue):
|
||||
lastdue = time.time()
|
||||
module.writeOrPoll(pname)
|
||||
@ -236,14 +237,14 @@ class Poller(PollerBase):
|
||||
# are comparable. Inserting a unique idx solves the problem.
|
||||
queue[idx] = (due, lastdue, (idx, module, pobj, pname, factor))
|
||||
heapify(queue)
|
||||
started_callback() # signal end of startup
|
||||
started_callback() # signal end of startup
|
||||
nregular = len(self.queues[REGULAR])
|
||||
while not self._stopped:
|
||||
due = float('inf')
|
||||
for _ in range(nregular):
|
||||
due = min(self.poll_next(DYNAMIC), self.poll_next(REGULAR))
|
||||
if due:
|
||||
break # no dynamic or regular polls due
|
||||
break # no dynamic or regular polls due
|
||||
due = min(due, self.poll_next(DYNAMIC), self.poll_next(SLOW))
|
||||
delay = due - time.time()
|
||||
if delay > 0:
|
||||
@ -255,7 +256,7 @@ class Poller(PollerBase):
|
||||
self._stopped = True
|
||||
|
||||
def __bool__(self):
|
||||
'''is there any poll item?'''
|
||||
"""is there any poll item?"""
|
||||
return any(self.queues.values())
|
||||
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
#
|
||||
# Module authors:
|
||||
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
|
||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
||||
#
|
||||
# *****************************************************************************
|
||||
"""Define validated data types."""
|
||||
|
@ -17,6 +17,7 @@
|
||||
#
|
||||
# Module authors:
|
||||
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
|
||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
||||
#
|
||||
# *****************************************************************************
|
||||
"""Dispatcher for SECoP Messages
|
||||
@ -50,7 +51,6 @@ from secop.protocol.messages import COMMANDREPLY, DESCRIPTIONREPLY, \
|
||||
HEARTBEATREPLY, IDENTREPLY, IDENTREQUEST, READREPLY, WRITEREPLY
|
||||
|
||||
|
||||
|
||||
def make_update(modulename, pobj):
|
||||
if pobj.readerror:
|
||||
return (ERRORPREFIX + EVENTREPLY, '%s:%s' % (modulename, pobj.export),
|
||||
@ -116,9 +116,9 @@ class Dispatcher:
|
||||
if not isinstance(err, SECoPError):
|
||||
err = InternalError(err)
|
||||
if str(err) == str(pobj.readerror):
|
||||
return # do not send updates for repeated errors
|
||||
return # do not send updates for repeated errors
|
||||
pobj.readerror = err
|
||||
pobj.timestamp = currenttime() # indicates the first time this error appeared
|
||||
pobj.timestamp = currenttime() # indicates the first time this error appeared
|
||||
self.broadcast_event(make_update(moduleobj.name, pobj))
|
||||
|
||||
def subscribe(self, conn, eventname):
|
||||
@ -272,7 +272,7 @@ class Dispatcher:
|
||||
pobj = moduleobj.parameters[pname]
|
||||
if pobj.constant is not None:
|
||||
# really needed? we could just construct a readreply instead....
|
||||
#raise ReadOnlyError('This parameter is constant and can not be accessed remotely.')
|
||||
# raise ReadOnlyError('This parameter is constant and can not be accessed remotely.')
|
||||
return pobj.datatype.export_value(pobj.constant)
|
||||
|
||||
readfunc = getattr(moduleobj, 'read_%s' % pname, None)
|
||||
|
@ -16,6 +16,7 @@
|
||||
#
|
||||
# Module authors:
|
||||
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
|
||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
||||
#
|
||||
# *****************************************************************************
|
||||
"""provides tcp interface to the SECoP Server"""
|
||||
@ -45,12 +46,13 @@ SPACE = b' '
|
||||
class OutputBufferOverflow(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class TCPRequestHandler(socketserver.BaseRequestHandler):
|
||||
|
||||
def setup(self):
|
||||
self.log = self.server.log
|
||||
# Queue of msgObjects to send
|
||||
self._queue = collections.deque() # do not use maxlen, as items might get lost
|
||||
self._queue = collections.deque() # do not use maxlen, as items might get lost
|
||||
# self.framing = self.server.framingCLS()
|
||||
# self.encoding = self.server.encodingCLS()
|
||||
|
||||
@ -80,9 +82,9 @@ class TCPRequestHandler(socketserver.BaseRequestHandler):
|
||||
# send bytestring
|
||||
outmsg = self._queue.popleft()
|
||||
if not outmsg:
|
||||
outmsg = ('error','InternalError', ['<unknown origin>', 'trying to send none-data', {}])
|
||||
outmsg = ('error', 'InternalError', ['<unknown origin>', 'trying to send none-data', {}])
|
||||
if len(outmsg) > 3:
|
||||
outmsg = ('error', 'InternalError', ['<unknown origin>', 'bad message format', {'msg':outmsg}])
|
||||
outmsg = ('error', 'InternalError', ['<unknown origin>', 'bad message format', {'msg': outmsg}])
|
||||
outdata = encode_msg_frame(*outmsg)
|
||||
try:
|
||||
mysocket.sendall(outdata)
|
||||
@ -97,7 +99,7 @@ class TCPRequestHandler(socketserver.BaseRequestHandler):
|
||||
# no timeout error, but no new data -> connection closed
|
||||
return
|
||||
data = data + newdata
|
||||
except socket.timeout as e:
|
||||
except socket.timeout:
|
||||
continue
|
||||
except socket.error as e:
|
||||
self.log.exception(e)
|
||||
@ -118,13 +120,12 @@ class TCPRequestHandler(socketserver.BaseRequestHandler):
|
||||
for idx, line in enumerate(HelpMessage.splitlines()):
|
||||
self.queue_async_reply((HELPREPLY, '%d' % (idx+1), line))
|
||||
continue
|
||||
result = None
|
||||
try:
|
||||
msg = decode_msg(origin)
|
||||
except Exception as err:
|
||||
# we have to decode 'origin' here
|
||||
# use latin-1, as utf-8 or ascii may lead to encoding errors
|
||||
msg = origin.decode('latin-1').split(' ', 3) + [None] # make sure len(msg) > 1
|
||||
msg = origin.decode('latin-1').split(' ', 3) + [None] # make sure len(msg) > 1
|
||||
result = (ERRORPREFIX + msg[0], msg[1], ['InternalError', str(err),
|
||||
{'exception': formatException(),
|
||||
'traceback': formatExtendedStack()}])
|
||||
@ -191,10 +192,10 @@ class TCPServer(HasProperties, socketserver.ThreadingTCPServer):
|
||||
allow_reuse_address = True
|
||||
|
||||
properties = {
|
||||
'bindto' : Property('hostname or ip address for binding',StringType(),
|
||||
default='localhost:%d' % DEF_PORT, export=False),
|
||||
'bindport' : Property('port number to bind',IntRange(1,65535),
|
||||
default=DEF_PORT, export=False),
|
||||
'bindto': Property('hostname or ip address for binding', StringType(),
|
||||
default='localhost:%d' % DEF_PORT, export=False),
|
||||
'bindport': Property('port number to bind', IntRange(1, 65535),
|
||||
default=DEF_PORT, export=False),
|
||||
'detailed_errors': Property('Flag to enable detailed Errorreporting.', BoolType(),
|
||||
default=False, export=False),
|
||||
}
|
||||
@ -202,7 +203,7 @@ class TCPServer(HasProperties, socketserver.ThreadingTCPServer):
|
||||
# XXX: create configurables from Metaclass!
|
||||
configurables = properties
|
||||
|
||||
def __init__(self, name, logger, options, srv): # pylint: disable=super-init-not-called
|
||||
def __init__(self, name, logger, options, srv): # pylint: disable=super-init-not-called
|
||||
self.dispatcher = srv.dispatcher
|
||||
self.name = name
|
||||
self.log = logger
|
||||
|
@ -18,6 +18,7 @@
|
||||
# Module authors:
|
||||
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
|
||||
# Alexander Lenz <alexander.lenz@frm2.tum.de>
|
||||
# Markus Zolliker <markus.zolliker@psi.ch>
|
||||
#
|
||||
# *****************************************************************************
|
||||
"""Define helpers"""
|
||||
@ -56,12 +57,13 @@ class Server:
|
||||
('module', None, None),
|
||||
('interface', "tcp", {"tcp": "protocol.interface.tcp.TCPServer"}),
|
||||
]
|
||||
|
||||
def __init__(self, name, parent_logger=None):
|
||||
cfg = getGeneralConfig()
|
||||
|
||||
# also handle absolut paths
|
||||
if os.path.abspath(name) == name and os.path.exists(name) and \
|
||||
name.endswith('.cfg'):
|
||||
name.endswith('.cfg'):
|
||||
self._cfgfile = name
|
||||
self._pidfile = os.path.join(cfg['piddir'],
|
||||
name[:-4].replace(os.path.sep, '_') + '.pid')
|
||||
@ -120,15 +122,13 @@ class Server:
|
||||
def _processCfg(self):
|
||||
self.log.debug('Parse config file %s ...' % self._cfgfile)
|
||||
|
||||
parser = configparser.SafeConfigParser()
|
||||
parser = configparser.ConfigParser()
|
||||
parser.optionxform = str
|
||||
|
||||
if not parser.read([self._cfgfile]):
|
||||
self.log.error('Couldn\'t read cfg file !')
|
||||
raise ConfigError('Couldn\'t read cfg file %r' % self._cfgfile)
|
||||
|
||||
|
||||
|
||||
for kind, devtype, classmapping in self.CFGSECTIONS:
|
||||
kinds = '%ss' % kind
|
||||
objs = OrderedDict()
|
||||
@ -180,12 +180,12 @@ class Server:
|
||||
raise ConfigError('cfgfile %r: needs exactly one node section!' % self._cfgfile)
|
||||
self.dispatcher, = tuple(self.nodes.values())
|
||||
|
||||
pollTable = dict()
|
||||
poll_table = dict()
|
||||
# all objs created, now start them up and interconnect
|
||||
for modname, modobj in self.modules.items():
|
||||
self.log.info('registering module %r' % modname)
|
||||
self.dispatcher.register_module(modobj, modname, modobj.properties['export'])
|
||||
modobj.pollerClass.add_to_table(pollTable, modobj)
|
||||
modobj.pollerClass.add_to_table(poll_table, modobj)
|
||||
# also call earlyInit on the modules
|
||||
modobj.earlyInit()
|
||||
|
||||
@ -205,7 +205,7 @@ class Server:
|
||||
# startModule must return either a timeout value or None (default 30 sec)
|
||||
timeout = modobj.startModule(started_callback=event.set) or 30
|
||||
start_events.append((time.time() + timeout, 'module %s' % modname, event))
|
||||
for poller in pollTable.values():
|
||||
for poller in poll_table.values():
|
||||
event = threading.Event()
|
||||
# poller.start must return either a timeout value or None (default 30 sec)
|
||||
timeout = poller.start(started_callback=event.set) or 30
|
||||
|
@ -193,7 +193,7 @@ class StringIO(Communicator):
|
||||
if not re.match(regexp, reply):
|
||||
self.closeConnection()
|
||||
raise CommunicationFailedError('bad response: %s does not match %s' %
|
||||
(reply, regexp))
|
||||
(reply, regexp))
|
||||
|
||||
def registerReconnectCallback(self, name, func):
|
||||
"""register reconnect callback
|
||||
@ -216,14 +216,14 @@ class StringIO(Communicator):
|
||||
self._reconnectCallbacks.pop(key)
|
||||
|
||||
def do_communicate(self, command):
|
||||
'''send a command and receive a reply
|
||||
"""send a command and receive a reply
|
||||
|
||||
using end_of_line, encoding and self._lock
|
||||
for commands without reply, join it with a query command,
|
||||
wait_before is respected for end_of_lines within a command.
|
||||
'''
|
||||
"""
|
||||
if not self.is_connected:
|
||||
self.read_is_connected() # try to reconnect
|
||||
self.read_is_connected() # try to reconnect
|
||||
try:
|
||||
with self._lock:
|
||||
# read garbage and wait before send
|
||||
@ -235,7 +235,7 @@ class StringIO(Communicator):
|
||||
for cmd in cmds:
|
||||
if self.wait_before:
|
||||
time.sleep(self.wait_before)
|
||||
if garbage is None: # read garbage only once
|
||||
if garbage is None: # read garbage only once
|
||||
garbage = b''
|
||||
data = self.readWithTimeout(0)
|
||||
while data:
|
||||
|
Reference in New Issue
Block a user