diff options
author | Zach White <skullydazed@gmail.com> | 2021-01-09 13:34:14 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-09 13:34:14 -0800 |
commit | 962bc8d9dd413690dbeadeaac971a5389697210f (patch) | |
tree | 02e7c5ebe87f466103901d72740fbf25c9d19a30 /lib/python/qmk/cli | |
parent | c550047ba68bd633d35b10d545ff240cf1fc9786 (diff) | |
download | qmk_firmware-962bc8d9dd413690dbeadeaac971a5389697210f.tar.gz qmk_firmware-962bc8d9dd413690dbeadeaac971a5389697210f.zip |
Use the schema to eliminate custom code (#11108)
* use the schema to eliminate custom code
* Update docs/reference_info_json.md
Co-authored-by: Ryan <fauxpark@gmail.com>
* make flake8 happy
* bugfix
* do not overwrite make vars from json
Co-authored-by: Ryan <fauxpark@gmail.com>
Diffstat (limited to 'lib/python/qmk/cli')
-rwxr-xr-x | lib/python/qmk/cli/generate/info_json.py | 48 | ||||
-rwxr-xr-x | lib/python/qmk/cli/generate/layouts.py | 4 | ||||
-rwxr-xr-x | lib/python/qmk/cli/generate/rules_mk.py | 10 |
3 files changed, 41 insertions, 21 deletions
diff --git a/lib/python/qmk/cli/generate/info_json.py b/lib/python/qmk/cli/generate/info_json.py index fba4b1c01..f3fc54ddc 100755 --- a/lib/python/qmk/cli/generate/info_json.py +++ b/lib/python/qmk/cli/generate/info_json.py | |||
@@ -4,14 +4,41 @@ Compile an info.json for a particular keyboard and pretty-print it. | |||
4 | """ | 4 | """ |
5 | import json | 5 | import json |
6 | 6 | ||
7 | from jsonschema import Draft7Validator, validators | ||
7 | from milc import cli | 8 | from milc import cli |
8 | 9 | ||
9 | from qmk.info_json_encoder import InfoJSONEncoder | ||
10 | from qmk.decorators import automagic_keyboard, automagic_keymap | 10 | from qmk.decorators import automagic_keyboard, automagic_keymap |
11 | from qmk.info import info_json | 11 | from qmk.info import info_json, _jsonschema |
12 | from qmk.info_json_encoder import InfoJSONEncoder | ||
12 | from qmk.path import is_keyboard | 13 | from qmk.path import is_keyboard |
13 | 14 | ||
14 | 15 | ||
16 | def pruning_validator(validator_class): | ||
17 | """Extends Draft7Validator to remove properties that aren't specified in the schema. | ||
18 | """ | ||
19 | validate_properties = validator_class.VALIDATORS["properties"] | ||
20 | |||
21 | def remove_additional_properties(validator, properties, instance, schema): | ||
22 | for prop in list(instance.keys()): | ||
23 | if prop not in properties: | ||
24 | del instance[prop] | ||
25 | |||
26 | for error in validate_properties(validator, properties, instance, schema): | ||
27 | yield error | ||
28 | |||
29 | return validators.extend(validator_class, {"properties": remove_additional_properties}) | ||
30 | |||
31 | |||
32 | def strip_info_json(kb_info_json): | ||
33 | """Remove the API-only properties from the info.json. | ||
34 | """ | ||
35 | pruning_draft_7_validator = pruning_validator(Draft7Validator) | ||
36 | schema = _jsonschema('keyboard') | ||
37 | validator = pruning_draft_7_validator(schema).validate | ||
38 | |||
39 | return validator(kb_info_json) | ||
40 | |||
41 | |||
15 | @cli.argument('-kb', '--keyboard', help='Keyboard to show info for.') | 42 | @cli.argument('-kb', '--keyboard', help='Keyboard to show info for.') |
16 | @cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.') | 43 | @cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.') |
17 | @cli.subcommand('Generate an info.json file for a keyboard.', hidden=False if cli.config.user.developer else True) | 44 | @cli.subcommand('Generate an info.json file for a keyboard.', hidden=False if cli.config.user.developer else True) |
@@ -22,7 +49,7 @@ def generate_info_json(cli): | |||
22 | """ | 49 | """ |
23 | # Determine our keyboard(s) | 50 | # Determine our keyboard(s) |
24 | if not cli.config.generate_info_json.keyboard: | 51 | if not cli.config.generate_info_json.keyboard: |
25 | cli.log.error('Missing paramater: --keyboard') | 52 | cli.log.error('Missing parameter: --keyboard') |
26 | cli.subcommands['info'].print_help() | 53 | cli.subcommands['info'].print_help() |
27 | return False | 54 | return False |
28 | 55 | ||
@@ -32,18 +59,7 @@ def generate_info_json(cli): | |||
32 | 59 | ||
33 | # Build the info.json file | 60 | # Build the info.json file |
34 | kb_info_json = info_json(cli.config.generate_info_json.keyboard) | 61 | kb_info_json = info_json(cli.config.generate_info_json.keyboard) |
35 | pared_down_json = {} | 62 | strip_info_json(kb_info_json) |
36 | |||
37 | for key in ('manufacturer', 'maintainer', 'usb', 'keyboard_name', 'width', 'height', 'debounce', 'diode_direction', 'features', 'community_layouts', 'layout_aliases', 'matrix_pins', 'rgblight', 'url'): | ||
38 | if key in kb_info_json: | ||
39 | pared_down_json[key] = kb_info_json[key] | ||
40 | |||
41 | pared_down_json['layouts'] = {} | ||
42 | if 'layouts' in kb_info_json: | ||
43 | for layout_name, layout in kb_info_json['layouts'].items(): | ||
44 | pared_down_json['layouts'][layout_name] = {} | ||
45 | pared_down_json['layouts'][layout_name]['key_count'] = layout.get('key_count', len(layout['layout'])) | ||
46 | pared_down_json['layouts'][layout_name]['layout'] = layout['layout'] | ||
47 | 63 | ||
48 | # Display the results | 64 | # Display the results |
49 | print(json.dumps(pared_down_json, indent=2, cls=InfoJSONEncoder)) | 65 | print(json.dumps(kb_info_json, indent=2, cls=InfoJSONEncoder)) |
diff --git a/lib/python/qmk/cli/generate/layouts.py b/lib/python/qmk/cli/generate/layouts.py index 273870e15..b7baae065 100755 --- a/lib/python/qmk/cli/generate/layouts.py +++ b/lib/python/qmk/cli/generate/layouts.py | |||
@@ -54,6 +54,10 @@ def generate_layouts(cli): | |||
54 | if kb_info_json['layouts'][layout_name]['c_macro']: | 54 | if kb_info_json['layouts'][layout_name]['c_macro']: |
55 | continue | 55 | continue |
56 | 56 | ||
57 | if 'matrix' not in kb_info_json['layouts'][layout_name]['layout'][0]: | ||
58 | cli.log.debug('%s/%s: No matrix data!', cli.config.generate_layouts.keyboard, layout_name) | ||
59 | continue | ||
60 | |||
57 | layout_keys = [] | 61 | layout_keys = [] |
58 | layout_matrix = [['KC_NO' for i in range(col_num)] for i in range(row_num)] | 62 | layout_matrix = [['KC_NO' for i in range(col_num)] for i in range(row_num)] |
59 | 63 | ||
diff --git a/lib/python/qmk/cli/generate/rules_mk.py b/lib/python/qmk/cli/generate/rules_mk.py index 2a7e91856..0fdccb404 100755 --- a/lib/python/qmk/cli/generate/rules_mk.py +++ b/lib/python/qmk/cli/generate/rules_mk.py | |||
@@ -37,26 +37,26 @@ def generate_rules_mk(cli): | |||
37 | 37 | ||
38 | # Bring in settings | 38 | # Bring in settings |
39 | for info_key, rule_key in info_to_rules.items(): | 39 | for info_key, rule_key in info_to_rules.items(): |
40 | rules_mk_lines.append(f'{rule_key} := {kb_info_json[info_key]}') | 40 | rules_mk_lines.append(f'{rule_key} ?= {kb_info_json[info_key]}') |
41 | 41 | ||
42 | # Find features that should be enabled | 42 | # Find features that should be enabled |
43 | if 'features' in kb_info_json: | 43 | if 'features' in kb_info_json: |
44 | for feature, enabled in kb_info_json['features'].items(): | 44 | for feature, enabled in kb_info_json['features'].items(): |
45 | if feature == 'bootmagic_lite' and enabled: | 45 | if feature == 'bootmagic_lite' and enabled: |
46 | rules_mk_lines.append('BOOTMAGIC_ENABLE := lite') | 46 | rules_mk_lines.append('BOOTMAGIC_ENABLE ?= lite') |
47 | else: | 47 | else: |
48 | feature = feature.upper() | 48 | feature = feature.upper() |
49 | enabled = 'yes' if enabled else 'no' | 49 | enabled = 'yes' if enabled else 'no' |
50 | rules_mk_lines.append(f'{feature}_ENABLE := {enabled}') | 50 | rules_mk_lines.append(f'{feature}_ENABLE ?= {enabled}') |
51 | 51 | ||
52 | # Set the LED driver | 52 | # Set the LED driver |
53 | if 'led_matrix' in kb_info_json and 'driver' in kb_info_json['led_matrix']: | 53 | if 'led_matrix' in kb_info_json and 'driver' in kb_info_json['led_matrix']: |
54 | driver = kb_info_json['led_matrix']['driver'] | 54 | driver = kb_info_json['led_matrix']['driver'] |
55 | rules_mk_lines.append(f'LED_MATRIX_DRIVER = {driver}') | 55 | rules_mk_lines.append(f'LED_MATRIX_DRIVER ?= {driver}') |
56 | 56 | ||
57 | # Add community layouts | 57 | # Add community layouts |
58 | if 'community_layouts' in kb_info_json: | 58 | if 'community_layouts' in kb_info_json: |
59 | rules_mk_lines.append(f'LAYOUTS = {" ".join(kb_info_json["community_layouts"])}') | 59 | rules_mk_lines.append(f'LAYOUTS ?= {" ".join(kb_info_json["community_layouts"])}') |
60 | 60 | ||
61 | # Show the results | 61 | # Show the results |
62 | rules_mk = '\n'.join(rules_mk_lines) + '\n' | 62 | rules_mk = '\n'.join(rules_mk_lines) + '\n' |