From 3935d94ab870b78810e220164920b27f5699f104 Mon Sep 17 00:00:00 2001 From: MattHag <16444067+MattHag@users.noreply.github.com> Date: Fri, 1 Nov 2024 21:02:42 +0100 Subject: [PATCH] Refactor: Completely remove UnsortedNamedInts Simplify the code by removing this unnecessary class. Replace with default NamedInts implementation. Related #2273 --- lib/logitech_receiver/common.py | 9 --------- lib/logitech_receiver/hidpp20.py | 2 +- lib/logitech_receiver/special_keys.py | 9 ++++----- lib/solaar/ui/diversion_rules.py | 7 +++---- tests/logitech_receiver/test_common.py | 13 ------------- tests/logitech_receiver/test_hidpp20_complex.py | 6 +++--- tests/logitech_receiver/test_setting_templates.py | 6 +++--- 7 files changed, 14 insertions(+), 38 deletions(-) diff --git a/lib/logitech_receiver/common.py b/lib/logitech_receiver/common.py index a55f8df169..309700b1f7 100644 --- a/lib/logitech_receiver/common.py +++ b/lib/logitech_receiver/common.py @@ -533,15 +533,6 @@ def flag_names(enum_class: Iterable, value: int) -> Generator[str]: yield f"unknown:{unknown_bits:06X}" -class UnsortedNamedInts(NamedInts): - def _sort_values(self): - pass - - def __or__(self, other): - c = UnsortedNamedInts if isinstance(other, UnsortedNamedInts) else NamedInts - return c(**self.__dict__, **other.__dict__) - - def strhex(x): assert x is not None """Produce a hex-string representation of a sequence of bytes.""" diff --git a/lib/logitech_receiver/hidpp20.py b/lib/logitech_receiver/hidpp20.py index b2fa67dd44..1331cbc604 100644 --- a/lib/logitech_receiver/hidpp20.py +++ b/lib/logitech_receiver/hidpp20.py @@ -257,7 +257,7 @@ def mapped_to(self) -> NamedInt: @property def remappable_to(self) -> common.NamedInts: self._device.keys._ensure_all_keys_queried() - ret = common.UnsortedNamedInts() + ret = common.NamedInts() if self.group_mask: # only keys with a non-zero gmask are remappable ret[self.default_task] = self.default_task # it should always be possible to map the key to itself for g in self.group_mask: diff --git a/lib/logitech_receiver/special_keys.py b/lib/logitech_receiver/special_keys.py index 281512ec52..e90e388f8e 100644 --- a/lib/logitech_receiver/special_keys.py +++ b/lib/logitech_receiver/special_keys.py @@ -24,7 +24,6 @@ import yaml from .common import NamedInts -from .common import UnsortedNamedInts _XDG_CONFIG_HOME = os.environ.get("XDG_CONFIG_HOME") or os.path.expanduser(os.path.join("~", ".config")) _keys_file_path = os.path.join(_XDG_CONFIG_HOME, "solaar", "keys.yaml") @@ -1233,7 +1232,7 @@ class CidGroup(IntEnum): HORIZONTAL_SCROLL._fallback = lambda x: f"unknown horizontal scroll:{x:04X}" # Construct universe for Persistent Remappable Keys setting (only for supported values) -KEYS = UnsortedNamedInts() +KEYS = NamedInts() KEYS_Default = 0x7FFFFFFF # Special value to reset key to default - has to be different from all others KEYS[KEYS_Default] = "Default" # Value to reset to default KEYS[0] = "None" # Value for no output @@ -1271,7 +1270,7 @@ class CidGroup(IntEnum): # Construct subsets for known devices def persistent_keys(action_ids): - keys = UnsortedNamedInts() + keys = NamedInts() keys[KEYS_Default] = "Default" # Value to reset to default keys[0] = "No Output (only as default)" for key in KEYS: @@ -1283,7 +1282,7 @@ def persistent_keys(action_ids): KEYS_KEYS_CONSUMER = persistent_keys([ACTIONID.Key, ACTIONID.Consumer]) KEYS_KEYS_MOUSE_HSCROLL = persistent_keys([ACTIONID.Key, ACTIONID.Mouse, ACTIONID.Hscroll]) -COLORS = UnsortedNamedInts( +COLORS = NamedInts( { # from Xorg rgb.txt,v 1.3 2000/08/17 "red": 0xFF0000, @@ -1424,7 +1423,7 @@ def persistent_keys(action_ids): } ) -COLORSPLUS = UnsortedNamedInts({"No change": -1}) +COLORSPLUS = NamedInts({"No change": -1}) for i in COLORS: COLORSPLUS[int(i)] = str(i) diff --git a/lib/solaar/ui/diversion_rules.py b/lib/solaar/ui/diversion_rules.py index f502d7eab5..890db0c4f2 100644 --- a/lib/solaar/ui/diversion_rules.py +++ b/lib/solaar/ui/diversion_rules.py @@ -38,7 +38,6 @@ from logitech_receiver import diversion as _DIV from logitech_receiver.common import NamedInt from logitech_receiver.common import NamedInts -from logitech_receiver.common import UnsortedNamedInts from logitech_receiver.settings import KIND as _SKIND from logitech_receiver.settings import Setting from logitech_receiver.settings_templates import SETTINGS @@ -1538,7 +1537,7 @@ def _all_choices(cls, setting): # choice and map-choice if isinstance(setting, Setting): setting = type(setting) if isinstance(setting, type) and issubclass(setting, Setting): - choices = UnsortedNamedInts() + choices = NamedInts() universe = getattr(setting, "choices_universe", None) if universe: choices |= universe @@ -1547,7 +1546,7 @@ def _all_choices(cls, setting): # choice and map-choice choices |= NamedInts(**{str(extra): int(extra)}) return choices, extra settings = cls.ALL_SETTINGS.get(setting, []) - choices = UnsortedNamedInts() + choices = NamedInts() extra = None for s in settings: ch, ext = cls._all_choices(s) @@ -1567,7 +1566,7 @@ def _setting_attributes(cls, setting_name, device=None): val_class = setting.validator_class if setting else None kind = val_class.kind if val_class else None if kind in cls.MULTIPLE: - keys = UnsortedNamedInts() + keys = NamedInts() for s in settings: universe = getattr(s, "keys_universe" if kind == _SKIND.map_choice else "choices_universe", None) if universe: diff --git a/tests/logitech_receiver/test_common.py b/tests/logitech_receiver/test_common.py index b4ed951249..f7f189027b 100644 --- a/tests/logitech_receiver/test_common.py +++ b/tests/logitech_receiver/test_common.py @@ -189,19 +189,6 @@ def test_named_ints_other(): assert list(union) == [common.NamedInt(0, "empty"), common.NamedInt(5, "critical"), common.NamedInt(50, "good")] -def test_unsorted_named_ints(): - named_ints = common.UnsortedNamedInts(critical=5, empty=0) - named_ints_2 = common.UnsortedNamedInts(good=50) - - union = named_ints.__or__(named_ints_2) - unionr = named_ints_2.__or__(named_ints) - - assert len(union) == 3 - assert list(union) == [common.NamedInt(5, "critical"), common.NamedInt(0, "empty"), common.NamedInt(50, "good")] - assert len(unionr) == 3 - assert list(unionr) == [common.NamedInt(50, "good"), common.NamedInt(5, "critical"), common.NamedInt(0, "empty")] - - @pytest.mark.parametrize( "bytes_input, expected_output", [ diff --git a/tests/logitech_receiver/test_hidpp20_complex.py b/tests/logitech_receiver/test_hidpp20_complex.py index 8ebcbec958..55f7ec1836 100644 --- a/tests/logitech_receiver/test_hidpp20_complex.py +++ b/tests/logitech_receiver/test_hidpp20_complex.py @@ -213,7 +213,7 @@ def test_reprogrammable_key_v4_key(device, index, cid, tid, flags, pos, group, g @pytest.mark.parametrize( "responses, index, mapped_to, remappable_to, mapping_flags", [ - (fake_hidpp.responses_key, 1, "Right Click", common.UnsortedNamedInts(Right_Click=81, Left_Click=80), []), + (fake_hidpp.responses_key, 1, "Right Click", common.NamedInts(Right_Click=81, Left_Click=80), []), (fake_hidpp.responses_key, 2, "Left Click", None, ["diverted"]), (fake_hidpp.responses_key, 3, "Mouse Back Button", None, ["diverted", "persistently diverted"]), (fake_hidpp.responses_key, 4, "Mouse Forward Button", None, ["diverted", "raw XY diverted"]), @@ -421,7 +421,7 @@ def test_KeysArrayV4_index(key, index): (special_keys.CONTROL.Virtual_Gesture_Button, 7, common.NamedInt(0x51, "Right Click"), None), ], ) -def test_KeysArrayV4_key(key, expected_index, expected_mapped_to, expected_remappable_to): +def test_keys_array_v4_key(key, expected_index, expected_mapped_to, expected_remappable_to): device_key._keys = _hidpp20.get_keys(device_key) device_key._keys._ensure_all_keys_queried() @@ -432,7 +432,7 @@ def test_KeysArrayV4_key(key, expected_index, expected_mapped_to, expected_remap assert index == expected_index assert mapped_to == expected_mapped_to if expected_remappable_to is not None: - assert list(remappable_to) == expected_remappable_to + assert list(sorted(remappable_to)) == sorted(expected_remappable_to) @pytest.mark.parametrize( diff --git a/tests/logitech_receiver/test_setting_templates.py b/tests/logitech_receiver/test_setting_templates.py index ec83497649..9afad79b1e 100644 --- a/tests/logitech_receiver/test_setting_templates.py +++ b/tests/logitech_receiver/test_setting_templates.py @@ -530,9 +530,9 @@ def test_simple_template(test, mocker, mock_gethostname): Setup( FeatureTest(settings_templates.ReprogrammableKeys, {0x50: 0x50, 0x51: 0x50, 0xC4: 0xC4}, {0x51: 0x51}, 4, offset=0x05), { - common.NamedInt(0x50, "Left Button"): common.UnsortedNamedInts(Left_Click=0x50, Right_Click=0x51), - common.NamedInt(0x51, "Right Button"): common.UnsortedNamedInts(Right_Click=0x51, Left_Click=0x50), - common.NamedInt(0xC4, "Smart Shift"): common.UnsortedNamedInts(Smart_Shift=0xC4, Left_Click=80, Right_Click=81), + common.NamedInt(0x50, "Left Button"): common.NamedInts(Left_Click=0x50, Right_Click=0x51), + common.NamedInt(0x51, "Right Button"): common.NamedInts(Right_Click=0x51, Left_Click=0x50), + common.NamedInt(0xC4, "Smart Shift"): common.NamedInts(Smart_Shift=0xC4, Left_Click=80, Right_Click=81), }, *responses_reprog_controls, fake_hidpp.Response("0051000051", 0x0530, "0051000051"), # right button set write