From 8f6e8eef4c4eca59a7592011bf07b18e382e18b4 Mon Sep 17 00:00:00 2001 From: Matthias Hagmann <16444067+MattHag@users.noreply.github.com> Date: Sat, 2 Mar 2024 16:59:35 +0100 Subject: [PATCH] Refactor: Move Device instantiation to factory class Related #2273 --- lib/logitech_receiver/device.py | 36 +++++++++++++++++---------------- lib/solaar/cli/__init__.py | 2 +- lib/solaar/listener.py | 2 +- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/lib/logitech_receiver/device.py b/lib/logitech_receiver/device.py index 211d257b17..75b3d5ee96 100644 --- a/lib/logitech_receiver/device.py +++ b/lib/logitech_receiver/device.py @@ -36,6 +36,25 @@ _IR = hidpp10_constants.INFO_SUBREGISTERS +class DeviceFactory: + @staticmethod + def create_device(device_info, setting_callback=None): + """Opens a Logitech Device found attached to the machine, by Linux device path. + :returns: An open file handle for the found receiver, or None. + """ + try: + handle = base.open_path(device_info.path) + if handle: + # a direct connected device might not be online (as reported by user) + return Device(None, None, None, handle=handle, device_info=device_info, setting_callback=setting_callback) + except OSError as e: + logger.exception("open %s", device_info) + if e.errno == _errno.EACCES: + raise + except Exception: + logger.exception("open %s", device_info) + + class Device: instances = [] read_register = hidpp10.read_register @@ -418,23 +437,6 @@ def ping(self): def notify_devices(self): # no need to notify, as there are none pass - @classmethod - def open(self, device_info, setting_callback=None): - """Opens a Logitech Device found attached to the machine, by Linux device path. - :returns: An open file handle for the found receiver, or None. - """ - try: - handle = base.open_path(device_info.path) - if handle: - # a direct connected device might not be online (as reported by user) - return Device(None, None, None, handle=handle, device_info=device_info, setting_callback=setting_callback) - except OSError as e: - logger.exception("open %s", device_info) - if e.errno == _errno.EACCES: - raise - except Exception: - logger.exception("open %s", device_info) - def close(self): handle, self.handle = self.handle, None if self in Device.instances: diff --git a/lib/solaar/cli/__init__.py b/lib/solaar/cli/__init__.py index 282155c2e1..01f2b13b3b 100644 --- a/lib/solaar/cli/__init__.py +++ b/lib/solaar/cli/__init__.py @@ -126,7 +126,7 @@ def _receivers_and_devices(dev_path=None): continue try: if dev_info.isDevice: - d = _device.Device.open(dev_info) + d = _device.DeviceFactory.create_device(dev_info) else: d = _receiver.ReceiverFactory.create_receiver(dev_info) diff --git a/lib/solaar/listener.py b/lib/solaar/listener.py index 65c36721a6..6b443497e4 100644 --- a/lib/solaar/listener.py +++ b/lib/solaar/listener.py @@ -246,7 +246,7 @@ def _start(device_info): if not isDevice: receiver = _receiver.ReceiverFactory.create_receiver(device_info, _setting_callback) else: - receiver = _device.Device.open(device_info, _setting_callback) + receiver = _device.DeviceFactory.create_device(device_info, _setting_callback) configuration.attach_to(receiver) if receiver: