diff options
author | Devaev Maxim <[email protected]> | 2019-09-26 04:23:50 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2019-09-26 04:23:50 +0300 |
commit | 5e1e3cdf640face90ac5898ea4de7484c5edeae1 (patch) | |
tree | 71997b466302594a43929076a817b5748927512e | |
parent | 2f767573d301dbc2b9403363a71ff7acf38049b4 (diff) |
refactoring
-rw-r--r-- | Makefile | 2 | ||||
-rwxr-xr-x | genmap.py | 8 | ||||
-rw-r--r-- | hid/src/keymap.h.mako | 4 | ||||
-rw-r--r-- | kvmd/data/keymap.yaml | 108 | ||||
-rw-r--r-- | kvmd/keymap.py | 103 | ||||
-rw-r--r-- | kvmd/keymap.py.mako (renamed from kvmd/data/keymap.yaml.mako) | 21 | ||||
-rw-r--r-- | kvmd/plugins/hid/serial.py | 2 | ||||
-rwxr-xr-x | setup.py | 4 | ||||
-rw-r--r-- | testenv/linters/vulture-wl.py | 4 | ||||
-rw-r--r-- | testenv/tests/test_keymap.py | 3 |
10 files changed, 128 insertions, 131 deletions
@@ -103,7 +103,7 @@ run-ipmi: testenv regen: testenv - for file in kvmd/data/keymap.yaml hid/src/keymap.h; do \ + for file in kvmd/keymap.py hid/src/keymap.h; do \ docker run --user `id -u`:`id -g` --rm \ --volume `pwd`:/src \ -it $(TESTENV_IMAGE) bash -c "cd src && ./genmap.py keymap.in $$file.mako $$file"; \ @@ -33,8 +33,8 @@ import mako.template # ===== @dataclasses.dataclass(frozen=True) class _KeyMapping: - arduino_hid_code: int - arduino_hid_key: str + serial_hid_code: int + serial_hid_key: str web_key: str @@ -47,8 +47,8 @@ def _read_keymap_in(path: str) -> List[_KeyMapping]: parts = list(map(str.strip, line.split())) if len(parts) >= 3: keymap.append(_KeyMapping( - arduino_hid_code=int(parts[0]), - arduino_hid_key=parts[1], + serial_hid_code=int(parts[0]), + serial_hid_key=parts[1], web_key=parts[2], )) return keymap diff --git a/hid/src/keymap.h.mako b/hid/src/keymap.h.mako index 6a965ca9..7eb4d5d0 100644 --- a/hid/src/keymap.h.mako +++ b/hid/src/keymap.h.mako @@ -29,8 +29,8 @@ <%! import operator %> INLINE KeyboardKeycode keymap(uint8_t code) { switch(code) { -% for km in sorted(keymap, key=operator.attrgetter("arduino_hid_key")): - case ${km.arduino_hid_code}: return ${km.arduino_hid_key}; +% for km in sorted(keymap, key=operator.attrgetter("serial_hid_key")): + case ${km.serial_hid_code}: return ${km.serial_hid_key}; % endfor default: return KEY_ERROR_UNDEFINED; } diff --git a/kvmd/data/keymap.yaml b/kvmd/data/keymap.yaml deleted file mode 100644 index 4981b5ff..00000000 --- a/kvmd/data/keymap.yaml +++ /dev/null @@ -1,108 +0,0 @@ -# ========================================================================== # -# # -# KVMD - The main Pi-KVM daemon. # -# # -# Copyright (C) 2018 Maxim Devaev <[email protected]> # -# # -# This program is free software: you can redistribute it and/or modify # -# it under the terms of the GNU General Public License as published by # -# the Free Software Foundation, either version 3 of the License, or # -# (at your option) any later version. # -# # -# This program is distributed in the hope that it will be useful, # -# but WITHOUT ANY WARRANTY; without even the implied warranty of # -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # -# GNU General Public License for more details. # -# # -# You should have received a copy of the GNU General Public License # -# along with this program. If not, see <https://www.gnu.org/licenses/>. # -# # -# ========================================================================== # - - -AltLeft: 79 -AltRight: 83 -ArrowDown: 75 -ArrowLeft: 74 -ArrowRight: 73 -ArrowUp: 76 -Backquote: 49 -Backslash: 46 -Backspace: 39 -BracketLeft: 44 -BracketRight: 45 -CapsLock: 53 -Comma: 50 -ControlLeft: 77 -ControlRight: 81 -Delete: 70 -Digit0: 36 -Digit1: 27 -Digit2: 28 -Digit3: 29 -Digit4: 30 -Digit5: 31 -Digit6: 32 -Digit7: 33 -Digit8: 34 -Digit9: 35 -End: 71 -Enter: 37 -Equal: 43 -Escape: 38 -F1: 54 -F10: 63 -F11: 64 -F12: 65 -F2: 55 -F3: 56 -F4: 57 -F5: 58 -F6: 59 -F7: 60 -F8: 61 -F9: 62 -Home: 68 -Insert: 67 -KeyA: 1 -KeyB: 2 -KeyC: 3 -KeyD: 4 -KeyE: 5 -KeyF: 6 -KeyG: 7 -KeyH: 8 -KeyI: 9 -KeyJ: 10 -KeyK: 11 -KeyL: 12 -KeyM: 13 -KeyN: 14 -KeyO: 15 -KeyP: 16 -KeyQ: 17 -KeyR: 18 -KeyS: 19 -KeyT: 20 -KeyU: 21 -KeyV: 22 -KeyW: 23 -KeyX: 24 -KeyY: 25 -KeyZ: 26 -MetaLeft: 80 -MetaRight: 84 -Minus: 42 -PageDown: 72 -PageUp: 69 -Pause: 85 -Period: 51 -PrintScreen: 66 -Quote: 48 -ScrollLock: 86 -Semicolon: 47 -ShiftLeft: 78 -ShiftRight: 82 -Slash: 52 -Space: 41 -Tab: 40 diff --git a/kvmd/keymap.py b/kvmd/keymap.py index e6dfecbb..be32bae2 100644 --- a/kvmd/keymap.py +++ b/kvmd/keymap.py @@ -20,17 +20,108 @@ # ========================================================================== # -import pkgutil +import dataclasses from typing import Dict -import yaml - # ===== -def _get_keymap() -> Dict[str, int]: - return yaml.safe_load(pkgutil.get_data("kvmd", "data/keymap.yaml").decode()) # type: ignore [email protected](frozen=True) +class SerialKey: + code: int + + [email protected](frozen=True) +class Key: + serial: SerialKey # ===== -KEYMAP = _get_keymap() +KEYMAP: Dict[str, Key] = { + "AltLeft": Key(serial=SerialKey(code=79)), + "AltRight": Key(serial=SerialKey(code=83)), + "ArrowDown": Key(serial=SerialKey(code=75)), + "ArrowLeft": Key(serial=SerialKey(code=74)), + "ArrowRight": Key(serial=SerialKey(code=73)), + "ArrowUp": Key(serial=SerialKey(code=76)), + "Backquote": Key(serial=SerialKey(code=49)), + "Backslash": Key(serial=SerialKey(code=46)), + "Backspace": Key(serial=SerialKey(code=39)), + "BracketLeft": Key(serial=SerialKey(code=44)), + "BracketRight": Key(serial=SerialKey(code=45)), + "CapsLock": Key(serial=SerialKey(code=53)), + "Comma": Key(serial=SerialKey(code=50)), + "ControlLeft": Key(serial=SerialKey(code=77)), + "ControlRight": Key(serial=SerialKey(code=81)), + "Delete": Key(serial=SerialKey(code=70)), + "Digit0": Key(serial=SerialKey(code=36)), + "Digit1": Key(serial=SerialKey(code=27)), + "Digit2": Key(serial=SerialKey(code=28)), + "Digit3": Key(serial=SerialKey(code=29)), + "Digit4": Key(serial=SerialKey(code=30)), + "Digit5": Key(serial=SerialKey(code=31)), + "Digit6": Key(serial=SerialKey(code=32)), + "Digit7": Key(serial=SerialKey(code=33)), + "Digit8": Key(serial=SerialKey(code=34)), + "Digit9": Key(serial=SerialKey(code=35)), + "End": Key(serial=SerialKey(code=71)), + "Enter": Key(serial=SerialKey(code=37)), + "Equal": Key(serial=SerialKey(code=43)), + "Escape": Key(serial=SerialKey(code=38)), + "F1": Key(serial=SerialKey(code=54)), + "F10": Key(serial=SerialKey(code=63)), + "F11": Key(serial=SerialKey(code=64)), + "F12": Key(serial=SerialKey(code=65)), + "F2": Key(serial=SerialKey(code=55)), + "F3": Key(serial=SerialKey(code=56)), + "F4": Key(serial=SerialKey(code=57)), + "F5": Key(serial=SerialKey(code=58)), + "F6": Key(serial=SerialKey(code=59)), + "F7": Key(serial=SerialKey(code=60)), + "F8": Key(serial=SerialKey(code=61)), + "F9": Key(serial=SerialKey(code=62)), + "Home": Key(serial=SerialKey(code=68)), + "Insert": Key(serial=SerialKey(code=67)), + "KeyA": Key(serial=SerialKey(code=1)), + "KeyB": Key(serial=SerialKey(code=2)), + "KeyC": Key(serial=SerialKey(code=3)), + "KeyD": Key(serial=SerialKey(code=4)), + "KeyE": Key(serial=SerialKey(code=5)), + "KeyF": Key(serial=SerialKey(code=6)), + "KeyG": Key(serial=SerialKey(code=7)), + "KeyH": Key(serial=SerialKey(code=8)), + "KeyI": Key(serial=SerialKey(code=9)), + "KeyJ": Key(serial=SerialKey(code=10)), + "KeyK": Key(serial=SerialKey(code=11)), + "KeyL": Key(serial=SerialKey(code=12)), + "KeyM": Key(serial=SerialKey(code=13)), + "KeyN": Key(serial=SerialKey(code=14)), + "KeyO": Key(serial=SerialKey(code=15)), + "KeyP": Key(serial=SerialKey(code=16)), + "KeyQ": Key(serial=SerialKey(code=17)), + "KeyR": Key(serial=SerialKey(code=18)), + "KeyS": Key(serial=SerialKey(code=19)), + "KeyT": Key(serial=SerialKey(code=20)), + "KeyU": Key(serial=SerialKey(code=21)), + "KeyV": Key(serial=SerialKey(code=22)), + "KeyW": Key(serial=SerialKey(code=23)), + "KeyX": Key(serial=SerialKey(code=24)), + "KeyY": Key(serial=SerialKey(code=25)), + "KeyZ": Key(serial=SerialKey(code=26)), + "MetaLeft": Key(serial=SerialKey(code=80)), + "MetaRight": Key(serial=SerialKey(code=84)), + "Minus": Key(serial=SerialKey(code=42)), + "PageDown": Key(serial=SerialKey(code=72)), + "PageUp": Key(serial=SerialKey(code=69)), + "Pause": Key(serial=SerialKey(code=85)), + "Period": Key(serial=SerialKey(code=51)), + "PrintScreen": Key(serial=SerialKey(code=66)), + "Quote": Key(serial=SerialKey(code=48)), + "ScrollLock": Key(serial=SerialKey(code=86)), + "Semicolon": Key(serial=SerialKey(code=47)), + "ShiftLeft": Key(serial=SerialKey(code=78)), + "ShiftRight": Key(serial=SerialKey(code=82)), + "Slash": Key(serial=SerialKey(code=52)), + "Space": Key(serial=SerialKey(code=41)), + "Tab": Key(serial=SerialKey(code=40)), +} diff --git a/kvmd/data/keymap.yaml.mako b/kvmd/keymap.py.mako index 820c0027..06b0c7d9 100644 --- a/kvmd/data/keymap.yaml.mako +++ b/kvmd/keymap.py.mako @@ -19,7 +19,26 @@ # # # ========================================================================== # + +import dataclasses + +from typing import Dict + + +# ===== [email protected](frozen=True) +class SerialKey: + code: int + + [email protected](frozen=True) +class Key: + serial: SerialKey + <%! import operator %> +# ===== +KEYMAP: Dict[str, Key] = { % for km in sorted(keymap, key=operator.attrgetter("web_key")): -${km.web_key}: ${km.arduino_hid_code} + "${km.web_key}": Key(serial=SerialKey(code=${km.serial_hid_code})), % endfor +} diff --git a/kvmd/plugins/hid/serial.py b/kvmd/plugins/hid/serial.py index 7b7c4275..5b78e5a9 100644 --- a/kvmd/plugins/hid/serial.py +++ b/kvmd/plugins/hid/serial.py @@ -82,7 +82,7 @@ class _KeyEvent(_BoolEvent): assert self.name in keymap.KEYMAP def make_command(self) -> bytes: - code = keymap.KEYMAP[self.name] + code = keymap.KEYMAP[self.name].serial.code key_bytes = bytes([code]) assert len(key_bytes) == 1, (self, key_bytes, code) state_bytes = (b"\x01" if self.state else b"\x00") @@ -93,10 +93,6 @@ def main() -> None: "kvmd.apps.ipmi", ], - package_data={ - "kvmd": ["data/*.yaml"], - }, - scripts=[ os.path.join("scripts", name) for name in os.listdir("scripts") diff --git a/testenv/linters/vulture-wl.py b/testenv/linters/vulture-wl.py index 9bdbc834..377c71a6 100644 --- a/testenv/linters/vulture-wl.py +++ b/testenv/linters/vulture-wl.py @@ -8,8 +8,8 @@ IpmiServer.handle_raw_request fake_rpi.RPi.GPIO -_KeyMapping.arduino_hid_code -_KeyMapping.arduino_hid_key +_KeyMapping.serial_hid_code +_KeyMapping.serial_hid_key _KeyMapping.web_key _ScriptWriter.get_args diff --git a/testenv/tests/test_keymap.py b/testenv/tests/test_keymap.py index f69accb1..4ee603d0 100644 --- a/testenv/tests/test_keymap.py +++ b/testenv/tests/test_keymap.py @@ -27,8 +27,7 @@ from kvmd.keymap import KEYMAP # ===== def test_ok__keymap() -> None: - assert type(KEYMAP["KeyA"]) == int # pylint: disable=unidiomatic-typecheck - assert KEYMAP["KeyA"] == 1 + assert KEYMAP["KeyA"].serial.code == 1 def test_fail__keymap() -> None: |