aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk/keymap.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python/qmk/keymap.py')
-rw-r--r--lib/python/qmk/keymap.py52
1 files changed, 16 insertions, 36 deletions
diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py
index 1024f8fc6..113b885de 100644
--- a/lib/python/qmk/keymap.py
+++ b/lib/python/qmk/keymap.py
@@ -2,8 +2,6 @@
2""" 2"""
3import os 3import os
4from traceback import format_exc 4from traceback import format_exc
5import re
6import glob
7 5
8import qmk.path 6import qmk.path
9import qmk.makefile 7import qmk.makefile
@@ -100,27 +98,6 @@ def write(keyboard, keymap, layout, layers):
100 98
101 return keymap_file 99 return keymap_file
102 100
103def 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 set with the 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 set(names)
123
124def list_keymaps(keyboard_name): 101def list_keymaps(keyboard_name):
125 """ List the available keymaps for a keyboard. 102 """ List the available keymaps for a keyboard.
126 103
@@ -130,25 +107,28 @@ def list_keymaps(keyboard_name):
130 Returns: 107 Returns:
131 a set with the names of the available keymaps 108 a set with the names of the available keymaps
132 """ 109 """
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 110 # parse all the rules.mk files for the keyboard
140 rules_mk = qmk.makefile.get_rules_mk(keyboard, revision) 111 rules_mk = qmk.makefile.get_rules_mk(keyboard_name)
141 names = set() 112 names = set()
142 113
143 if rules_mk: 114 if rules_mk:
144 # get the keymaps from the keyboard's directory 115 # qmk_firmware/keyboards
145 kb_base_path = os.path.join(os.getcwd(), "keyboards", keyboard) + os.path.sep 116 keyboards_dir = os.path.join(os.getcwd(), "keyboards")
146 names = find_keymaps(kb_base_path, revision) 117 # path to the keyboard's directory
118 kb_path = os.path.join(keyboards_dir, keyboard_name)
119 # walk up the directory tree until keyboards_dir
120 # and collect all directories' name with keymap.c file in it
121 while kb_path != keyboards_dir:
122 keymaps_dir = os.path.join(kb_path, "keymaps")
123 if os.path.exists(keymaps_dir):
124 names = names.union([keymap for keymap in os.listdir(keymaps_dir) if os.path.isfile(os.path.join(keymaps_dir, keymap, "keymap.c"))])
125 kb_path = os.path.dirname(kb_path)
147 126
148 # if community layouts are supported, get them 127 # if community layouts are supported, get them
149 if "LAYOUTS" in rules_mk: 128 if "LAYOUTS" in rules_mk:
150 for layout in rules_mk["LAYOUTS"]["value"].split(): 129 for layout in rules_mk["LAYOUTS"].split():
151 cl_base_path = os.path.join(os.getcwd(), "layouts", "community", layout) + os.path.sep 130 cl_path = os.path.join(os.getcwd(), "layouts", "community", layout)
152 names = names.union(find_keymaps(cl_base_path, revision, community = True)) 131 if os.path.exists(cl_path):
132 names = names.union([keymap for keymap in os.listdir(cl_path) if os.path.isfile(os.path.join(cl_path, keymap, "keymap.c"))])
153 133
154 return sorted(names) 134 return sorted(names)