refactoring

This commit is contained in:
Devaev Maxim 2019-04-13 06:18:33 +03:00
parent 5bf758e232
commit 0c82937d3c
9 changed files with 110 additions and 85 deletions

View File

@ -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:

105
genmap.py
View File

@ -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 <keymap.in> <template> <out>" % (sys.argv[0])
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 <mdevaev@gmail.com> #
# #
# 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)
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 <mdevaev@gmail.com> #
# #
# 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()

37
hid/src/keymap.h.mako Normal file
View File

@ -0,0 +1,37 @@
/*****************************************************************************
# #
# KVMD - The main Pi-KVM daemon. #
# #
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
# #
# 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;
}
}

View File

@ -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
# ----------------------------------

View File

@ -0,0 +1,25 @@
# ========================================================================== #
# #
# KVMD - The main Pi-KVM daemon. #
# #
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
# #
# 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

View File

@ -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

View File

@ -4,3 +4,6 @@ _MassStorageDeviceInfo.real
_MassStorageDeviceInfo.hw
_MassStorageDeviceInfo.image
fake_rpi.RPi.GPIO
_KeyMapping.kvmd_code
_KeyMapping.arduino_hid_key
_KeyMapping.web_key

View File

@ -7,7 +7,4 @@ pyudev
pyyaml
pyserial
setproctitle
systemd-python
dbus-python
pygments
tox

View File

@ -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]