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 /genmap.py | |
parent | 5bf758e232361e2b7a6b64e4b1750a10d896bb2d (diff) |
refactoring
Diffstat (limited to 'genmap.py')
-rwxr-xr-x | genmap.py | 105 |
1 files changed, 32 insertions, 73 deletions
@@ -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() |