Skip to content

Commit

Permalink
Cleanup make_key/make_argumented_key calls
Browse files Browse the repository at this point in the history
  • Loading branch information
xs5871 committed Jul 1, 2024
1 parent c17700b commit 64071b5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 59 deletions.
67 changes: 25 additions & 42 deletions kmk/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,28 @@ class AX:


def maybe_make_key(
code: Optional[int],
names: Tuple[str, ...],
*args,
**kwargs,
) -> Callable[[str], Key]:
def closure(candidate):
if candidate in names:
return make_key(code=code, names=names, *args, **kwargs)
return make_key(names=names, *args, **kwargs)

return closure


def maybe_make_argumented_key(
names: Tuple[str, ...] = tuple(), # NOQA
constructor: Optional[[Key, Callable[[...], Key]]] = None,
*constructor_args,
**constructor_kwargs,
names: Tuple[str, ...],
constructor: [Key, Callable[[...], Key]],
**kwargs,
) -> Callable[[str], Key]:
def closure(candidate):
if candidate in names:
return make_argumented_key(
names,
constructor,
*constructor_args,
**constructor_kwargs,
names=names,
constructor=constructor,
**kwargs,
)

return closure
Expand Down Expand Up @@ -99,9 +96,9 @@ def maybe_make_alpha_key(candidate: str) -> Optional[Key]:
candidate_upper = candidate.upper()
if candidate_upper in ALL_ALPHAS:
return make_key(
code=4 + ALL_ALPHAS.index(candidate_upper),
names=(candidate_upper, candidate.lower()),
constructor=KeyboardKey,
code=4 + ALL_ALPHAS.index(candidate_upper),
)


Expand All @@ -113,9 +110,9 @@ def maybe_make_numeric_key(candidate: str) -> Optional[Key]:
offset = ALL_NUMBER_ALIASES.index(candidate)

return make_key(
code=30 + offset,
names=(ALL_NUMBERS[offset], ALL_NUMBER_ALIASES[offset]),
constructor=KeyboardKey,
code=30 + offset,
)


Expand All @@ -137,7 +134,7 @@ def maybe_make_mod_key(candidate: str) -> Optional[Key]:

for code, names in mods:
if candidate in names:
return make_key(code=code, names=names, constructor=ModifierKey)
return make_key(names=names, constructor=ModifierKey, code=code)


def maybe_make_more_ascii(candidate: str) -> Optional[Key]:
Expand All @@ -162,7 +159,7 @@ def maybe_make_more_ascii(candidate: str) -> Optional[Key]:

for code, names in codes:
if candidate in names:
return make_key(code=code, names=names, constructor=KeyboardKey)
return make_key(names=names, constructor=KeyboardKey, code=code)


def maybe_make_fn_key(candidate: str) -> Optional[Key]:
Expand Down Expand Up @@ -195,7 +192,7 @@ def maybe_make_fn_key(candidate: str) -> Optional[Key]:

for code, names in codes:
if candidate in names:
return make_key(code=code, names=names, constructor=KeyboardKey)
return make_key(names=names, constructor=KeyboardKey, code=code)


def maybe_make_navlock_key(candidate: str) -> Optional[Key]:
Expand Down Expand Up @@ -224,7 +221,7 @@ def maybe_make_navlock_key(candidate: str) -> Optional[Key]:

for code, names in codes:
if candidate in names:
return make_key(code=code, names=names, constructor=KeyboardKey)
return make_key(names=names, constructor=KeyboardKey, code=code)


def maybe_make_numpad_key(candidate: str) -> Optional[Key]:
Expand Down Expand Up @@ -253,7 +250,7 @@ def maybe_make_numpad_key(candidate: str) -> Optional[Key]:

for code, names in codes:
if candidate in names:
return make_key(code=code, names=names, constructor=KeyboardKey)
return make_key(names=names, constructor=KeyboardKey, code=code)


def maybe_make_shifted_key(candidate: str) -> Optional[Key]:
Expand Down Expand Up @@ -284,7 +281,7 @@ def maybe_make_shifted_key(candidate: str) -> Optional[Key]:
for code, names in codes:
if candidate in names:
return make_key(
code=code, names=names, constructor=ModifiedKey, modifier=KC.LSFT
names=names, constructor=ModifiedKey, code=code, modifier=KC.LSFT
)


Expand Down Expand Up @@ -315,7 +312,7 @@ def maybe_make_international_key(candidate: str) -> Optional[Key]:

for code, names in codes:
if candidate in names:
return make_key(code=code, names=names, constructor=KeyboardKey)
return make_key(names=names, constructor=KeyboardKey, code=code)


def maybe_make_firmware_key(candidate: str) -> Optional[Key]:
Expand All @@ -341,13 +338,11 @@ def maybe_make_firmware_key(candidate: str) -> Optional[Key]:
maybe_make_numeric_key,
maybe_make_firmware_key,
maybe_make_key(
None,
('BKDL',),
on_press=handlers.bkdl_pressed,
on_release=handlers.bkdl_released,
),
maybe_make_key(
None,
('GESC', 'GRAVE_ESC'),
on_press=handlers.gesc_pressed,
on_release=handlers.gesc_released,
Expand Down Expand Up @@ -540,18 +535,13 @@ class MouseKey(_DefaultKey):


def make_key(
code: Optional[int] = None,
names: Tuple[str, ...] = tuple(), # NOQA
names: Tuple[str, ...],
constructor: Key = Key,
**kwargs,
) -> Key:
'''
Create a new key, aliased by `names` in the KC lookup table.
If a code is not specified, the key is assumed to be a custom
internal key to be handled in a state callback rather than
sent directly to the OS.
Names are globally unique. If a later key is created with
the same name as an existing entry in `KC`, it will overwrite
the existing entry.
Expand All @@ -561,10 +551,7 @@ def make_key(
All **kwargs are passed to the Key constructor
'''

if code is None:
key = constructor(**kwargs)
else:
key = constructor(code, **kwargs)
key = constructor(**kwargs)

for name in names:
KC[name] = key
Expand All @@ -575,19 +562,15 @@ def make_key(
# Argumented keys are implicitly internal, so auto-gen of code
# is almost certainly the best plan here
def make_argumented_key(
constructor: Optional[[Key, Callable[[...], Key]]] = None,
names: Tuple[str, ...] = tuple(), # NOQA
*constructor_args,
**constructor_kwargs,
names: Tuple[str, ...],
constructor: [Key, Callable[[...], Key]],
**_kwargs,
) -> Key:

def _argumented_key(*user_args, **user_kwargs) -> Key:
if constructor:
return constructor(*user_args, **user_kwargs, **constructor_kwargs)
else:
return Key(*constructor_args, **constructor_kwargs)
def argumented_key(*args, **kwargs) -> Key:
return constructor(*args, **kwargs, **_kwargs)

for name in names:
KC[name] = _argumented_key
KC[name] = argumented_key

return _argumented_key
return argumented_key
25 changes: 8 additions & 17 deletions tests/test_kmk_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,9 @@ def test_invalid_key_lower(self):

def test_custom_key(self):
created = make_key(
KC.N2.code,
names=(
'EURO',
'€',
),
names=('EURO', '€'),
constructor=ModifiedKey,
code=KC.N2.code,
modifier=KC.LSFT(KC.ROPT),
)
assert created is KC.get('EURO')
Expand Down Expand Up @@ -174,12 +171,9 @@ def test_invalid_key_lower(self):

def test_custom_key(self):
created = make_key(
KC['N2'].code,
names=(
'EURO',
'€',
),
names=('EURO', '€'),
constructor=ModifiedKey,
code=KC['N2'].code,
modifier=KC.LSFT(KC.ROPT),
)
assert created is KC['EURO']
Expand Down Expand Up @@ -223,12 +217,9 @@ def test_invalid_key_lower(self):

def test_custom_key(self):
created = make_key(
KC.get('N2').code,
names=(
'EURO',
'€',
),
names=('EURO', '€'),
constructor=ModifiedKey,
code=KC.get('N2').code,
modifier=KC.LSFT(KC.ROPT),
)
assert created is KC.get('EURO')
Expand All @@ -250,8 +241,8 @@ def setUp(self):
KC.clear()

def test_make_key_new_instance(self):
key1 = make_key(code=1, constructor=KeyboardKey)
key2 = make_key(code=1, constructor=KeyboardKey)
key1 = make_key(names=(), constructor=KeyboardKey, code=1)
key2 = make_key(names=(), constructor=KeyboardKey, code=1)
assert key1 is not key2
assert key1.code == key2.code

Expand Down

0 comments on commit 64071b5

Please sign in to comment.