Refactor: Move Device instantiation to factory class

Related #2273
This commit is contained in:
Matthias Hagmann 2024-03-02 16:59:35 +01:00 committed by Peter F. Patel-Schneider
parent 51e44052b0
commit 8f6e8eef4c
3 changed files with 21 additions and 19 deletions

View File

@ -36,6 +36,25 @@ _R = hidpp10_constants.REGISTERS
_IR = hidpp10_constants.INFO_SUBREGISTERS _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: class Device:
instances = [] instances = []
read_register = hidpp10.read_register read_register = hidpp10.read_register
@ -418,23 +437,6 @@ class Device:
def notify_devices(self): # no need to notify, as there are none def notify_devices(self): # no need to notify, as there are none
pass 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): def close(self):
handle, self.handle = self.handle, None handle, self.handle = self.handle, None
if self in Device.instances: if self in Device.instances:

View File

@ -126,7 +126,7 @@ def _receivers_and_devices(dev_path=None):
continue continue
try: try:
if dev_info.isDevice: if dev_info.isDevice:
d = _device.Device.open(dev_info) d = _device.DeviceFactory.create_device(dev_info)
else: else:
d = _receiver.ReceiverFactory.create_receiver(dev_info) d = _receiver.ReceiverFactory.create_receiver(dev_info)

View File

@ -246,7 +246,7 @@ def _start(device_info):
if not isDevice: if not isDevice:
receiver = _receiver.ReceiverFactory.create_receiver(device_info, _setting_callback) receiver = _receiver.ReceiverFactory.create_receiver(device_info, _setting_callback)
else: else:
receiver = _device.Device.open(device_info, _setting_callback) receiver = _device.DeviceFactory.create_device(device_info, _setting_callback)
configuration.attach_to(receiver) configuration.attach_to(receiver)
if receiver: if receiver: