Refactor: Move Receiver instantiation to factory class

Related #2350
This commit is contained in:
Matthias Hagmann 2024-03-02 16:53:17 +01:00 committed by Peter F. Patel-Schneider
parent 5edf5e7419
commit 51e44052b0
3 changed files with 26 additions and 20 deletions

View File

@ -31,6 +31,25 @@ _R = hidpp10_constants.REGISTERS
_IR = hidpp10_constants.INFO_SUBREGISTERS
class ReceiverFactory:
@staticmethod
def create_receiver(device_info, setting_callback=None):
"""Opens a Logitech Receiver 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:
return Receiver(handle, device_info.path, device_info.product_id, 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 Receiver:
"""A Unifying Receiver instance.
@ -376,20 +395,3 @@ class Receiver:
__repr__ = __str__
__bool__ = __nonzero__ = lambda self: self.handle is not None
@classmethod
def open(self, device_info, setting_callback=None):
"""Opens a Logitech Receiver 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:
return Receiver(handle, device_info.path, device_info.product_id, 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)

View File

@ -110,7 +110,7 @@ def _receivers(dev_path=None):
if dev_path is not None and dev_path != dev_info.path:
continue
try:
r = _receiver.Receiver.open(dev_info)
r = _receiver.ReceiverFactory.create_receiver(dev_info)
if logger.isEnabledFor(logging.DEBUG):
logger.debug("[%s] => %s", dev_info.path, r)
if r:
@ -125,7 +125,11 @@ def _receivers_and_devices(dev_path=None):
if dev_path is not None and dev_path != dev_info.path:
continue
try:
d = _device.Device.open(dev_info) if dev_info.isDevice else _receiver.Receiver.open(dev_info)
if dev_info.isDevice:
d = _device.Device.open(dev_info)
else:
d = _receiver.ReceiverFactory.create_receiver(dev_info)
if logger.isEnabledFor(logging.DEBUG):
logger.debug("[%s] => %s", dev_info.path, d)
if d is not None:

View File

@ -244,7 +244,7 @@ def _start(device_info):
assert _status_callback and _setting_callback
isDevice = device_info.isDevice
if not isDevice:
receiver = _receiver.Receiver.open(device_info, _setting_callback)
receiver = _receiver.ReceiverFactory.create_receiver(device_info, _setting_callback)
else:
receiver = _device.Device.open(device_info, _setting_callback)
configuration.attach_to(receiver)