updates from mlz repo

- asynconn: raise * from *
- asynconn: correct handling for timeout in AsynSerial
- add new py35compat
- target unit $

Change-Id: I052185ad3ebb3e3d1e3374f7ece9c7df06223951
This commit is contained in:
2021-11-10 14:37:57 +01:00
parent 41ce909172
commit a7b741eaa4
3 changed files with 90 additions and 35 deletions

View File

@ -48,6 +48,7 @@ class ConnectionClosed(ConnectionError):
class AsynConn:
timeout = 1 # inter byte timeout
scheme = None
SCHEME_MAP = {}
connection = None # is not None, if connected
defaultport = None
@ -62,11 +63,11 @@ class AsynConn:
except (ValueError, TypeError, AssertionError):
if 'COM' in uri:
raise ValueError("the correct uri for a COM port is: "
"'serial://COM<i>[?<option>=<value>[+<option>=value ...]]'")
"'serial://COM<i>[?<option>=<value>[+<option>=value ...]]'") from None
if '/dev' in uri:
raise ValueError("the correct uri for a serial port is: "
"'serial:///dev/<tty>[?<option>=<value>[+<option>=value ...]]'")
raise ValueError('invalid uri: %s' % uri)
"'serial:///dev/<tty>[?<option>=<value>[+<option>=value ...]]'") from None
raise ValueError('invalid uri: %s' % uri) from None
iocls = cls.SCHEME_MAP['tcp']
uri = 'tcp://%s:%d' % host_port
return object.__new__(iocls)
@ -80,7 +81,9 @@ class AsynConn:
@classmethod
def __init_subclass__(cls):
cls.SCHEME_MAP[cls.scheme] = cls
"""register subclass to scheme, if available"""
if cls.scheme:
cls.SCHEME_MAP[cls.scheme] = cls
def disconnect(self):
raise NotImplementedError
@ -166,7 +169,7 @@ class AsynTcp(AsynConn):
self.connection = tcpSocket(uri, self.defaultport, self.timeout)
except (ConnectionRefusedError, socket.gaierror) as e:
# indicate that retrying might make sense
raise CommunicationFailedError(str(e))
raise CommunicationFailedError(str(e)) from None
def disconnect(self):
if self.connection:
@ -237,8 +240,8 @@ class AsynSerial(AsynConn):
options = dict((kv.split('=') for kv in uri[1].split('+')))
except IndexError: # no uri[1], no options
options = {}
except ValueError:
raise ConfigError('illegal serial options')
except ValueError as e:
raise ConfigError('illegal serial options') from e
parity = options.pop('parity', None) # only parity is to be treated as text
for k, v in options.items():
try:
@ -251,14 +254,12 @@ class AsynSerial(AsynConn):
if not fullname.startswith(name):
raise ConfigError('illegal parity: %s' % parity)
options['parity'] = name[0]
if 'timeout' in options:
options['timeout'] = float(self.timeout)
else:
if 'timeout' not in options:
options['timeout'] = self.timeout
try:
self.connection = Serial(dev, **options)
except ValueError as e:
raise ConfigError(e)
raise ConfigError(e) from None
# TODO: turn exceptions into ConnectionFailedError, where a retry makes sense
def disconnect(self):