Skip to content

Commit

Permalink
Refactor: Move Device instantiation to factory class
Browse files Browse the repository at this point in the history
Related #2273
  • Loading branch information
MattHag authored and pfps committed Mar 3, 2024
1 parent 51e4405 commit 8f6e8ee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
36 changes: 19 additions & 17 deletions lib/logitech_receiver/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/solaar/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion lib/solaar/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 8f6e8ee

Please sign in to comment.