diff options
| author | Erovia <erovia@users.noreply.github.com> | 2019-10-13 19:07:22 +0200 |
|---|---|---|
| committer | skullydazed <skullydazed@users.noreply.github.com> | 2020-02-15 15:19:03 -0800 |
| commit | 26f53d38d932a21be9edfc3d55e585c21050c3a2 (patch) | |
| tree | f4d19abe22da554fe9a40928f6fc3eca6163e2ab /lib/python/qmk/keymap.py | |
| parent | f96085af3877a8c42d0767d13d47c239ce08ef0f (diff) | |
| download | qmk_firmware-26f53d38d932a21be9edfc3d55e585c21050c3a2.tar.gz qmk_firmware-26f53d38d932a21be9edfc3d55e585c21050c3a2.zip | |
Another major refactoring, add documentation
Move all useful functions to the qmk module and use the cli subcommand
as a wrapper around it.
Add both inline comments and documentation.
Diffstat (limited to 'lib/python/qmk/keymap.py')
| -rw-r--r-- | lib/python/qmk/keymap.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index f4a2893f3..3927f791a 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py | |||
| @@ -1,8 +1,13 @@ | |||
| 1 | """Functions that help you work with QMK keymaps. | 1 | """Functions that help you work with QMK keymaps. |
| 2 | """ | 2 | """ |
| 3 | import os | 3 | import os |
| 4 | from traceback import format_exc | ||
| 5 | import re | ||
| 6 | import glob | ||
| 4 | 7 | ||
| 5 | import qmk.path | 8 | import qmk.path |
| 9 | import qmk.makefile | ||
| 10 | from qmk.errors import NoSuchKeyboardError | ||
| 6 | 11 | ||
| 7 | # The `keymap.c` template to use when a keyboard doesn't have its own | 12 | # The `keymap.c` template to use when a keyboard doesn't have its own |
| 8 | DEFAULT_KEYMAP_C = """#include QMK_KEYBOARD_H | 13 | DEFAULT_KEYMAP_C = """#include QMK_KEYBOARD_H |
| @@ -94,3 +99,57 @@ def write(keyboard, keymap, layout, layers): | |||
| 94 | keymap_fd.write(keymap_c) | 99 | keymap_fd.write(keymap_c) |
| 95 | 100 | ||
| 96 | return keymap_file | 101 | return keymap_file |
| 102 | |||
| 103 | def find_keymaps(base_path, revision = "", community = False): | ||
| 104 | """ Find the available keymaps for a keyboard and revision pair. | ||
| 105 | |||
| 106 | Args: | ||
| 107 | base_path: The base path of the keyboard. | ||
| 108 | |||
| 109 | revision: The keyboard's revision. | ||
| 110 | |||
| 111 | community: Set to True for the layouts under layouts/community. | ||
| 112 | |||
| 113 | Returns: | ||
| 114 | a list with whe keymaps's names | ||
| 115 | """ | ||
| 116 | path_wildcard = os.path.join(base_path, "**", "keymap.c") | ||
| 117 | if community: | ||
| 118 | path_regex = re.compile(r"^" + re.escape(base_path) + "(\S+)" + os.path.sep + "keymap\.c") | ||
| 119 | else: | ||
| 120 | path_regex = re.compile(r"^" + re.escape(base_path) + "(?:" + re.escape(revision) + os.path.sep + ")?keymaps" + os.path.sep + "(\S+)" + os.path.sep + "keymap\.c") | ||
| 121 | names = [path_regex.sub(lambda name: name.group(1), path) for path in glob.iglob(path_wildcard, recursive = True) if path_regex.search(path)] | ||
| 122 | return names | ||
| 123 | |||
| 124 | def list_keymaps(keyboard_name): | ||
| 125 | """ List the available keymaps for a keyboard. | ||
| 126 | |||
| 127 | Args: | ||
| 128 | keyboard_name: the keyboards full name with vendor and revision if necessary, example: clueboard/66/rev3 | ||
| 129 | |||
| 130 | Returns: | ||
| 131 | a list with the names of the available keymaps | ||
| 132 | """ | ||
| 133 | if os.path.sep in keyboard_name: | ||
| 134 | keyboard, revision = os.path.split(os.path.normpath(keyboard_name)) | ||
| 135 | else: | ||
| 136 | keyboard = keyboard_name | ||
| 137 | revision = "" | ||
| 138 | |||
| 139 | # parse all the rules.mk files for the keyboard | ||
| 140 | rules_mk = qmk.makefile.get_rules_mk(keyboard, revision) | ||
| 141 | names = list() | ||
| 142 | |||
| 143 | if rules_mk: | ||
| 144 | # get the keymaps from the keyboard's directory | ||
| 145 | kb_base_path = os.path.join(os.getcwd(), "keyboards", keyboard) + os.path.sep | ||
| 146 | names = find_keymaps(kb_base_path, revision) | ||
| 147 | |||
| 148 | # if community layouts are supported, get them | ||
| 149 | if "LAYOUTS" in rules_mk: | ||
| 150 | for layout in rules_mk["LAYOUTS"]["value"].split(): | ||
| 151 | cl_base_path = os.path.join(os.getcwd(), "layouts", "community", layout) + os.path.sep | ||
| 152 | names = names + find_keymaps(cl_base_path, revision, community = True) | ||
| 153 | |||
| 154 | names.sort() | ||
| 155 | return names | ||
