diff options
| -rw-r--r-- | docs/cli.md | 10 | ||||
| -rw-r--r-- | lib/python/qmk/cli/list/keymaps.py | 84 | ||||
| -rw-r--r-- | lib/python/qmk/keymap.py | 59 | ||||
| -rw-r--r-- | lib/python/qmk/makefile.py | 77 | ||||
| -rw-r--r-- | lib/python/qmk/path.py | 18 |
5 files changed, 172 insertions, 76 deletions
diff --git a/docs/cli.md b/docs/cli.md index f1c158af4..f2b433fbb 100644 --- a/docs/cli.md +++ b/docs/cli.md | |||
| @@ -252,6 +252,16 @@ This command lists all the keyboards currently defined in `qmk_firmware` | |||
| 252 | qmk list-keyboards | 252 | qmk list-keyboards |
| 253 | ``` | 253 | ``` |
| 254 | 254 | ||
| 255 | ## `qmk list_keymaps` | ||
| 256 | |||
| 257 | This command lists all the keymaps for a specified keyboard (and revision). | ||
| 258 | |||
| 259 | **Usage**: | ||
| 260 | |||
| 261 | ``` | ||
| 262 | qmk list_keymaps -kb planck/ez | ||
| 263 | ``` | ||
| 264 | |||
| 255 | ## `qmk new-keymap` | 265 | ## `qmk new-keymap` |
| 256 | 266 | ||
| 257 | This command creates a new keymap based on a keyboard's existing default keymap. | 267 | This command creates a new keymap based on a keyboard's existing default keymap. |
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 | """ |
| 3 | import os | ||
| 4 | import re | ||
| 5 | import glob | ||
| 6 | from bs4 import UnicodeDammit | ||
| 7 | |||
| 8 | from milc import cli | 3 | from milc import cli |
| 9 | 4 | import qmk.keymap | |
| 10 | def unicode_text(filename): | 5 | from 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 | |||
| 22 | def 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 | |||
| 27 | def 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 | |||
| 47 | def 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) | ||
diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index f4a2893f3..3927f791a 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py | |||
| @@ -1,8 +1,13 @@ | |||
| 1 | """Functions that help you work with QMK keymaps. | 1 | """Functions that help you work with QMK keymaps. |
| 2 | """ | 2 | """ |
| 3 | import os | 3 | import os |
| 4 | from traceback import format_exc | ||
| 5 | import re | ||
| 6 | import glob | ||
| 4 | 7 | ||
| 5 | import qmk.path | 8 | import qmk.path |
| 9 | import qmk.makefile | ||
| 10 | from qmk.errors import NoSuchKeyboardError | ||
| 6 | 11 | ||
| 7 | # The `keymap.c` template to use when a keyboard doesn't have its own | 12 | # The `keymap.c` template to use when a keyboard doesn't have its own |
| 8 | DEFAULT_KEYMAP_C = """#include QMK_KEYBOARD_H | 13 | DEFAULT_KEYMAP_C = """#include QMK_KEYBOARD_H |
| @@ -94,3 +99,57 @@ def write(keyboard, keymap, layout, layers): | |||
| 94 | keymap_fd.write(keymap_c) | 99 | keymap_fd.write(keymap_c) |
| 95 | 100 | ||
| 96 | return keymap_file | 101 | return keymap_file |
| 102 | |||
| 103 | def 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 list with whe 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 names | ||
| 123 | |||
| 124 | def list_keymaps(keyboard_name): | ||
| 125 | """ List the available keymaps for a keyboard. | ||
| 126 | |||
| 127 | Args: | ||
| 128 | keyboard_name: the keyboards full name with vendor and revision if necessary, example: clueboard/66/rev3 | ||
| 129 | |||
| 130 | Returns: | ||
| 131 | a list with the names of the available keymaps | ||
| 132 | """ | ||
| 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 | ||
| 140 | rules_mk = qmk.makefile.get_rules_mk(keyboard, revision) | ||
| 141 | names = list() | ||
| 142 | |||
| 143 | if rules_mk: | ||
| 144 | # get the keymaps from the keyboard's directory | ||
| 145 | kb_base_path = os.path.join(os.getcwd(), "keyboards", keyboard) + os.path.sep | ||
| 146 | names = find_keymaps(kb_base_path, revision) | ||
| 147 | |||
| 148 | # if community layouts are supported, get them | ||
| 149 | if "LAYOUTS" in rules_mk: | ||
| 150 | for layout in rules_mk["LAYOUTS"]["value"].split(): | ||
| 151 | cl_base_path = os.path.join(os.getcwd(), "layouts", "community", layout) + os.path.sep | ||
| 152 | names = names + find_keymaps(cl_base_path, revision, community = True) | ||
| 153 | |||
| 154 | names.sort() | ||
| 155 | return names | ||
diff --git a/lib/python/qmk/makefile.py b/lib/python/qmk/makefile.py new file mode 100644 index 000000000..ce64431f9 --- /dev/null +++ b/lib/python/qmk/makefile.py | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | """ Functions for working with Makefiles | ||
| 2 | """ | ||
| 3 | import os | ||
| 4 | import glob | ||
| 5 | import re | ||
| 6 | |||
| 7 | import qmk.path | ||
| 8 | from qmk.errors import NoSuchKeyboardError | ||
| 9 | |||
| 10 | def parse_rules_mk(file_path): | ||
| 11 | """ Parse a rules.mk file | ||
| 12 | |||
| 13 | Args: | ||
| 14 | file_path: path to the rules.mk file | ||
| 15 | |||
| 16 | Returns: | ||
| 17 | a dictionary with the file's content | ||
| 18 | """ | ||
| 19 | # regex to match lines with comment at the end | ||
| 20 | # group(1) = option's name | ||
| 21 | # group(2) = operator (eg.: '=', '+=') | ||
| 22 | # group(3) = value(s) | ||
| 23 | commented_regex = re.compile(r"^\s*(\w+)\s*([\:\+\-]?=)\s*(.*?)(?=\s*\#)") | ||
| 24 | # regex to match lines without comment at the end | ||
| 25 | # group(1) = option's name | ||
| 26 | # group(2) = operator (eg.: '=', '+=') | ||
| 27 | # group(3) = value(s) | ||
| 28 | uncommented_regex = re.compile(r"^\s*(\w+)\s*([\:\+\-]?=)\s*(.*?)(?=\s*$)") | ||
| 29 | mk_content = qmk.path.unicode_lines(file_path) | ||
| 30 | parsed_file = dict() | ||
| 31 | for line in mk_content: | ||
| 32 | found = commented_regex.search(line) if "#" in line else uncommented_regex.search(line) | ||
| 33 | if found: | ||
| 34 | parsed_file[found.group(1)] = dict(operator = found.group(2), value = found.group(3)) | ||
| 35 | return parsed_file | ||
| 36 | |||
| 37 | def merge_rules_mk_files(base, revision): | ||
| 38 | """ Merge a keyboard revision's rules.mk file with | ||
| 39 | the 'base' rules.mk file | ||
| 40 | |||
| 41 | Args: | ||
| 42 | base: the base rules.mk file's content as dictionary | ||
| 43 | revision: the revision's rules.mk file's content as dictionary | ||
| 44 | |||
| 45 | Returns: | ||
| 46 | a dictionary with the merged content | ||
| 47 | """ | ||
| 48 | return {**base, **revision} | ||
| 49 | |||
| 50 | def get_rules_mk(keyboard, revision = ""): | ||
| 51 | """ Get a rules.mk for a keyboard | ||
| 52 | |||
| 53 | Args: | ||
| 54 | keyboard: name of the keyboard | ||
| 55 | revision: revision of the keyboard | ||
| 56 | |||
| 57 | Returns: | ||
| 58 | a dictionary with the content of the rules.mk file | ||
| 59 | """ | ||
| 60 | base_path = os.path.join(os.getcwd(), "keyboards", keyboard) + os.path.sep | ||
| 61 | rules_mk = dict() | ||
| 62 | if os.path.exists(base_path + os.path.sep + revision): | ||
| 63 | rules_mk_path_wildcard = os.path.join(base_path, "**", "rules.mk") | ||
| 64 | rules_mk_regex = re.compile(r"^" + base_path + "(?:" + revision + os.path.sep + ")?rules.mk") | ||
| 65 | paths = [path for path in glob.iglob(rules_mk_path_wildcard, recursive = True) if rules_mk_regex.search(path)] | ||
| 66 | for file_path in paths: | ||
| 67 | rules_mk[revision if revision in file_path else "base"] = parse_rules_mk(file_path) | ||
| 68 | else: | ||
| 69 | raise NoSuchKeyboardError("The requested keyboard and/or revision does not exist.") | ||
| 70 | |||
| 71 | # if the base or the revision directory does not contain a rules.mk | ||
| 72 | if len(rules_mk) == 1: | ||
| 73 | rules_mk = rules_mk[revision] | ||
| 74 | # if both directories contain rules.mk files | ||
| 75 | elif len(rules_mk) == 2: | ||
| 76 | rules_mk = merge_rules_mk_files(rules_mk["base"], rules_mk[revision]) | ||
| 77 | return rules_mk | ||
diff --git a/lib/python/qmk/path.py b/lib/python/qmk/path.py index cf087265f..e7e3ec53f 100644 --- a/lib/python/qmk/path.py +++ b/lib/python/qmk/path.py | |||
| @@ -5,6 +5,7 @@ import os | |||
| 5 | 5 | ||
| 6 | from qmk.errors import NoSuchKeyboardError | 6 | from qmk.errors import NoSuchKeyboardError |
| 7 | 7 | ||
| 8 | from bs4 import UnicodeDammit | ||
| 8 | 9 | ||
| 9 | def keymap(keyboard): | 10 | def keymap(keyboard): |
| 10 | """Locate the correct directory for storing a keymap. | 11 | """Locate the correct directory for storing a keymap. |
| @@ -33,3 +34,20 @@ def normpath(path): | |||
| 33 | return os.path.normpath(path) | 34 | return os.path.normpath(path) |
| 34 | 35 | ||
| 35 | return os.path.normpath(os.path.join(os.environ['ORIG_CWD'], path)) | 36 | return os.path.normpath(os.path.join(os.environ['ORIG_CWD'], path)) |
| 37 | |||
| 38 | def unicode_text(filename): | ||
| 39 | """Returns the contents of filename as a UTF-8 string. Tries to DTRT when it comes to encoding. | ||
| 40 | """ | ||
| 41 | with open(filename, "rb") as fd: | ||
| 42 | text = UnicodeDammit(fd.read()) | ||
| 43 | |||
| 44 | if text.contains_replacement_characters: | ||
| 45 | log_warning("%s: Could not determine file encoding, some characters were replaced." % (filename,)) | ||
| 46 | |||
| 47 | return text.unicode_markup or "" | ||
| 48 | |||
| 49 | |||
| 50 | def unicode_lines(filename): | ||
| 51 | """Returns the contents of filename as a UTF-8 string. Tries to DTRT when it comes to encoding. | ||
| 52 | """ | ||
| 53 | return unicode_text(filename).split("\n") | ||
