diff --git a/Makefile b/Makefile index d9f77c5f..cad1e342 100644 --- a/Makefile +++ b/Makefile @@ -49,8 +49,12 @@ shell: make _run_cmd CMD=/bin/bash -regen: - python3 genmap.py +regen: _testenv + for file in kvmd/data/keymap.yaml 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"; \ + done release: diff --git a/genmap.py b/genmap.py index 74424414..b601d007 100755 --- a/genmap.py +++ b/genmap.py @@ -21,95 +21,54 @@ # ========================================================================== # +import sys import textwrap from typing import List from typing import NamedTuple -import yaml +import mako.template # ===== -class KeyMapping(NamedTuple): +class _KeyMapping(NamedTuple): kvmd_code: int arduino_hid_key: str - js_key: str + web_key: str + + +def _read_keymap_in(path: str) -> List[_KeyMapping]: + keymap: List[_KeyMapping] = [] + with open(path) as keymap_file: + for line in keymap_file: + line = line.strip() + if len(line) > 0 and not line.startswith("#"): + parts = list(map(str.strip, line.split())) + if len(parts) >= 3: + keymap.append(_KeyMapping( + kvmd_code=int(parts[0]), + arduino_hid_key=parts[1], + web_key=parts[2], + )) + return keymap + + +def _render_keymap(keymap: List[_KeyMapping], template_path: str, out_path: str) -> None: + with open(template_path) as template_file: + with open(out_path, "w") as out_file: + template = textwrap.dedent(template_file.read()) + rendered = mako.template.Template(template).render(keymap=keymap) + out_file.write(rendered) # ===== def main() -> None: - keymap: List[KeyMapping] = [] - with open("keymap.in") as keymap_file: - for row in keymap_file: - if not row.startswith("#"): - parts = row.split() - keymap.append(KeyMapping( - kvmd_code=int(parts[0]), - arduino_hid_key=parts[1], - js_key=parts[2], - )) + assert len(sys.argv) == 4, "%s