summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRyan <fauxpark@gmail.com>2023-05-20 22:14:43 +1000
committerGitHub <noreply@github.com>2023-05-20 22:14:43 +1000
commit102c42b14bc79a178d16ae5217d739490a312ea4 (patch)
tree89db38db0fd565ecd27b613d071479cc00c5bf05 /lib
parentb93f05dc35e911a5c5f758722545d96c66be88c6 (diff)
`qmk find`: usability improvements (#20440)
Diffstat (limited to 'lib')
-rw-r--r--lib/python/qmk/cli/find.py12
-rw-r--r--lib/python/qmk/makefile.py2
-rw-r--r--lib/python/qmk/search.py56
3 files changed, 50 insertions, 20 deletions
diff --git a/lib/python/qmk/cli/find.py b/lib/python/qmk/cli/find.py
index b6f74380ab..b8340f5f33 100644
--- a/lib/python/qmk/cli/find.py
+++ b/lib/python/qmk/cli/find.py
@@ -11,13 +11,17 @@ from qmk.search import search_keymap_targets
action='append',
default=[],
help= # noqa: `format-python` and `pytest` don't agree here.
- "Filter the list of keyboards based on the supplied value in rules.mk. Matches info.json structure, and accepts the formats 'features.rgblight=true' or 'exists(matrix_pins.direct)'. May be passed multiple times, all filters need to match. Value may include wildcards such as '*' and '?'." # noqa: `format-python` and `pytest` don't agree here.
+ "Filter the list of keyboards based on their info.json data. Accepts the formats key=value, function(key), or function(key,value), eg. 'features.rgblight=true'. Valid functions are 'absent', 'contains', 'exists' and 'length'. May be passed multiple times; all filters need to match. Value may include wildcards such as '*' and '?'." # noqa: `format-python` and `pytest` don't agree here.
)
+@cli.argument('-p', '--print', arg_only=True, action='append', default=[], help="For each matched target, print the value of the supplied info.json key. May be passed multiple times.")
@cli.argument('-km', '--keymap', type=str, default='default', help="The keymap name to build. Default is 'default'.")
@cli.subcommand('Find builds which match supplied search criteria.')
def find(cli):
"""Search through all keyboards and keymaps for a given search criteria.
"""
- targets = search_keymap_targets(cli.args.keymap, cli.args.filter)
- for target in targets:
- print(f'{target[0]}:{target[1]}')
+ targets = search_keymap_targets(cli.args.keymap, cli.args.filter, cli.args.print)
+ for keyboard, keymap, print_vals in targets:
+ print(f'{keyboard}:{keymap}')
+
+ for key, val in print_vals:
+ print(f' {key}={val}')
diff --git a/lib/python/qmk/makefile.py b/lib/python/qmk/makefile.py
index 02c2e70050..ae95abbf23 100644
--- a/lib/python/qmk/makefile.py
+++ b/lib/python/qmk/makefile.py
@@ -18,7 +18,7 @@ def parse_rules_mk_file(file, rules_mk=None):
file = Path(file)
if file.exists():
- rules_mk_lines = file.read_text().split("\n")
+ rules_mk_lines = file.read_text(encoding='utf-8').split("\n")
for line in rules_mk_lines:
# Filter out comments
diff --git a/lib/python/qmk/search.py b/lib/python/qmk/search.py
index af48900e6b..8728890b27 100644
--- a/lib/python/qmk/search.py
+++ b/lib/python/qmk/search.py
@@ -45,7 +45,7 @@ def _load_keymap_info(keyboard, keymap):
return (keyboard, keymap, keymap_json(keyboard, keymap))
-def search_keymap_targets(keymap='default', filters=[]):
+def search_keymap_targets(keymap='default', filters=[], print_vals=[]):
targets = []
with multiprocessing.Pool() as pool:
@@ -66,14 +66,43 @@ def search_keymap_targets(keymap='default', filters=[]):
cli.log.info('Parsing data for all matching keyboard/keymap combinations...')
valid_keymaps = [(e[0], e[1], dotty(e[2])) for e in pool.starmap(_load_keymap_info, target_list)]
+ function_re = re.compile(r'^(?P<function>[a-zA-Z]+)\((?P<key>[a-zA-Z0-9_\.]+)(,\s*(?P<value>[^#]+))?\)$')
equals_re = re.compile(r'^(?P<key>[a-zA-Z0-9_\.]+)\s*=\s*(?P<value>[^#]+)$')
- exists_re = re.compile(r'^exists\((?P<key>[a-zA-Z0-9_\.]+)\)$')
- for filter_txt in filters:
- f = equals_re.match(filter_txt)
- if f is not None:
- key = f.group('key')
- value = f.group('value')
- cli.log.info(f'Filtering on condition ("{key}" == "{value}")...')
+
+ for filter_expr in filters:
+ function_match = function_re.match(filter_expr)
+ equals_match = equals_re.match(filter_expr)
+
+ if function_match is not None:
+ func_name = function_match.group('function').lower()
+ key = function_match.group('key')
+ value = function_match.group('value')
+
+ if value is not None:
+ if func_name == 'length':
+ valid_keymaps = filter(lambda e: key in e[2] and len(e[2].get(key)) == int(value), valid_keymaps)
+ elif func_name == 'contains':
+ valid_keymaps = filter(lambda e: key in e[2] and value in e[2].get(key), valid_keymaps)
+ else:
+ cli.log.warning(f'Unrecognized filter expression: {function_match.group(0)}')
+ continue
+
+ cli.log.info(f'Filtering on condition: {{fg_green}}{func_name}{{fg_reset}}({{fg_cyan}}{key}{{fg_reset}}, {{fg_cyan}}{value}{{fg_reset}})...')
+ else:
+ if func_name == 'exists':
+ valid_keymaps = filter(lambda e: key in e[2], valid_keymaps)
+ elif func_name == 'absent':
+ valid_keymaps = filter(lambda e: key not in e[2], valid_keymaps)
+ else:
+ cli.log.warning(f'Unrecognized filter expression: {function_match.group(0)}')
+ continue
+
+ cli.log.info(f'Filtering on condition: {{fg_green}}{func_name}{{fg_reset}}({{fg_cyan}}{key}{{fg_reset}})...')
+
+ elif equals_match is not None:
+ key = equals_match.group('key')
+ value = equals_match.group('value')
+ cli.log.info(f'Filtering on condition: {{fg_cyan}}{key}{{fg_reset}} == {{fg_cyan}}{value}{{fg_reset}}...')
def _make_filter(k, v):
expr = fnmatch.translate(v)
@@ -87,13 +116,10 @@ def search_keymap_targets(keymap='default', filters=[]):
return f
valid_keymaps = filter(_make_filter(key, value), valid_keymaps)
+ else:
+ cli.log.warning(f'Unrecognized filter expression: {filter_expr}')
+ continue
- f = exists_re.match(filter_txt)
- if f is not None:
- key = f.group('key')
- cli.log.info(f'Filtering on condition (exists: "{key}")...')
- valid_keymaps = filter(lambda e: e[2].get(key) is not None, valid_keymaps)
-
- targets = [(e[0], e[1]) for e in valid_keymaps]
+ targets = [(e[0], e[1], [(p, e[2].get(p)) for p in print_vals]) for e in valid_keymaps]
return targets