Skip to content

Commit

Permalink
Refactor: Completely remove UnsortedNamedInts
Browse files Browse the repository at this point in the history
Simplify the code by removing this unnecessary class. Replace with
default NamedInts implementation.

Related #2273
  • Loading branch information
MattHag committed Nov 1, 2024
1 parent 8518604 commit 3ce6bae
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 38 deletions.
9 changes: 0 additions & 9 deletions lib/logitech_receiver/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
2 changes: 1 addition & 1 deletion lib/logitech_receiver/hidpp20.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,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:
Expand Down
9 changes: 4 additions & 5 deletions lib/logitech_receiver/special_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand Down Expand Up @@ -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)

Expand Down
7 changes: 3 additions & 4 deletions lib/solaar/ui/diversion_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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:
Expand Down
13 changes: 0 additions & 13 deletions tests/logitech_receiver/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down
6 changes: 3 additions & 3 deletions tests/logitech_receiver/test_hidpp20_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,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"]),
Expand Down Expand Up @@ -420,7 +420,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()

Expand All @@ -431,7 +431,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(
Expand Down
6 changes: 3 additions & 3 deletions tests/logitech_receiver/test_setting_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3ce6bae

Please sign in to comment.