aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk/cli/list/keymaps.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python/qmk/cli/list/keymaps.py')
-rw-r--r--lib/python/qmk/cli/list/keymaps.py84
1 files changed, 8 insertions, 76 deletions
diff --git a/lib/python/qmk/cli/list/keymaps.py b/lib/python/qmk/cli/list/keymaps.py
index 5aed37b81..494fe66a1 100644
--- a/lib/python/qmk/cli/list/keymaps.py
+++ b/lib/python/qmk/cli/list/keymaps.py
@@ -1,57 +1,8 @@
1"""List the keymaps for a specific keyboard 1"""List the keymaps for a specific keyboard
2""" 2"""
3import os
4import re
5import glob
6from bs4 import UnicodeDammit
7
8from milc import cli 3from milc import cli
9 4import qmk.keymap
10def unicode_text(filename): 5from qmk.errors import NoSuchKeyboardError
11 """Returns the contents of filename as a UTF-8 string. Tries to DTRT when it comes to encoding.
12 """
13 with open(filename, "rb") as fd:
14 text = UnicodeDammit(fd.read())
15
16 if text.contains_replacement_characters:
17 log_warning("%s: Could not determine file encoding, some characters were replaced." % (filename,))
18
19 return text.unicode_markup or ""
20
21
22def unicode_lines(filename):
23 """Returns the contents of filename as a UTF-8 string. Tries to DTRT when it comes to encoding.
24 """
25 return unicode_text(filename).split("\n")
26
27def parse_rules_mk(keyboard, revision = ""):
28 base_path = os.path.join(os.getcwd(), "keyboards", keyboard) + os.path.sep
29 rules_mk = dict()
30 if os.path.exists(base_path + os.path.sep + revision):
31 rules_mk_path_wildcard = os.path.join(base_path, "**", "rules.mk")
32 rules_mk_regex = re.compile(r"^" + base_path + "(?:" + revision + os.path.sep + ")?rules.mk")
33 paths = [path for path in glob.iglob(rules_mk_path_wildcard, recursive = True) if rules_mk_regex.search(path)]
34
35 config_regex = re.compile(r"^\s*(\S+)\s*=\s*((?:\s*\S+)+)")
36 for file_path in paths:
37 rules_mk_content = unicode_lines(file_path)
38 parsed_file = dict()
39 for line in rules_mk_content:
40 found = config_regex.search(line)
41 if found:
42 parsed_file[found.group(1)] = found.group(2)
43 version = file_path.replace(base_path, "").replace(os.path.sep, "").replace("rules.mk", "")
44 rules_mk[version if version else "base"] = parsed_file
45 return rules_mk
46
47def find_keymaps(base_path, revision = "", community = False):
48 path_wildcard = os.path.join(base_path, "**", "keymap.c")
49 if community:
50 path_regex = re.compile(r"^" + re.escape(base_path) + "(\S+)" + os.path.sep + "keymap\.c")
51 else:
52 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")
53 names = [path_regex.sub(lambda name: name.group(1), path) for path in glob.iglob(path_wildcard, recursive = True) if path_regex.search(path)]
54 return names
55 6
56@cli.argument("-kb", "--keyboard", help="Specify keyboard name. Example: 1upkeyboards/1up60hse") 7@cli.argument("-kb", "--keyboard", help="Specify keyboard name. Example: 1upkeyboards/1up60hse")
57@cli.subcommand("List the keymaps for a specific keyboard") 8@cli.subcommand("List the keymaps for a specific keyboard")
@@ -60,29 +11,10 @@ def list_keymaps(cli):
60 """ 11 """
61 # ask for user input if keyboard was not provided in the command line 12 # ask for user input if keyboard was not provided in the command line
62 keyboard_name = cli.config.list_keymaps.keyboard if cli.config.list_keymaps.keyboard else input("Keyboard Name: ") 13 keyboard_name = cli.config.list_keymaps.keyboard if cli.config.list_keymaps.keyboard else input("Keyboard Name: ")
63 if os.path.sep in keyboard_name:
64 keyboard, revision = os.path.split(os.path.normpath(keyboard_name))
65 else:
66 keyboard = keyboard_name
67 revision = ""
68
69 # get all the rules.mk files for the keyboard
70 rules_mk = parse_rules_mk(keyboard, revision)
71 names = list()
72
73 if rules_mk:
74 if "base" in rules_mk or revision:
75 kb_base_path = os.path.join(os.getcwd(), "keyboards", keyboard) + os.path.sep
76 names = find_keymaps(kb_base_path, revision)
77
78 for rev, data in rules_mk.items():
79 if "LAYOUTS" in data:
80 for layout in data["LAYOUTS"].split():
81 cl_base_path = os.path.join(os.getcwd(), "layouts", "community", layout) + os.path.sep
82 names = names + find_keymaps(cl_base_path, rev, community = True)
83
84 names.sort()
85 14
86 for name in names: 15 try:
87 # We echo instead of cli.log.info to allow easier piping of this output 16 for name in qmk.keymap.list_keymaps(keyboard_name):
88 cli.echo(keyboard_name + ":" + name) 17 # We echo instead of cli.log.info to allow easier piping of this output
18 cli.echo(keyboard_name + ":" + name)
19 except NoSuchKeyboardError as e:
20 cli.echo("{fg_red}" + e.message)