diff options
author | Devaev Maxim <[email protected]> | 2019-04-13 06:18:33 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2019-04-13 06:18:33 +0300 |
commit | 0c82937d3c0dfd7c9436a2cec62f9f56a5f11aa8 (patch) | |
tree | 6cb55a29950eb75ac6078d362f7b92e56047bfe5 | |
parent | 5bf758e232361e2b7a6b64e4b1750a10d896bb2d (diff) |
refactoring
-rw-r--r-- | Makefile | 8 | ||||
-rwxr-xr-x | genmap.py | 105 | ||||
-rw-r--r-- | hid/src/keymap.h.mako | 37 | ||||
-rw-r--r-- | keymap.in | 6 | ||||
-rw-r--r-- | kvmd/data/keymap.yaml.mako | 25 | ||||
-rw-r--r-- | testenv/Dockerfile | 5 | ||||
-rw-r--r-- | testenv/linters/vulture-wl.py | 3 | ||||
-rw-r--r-- | testenv/requirements.txt | 3 | ||||
-rw-r--r-- | testenv/tox.ini | 3 |
9 files changed, 110 insertions, 85 deletions
@@ -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: @@ -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], - )) - - path = "kvmd/data/keymap.yaml" - with open(path, "w") as keymap_yaml_file: - keymap_yaml_file.write(textwrap.dedent(""" - # ========================================================================== # - # # - # 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/>. # - # # - # ========================================================================== # - """).strip() + "\n\n\n") - yaml.dump({ - km.js_key: km.kvmd_code - for km in keymap - }, keymap_yaml_file, indent=4, default_flow_style=False) - print("Generated:", path) + assert len(sys.argv) == 4, "%s <keymap.in> <template> <out>" % (sys.argv[0]) - path = "hid/src/keymap.h" - with open(path, "w") as hid_header_file: - hid_header_file.write(textwrap.dedent(""" - /***************************************************************************** - # # - # 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/>. # - # # - *****************************************************************************/ - """).strip() + "\n\n\n") - hid_header_file.write("#pragma once\n\n#include <HID-Project.h>\n\n#include \"inline.h\"\n\n\n") - hid_header_file.write("INLINE KeyboardKeycode keymap(uint8_t code) {\n\tswitch(code) {\n") - for km in sorted(keymap, key=(lambda km: km.arduino_hid_key)): - hid_header_file.write("\t\tcase {km.kvmd_code}: return {km.arduino_hid_key};\n".format(km=km)) - hid_header_file.write("\t\tdefault: return KEY_ERROR_UNDEFINED;\n\t}\n}\n") - print("Generated:", path) + keymap = _read_keymap_in(sys.argv[1]) + _render_keymap(keymap, sys.argv[2], sys.argv[3]) +# ===== if __name__ == "__main__": main() diff --git a/hid/src/keymap.h.mako b/hid/src/keymap.h.mako new file mode 100644 index 00000000..f8c85d79 --- /dev/null +++ b/hid/src/keymap.h.mako @@ -0,0 +1,37 @@ +/***************************************************************************** +# # +# 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/>. # +# # +*****************************************************************************/ + + +#pragma once + +#include <HID-Project.h> + +#include "inline.h" + +<%! import operator %> +INLINE KeyboardKeycode keymap(uint8_t code) { + switch(code) { +% for km in sorted(keymap, key=operator.attrgetter("arduino_hid_key")): + case ${km.kvmd_code}: return ${km.arduino_hid_key}; +% endfor + default: return KEY_ERROR_UNDEFINED; + } +} @@ -18,11 +18,11 @@ # along with this program. If not, see <https://www.gnu.org/licenses/>. # # # # ========================================================================== # -# -# + + # https://github.com/NicoHood/HID/blob/master/src/HID-APIs/ImprovedKeylayouts.h # https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code -# + # ---------------------------------- # KVMD | Arduino HID | JS # ---------------------------------- diff --git a/kvmd/data/keymap.yaml.mako b/kvmd/data/keymap.yaml.mako new file mode 100644 index 00000000..8814b20b --- /dev/null +++ b/kvmd/data/keymap.yaml.mako @@ -0,0 +1,25 @@ +# ========================================================================== # +# # +# 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/>. # +# # +# ========================================================================== # + +<%! import operator %> +% for km in sorted(keymap, key=operator.attrgetter("web_key")): +${km.web_key}: ${km.kvmd_code} +% endfor diff --git a/testenv/Dockerfile b/testenv/Dockerfile index d958a501..91790432 100644 --- a/testenv/Dockerfile +++ b/testenv/Dockerfile @@ -32,8 +32,10 @@ RUN pacman -Syy \ && user-packer -S --noconfirm \ python \ python-pip \ + python-tox \ python-systemd \ python-dbus \ + python-mako \ nginx-mainline \ ustreamer \ socat \ @@ -42,9 +44,6 @@ RUN pacman -Syy \ && rm -rf /.npm \ && (pacman -Sc --noconfirm || true) -COPY testenv/requirements.txt requirements.txt -RUN pip install -r requirements.txt - RUN mkdir -p /etc/kvmd/nginx CMD /bin/bash diff --git a/testenv/linters/vulture-wl.py b/testenv/linters/vulture-wl.py index c0835cae..68110436 100644 --- a/testenv/linters/vulture-wl.py +++ b/testenv/linters/vulture-wl.py @@ -4,3 +4,6 @@ _MassStorageDeviceInfo.real _MassStorageDeviceInfo.hw _MassStorageDeviceInfo.image fake_rpi.RPi.GPIO +_KeyMapping.kvmd_code +_KeyMapping.arduino_hid_key +_KeyMapping.web_key diff --git a/testenv/requirements.txt b/testenv/requirements.txt index b5dc4511..27e4f479 100644 --- a/testenv/requirements.txt +++ b/testenv/requirements.txt @@ -7,7 +7,4 @@ pyudev pyyaml pyserial setproctitle -systemd-python -dbus-python pygments -tox diff --git a/testenv/tox.ini b/testenv/tox.ini index 3be89343..55d51892 100644 --- a/testenv/tox.ini +++ b/testenv/tox.ini @@ -1,9 +1,10 @@ [tox] envlist = flake8, pylint, mypy, vulture, pytest, eslint, htmlhint -skipsdist = True +skipsdist = true [testenv] basepython = python3.7 +sitepackages = true changedir = /src [testenv:flake8] |