From d6b18cd426c1d189cc72e147e36015027e6cb151 Mon Sep 17 00:00:00 2001 From: Daniel Pavel Date: Thu, 29 Nov 2012 12:34:20 +0200 Subject: [PATCH] python 3 fixes --- app/listener.py | 2 +- app/ui/__init__.py | 8 ++++---- app/ui/action.py | 6 +++--- bin/hidconsole | 2 +- bin/scan | 2 +- bin/solaar | 2 +- lib/logitech/scanner.py | 2 +- lib/logitech/unifying_receiver/base.py | 24 ++++++++++++---------- lib/logitech/unifying_receiver/common.py | 2 +- lib/logitech/unifying_receiver/listener.py | 2 +- 10 files changed, 27 insertions(+), 25 deletions(-) diff --git a/app/listener.py b/app/listener.py index b130223e..eb3710ef 100644 --- a/app/listener.py +++ b/app/listener.py @@ -16,7 +16,7 @@ class _DUMMY_RECEIVER(object): __slots__ = ['name', 'max_devices', 'status'] name = _lur.Receiver.name max_devices = _lur.Receiver.max_devices - status = 'Receiver not found' + status = 'Receiver not found.' __bool__ = __nonzero__ = lambda self: False __str__ = lambda self: 'DUMMY' DUMMY = _DUMMY_RECEIVER() diff --git a/app/ui/__init__.py b/app/ui/__init__.py index 7f8d530e..5045ee7a 100644 --- a/app/ui/__init__.py +++ b/app/ui/__init__.py @@ -7,11 +7,11 @@ GObject.threads_init() from solaar import NAME -_APP_ICONS = (NAME + '-fail', NAME + '-init', NAME) +_APP_ICONS = (NAME + '-init', NAME + '-fail', NAME) def appicon(receiver_status): - return (_APP_ICONS[0] if receiver_status < 0 else - _APP_ICONS[1] if receiver_status < 1 else - _APP_ICONS[2]) + return (_APP_ICONS[1] if type(receiver_status) == str else + _APP_ICONS[2] if receiver_status else + _APP_ICONS[0]) _ICON_THEME = Gtk.IconTheme.get_default() diff --git a/app/ui/action.py b/app/ui/action.py index 87d58e79..355320ec 100644 --- a/app/ui/action.py +++ b/app/ui/action.py @@ -47,13 +47,13 @@ def _show_about_window(action): about.set_comments('Shows status of devices connected\nto a Logitech Unifying Receiver.') about.set_license_type(Gtk.License.GPL_2_0) - about.set_copyright('\xC2\xA9 2012 Daniel Pavel') + about.set_copyright(b'\xC2\xA9'.decode('utf-8') + ' 2012 Daniel Pavel') about.set_authors(('Daniel Pavel http://github.com/pwr',)) try: about.add_credit_section('Testing', ('Douglas Wagner',)) - except Exception as e: - print e + except Exception: + pass about.set_website('http://github.com/pwr/Solaar/wiki') about.set_website_label('Solaar Wiki') diff --git a/bin/hidconsole b/bin/hidconsole index 6c8e21d2..35909236 100755 --- a/bin/hidconsole +++ b/bin/hidconsole @@ -4,5 +4,5 @@ Z=`readlink -f "$0"` LIB=`readlink -f $(dirname "$Z")/../lib` export PYTHONPATH=$LIB -PYTHON=`which python python2 python3 | head -n 1` +PYTHON=${PYTHON:-`which python python2 python3 | head -n 1`} exec $PYTHON -u -m hidapi.hidconsole "$@" diff --git a/bin/scan b/bin/scan index 0bfcbd04..099df44b 100755 --- a/bin/scan +++ b/bin/scan @@ -4,5 +4,5 @@ Z=`readlink -f "$0"` LIB=`readlink -f $(dirname "$Z")/../lib` export PYTHONPATH=$LIB -PYTHON=`which python python2 python3 | head -n 1` +PYTHON=${PYTHON:-`which python python2 python3 | head -n 1`} exec $PYTHON -u -m logitech.scanner "$@" diff --git a/bin/solaar b/bin/solaar index 597768ee..ca7f4bdd 100755 --- a/bin/solaar +++ b/bin/solaar @@ -8,5 +8,5 @@ SHARE=`readlink -f $(dirname "$Z")/../share` export PYTHONPATH=$APP:$LIB export XDG_DATA_DIRS=${SHARE}_override:$SHARE:$XDG_DATA_DIRS -PYTHON=`which python python2 python3 | head -n 1` +PYTHON=${PYTHON:-`which python python2 python3 | head -n 1`} exec $PYTHON -u -m solaar "$@" diff --git a/lib/logitech/scanner.py b/lib/logitech/scanner.py index 3bf3f79b..08c9dbaf 100644 --- a/lib/logitech/scanner.py +++ b/lib/logitech/scanner.py @@ -19,7 +19,7 @@ def print_receiver(receiver): print (" Reported %d paired device(s)." % len(receiver)) activity = receiver.request(0x83B3) if activity: - activity = [(d, ord(activity[d - 1])) for d in range(1, receiver.max_devices)] + activity = [(d, ord(activity[d - 1:d])) for d in range(1, receiver.max_devices)] print(" Device activity counters: %s" % ', '.join(('%d=%d' % (d, a)) for d, a in activity if a > 0)) def scan_devices(receiver): diff --git a/lib/logitech/unifying_receiver/base.py b/lib/logitech/unifying_receiver/base.py index 76b9b12b..706c4239 100644 --- a/lib/logitech/unifying_receiver/base.py +++ b/lib/logitech/unifying_receiver/base.py @@ -12,8 +12,8 @@ _log = getLogger('LUR.base') del getLogger from .common import strhex as _strhex, KwException as _KwException -import hidpp10 as _hidpp10 -import hidpp20 as _hidpp20 +from . import hidpp10 as _hidpp10 +from . import hidpp20 as _hidpp20 import hidapi as _hid # @@ -45,7 +45,7 @@ class NoReceiver(_KwException): class NoSuchDevice(_KwException): """Raised when trying to reach a device number not paired to the receiver.""" - pass + pass class DeviceUnreachable(_KwException): @@ -135,14 +135,14 @@ def write(handle, devnumber, data): else: wdata = _pack('!BB5s', 0x10, devnumber, data) if _log.isEnabledFor(_DEBUG): - _log.debug("(%s) <= w[%02X %02X %s %s]", handle, ord(wdata[0]), devnumber, _strhex(wdata[2:4]), _strhex(wdata[4:])) + _log.debug("(%s) <= w[%02X %02X %s %s]", handle, ord(wdata[:1]), devnumber, _strhex(wdata[2:4]), _strhex(wdata[4:])) try: _hid.write(int(handle), wdata) except Exception as reason: _log.error("write failed, assuming handle %s no longer available", repr(handle)) close(handle) - raise NoReceiver(reason) + raise NoReceiver(reason=reason) def read(handle, timeout=DEFAULT_TIMEOUT): @@ -167,7 +167,7 @@ def _read(handle, timeout): except Exception as reason: _log.error("read failed, assuming handle %s no longer available", repr(handle)) close(handle) - raise NoReceiver(reason) + raise NoReceiver(reason=reason) if data: report_id = ord(data[:1]) @@ -192,7 +192,7 @@ def _skip_incoming(handle): except Exception as reason: _log.error("read failed, assuming receiver %s no longer available", handle) close(handle) - raise NoReceiver(reason) + raise NoReceiver(reason=reason) if data: report_id = ord(data[:1]) @@ -320,7 +320,8 @@ def request(handle, devnumber, request_id, *params): if delta >= timeout: _log.warn("timeout on device %d request {%04X} params[%s]", devnumber, request_id, _strhex(params)) - raise DeviceUnreachable(number=devnumber, request=request_id) + break + # raise DeviceUnreachable(number=devnumber, request=request_id) def ping(handle, devnumber): @@ -362,14 +363,15 @@ def ping(handle, devnumber): return 1.0 if error == _hidpp10.ERROR.resource_error: # device unreachable - raise DeviceUnreachable(number=devnumber, request=request_id) + # raise DeviceUnreachable(number=devnumber, request=request_id) + break if error == _hidpp10.ERROR.unknown_device: # no paired device with that number _log.error("(%s) device %d error on ping request: unknown device", handle, devnumber) - raise NoSuchDevice(devnumber) + raise NoSuchDevice(number=devnumber, request=request_id) _unhandled(report_id, number, data) if delta >= _PING_TIMEOUT: _log.warn("(%s) timeout on device %d ping", handle, devnumber) - raise DeviceUnreachable(number=devnumber, request=request_id) + # raise DeviceUnreachable(number=devnumber, request=request_id) diff --git a/lib/logitech/unifying_receiver/common.py b/lib/logitech/unifying_receiver/common.py index 94adc35b..199a569d 100644 --- a/lib/logitech/unifying_receiver/common.py +++ b/lib/logitech/unifying_receiver/common.py @@ -8,7 +8,7 @@ from struct import pack as _pack class NamedInt(int): """An integer with an attached name.""" - __slots__ = ['name'] + # __slots__ = ['name'] def __new__(cls, value, name): obj = int.__new__(cls, value) diff --git a/lib/logitech/unifying_receiver/listener.py b/lib/logitech/unifying_receiver/listener.py index a8f6dd4d..79ee03b6 100644 --- a/lib/logitech/unifying_receiver/listener.py +++ b/lib/logitech/unifying_receiver/listener.py @@ -110,7 +110,7 @@ class EventsListener(_threading.Thread): self.has_started() - last_tick = _timestamp() if self.tick_period else 0 + last_tick = 0 while self._active: if self._queued_events.empty():