From d69a21056c992834b356b572004ba84c840b73aa Mon Sep 17 00:00:00 2001 From: Scott Petersen Date: Tue, 28 Feb 2017 12:59:17 -0800 Subject: [PATCH] A few more fixes for supporting Python 2 and 3. --- alarmdecoder/devices.py | 21 ++++++++++++++------- alarmdecoder/util.py | 8 ++++---- bin/ad2-firmwareupload | 13 +++++++------ setup.py | 2 +- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/alarmdecoder/devices.py b/alarmdecoder/devices.py index 3df9733..4345a60 100644 --- a/alarmdecoder/devices.py +++ b/alarmdecoder/devices.py @@ -800,6 +800,10 @@ class SerialDevice(Device): :raises: py:class:`~alarmdecoder.util.CommError` """ try: + # Hack to support unicode under Python 2.x + if isinstance(data, str) or (sys.version_info < (3,) and isinstance(data, unicode)): + data = data.encode('utf-8') + self._device.write(data) except serial.SerialTimeoutException: @@ -821,7 +825,7 @@ class SerialDevice(Device): ret = None try: - ret = self._device.read(1) + ret = self._device.read(1).decode('utf-8') except serial.SerialException as err: raise CommError('Error reading from device: {0}'.format(str(err)), err) @@ -890,7 +894,7 @@ class SerialDevice(Device): finally: timer.cancel() - return ret + return ret.decode('utf-8') def purge(self): """ @@ -1096,6 +1100,9 @@ class SocketDevice(Device): data_sent = None try: + if isinstance(data, str): + data = data.encode('utf-8') + data_sent = self._device.send(data) if data_sent == 0: @@ -1118,7 +1125,7 @@ class SocketDevice(Device): data = None try: - read_ready, _, _ = select.select([self._device], [], [], 0) + read_ready, _, _ = select.select([self._device], [], []) if (len(read_ready) != 0): data = self._device.recv(1) @@ -1126,7 +1133,7 @@ class SocketDevice(Device): except socket.error as err: raise CommError('Error while reading from device: {0}'.format(str(err)), err) - return data + return data.decode('utf-8') def read_line(self, timeout=0.0, purge_buffer=False): """ @@ -1158,7 +1165,7 @@ class SocketDevice(Device): try: while timeout_event.reading: - read_ready, _, _ = select.select([self._device], [], [], 0) + read_ready, _, _ = select.select([self._device], [], []) if (len(read_ready) == 0): time.sleep(0.01) @@ -1166,7 +1173,7 @@ class SocketDevice(Device): buf = self._device.recv(1) - if buf != b'': + if buf != b'' and buf != b"\xff": ub = bytes_hack(buf) self._buffer += ub @@ -1203,7 +1210,7 @@ class SocketDevice(Device): finally: timer.cancel() - return ret + return ret.decode('utf-8') def purge(self): """ diff --git a/alarmdecoder/util.py b/alarmdecoder/util.py index a57c911..94d64d4 100644 --- a/alarmdecoder/util.py +++ b/alarmdecoder/util.py @@ -94,7 +94,7 @@ class Firmware(object): if line[0] == ':': dev.write(line + "\r") - response = dev.read_line(timeout=5.0, purge_buffer=True) + response = dev.read_line(timeout=5.0, purge_buffer=True) #.decode('utf-8') if debug: stage_callback(Firmware.STAGE_DEBUG, data="line={0} - line={1} response={2}".format(line_cnt, line, response)); @@ -135,7 +135,7 @@ class Firmware(object): while timeout_event.reading: try: - char = dev.read() + char = dev.read() #.decode('utf-8') if char is not None and char != '': if char == pattern[position]: @@ -179,13 +179,13 @@ class Firmware(object): try: stage_callback(Firmware.STAGE_BOOT) dev.write("=") - read_until('!boot', timeout=15.0) + read_until(u'!boot', timeout=15.0) # Get ourselves into the boot loader and wait for indication # that it's ready for the firmware upload. stage_callback(Firmware.STAGE_LOAD) dev.write("=") - read_until('!load', timeout=15.0) + read_until(u'!load', timeout=15.0) except TimeoutError as err: retry -= 1 diff --git a/bin/ad2-firmwareupload b/bin/ad2-firmwareupload index 507bb5e..228312c 100755 --- a/bin/ad2-firmwareupload +++ b/bin/ad2-firmwareupload @@ -2,6 +2,7 @@ import os import sys, time +import traceback import alarmdecoder def handle_firmware(stage, **kwargs): @@ -68,12 +69,12 @@ def main(): time.sleep(3) alarmdecoder.util.Firmware.upload(dev, firmware, handle_firmware, debug=debug) - except alarmdecoder.util.NoDeviceError, ex: - print "Error: Could not find device: {0}".format(ex) - except alarmdecoder.util.UploadError, ex: - print "Error: Error uploading firmware: {0}".format(ex) - except Exception, ex: - print "Error: {0}".format(ex) + except alarmdecoder.util.NoDeviceError as ex: + print("Error: Could not find device: {0}".format(ex)) + except alarmdecoder.util.UploadError as ex: + print("Error: Error uploading firmware: {0}".format(ex)) + except Exception as ex: + print("Error: {0}: {1}".format(ex, traceback.format_exc())) finally: if dev is not None: dev.close() diff --git a/setup.py b/setup.py index e053e1b..1d907c9 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ if sys.version_info < (3,): extra_requirements.append('future==0.14.3') setup(name='alarmdecoder', - version='0.10.3', + version='0.12.2', description='Python interface for the AlarmDecoder (AD2) family ' 'of alarm devices which includes the AD2USB, AD2SERIAL and AD2PI.', long_description=readme(),