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 +- lib/python/qmk/cli/generate/rules_mk.py | 13 +++ lib/python/qmk/info.py | 151 ++++++++++++++++++++++++++++--- requirements.txt | 1 + 4 files changed, 155 insertions(+), 12 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 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'])) diff --git a/lib/python/qmk/cli/generate/rules_mk.py b/lib/python/qmk/cli/generate/rules_mk.py index 4268ae047b..72ed3c45fa 100755 --- a/lib/python/qmk/cli/generate/rules_mk.py +++ b/lib/python/qmk/cli/generate/rules_mk.py @@ -6,6 +6,10 @@ from qmk.decorators import automagic_keyboard, automagic_keymap from qmk.info import info_json from qmk.path import is_keyboard, normpath +info_to_rules = { + 'bootloader': 'BOOTLOADER', + 'processor': 'MCU' +} @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") @@ -30,6 +34,10 @@ def generate_rules_mk(cli): kb_info_json = info_json(cli.config.generate_rules_mk.keyboard) rules_mk_lines = ['# This file was generated by `qmk generate-rules-mk`. Do not edit or copy.', ''] + # Bring in settings + for info_key, rule_key in info_to_rules.items(): + rules_mk_lines.append(f'{rule_key} := {kb_info_json[info_key]}') + # Find features that should be enabled if 'features' in kb_info_json: for feature, enabled in kb_info_json['features'].items(): @@ -37,6 +45,11 @@ def generate_rules_mk(cli): enabled = 'yes' if enabled else 'no' rules_mk_lines.append(f'{feature}_ENABLE := {enabled}') + # Set the LED driver + if 'led_matrix' in kb_info_json and 'driver' in kb_info_json['led_matrix']: + driver = kb_info_json['led_matrix']['driver'] + rules_mk_lines.append(f'LED_MATRIX_DRIVER = {driver}') + # Add community layouts if 'community_layouts' in kb_info_json: rules_mk_lines.append(f'LAYOUTS = {" ".join(kb_info_json["community_layouts"])}') diff --git a/lib/python/qmk/info.py b/lib/python/qmk/info.py index 4611874e85..1cf12190d6 100644 --- a/lib/python/qmk/info.py +++ b/lib/python/qmk/info.py @@ -4,6 +4,7 @@ import json from glob import glob from pathlib import Path +import jsonschema from milc import cli from qmk.constants import CHIBIOS_PROCESSORS, LUFA_PROCESSORS, VUSB_PROCESSORS, LED_INDICATORS @@ -13,6 +14,17 @@ from qmk.keymap import list_keymaps from qmk.makefile import parse_rules_mk_file from qmk.math import compute +led_matrix_properties = { + 'driver_count': 'LED_DRIVER_COUNT', + 'driver_addr1': 'LED_DRIVER_ADDR_1', + 'driver_addr2': 'LED_DRIVER_ADDR_2', + 'driver_addr3': 'LED_DRIVER_ADDR_3', + 'driver_addr4': 'LED_DRIVER_ADDR_4', + 'led_count': 'LED_DRIVER_LED_COUNT', + 'timeout': 'ISSI_TIMEOUT', + 'persistence': 'ISSI_PERSISTENCE' +} + rgblight_properties = { 'led_count': 'RGBLED_NUM', 'pin': 'RGB_DI_PIN', @@ -80,6 +92,15 @@ def info_json(keyboard): info_data = _extract_config_h(info_data) info_data = _extract_rules_mk(info_data) + # Validate against the jsonschema + try: + keyboard_api_validate(info_data) + + except jsonschema.ValidationError as e: + cli.log.error('Invalid info.json data: %s', e.message) + print(dir(e)) + exit() + # Make sure we have at least one layout if not info_data.get('layouts'): _log_error(info_data, 'No LAYOUTs defined! Need at least one layout defined in the keyboard.h or info.json.') @@ -102,6 +123,50 @@ def info_json(keyboard): return info_data +def _json_load(json_file): + """Load a json file from disk. + + Note: file must be a Path object. + """ + try: + return json.load(json_file.open()) + + except json.decoder.JSONDecodeError as e: + cli.log.error('Invalid JSON encountered attempting to load {fg_cyan}%s{fg_reset}:\n\t{fg_red}%s', json_file, e) + exit(1) + + +def _jsonschema(schema_name): + """Read a jsonschema file from disk. + """ + schema_path = Path(f'data/schemas/{schema_name}.jsonschema') + + if not schema_path.exists(): + schema_path = Path('data/schemas/false.jsonschema') + + return _json_load(schema_path) + + +def keyboard_validate(data): + """Validates data against the keyboard jsonschema. + """ + schema = _jsonschema('keyboard') + validator = jsonschema.Draft7Validator(schema).validate + + return validator(data) + + +def keyboard_api_validate(data): + """Validates data against the api_keyboard jsonschema. + """ + base = _jsonschema('keyboard') + relative = _jsonschema('api_keyboard') + resolver = jsonschema.RefResolver.from_schema(base) + validator = jsonschema.Draft7Validator(relative, resolver=resolver).validate + + return validator(data) + + def _extract_debounce(info_data, config_c): """Handle debounce. """ @@ -109,7 +174,7 @@ def _extract_debounce(info_data, config_c): _log_warning(info_data, 'Debounce is specified in both info.json and config.h, the config.h value wins.') if 'DEBOUNCE' in config_c: - info_data['debounce'] = config_c.get('DEBOUNCE') + info_data['debounce'] = int(config_c['DEBOUNCE']) return info_data @@ -181,8 +246,36 @@ def _extract_features(info_data, rules): return info_data +def _extract_led_drivers(info_data, rules): + """Find all the LED drivers set in rules.mk. + """ + if 'LED_MATRIX_DRIVER' in rules: + if 'led_matrix' not in info_data: + info_data['led_matrix'] = {} + + if info_data['led_matrix'].get('driver'): + _log_warning(info_data, 'LED Matrix driver is specified in both info.json and rules.mk, the rules.mk value wins.') + + info_data['led_matrix']['driver'] = rules['LED_MATRIX_DRIVER'] + + return info_data + + +def _extract_led_matrix(info_data, config_c): + """Handle the led_matrix configuration. + """ + led_matrix = info_data.get('led_matrix', {}) + + for json_key, config_key in led_matrix_properties.items(): + if config_key in config_c: + if json_key in led_matrix: + _log_warning(info_data, 'LED Matrix: %s is specified in both info.json and config.h, the config.h value wins.' % (json_key,)) + + led_matrix[json_key] = config_c[config_key] + + def _extract_rgblight(info_data, config_c): - """Handle the rgblight configuration + """Handle the rgblight configuration. """ rgblight = info_data.get('rgblight', {}) animations = rgblight.get('animations', {}) @@ -303,6 +396,7 @@ def _extract_config_h(info_data): _extract_indicators(info_data, config_c) _extract_matrix_info(info_data, config_c) _extract_usb_info(info_data, config_c) + _extract_led_matrix(info_data, config_c) _extract_rgblight(info_data, config_c) return info_data @@ -326,6 +420,7 @@ def _extract_rules_mk(info_data): _extract_community_layouts(info_data, rules) _extract_features(info_data, rules) + _extract_led_drivers(info_data, rules) return info_data @@ -412,13 +507,28 @@ def arm_processor_rules(info_data, rules): """Setup the default info for an ARM board. """ info_data['processor_type'] = 'arm' - info_data['bootloader'] = rules['BOOTLOADER'] if 'BOOTLOADER' in rules else 'unknown' - info_data['processor'] = rules['MCU'] if 'MCU' in rules else 'unknown' info_data['protocol'] = 'ChibiOS' - if info_data['bootloader'] == 'unknown': + if 'MCU' in rules: + if 'processor' in info_data: + _log_warning(info_data, 'Processor/MCU is specified in both info.json and rules.mk, the rules.mk value wins.') + + info_data['processor'] = rules['MCU'] + + elif 'processor' not in info_data: + info_data['processor'] = 'unknown' + + if 'BOOTLOADER' in rules: + if 'bootloader' in info_data: + _log_warning(info_data, 'Bootloader is specified in both info.json and rules.mk, the rules.mk value wins.') + + info_data['bootloader'] = rules['BOOTLOADER'] + + else: if 'STM32' in info_data['processor']: info_data['bootloader'] = 'stm32-dfu' + else: + info_data['bootloader'] = 'unknown' if 'STM32' in info_data['processor']: info_data['platform'] = 'STM32' @@ -436,9 +546,25 @@ def avr_processor_rules(info_data, rules): info_data['processor_type'] = 'avr' info_data['bootloader'] = rules['BOOTLOADER'] if 'BOOTLOADER' in rules else 'atmel-dfu' info_data['platform'] = rules['ARCH'] if 'ARCH' in rules else 'unknown' - info_data['processor'] = rules['MCU'] if 'MCU' in rules else 'unknown' info_data['protocol'] = 'V-USB' if rules.get('MCU') in VUSB_PROCESSORS else 'LUFA' + if 'MCU' in rules: + if 'processor' in info_data: + _log_warning(info_data, 'Processor/MCU is specified in both info.json and rules.mk, the rules.mk value wins.') + + info_data['processor'] = rules['MCU'] + + elif 'processor' not in info_data: + info_data['processor'] = 'unknown' + + if 'BOOTLOADER' in rules: + if 'bootloader' in info_data: + _log_warning(info_data, 'Bootloader is specified in both info.json and rules.mk, the rules.mk value wins.') + + info_data['bootloader'] = rules['BOOTLOADER'] + else: + info_data['bootloader'] = 'atmel-dfu' + # FIXME(fauxpark/anyone): Eventually we should detect the protocol by looking at PROTOCOL inherited from mcu_selection.mk: # info_data['protocol'] = 'V-USB' if rules.get('PROTOCOL') == 'VUSB' else 'LUFA' @@ -463,10 +589,13 @@ def merge_info_jsons(keyboard, info_data): for info_file in find_info_json(keyboard): # Load and validate the JSON data try: - new_info_data = json.load(info_file.open('r')) - except Exception as e: - _log_error(info_data, "Invalid JSON in file %s: %s: %s" % (str(info_file), e.__class__.__name__, e)) - new_info_data = {} + new_info_data = _json_load(info_file) + keyboard_validate(new_info_data) + + except jsonschema.ValidationError as e: + cli.log.error('Invalid info.json data: %s', e.message) + cli.log.error('Not including file %s', info_file) + continue if not isinstance(new_info_data, dict): _log_error(info_data, "Invalid file %s, root object should be a dictionary." % (str(info_file),)) @@ -479,7 +608,7 @@ def merge_info_jsons(keyboard, info_data): # Deep merge certain keys # FIXME(skullydazed/anyone): this should be generalized more so that we can inteligently merge more than one level deep. It would be nice if we could filter on valid keys too. That may have to wait for a future where we use openapi or something. - for key in ('features', 'layout_aliases', 'matrix_pins', 'rgblight', 'usb'): + for key in ('features', 'layout_aliases', 'led_matrix', 'matrix_pins', 'rgblight', 'usb'): if key in new_info_data: if key not in info_data: info_data[key] = {} diff --git a/requirements.txt b/requirements.txt index 6e907cf8e8..f4d43da8d6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,5 +3,6 @@ appdirs argcomplete colorama hjson +jsonschema milc pygments -- 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 --- data/schemas/keyboard.jsonschema | 2 +- docs/reference_info_json.md | 1 + lib/python/qmk/cli/generate/info_json.py | 48 ++++++++++++++++--------- lib/python/qmk/cli/generate/layouts.py | 4 +++ lib/python/qmk/cli/generate/rules_mk.py | 10 +++--- lib/python/qmk/constants.py | 2 +- lib/python/qmk/info.py | 60 +++++++++++++++----------------- setup.cfg | 2 ++ 8 files changed, 75 insertions(+), 54 deletions(-) (limited to 'lib/python/qmk/cli/generate/info_json.py') diff --git a/data/schemas/keyboard.jsonschema b/data/schemas/keyboard.jsonschema index e13771e92a..f76c7fd189 100644 --- a/data/schemas/keyboard.jsonschema +++ b/data/schemas/keyboard.jsonschema @@ -25,7 +25,7 @@ }, "processor": { "type": "string", - "enum": ["MK20DX128", "MK20DX256", "MKL26Z64", "STM32F042", "STM32F072", "STM32F103", "STM32F303", "STM32F401", "STM32F411", "at90usb1286", "at90usb646", "atmega16u2", "atmega328p", "atmega32a", "atmega32u2", "atmega32u4", "attiny85", "cortex-m4"] + "enum": ["MK20DX128", "MK20DX256", "MKL26Z64", "STM32F042", "STM32F072", "STM32F103", "STM32F303", "STM32F401", "STM32F411", "at90usb1286", "at90usb646", "atmega16u2", "atmega328p", "atmega32a", "atmega32u2", "atmega32u4", "attiny85", "cortex-m4", "unknown"] }, "bootloader": { "type": "string", diff --git a/docs/reference_info_json.md b/docs/reference_info_json.md index 47506bc92d..c9864ea2de 100644 --- a/docs/reference_info_json.md +++ b/docs/reference_info_json.md @@ -106,6 +106,7 @@ Example: ["A7", "B1"], [null, "B2"] ] + } } ``` 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)) diff --git a/lib/python/qmk/cli/generate/layouts.py b/lib/python/qmk/cli/generate/layouts.py index 273870e15c..b7baae0651 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): if kb_info_json['layouts'][layout_name]['c_macro']: continue + if 'matrix' not in kb_info_json['layouts'][layout_name]['layout'][0]: + cli.log.debug('%s/%s: No matrix data!', cli.config.generate_layouts.keyboard, layout_name) + continue + layout_keys = [] layout_matrix = [['KC_NO' for i in range(col_num)] for i in range(row_num)] diff --git a/lib/python/qmk/cli/generate/rules_mk.py b/lib/python/qmk/cli/generate/rules_mk.py index 2a7e918569..0fdccb4048 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): # Bring in settings for info_key, rule_key in info_to_rules.items(): - rules_mk_lines.append(f'{rule_key} := {kb_info_json[info_key]}') + rules_mk_lines.append(f'{rule_key} ?= {kb_info_json[info_key]}') # Find features that should be enabled if 'features' in kb_info_json: for feature, enabled in kb_info_json['features'].items(): if feature == 'bootmagic_lite' and enabled: - rules_mk_lines.append('BOOTMAGIC_ENABLE := lite') + rules_mk_lines.append('BOOTMAGIC_ENABLE ?= lite') else: feature = feature.upper() enabled = 'yes' if enabled else 'no' - rules_mk_lines.append(f'{feature}_ENABLE := {enabled}') + rules_mk_lines.append(f'{feature}_ENABLE ?= {enabled}') # Set the LED driver if 'led_matrix' in kb_info_json and 'driver' in kb_info_json['led_matrix']: driver = kb_info_json['led_matrix']['driver'] - rules_mk_lines.append(f'LED_MATRIX_DRIVER = {driver}') + rules_mk_lines.append(f'LED_MATRIX_DRIVER ?= {driver}') # Add community layouts if 'community_layouts' in kb_info_json: - rules_mk_lines.append(f'LAYOUTS = {" ".join(kb_info_json["community_layouts"])}') + rules_mk_lines.append(f'LAYOUTS ?= {" ".join(kb_info_json["community_layouts"])}') # Show the results rules_mk = '\n'.join(rules_mk_lines) + '\n' diff --git a/lib/python/qmk/constants.py b/lib/python/qmk/constants.py index 675832c506..6a643070fd 100644 --- a/lib/python/qmk/constants.py +++ b/lib/python/qmk/constants.py @@ -26,5 +26,5 @@ ROW_LETTERS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop' LED_INDICATORS = { 'caps_lock': 'LED_CAPS_LOCK_PIN', 'num_lock': 'LED_NUM_LOCK_PIN', - 'scrol_lock': 'LED_SCROLL_LOCK_PIN' + 'scrol_lock': 'LED_SCROLL_LOCK_PIN', } diff --git a/lib/python/qmk/info.py b/lib/python/qmk/info.py index 28c281a4bc..0ea5136a97 100644 --- a/lib/python/qmk/info.py +++ b/lib/python/qmk/info.py @@ -1,6 +1,7 @@ """Functions that help us generate and use info.json files. """ import json +from collections.abc import Mapping from glob import glob from pathlib import Path @@ -140,6 +141,8 @@ def _json_load(json_file): def _jsonschema(schema_name): """Read a jsonschema file from disk. + + FIXME(skullydazed/anyone): Refactor to make this a public function. """ schema_path = Path(f'data/schemas/{schema_name}.jsonschema') @@ -638,49 +641,44 @@ def unknown_processor_rules(info_data, rules): return info_data +def deep_update(origdict, newdict): + """Update a dictionary in place, recursing to do a deep copy. + """ + for key, value in newdict.items(): + if isinstance(value, Mapping): + origdict[key] = deep_update(origdict.get(key, {}), value) + + else: + origdict[key] = value + + return origdict + + def merge_info_jsons(keyboard, info_data): """Return a merged copy of all the info.json files for a keyboard. """ for info_file in find_info_json(keyboard): # Load and validate the JSON data + new_info_data = _json_load(info_file) + + if not isinstance(new_info_data, dict): + _log_error(info_data, "Invalid file %s, root object should be a dictionary." % (str(info_file),)) + continue + try: - new_info_data = _json_load(info_file) keyboard_validate(new_info_data) - except jsonschema.ValidationError as e: json_path = '.'.join([str(p) for p in e.absolute_path]) - cli.log.error('Invalid info.json data: %s: %s: %s', info_file, json_path, e.message) + cli.log.error('Not including data from file: %s', info_file) + cli.log.error('\t%s: %s', json_path, e.message) continue - if not isinstance(new_info_data, dict): - _log_error(info_data, "Invalid file %s, root object should be a dictionary." % (str(info_file),)) - continue - - # Copy whitelisted keys into `info_data` - for key in ('debounce', 'diode_direction', 'indicators', 'keyboard_name', 'manufacturer', 'identifier', 'url', 'maintainer', 'processor', 'bootloader', 'width', 'height'): - if key in new_info_data: - info_data[key] = new_info_data[key] - - # Deep merge certain keys - # FIXME(skullydazed/anyone): this should be generalized more so that we can inteligently merge more than one level deep. It would be nice if we could filter on valid keys too. That may have to wait for a future where we use openapi or something. - for key in ('features', 'layout_aliases', 'led_matrix', 'matrix_pins', 'rgblight', 'usb'): - if key in new_info_data: - if key not in info_data: - info_data[key] = {} - - info_data[key].update(new_info_data[key]) - - # Merge the layouts - if 'community_layouts' in new_info_data: - if 'community_layouts' in info_data: - for layout in new_info_data['community_layouts']: - if layout not in info_data['community_layouts']: - info_data['community_layouts'].append(layout) - else: - info_data['community_layouts'] = new_info_data['community_layouts'] + # Mark the layouts as coming from json + for layout in new_info_data.get('layouts', {}).values(): + layout['c_macro'] = False - if 'layouts' in new_info_data: - _merge_layouts(info_data, new_info_data) + # Update info_data with the new data + deep_update(info_data, new_info_data) return info_data diff --git a/setup.cfg b/setup.cfg index 5ef2f9ba09..baa6a03967 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,6 +3,8 @@ ignore = # QMK is ok with long lines. E501 + # Conflicts with our yapf config + E231 per_file_ignores = **/__init__.py:F401 -- cgit v1.2.3