From 62c1bcba71245e64be9a4813d749c0f04805fbcb Mon Sep 17 00:00:00 2001 From: Erovia Date: Wed, 9 Oct 2019 20:53:18 +0200 Subject: Get all buildable keymaps for each revision The command now return all keymaps that's buildable for a keyboard/revision. If the base directory of a keyboard does not contain a 'rules.mk' file, nothing is returned. If the base directory contains a 'keymaps' directory, those keycaps will be returned for every revision. --- lib/python/qmk/cli/list/keymaps.py | 59 +++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 27 deletions(-) (limited to 'lib/python') diff --git a/lib/python/qmk/cli/list/keymaps.py b/lib/python/qmk/cli/list/keymaps.py index d55ff91fc3..2f3014f464 100644 --- a/lib/python/qmk/cli/list/keymaps.py +++ b/lib/python/qmk/cli/list/keymaps.py @@ -10,72 +10,77 @@ from milc import cli def unicode_text(filename): """Returns the contents of filename as a UTF-8 string. Tries to DTRT when it comes to encoding. """ - with open(filename, 'rb') as fd: + with open(filename, "rb") as fd: text = UnicodeDammit(fd.read()) if text.contains_replacement_characters: - log_warning('%s: Could not determine file encoding, some characters were replaced.' % (filename,)) + log_warning("%s: Could not determine file encoding, some characters were replaced." % (filename,)) - return text.unicode_markup or '' + return text.unicode_markup or "" def unicode_lines(filename): """Returns the contents of filename as a UTF-8 string. Tries to DTRT when it comes to encoding. """ - return unicode_text(filename).split('\n') + return unicode_text(filename).split("\n") -def parse_rules_mk(keyboard): +def parse_rules_mk(keyboard, revision = ""): base_path = os.path.join(os.getcwd(), "keyboards", keyboard) + os.path.sep rules_mk_path_wildcard = os.path.join(base_path, "**", "rules.mk") - paths = [path for path in glob.iglob(rules_mk_path_wildcard, recursive=True) if "keymaps" not in path] + rules_mk_regex = re.compile(r"^" + base_path + "(?:" + revision + os.path.sep + ")?rules.mk") + paths = [path for path in glob.iglob(rules_mk_path_wildcard, recursive = True) if rules_mk_regex.search(path)] rules_mk = dict() - for file in paths: - rules_mk_content = unicode_lines(file) + config_regex = re.compile(r"^\s*(\S+)\s*=\s*((?:\s*\S+)+)") + for file_path in paths: + rules_mk_content = unicode_lines(file_path) parsed_file = dict() for line in rules_mk_content: - found = re.search(r'^\s*(\w+)\s*=\s*((?:\s*\w+)+)', line) + found = config_regex.search(line) if found: parsed_file[found.group(1)] = found.group(2) - version = file.replace(base_path, "").replace(os.path.sep, "").replace("rules.mk", "") + version = file_path.replace(base_path, "").replace(os.path.sep, "").replace("rules.mk", "") rules_mk[version if version else "base"] = parsed_file return rules_mk -def find_keymaps(base_path, name_finder, community = False): +def find_keymaps(base_path, revision = "", community = False): path_wildcard = os.path.join(base_path, "**", "keymap.c") if community: - paths = [path for path in glob.iglob(path_wildcard, recursive=True)] + path_regex = re.compile(r"^" + re.escape(base_path) + "(\S+)" + os.path.sep + "keymap\.c") + names = [revision + os.path.sep + path_regex.sub(lambda name: name.group(1), path) for path in glob.iglob(path_wildcard, recursive = True)] else: - paths = [path for path in glob.iglob(path_wildcard, recursive=True) if 'keymaps' in path] - return list(map(name_finder, paths)) + 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 = [revision + os.path.sep + path_regex.sub(lambda name: name.group(1), path) if revision else path_regex.sub(lambda name: name.group(1), path) for path in glob.iglob(path_wildcard, recursive = True) if path_regex.search(path)] + return names -@cli.argument('-kb', '--keyboard', help='Specify keyboard name. Example: 1upkeyboards/1up60hse') +@cli.argument("-kb", "--keyboard", help="Specify keyboard name. Example: 1upkeyboards/1up60hse") +@cli.argument("-rv", "--revision", help="Specify the revison name. All revision if not specified. Example: rev6") @cli.subcommand("List the keymaps for a specific keyboard") def list_keymaps(cli): """List the keymaps for a specific keyboard """ # ask for user input if keyboard was not provided in the command line keyboard = cli.config.list_keymaps.keyboard if cli.config.list_keymaps.keyboard else input("Keyboard Name: ") - - kb_base_path = os.path.join(os.getcwd(), "keyboards", keyboard) + os.path.sep - km_find_name = lambda path: path.replace(kb_base_path, keyboard + os.path.sep).replace("/keymaps/", ":").replace(os.path.sep + "keymap.c", "") - names = find_keymaps(kb_base_path, km_find_name) + revision = cli.config.list_keymaps.revision if cli.config.list_keymaps.revision else "" # get all the rules.mk files for the keyboard - rules_mk = parse_rules_mk(keyboard) + rules_mk = parse_rules_mk(keyboard, revision) + + if "base" in rules_mk or revision: + keyboard_name = keyboard + os.path.sep + revision + kb_base_path = os.path.join(os.getcwd(), "keyboards", keyboard) + os.path.sep + names = find_keymaps(kb_base_path, revision) + else: + names = list() - for version, data in rules_mk.items(): + for rev, data in rules_mk.items(): if "LAYOUTS" in data: - if version == "base": - cl_find_name = lambda path: path.replace(cl_base_path, keyboard + ":").replace(os.path.sep + "keymap.c", "") - else: - cl_find_name = lambda path: path.replace(cl_base_path, keyboard + os.path.sep + version + ":").replace(os.path.sep + "keymap.c", "") for layout in data["LAYOUTS"].split(): cl_base_path = os.path.join(os.getcwd(), "layouts", "community", layout) + os.path.sep - names = names + find_keymaps(cl_base_path, cl_find_name, community = True) + names = names + find_keymaps(cl_base_path, rev, community = True) names.sort() for name in names: # We echo instead of cli.log.info to allow easier piping of this output - cli.echo(name) + cli.echo(keyboard + os.path.sep + name) -- cgit v1.2.3