From 644ded703866be394b8577d2dbc34c9893c46a0b Mon Sep 17 00:00:00 2001 From: Scott Petersen Date: Mon, 12 Dec 2016 14:02:43 -0800 Subject: [PATCH] Hack for existing installs without python-future. --- alarmdecoder/decoder.py | 7 +++++-- alarmdecoder/devices.py | 34 +++++++++++++++++++++++++++------- alarmdecoder/messages.py | 5 ++++- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/alarmdecoder/decoder.py b/alarmdecoder/decoder.py index 4f98690..057fb49 100644 --- a/alarmdecoder/decoder.py +++ b/alarmdecoder/decoder.py @@ -9,7 +9,10 @@ Provides the main AlarmDecoder class. import time import re -from builtins import chr +try: + from builtins import chr +except ImportError: + pass from .event import event from .util import InvalidMessageError @@ -456,7 +459,7 @@ class AlarmDecoder(object): def _handle_version(self, data): """ Handles received version data. - + :param data: Version string to parse :type data: string """ diff --git a/alarmdecoder/devices.py b/alarmdecoder/devices.py index 75221fb..3df9733 100644 --- a/alarmdecoder/devices.py +++ b/alarmdecoder/devices.py @@ -21,7 +21,7 @@ import serial import serial.tools.list_ports import socket import select -from builtins import bytes +import sys from .util import CommError, TimeoutError, NoDeviceError, InvalidMessageError from .event import event @@ -64,6 +64,20 @@ except ImportError: have_openssl = False +def bytes_hack(buf): + """ + Hacky workaround for old installs of the library on systems without python-future that were + keeping the 2to3 update from working after auto-update. + """ + ub = None + if sys.version_info > (3,): + ub = buf + else: + ub = bytes(buf) + + return ub + + class Device(object): """ Base class for all `AlarmDecoder`_ (AD2) device types. @@ -550,9 +564,11 @@ class USBDevice(Device): buf = self._device.read_data(1) if buf != b'': - self._buffer += buf + ub = bytes_hack(buf) + + self._buffer += ub - if buf == b"\n": + if ub == b"\n": self._buffer = self._buffer.rstrip(b"\r\n") if len(self._buffer) > 0: @@ -846,9 +862,11 @@ class SerialDevice(Device): # NOTE: AD2SERIAL apparently sends down \xFF on boot. if buf != b'' and buf != b"\xff": - self._buffer += buf + ub = bytes_hack(buf) - if buf == b"\n": + self._buffer += ub + + if ub == b"\n": self._buffer = self._buffer.rstrip(b"\r\n") if len(self._buffer) > 0: @@ -1149,9 +1167,11 @@ class SocketDevice(Device): buf = self._device.recv(1) if buf != b'': - self._buffer += buf + ub = bytes_hack(buf) + + self._buffer += ub - if buf == b"\n": + if ub == b"\n": self._buffer = self._buffer.rstrip(b"\r\n") if len(self._buffer) > 0: diff --git a/alarmdecoder/messages.py b/alarmdecoder/messages.py index 0e07d40..d1bd0e1 100644 --- a/alarmdecoder/messages.py +++ b/alarmdecoder/messages.py @@ -15,7 +15,10 @@ devices. import re import datetime -from reprlib import repr +try: + from reprlib import repr +except ImportError: + from repr import repr from .util import InvalidMessageError from .panels import PANEL_TYPES, ADEMCO, DSC