summaryrefslogtreecommitdiff
path: root/lib/python/qmk/keymap.py
diff options
context:
space:
mode:
authorskullydazed <skullydazed@users.noreply.github.com>2019-07-15 12:14:27 -0700
committerFlorian Didron <fdidron@users.noreply.github.com>2019-07-19 10:05:36 +0900
commit7e080e7ecd6dff6f06579ce8b1623bc22326e166 (patch)
treeb5b45a7f6ebe4c60e9ae1835b2c8873178ccd592 /lib/python/qmk/keymap.py
parent1879c6e7342c78995b38dfbd62cd4504cc40b39f (diff)
QMK CLI and JSON keymap support (#6176)
* Script to generate keymap.c from JSON file. * Support for keymap.json * Add a warning about the keymap.c getting overwritten. * Fix keymap generating * Install the python deps * Flesh out more of the python environment * Remove defunct json2keymap * Style everything with yapf * Polish up python support * Hide json keymap.c into the .build dir * Polish up qmk-compile-json * Make milc work with positional arguments * Fix a couple small things * Fix some errors and make the CLI more understandable * Make the qmk wrapper more robust * Add basic QMK Doctor * Clean up docstrings and flesh them out as needed * remove unused compile_firmware() function
Diffstat (limited to 'lib/python/qmk/keymap.py')
-rw-r--r--lib/python/qmk/keymap.py100
1 files changed, 100 insertions, 0 deletions
diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py
new file mode 100644
index 0000000000..6eccab788a
--- /dev/null
+++ b/lib/python/qmk/keymap.py
@@ -0,0 +1,100 @@
+"""Functions that help you work with QMK keymaps.
+"""
+import json
+import logging
+import os
+from traceback import format_exc
+
+import qmk.path
+from qmk.errors import NoSuchKeyboardError
+
+# The `keymap.c` template to use when a keyboard doesn't have its own
+DEFAULT_KEYMAP_C = """#include QMK_KEYBOARD_H
+
+/* THIS FILE WAS GENERATED!
+ *
+ * This file was generated by qmk-compile-json. You may or may not want to
+ * edit it directly.
+ */
+
+const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
+__KEYMAP_GOES_HERE__
+};
+"""
+
+
+def template(keyboard):
+ """Returns the `keymap.c` template for a keyboard.
+
+ If a template exists in `keyboards/<keyboard>/templates/keymap.c` that
+ text will be used instead of `DEFAULT_KEYMAP_C`.
+
+ Args:
+ keyboard
+ The keyboard to return a template for.
+ """
+ template_name = 'keyboards/%s/templates/keymap.c' % keyboard
+
+ if os.path.exists(template_name):
+ with open(template_name, 'r') as fd:
+ return fd.read()
+
+ return DEFAULT_KEYMAP_C
+
+
+def generate(keyboard, layout, layers):
+ """Returns a keymap.c for the specified keyboard, layout, and layers.
+
+ Args:
+ keyboard
+ The name of the keyboard
+
+ layout
+ The LAYOUT macro this keymap uses.
+
+ layers
+ An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.
+ """
+ layer_txt = []
+ for layer_num, layer in enumerate(layers):
+ if layer_num != 0:
+ layer_txt[-1] = layer_txt[-1] + ','
+ layer_keys = ', '.join(layer)
+ layer_txt.append('\t[%s] = %s(%s)' % (layer_num, layout, layer_keys))
+
+ keymap = '\n'.join(layer_txt)
+ keymap_c = template(keyboard, keymap)
+
+ return keymap_c.replace('__KEYMAP_GOES_HERE__', keymap)
+
+
+def write(keyboard, keymap, layout, layers):
+ """Generate the `keymap.c` and write it to disk.
+
+ Returns the filename written to.
+
+ Args:
+ keyboard
+ The name of the keyboard
+
+ keymap
+ The name of the keymap
+
+ layout
+ The LAYOUT macro this keymap uses.
+
+ layers
+ An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.
+ """
+ keymap_c = generate(keyboard, layout, layers)
+ keymap_path = qmk.path.keymap(keyboard)
+ keymap_dir = os.path.join(keymap_path, keymap)
+ keymap_file = os.path.join(keymap_dir, 'keymap.c')
+
+ if not os.path.exists(keymap_dir):
+ os.makedirs(keymap_dir)
+
+ with open(keymap_file, 'w') as keymap_fd:
+ keymap_fd.write(keymap_c)
+
+ return keymap_file