Class should be capital, use get rather than exception

This commit is contained in:
John Morrison 2022-04-30 18:00:12 +01:00 committed by xs5871
parent 73e95bfdde
commit 12e7a1b42c
3 changed files with 13 additions and 14 deletions

View File

@ -1,11 +1,11 @@
# Keymap String KeyNames
Enables referring to keys by 'NAME' rather than KC.NAME.
Enables referring to keys by `'NAME'` rather than `KC.NAME`.
For example:
```python
from kmk.extensions.keymap_string_keynames import keymap_string_keynames
from kmk.extensions.keymap_string_keynames import Keymap_string_keynames
# Normal
# keyboard.keymap = [[ KC.A, KC.B, KC.RESET ]]
@ -16,7 +16,7 @@ from kmk.extensions.keymap_string_keynames import keymap_string_keynames
# String names
keyboard.keymap = [[ 'A' , 'B', 'RESET' ]]
keymap_string_keynames = keymap_string_keynames()
keymap_string_keynames = Keymap_string_keynames()
# Enabling debug will show each replacement or failure.
# This is recommended during the initial development of a keyboard.

View File

@ -2,7 +2,7 @@ from kmk.extensions import Extension
from kmk.keys import KC
class keymap_string_keynames(Extension):
class Keymap_string_keynames(Extension):
#####
# User-configurable
debug_enabled = False
@ -17,15 +17,14 @@ class keymap_string_keynames(Extension):
for _, layer in enumerate(keyboard.keymap):
for key_idx, key in enumerate(layer):
if isinstance(key, str):
try:
replacement = KC[key]
layer[key_idx] = replacement
if self.debug_enabled:
print(f"Replacing '{key}' with {replacement}")
except KeyError:
layer[key_idx] = KC.NO
replacement = KC.get(key)
if replacement is None:
replacement = KC.NO
if self.debug_enabled:
print(f"Failed replacing '{key}'. Using KC.NO")
elif self.debug_enabled:
print(f"Replacing '{key}' with {replacement}")
layer[key_idx] = replacement
def before_matrix_scan(self, keyboard):
return

View File

@ -1,6 +1,6 @@
import unittest
from kmk.extensions.keymap_string_keynames import keymap_string_keynames
from kmk.extensions.keymap_string_keynames import Keymap_string_keynames
from kmk.keys import KC
from tests.keyboard_test import KeyboardTest
@ -8,14 +8,14 @@ from tests.keyboard_test import KeyboardTest
class Test_extension_keymap_string_keynames(unittest.TestCase):
def test_basic_kmk_keyboard_replace_string_primary_name(self):
keyboard = KeyboardTest(
[], [['1', '2', '3', '4']], extensions={keymap_string_keynames()}
[], [['1', '2', '3', '4']], extensions={Keymap_string_keynames()}
)
keyboard.test('Simple key press', [(0, True), (0, False)], [{KC.N1}, {}])
def test_basic_kmk_keyboard_replace_string_secondary_name(self):
keyboard = KeyboardTest(
[], [['N1', 'N2', 'N3', 'N4']], extensions={keymap_string_keynames()}
[], [['N1', 'N2', 'N3', 'N4']], extensions={Keymap_string_keynames()}
)
keyboard.test('Simple key press', [(0, True), (0, False)], [{KC.N1}, {}])