Skip to content

Commit

Permalink
Refactor: Move Receiver instantiation to factory class
Browse files Browse the repository at this point in the history
Related #2350
  • Loading branch information
MattHag committed Mar 2, 2024
1 parent 7d127ff commit 042907b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
36 changes: 19 additions & 17 deletions lib/logitech_receiver/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@
_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.
Expand Down Expand Up @@ -376,20 +395,3 @@ def __str__(self):
__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)
8 changes: 6 additions & 2 deletions lib/solaar/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/solaar/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 042907b

Please sign in to comment.