From 47b9b110097a864d6ab76516b2213afd59948527 Mon Sep 17 00:00:00 2001 From: Zach White Date: Wed, 30 Dec 2020 10:27:37 -0800 Subject: Configure keyboard matrix from info.json (#10817) * Make parameters from info.json available to the build system * move all clueboard settings to info.json * code formatting * make flake8 happy * make flake8 happy * make qmk lint happy * Add support for specifying led indicators in json * move led indicators to the clueboard info.json * Apply suggestions from code review Co-authored-by: Erovia * add missing docstring Co-authored-by: Erovia --- lib/python/qmk/cli/generate/info_json.py | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 lib/python/qmk/cli/generate/info_json.py (limited to 'lib/python/qmk/cli/generate/info_json.py') diff --git a/lib/python/qmk/cli/generate/info_json.py b/lib/python/qmk/cli/generate/info_json.py new file mode 100755 index 0000000000..7e6654e45d --- /dev/null +++ b/lib/python/qmk/cli/generate/info_json.py @@ -0,0 +1,49 @@ +"""Keyboard information script. + +Compile an info.json for a particular keyboard and pretty-print it. +""" +import json + +from milc import cli + +from qmk.info_json_encoder import InfoJSONEncoder +from qmk.decorators import automagic_keyboard, automagic_keymap +from qmk.info import info_json +from qmk.path import is_keyboard + + +@cli.argument('-kb', '--keyboard', help='Keyboard to show info for.') +@cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.') +@cli.subcommand('Generate an info.json file for a keyboard.', hidden=False if cli.config.user.developer else True) +@automagic_keyboard +@automagic_keymap +def generate_info_json(cli): + """Generate an info.json file for a keyboard + """ + # Determine our keyboard(s) + if not cli.config.generate_info_json.keyboard: + cli.log.error('Missing paramater: --keyboard') + cli.subcommands['info'].print_help() + return False + + if not is_keyboard(cli.config.generate_info_json.keyboard): + cli.log.error('Invalid keyboard: "%s"', cli.config.generate_info_json.keyboard) + return False + + # Build the info.json file + kb_info_json = info_json(cli.config.generate_info_json.keyboard) + pared_down_json = {} + + for key in ('manufacturer', 'maintainer', 'usb', 'keyboard_name', 'width', 'height', 'debounce', 'diode_direction', 'features', 'community_layouts', 'layout_aliases', 'matrix_pins', 'rgblight', 'url'): + if key in kb_info_json: + pared_down_json[key] = kb_info_json[key] + + pared_down_json['layouts'] = {} + if 'layouts' in pared_down_json: + for layout_name, layout in kb_info_json['layouts'].items(): + pared_down_json['layouts'][layout_name] = {} + pared_down_json['layouts'][layout_name]['key_count'] = layout.get('key_count', len(layout['layout'])) + pared_down_json['layouts'][layout_name]['layout'] = layout['layout'] + + # Display the results + print(json.dumps(pared_down_json, indent=2, cls=InfoJSONEncoder)) -- cgit v1.2.3 From ededff8556daff544633cb143cb6d939afd09014 Mon Sep 17 00:00:00 2001 From: Zach White Date: Tue, 1 Dec 2020 12:52:02 -0800 Subject: validate keyboard data with jsonschema --- lib/python/qmk/cli/generate/info_json.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/python/qmk/cli/generate/info_json.py') diff --git a/lib/python/qmk/cli/generate/info_json.py b/lib/python/qmk/cli/generate/info_json.py index 7e6654e45d..fba4b1c014 100755 --- a/lib/python/qmk/cli/generate/info_json.py +++ b/lib/python/qmk/cli/generate/info_json.py @@ -39,7 +39,7 @@ def generate_info_json(cli): pared_down_json[key] = kb_info_json[key] pared_down_json['layouts'] = {} - if 'layouts' in pared_down_json: + if 'layouts' in kb_info_json: for layout_name, layout in kb_info_json['layouts'].items(): pared_down_json['layouts'][layout_name] = {} pared_down_json['layouts'][layout_name]['key_count'] = layout.get('key_count', len(layout['layout'])) -- cgit v1.2.3 From 962bc8d9dd413690dbeadeaac971a5389697210f Mon Sep 17 00:00:00 2001 From: Zach White Date: Sat, 9 Jan 2021 13:34:14 -0800 Subject: 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 * make flake8 happy * bugfix * do not overwrite make vars from json Co-authored-by: Ryan --- lib/python/qmk/cli/generate/info_json.py | 48 +++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 16 deletions(-) (limited to 'lib/python/qmk/cli/generate/info_json.py') diff --git a/lib/python/qmk/cli/generate/info_json.py b/lib/python/qmk/cli/generate/info_json.py index fba4b1c014..f3fc54ddcf 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. """ import json +from jsonschema import Draft7Validator, validators from milc import cli -from qmk.info_json_encoder import InfoJSONEncoder from qmk.decorators import automagic_keyboard, automagic_keymap -from qmk.info import info_json +from qmk.info import info_json, _jsonschema +from qmk.info_json_encoder import InfoJSONEncoder from qmk.path import is_keyboard +def pruning_validator(validator_class): + """Extends Draft7Validator to remove properties that aren't specified in the schema. + """ + validate_properties = validator_class.VALIDATORS["properties"] + + def remove_additional_properties(validator, properties, instance, schema): + for prop in list(instance.keys()): + if prop not in properties: + del instance[prop] + + for error in validate_properties(validator, properties, instance, schema): + yield error + + return validators.extend(validator_class, {"properties": remove_additional_properties}) + + +def strip_info_json(kb_info_json): + """Remove the API-only properties from the info.json. + """ + pruning_draft_7_validator = pruning_validator(Draft7Validator) + schema = _jsonschema('keyboard') + validator = pruning_draft_7_validator(schema).validate + + return validator(kb_info_json) + + @cli.argument('-kb', '--keyboard', help='Keyboard to show info for.') @cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.') @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): """ # Determine our keyboard(s) if not cli.config.generate_info_json.keyboard: - cli.log.error('Missing paramater: --keyboard') + cli.log.error('Missing parameter: --keyboard') cli.subcommands['info'].print_help() return False @@ -32,18 +59,7 @@ def generate_info_json(cli): # Build the info.json file kb_info_json = info_json(cli.config.generate_info_json.keyboard) - pared_down_json = {} - - for key in ('manufacturer', 'maintainer', 'usb', 'keyboard_name', 'width', 'height', 'debounce', 'diode_direction', 'features', 'community_layouts', 'layout_aliases', 'matrix_pins', 'rgblight', 'url'): - if key in kb_info_json: - pared_down_json[key] = kb_info_json[key] - - pared_down_json['layouts'] = {} - if 'layouts' in kb_info_json: - for layout_name, layout in kb_info_json['layouts'].items(): - pared_down_json['layouts'][layout_name] = {} - pared_down_json['layouts'][layout_name]['key_count'] = layout.get('key_count', len(layout['layout'])) - pared_down_json['layouts'][layout_name]['layout'] = layout['layout'] + strip_info_json(kb_info_json) # Display the results - print(json.dumps(pared_down_json, indent=2, cls=InfoJSONEncoder)) + print(json.dumps(kb_info_json, indent=2, cls=InfoJSONEncoder)) -- cgit v1.2.3