From 4c3335b00bbcb4fa96056babcae6b10b4ab877a1 Mon Sep 17 00:00:00 2001 From: Erovia Date: Tue, 8 Oct 2019 21:50:21 +0200 Subject: CLI: add support for list_keymaps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List all the available keymaps for a given keyboard Add bs4 to requirements.txt UnicodeDammit is needed from bs4 for reading files. Major update to work better with revisions Find the community keymaps supported by each revision. 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. Fix help message. Try to figure out revision, drop -rv/--revision argument Fix output format 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. Add test for list_keymaps Fix regex for parsing rules.mk files I don't know why it couldn't put it together before... ¯\_(ツ)_/¯ Drop bs4 dependency, update docs, minor improvements Return only the unique keymaps Fix merging community and base keymaps 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. Fix commandline parsing and flake8 findings, rebase Fixed commandline and config parsing. Thx @xplusplus. Rebased on master and fixed merge conflicts. Code cleanup, use pathlib, use pytest keyboard Clean up checks and logics that are unnecessary due to MILC updates. Use pathlib instead of os.path for readability. Use the 'pytest' keyboard for the tests. Add community layout for 'handwired/onekey/pytest' so we can test community layouts. Pathlib-ify qmk.keymap.list_keymaps() fix list_keymaps for python 3.5 --- lib/python/qmk/makefile.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 lib/python/qmk/makefile.py (limited to 'lib/python/qmk/makefile.py') diff --git a/lib/python/qmk/makefile.py b/lib/python/qmk/makefile.py new file mode 100644 index 0000000000..8645056d2d --- /dev/null +++ b/lib/python/qmk/makefile.py @@ -0,0 +1,83 @@ +""" Functions for working with Makefiles +""" +from pathlib import Path + +from qmk.errors import NoSuchKeyboardError + + +def parse_rules_mk_file(file, rules_mk=None): + """Turn a rules.mk file into a dictionary. + + Args: + file: path to the rules.mk file + rules_mk: already parsed rules.mk the new file should be merged with + + Returns: + a dictionary with the file's content + """ + if not rules_mk: + rules_mk = {} + + file = Path(file) + if file.exists(): + rules_mk_lines = file.read_text().split("\n") + + for line in rules_mk_lines: + # Filter out comments + if line.strip().startswith("#"): + continue + + # Strip in-line comments + if '#' in line: + line = line[:line.index('#')].strip() + + if '=' in line: + # Append + if '+=' in line: + key, value = line.split('+=', 1) + if key.strip() not in rules_mk: + rules_mk[key.strip()] = value.strip() + else: + rules_mk[key.strip()] += ' ' + value.strip() + # Set if absent + elif "?=" in line: + key, value = line.split('?=', 1) + if key.strip() not in rules_mk: + rules_mk[key.strip()] = value.strip() + else: + if ":=" in line: + line.replace(":", "") + key, value = line.split('=', 1) + rules_mk[key.strip()] = value.strip() + + return rules_mk + + +def get_rules_mk(keyboard): + """ Get a rules.mk for a keyboard + + Args: + keyboard: name of the keyboard + + Raises: + NoSuchKeyboardError: when the keyboard does not exists + + Returns: + a dictionary with the content of the rules.mk file + """ + # Start with qmk_firmware/keyboards + kb_path = Path.cwd() / "keyboards" + # walk down the directory tree + # and collect all rules.mk files + kb_dir = kb_path / keyboard + if kb_dir.exists(): + rules_mk = dict() + for directory in Path(keyboard).parts: + kb_path = kb_path / directory + rules_mk_path = kb_path / "rules.mk" + if rules_mk_path.exists(): + rules_mk = parse_rules_mk_file(rules_mk_path, rules_mk) + else: + raise NoSuchKeyboardError("The requested keyboard and/or revision does not exist.") + + return rules_mk -- cgit v1.2.3