diff options
| author | Chris Liechti <cliechti@gmx.net> | 2015-08-30 21:28:04 +0200 |
|---|---|---|
| committer | Chris Liechti <cliechti@gmx.net> | 2015-08-30 21:28:04 +0200 |
| commit | 033f17c9d5c40fe8b5e55b6ada1d483b612ca092 (patch) | |
| tree | 5ada169ba7c6a8036357904772075467cda8d4ea /serial/urlhandler | |
| parent | 518b0d31aafdb9ea52a47eb850490d652af2ad96 (diff) | |
| download | pyserial-git-033f17c9d5c40fe8b5e55b6ada1d483b612ca092.tar.gz | |
pep-8 and small cleanups
Diffstat (limited to 'serial/urlhandler')
| -rw-r--r-- | serial/urlhandler/protocol_hwgrep.py | 3 | ||||
| -rw-r--r-- | serial/urlhandler/protocol_loop.py | 36 | ||||
| -rw-r--r-- | serial/urlhandler/protocol_socket.py | 38 | ||||
| -rw-r--r-- | serial/urlhandler/protocol_spy.py | 1 |
4 files changed, 44 insertions, 34 deletions
diff --git a/serial/urlhandler/protocol_hwgrep.py b/serial/urlhandler/protocol_hwgrep.py index 90626ff..e184ec5 100644 --- a/serial/urlhandler/protocol_hwgrep.py +++ b/serial/urlhandler/protocol_hwgrep.py @@ -20,6 +20,7 @@ try: except NameError: basestring = str # python 3 + class Serial(serial.Serial): """Just inherit the native Serial port implementation and patch the port property.""" @@ -43,8 +44,6 @@ class Serial(serial.Serial): # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if __name__ == '__main__': - #~ s = Serial('hwgrep://ttyS0') s = Serial(None) s.port = 'hwgrep://ttyS0' print(s) - diff --git a/serial/urlhandler/protocol_loop.py b/serial/urlhandler/protocol_loop.py index 79d2c56..f5104b4 100644 --- a/serial/urlhandler/protocol_loop.py +++ b/serial/urlhandler/protocol_loop.py @@ -17,7 +17,6 @@ # - "debug" print diagnostic messages import logging import numbers -import threading import time try: import urlparse @@ -91,13 +90,6 @@ class Serial(SerialBase): if self.logger: self.logger.info('_reconfigure_port()') - def close(self): - """Close port""" - if self.is_open: - self.is_open = False - # in case of quick reconnects, give the server some time - time.sleep(0.3) - def from_url(self, url): """extract host and port from an URL string""" parts = urlparse.urlsplit(url) @@ -121,7 +113,8 @@ class Serial(SerialBase): @property def in_waiting(self): """Return the number of bytes currently in the input buffer.""" - if not self.is_open: raise portNotOpenError + if not self.is_open: + raise portNotOpenError if self.logger: # attention the logged value can differ from return value in # threaded environments... @@ -134,7 +127,8 @@ class Serial(SerialBase): return less characters as requested. With no timeout it will block until the requested number of bytes is read. """ - if not self.is_open: raise portNotOpenError + if not self.is_open: + raise portNotOpenError if self._timeout is not None: timeout = time.time() + self._timeout else: @@ -142,7 +136,7 @@ class Serial(SerialBase): data = bytearray() while size > 0 and self.is_open: try: - data += self.queue.get(timeout=self._timeout) # XXX inter char timeout + data += self.queue.get(timeout=self._timeout) # XXX inter char timeout except queue.Empty: break else: @@ -159,14 +153,15 @@ class Serial(SerialBase): connection is blocked. May raise SerialException if the connection is closed. """ - if not self.is_open: raise portNotOpenError + if not self.is_open: + raise portNotOpenError data = to_bytes(data) # calculate aprox time that would be used to send the data time_used_to_send = 10.0*len(data) / self._baudrate # when a write timeout is configured check if we would be successful # (not sending anything, not even the part that would have time) if self._write_timeout is not None and time_used_to_send > self._write_timeout: - time.sleep(self._write_timeout) # must wait so that unit test succeeds + time.sleep(self._write_timeout) # must wait so that unit test succeeds raise writeTimeoutError for byte in iterbytes(data): self.queue.put(byte, timeout=self._write_timeout) @@ -174,7 +169,8 @@ class Serial(SerialBase): def reset_input_buffer(self): """Clear input buffer, discarding all that is in the buffer.""" - if not self.is_open: raise portNotOpenError + if not self.is_open: + raise portNotOpenError if self.logger: self.logger.info('reset_input_buffer()') try: @@ -188,7 +184,8 @@ class Serial(SerialBase): Clear output buffer, aborting the current output and discarding all that is in the buffer. """ - if not self.is_open: raise portNotOpenError + if not self.is_open: + raise portNotOpenError if self.logger: self.logger.info('reset_output_buffer()') try: @@ -218,7 +215,8 @@ class Serial(SerialBase): @property def cts(self): """Read terminal status line: Clear To Send""" - if not self.is_open: raise portNotOpenError + if not self.is_open: + raise portNotOpenError if self.logger: self.logger.info('CTS -> state of RTS (%r)' % (self._rts_state,)) return self._rts_state @@ -233,7 +231,8 @@ class Serial(SerialBase): @property def ri(self): """Read terminal status line: Ring Indicator""" - if not self.is_open: raise portNotOpenError + if not self.is_open: + raise portNotOpenError if self.logger: self.logger.info('returning dummy for RI') return False @@ -241,7 +240,8 @@ class Serial(SerialBase): @property def cd(self): """Read terminal status line: Carrier Detect""" - if not self.is_open: raise portNotOpenError + if not self.is_open: + raise portNotOpenError if self.logger: self.logger.info('returning dummy for CD') return True diff --git a/serial/urlhandler/protocol_socket.py b/serial/urlhandler/protocol_socket.py index 99e532f..a4297bb 100644 --- a/serial/urlhandler/protocol_socket.py +++ b/serial/urlhandler/protocol_socket.py @@ -27,7 +27,7 @@ try: except ImportError: import urllib.parse as urlparse -from serial.serialutil import * +from serial.serialutil import SerialBase, SerialException, portNotOpenError, to_bytes # map log level names to constants. used in from_url() LOGGER_LEVELS = { @@ -39,6 +39,7 @@ LOGGER_LEVELS = { POLL_TIMEOUT = 2 + class Serial(SerialBase): """Serial port implementation for plain sockets.""" @@ -61,7 +62,7 @@ class Serial(SerialBase): self._socket = None raise SerialException("Could not open port %s: %s" % (self.portstr, msg)) - self._socket.settimeout(POLL_TIMEOUT) # used for write timeout support :/ + self._socket.settimeout(POLL_TIMEOUT) # used for write timeout support :/ # not that there anything to configure... self._reconfigure_port() @@ -116,7 +117,8 @@ class Serial(SerialBase): raise ValueError('unknown option: %r' % (option,)) # get host and port host, port = parts.hostname, parts.port - if not 0 <= port < 65536: raise ValueError("port not in range 0...65535") + if not 0 <= port < 65536: + raise ValueError("port not in range 0...65535") except ValueError as e: raise SerialException('expected a string in the form "socket://<host>:<port>[?logging={debug|info|warning|error}]": %s' % e) return (host, port) @@ -126,7 +128,8 @@ class Serial(SerialBase): @property def in_waiting(self): """Return the number of bytes currently in the input buffer.""" - if not self.is_open: raise portNotOpenError + if not self.is_open: + raise portNotOpenError # Poll the socket to see if it is ready for reading. # If ready, at least one byte will be to read. lr, lw, lx = select.select([self._socket], [], [], 0) @@ -138,7 +141,8 @@ class Serial(SerialBase): return less characters as requested. With no timeout it will block until the requested number of bytes is read. """ - if not self.is_open: raise portNotOpenError + if not self.is_open: + raise portNotOpenError data = bytearray() if self._timeout is not None: timeout = time.time() + self._timeout @@ -171,7 +175,8 @@ class Serial(SerialBase): connection is blocked. May raise SerialException if the connection is closed. """ - if not self.is_open: raise portNotOpenError + if not self.is_open: + raise portNotOpenError try: self._socket.sendall(to_bytes(data)) except socket.error as e: @@ -181,7 +186,8 @@ class Serial(SerialBase): def reset_input_buffer(self): """Clear input buffer, discarding all that is in the buffer.""" - if not self.is_open: raise portNotOpenError + if not self.is_open: + raise portNotOpenError if self.logger: self.logger.info('ignored reset_input_buffer') @@ -190,7 +196,8 @@ class Serial(SerialBase): Clear output buffer, aborting the current output and discarding all that is in the buffer. """ - if not self.is_open: raise portNotOpenError + if not self.is_open: + raise portNotOpenError if self.logger: self.logger.info('ignored reset_output_buffer') @@ -199,7 +206,8 @@ class Serial(SerialBase): Send break condition. Timed, returns to idle state after given duration. """ - if not self.is_open: raise portNotOpenError + if not self.is_open: + raise portNotOpenError if self.logger: self.logger.info('ignored send_break(%r)' % (duration,)) @@ -222,7 +230,8 @@ class Serial(SerialBase): @property def cts(self): """Read terminal status line: Clear To Send""" - if not self.is_open: raise portNotOpenError + if not self.is_open: + raise portNotOpenError if self.logger: self.logger.info('returning dummy for cts') return True @@ -230,7 +239,8 @@ class Serial(SerialBase): @property def dsr(self): """Read terminal status line: Data Set Ready""" - if not self.is_open: raise portNotOpenError + if not self.is_open: + raise portNotOpenError if self.logger: self.logger.info('returning dummy for dsr') return True @@ -238,7 +248,8 @@ class Serial(SerialBase): @property def ri(self): """Read terminal status line: Ring Indicator""" - if not self.is_open: raise portNotOpenError + if not self.is_open: + raise portNotOpenError if self.logger: self.logger.info('returning dummy for ri') return False @@ -246,7 +257,8 @@ class Serial(SerialBase): @property def cd(self): """Read terminal status line: Carrier Detect""" - if not self.is_open: raise portNotOpenError + if not self.is_open: + raise portNotOpenError if self.logger: self.logger.info('returning dummy for cd)') return True diff --git a/serial/urlhandler/protocol_spy.py b/serial/urlhandler/protocol_spy.py index 9a7c75c..647b760 100644 --- a/serial/urlhandler/protocol_spy.py +++ b/serial/urlhandler/protocol_spy.py @@ -263,4 +263,3 @@ if __name__ == '__main__': s = Serial(None) s.port = 'spy:///dev/ttyS0' print(s) - |
