summaryrefslogtreecommitdiff
path: root/lib/python/qmk/keymap.py
diff options
context:
space:
mode:
authorErovia <erovia@users.noreply.github.com>2019-10-28 09:23:06 +0100
committerskullydazed <skullydazed@users.noreply.github.com>2020-02-15 15:19:03 -0800
commit988bfffca2715df3f227c462533d350ecbeac6c0 (patch)
tree975be087ae9944b31516a44f193b595791d93ed2 /lib/python/qmk/keymap.py
parent8ff72d9517132fe82c6e2d1dc8fc56f7cab4b6a0 (diff)
Major rework, no regex/globbing, more walking
Instead of using regexes and globbing to find the rules.mk and keymap.c files, walk the directory tree to find them. Also, do away with the concept of revision.
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 1024f8fc6e..113b885de3 100644
--- a/lib/python/qmk/keymap.py
+++ b/lib/python/qmk/keymap.py
@@ -2,8 +2,6 @@
"""
import os
from traceback import format_exc
-import re
-import glob
import qmk.path
import qmk.makefile
@@ -100,27 +98,6 @@ def write(keyboard, keymap, layout, layers):
return keymap_file
-def find_keymaps(base_path, revision = "", community = False):
- """ Find the available keymaps for a keyboard and revision pair.
-
- Args:
- base_path: The base path of the keyboard.
-
- revision: The keyboard's revision.
-
- community: Set to True for the layouts under layouts/community.
-
- Returns:
- a set with the keymaps's names
- """
- path_wildcard = os.path.join(base_path, "**", "keymap.c")
- if community:
- path_regex = re.compile(r"^" + re.escape(base_path) + "(\S+)" + os.path.sep + "keymap\.c")
- else:
- 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")
- names = [path_regex.sub(lambda name: name.group(1), path) for path in glob.iglob(path_wildcard, recursive = True) if path_regex.search(path)]
- return set(names)
-
def list_keymaps(keyboard_name):
""" List the available keymaps for a keyboard.
@@ -130,25 +107,28 @@ def list_keymaps(keyboard_name):
Returns:
a set with the names of the available keymaps
"""
- if os.path.sep in keyboard_name:
- keyboard, revision = os.path.split(os.path.normpath(keyboard_name))
- else:
- keyboard = keyboard_name
- revision = ""
-
# parse all the rules.mk files for the keyboard
- rules_mk = qmk.makefile.get_rules_mk(keyboard, revision)
+ rules_mk = qmk.makefile.get_rules_mk(keyboard_name)
names = set()
if rules_mk:
- # get the keymaps from the keyboard's directory
- kb_base_path = os.path.join(os.getcwd(), "keyboards", keyboard) + os.path.sep
- names = find_keymaps(kb_base_path, revision)
+ # qmk_firmware/keyboards
+ keyboards_dir = os.path.join(os.getcwd(), "keyboards")
+ # path to the keyboard's directory
+ kb_path = os.path.join(keyboards_dir, keyboard_name)
+ # walk up the directory tree until keyboards_dir
+ # and collect all directories' name with keymap.c file in it
+ while kb_path != keyboards_dir:
+ keymaps_dir = os.path.join(kb_path, "keymaps")
+ if os.path.exists(keymaps_dir):
+ names = names.union([keymap for keymap in os.listdir(keymaps_dir) if os.path.isfile(os.path.join(keymaps_dir, keymap, "keymap.c"))])
+ kb_path = os.path.dirname(kb_path)
# if community layouts are supported, get them
if "LAYOUTS" in rules_mk:
- for layout in rules_mk["LAYOUTS"]["value"].split():
- cl_base_path = os.path.join(os.getcwd(), "layouts", "community", layout) + os.path.sep
- names = names.union(find_keymaps(cl_base_path, revision, community = True))
+ for layout in rules_mk["LAYOUTS"].split():
+ cl_path = os.path.join(os.getcwd(), "layouts", "community", layout)
+ if os.path.exists(cl_path):
+ names = names.union([keymap for keymap in os.listdir(cl_path) if os.path.isfile(os.path.join(cl_path, keymap, "keymap.c"))])
return sorted(names)