Remove factory wrapper classes

A module level function is sufficient, no wrapper needed.
This commit is contained in:
MattHag 2024-09-28 20:05:11 +02:00 committed by Peter F. Patel-Schneider
parent ef6b7dec2c
commit cba3533869
6 changed files with 67 additions and 71 deletions

View File

@ -60,9 +60,7 @@ class LowLevelInterface(Protocol):
...
class DeviceFactory:
@staticmethod
def create_device(low_level: LowLevelInterface, device_info, setting_callback=None):
def create_device(low_level: LowLevelInterface, 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.
"""

View File

@ -512,9 +512,7 @@ receiver_class_mapping = {
}
class ReceiverFactory:
@staticmethod
def create_receiver(low_level: LowLevelInterface, device_info, setting_callback=None) -> Optional[Receiver]:
def create_receiver(low_level: LowLevelInterface, device_info, setting_callback=None) -> Optional[Receiver]:
"""Opens a Logitech Receiver found attached to the machine, by Linux device path."""
try:

View File

@ -106,7 +106,7 @@ def _receivers(dev_path=None):
if dev_path is not None and dev_path != dev_info.path:
continue
try:
r = receiver.ReceiverFactory.create_receiver(base, dev_info)
r = receiver.create_receiver(base, dev_info)
if logger.isEnabledFor(logging.DEBUG):
logger.debug("[%s] => %s", dev_info.path, r)
if r:
@ -122,9 +122,9 @@ def _receivers_and_devices(dev_path=None):
continue
try:
if dev_info.isDevice:
d = device.DeviceFactory.create_device(base, dev_info)
d = device.create_device(base, dev_info)
else:
d = receiver.ReceiverFactory.create_receiver(base, dev_info)
d = receiver.create_receiver(base, dev_info)
if logger.isEnabledFor(logging.DEBUG):
logger.debug("[%s] => %s", dev_info.path, d)

View File

@ -255,9 +255,9 @@ def _start(device_info):
assert _status_callback and _setting_callback
isDevice = device_info.isDevice
if not isDevice:
receiver_ = logitech_receiver.receiver.ReceiverFactory.create_receiver(base, device_info, _setting_callback)
receiver_ = logitech_receiver.receiver.create_receiver(base, device_info, _setting_callback)
else:
receiver_ = logitech_receiver.device.DeviceFactory.create_device(base, device_info, _setting_callback)
receiver_ = logitech_receiver.device.create_device(base, device_info, _setting_callback)
if receiver_:
configuration.attach_to(receiver_)
if receiver_.bluetooth and receiver_.hid_serial:

View File

@ -82,12 +82,12 @@ def test_create_device(device_info, responses, expected_success):
low_level_mock = LowLevelInterfaceFake(responses)
if expected_success is None:
with pytest.raises(PermissionError):
device.DeviceFactory.create_device(low_level_mock, device_info)
device.create_device(low_level_mock, device_info)
elif not expected_success:
with pytest.raises(TypeError):
device.DeviceFactory.create_device(low_level_mock, device_info)
device.create_device(low_level_mock, device_info)
else:
test_device = device.DeviceFactory.create_device(low_level_mock, device_info)
test_device = device.create_device(low_level_mock, device_info)
assert bool(test_device) == expected_success
@ -98,7 +98,7 @@ def test_create_device(device_info, responses, expected_success):
def test_device_name(device_info, responses, expected_codename, expected_name, expected_kind):
low_level = LowLevelInterfaceFake(responses)
test_device = device.DeviceFactory.create_device(low_level, device_info)
test_device = device.create_device(low_level, device_info)
assert test_device.codename == expected_codename
assert test_device.name == expected_name

View File

@ -133,12 +133,12 @@ def test_receiver_factory_create_receiver(device_info, responses, handle, serial
if handle is False:
with pytest.raises(Exception): # noqa: B017
receiver.ReceiverFactory.create_receiver(mock_low_level, device_info, lambda x: x)
receiver.create_receiver(mock_low_level, device_info, lambda x: x)
elif handle is None:
r = receiver.ReceiverFactory.create_receiver(mock_low_level, device_info, lambda x: x)
r = receiver.create_receiver(mock_low_level, device_info, lambda x: x)
assert r is None
else:
r = receiver.ReceiverFactory.create_receiver(mock_low_level, device_info, lambda x: x)
r = receiver.create_receiver(mock_low_level, device_info, lambda x: x)
assert r.handle == handle
assert r.serial == serial
assert r.max_devices == max_devices
@ -155,7 +155,7 @@ def test_receiver_factory_create_receiver(device_info, responses, handle, serial
def test_receiver_factory_props(device_info, responses, firmware, codename, remaining_pairings, pairing_info, count):
mock_low_level = LowLevelInterfaceFake(responses)
r = receiver.ReceiverFactory.create_receiver(mock_low_level, device_info, lambda x: x)
r = receiver.create_receiver(mock_low_level, device_info, lambda x: x)
assert len(r.firmware) == firmware if firmware is not None else firmware is None
assert r.device_codename(2) == codename
@ -175,7 +175,7 @@ def test_receiver_factory_props(device_info, responses, firmware, codename, rema
def test_receiver_factory_string(device_info, responses, status_str, strng):
mock_low_level = LowLevelInterfaceFake(responses)
r = receiver.ReceiverFactory.create_receiver(mock_low_level, device_info, lambda x: x)
r = receiver.create_receiver(mock_low_level, device_info, lambda x: x)
assert r.status_string() == status_str
assert str(r) == strng
@ -191,7 +191,7 @@ def test_receiver_factory_string(device_info, responses, status_str, strng):
def test_receiver_factory_no_device(device_info, responses):
mock_low_level = LowLevelInterfaceFake(responses)
r = receiver.ReceiverFactory.create_receiver(mock_low_level, device_info, lambda x: x)
r = receiver.create_receiver(mock_low_level, device_info, lambda x: x)
with pytest.raises(exceptions.NoSuchDevice):
r.device_pairing_information(1)