mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-29 09:01:54 +08:00
refactoring
This commit is contained in:
105
genmap.py
105
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 <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()
|
||||
|
||||
Reference in New Issue
Block a user