diff options
author | Zach White <skullydazed@gmail.com> | 2021-03-24 09:26:38 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-24 09:26:38 -0700 |
commit | 299008be36076343edadb7a36bf2fff820425ad1 (patch) | |
tree | d47d2fddc54b329b4ddb3128ef070b4cdba776a4 /lib/python | |
parent | 723d9af04d610c4a2fcb8fcf39e8420c71ed4df0 (diff) |
Add support for qmk_configurator style aliases (#11954)
* Add support for qmk_configurator style aliases
* add the keyboard aliases to the api data
* add support for a keyboard metadata file
* make flake8 happy
Diffstat (limited to 'lib/python')
-rw-r--r-- | lib/python/qmk/cli/c2json.py | 3 | ||||
-rwxr-xr-x | lib/python/qmk/cli/compile.py | 3 | ||||
-rw-r--r-- | lib/python/qmk/cli/flash.py | 3 | ||||
-rwxr-xr-x | lib/python/qmk/cli/generate/api.py | 50 | ||||
-rwxr-xr-x | lib/python/qmk/cli/generate/config_h.py | 8 | ||||
-rwxr-xr-x | lib/python/qmk/cli/generate/info_json.py | 8 | ||||
-rwxr-xr-x | lib/python/qmk/cli/generate/layouts.py | 3 | ||||
-rwxr-xr-x | lib/python/qmk/cli/generate/rules_mk.py | 8 | ||||
-rwxr-xr-x | lib/python/qmk/cli/info.py | 4 | ||||
-rw-r--r-- | lib/python/qmk/cli/list/keymaps.py | 8 | ||||
-rwxr-xr-x | lib/python/qmk/cli/new/keymap.py | 3 | ||||
-rw-r--r-- | lib/python/qmk/commands.py | 10 | ||||
-rw-r--r-- | lib/python/qmk/info.py | 69 | ||||
-rw-r--r-- | lib/python/qmk/json_schema.py | 68 | ||||
-rw-r--r-- | lib/python/qmk/keyboard.py | 24 | ||||
-rw-r--r-- | lib/python/qmk/path.py | 1 | ||||
-rw-r--r-- | lib/python/qmk/tests/test_cli_commands.py | 4 |
17 files changed, 171 insertions, 106 deletions
diff --git a/lib/python/qmk/cli/c2json.py b/lib/python/qmk/cli/c2json.py index b9d55ebdbc..a97e212223 100644 --- a/lib/python/qmk/cli/c2json.py +++ b/lib/python/qmk/cli/c2json.py @@ -7,12 +7,13 @@ from milc import cli import qmk.keymap import qmk.path from qmk.info_json_encoder import InfoJSONEncoder +from qmk.keyboard import keyboard_folder @cli.argument('--no-cpp', arg_only=True, action='store_false', help='Do not use \'cpp\' on keymap.c') @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") -@cli.argument('-kb', '--keyboard', arg_only=True, required=True, help='The keyboard\'s name') +@cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, required=True, help='The keyboard\'s name') @cli.argument('-km', '--keymap', arg_only=True, required=True, help='The keymap\'s name') @cli.argument('filename', arg_only=True, help='keymap.c file') @cli.subcommand('Creates a keymap.json from a keymap.c file.') diff --git a/lib/python/qmk/cli/compile.py b/lib/python/qmk/cli/compile.py index db195f78a5..5793e98928 100755 --- a/lib/python/qmk/cli/compile.py +++ b/lib/python/qmk/cli/compile.py @@ -7,10 +7,11 @@ from milc import cli import qmk.path from qmk.decorators import automagic_keyboard, automagic_keymap from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json +from qmk.keyboard import keyboard_folder @cli.argument('filename', nargs='?', arg_only=True, type=qmk.path.FileType('r'), help='The configurator export to compile') -@cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.') +@cli.argument('-kb', '--keyboard', type=keyboard_folder, help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.') @cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Ignored when a configurator export is supplied.') @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.") @cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs to run.") diff --git a/lib/python/qmk/cli/flash.py b/lib/python/qmk/cli/flash.py index 173dee3df5..c9273c3f98 100644 --- a/lib/python/qmk/cli/flash.py +++ b/lib/python/qmk/cli/flash.py @@ -9,6 +9,7 @@ from milc import cli import qmk.path from qmk.decorators import automagic_keyboard, automagic_keymap from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json +from qmk.keyboard import keyboard_folder def print_bootloader_help(): @@ -33,7 +34,7 @@ def print_bootloader_help(): @cli.argument('-b', '--bootloaders', action='store_true', help='List the available bootloaders.') @cli.argument('-bl', '--bootloader', default='flash', help='The flash command, corresponding to qmk\'s make options of bootloaders.') @cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.') -@cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.') +@cli.argument('-kb', '--keyboard', type=keyboard_folder, help='The keyboard to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.') @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.") @cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs to run.") @cli.argument('-e', '--env', arg_only=True, action='append', default=[], help="Set a variable to be passed to make. May be passed multiple times.") diff --git a/lib/python/qmk/cli/generate/api.py b/lib/python/qmk/cli/generate/api.py index 6d111f244c..9870f7201d 100755 --- a/lib/python/qmk/cli/generate/api.py +++ b/lib/python/qmk/cli/generate/api.py @@ -9,6 +9,7 @@ from milc import cli from qmk.datetime import current_datetime from qmk.info import info_json from qmk.info_json_encoder import InfoJSONEncoder +from qmk.json_schema import json_load from qmk.keyboard import list_keyboards @@ -18,43 +19,58 @@ def generate_api(cli): """ api_data_dir = Path('api_data') v1_dir = api_data_dir / 'v1' - keyboard_list = v1_dir / 'keyboard_list.json' - keyboard_all = v1_dir / 'keyboards.json' - usb_file = v1_dir / 'usb.json' + keyboard_all_file = v1_dir / 'keyboards.json' # A massive JSON containing everything + keyboard_list_file = v1_dir / 'keyboard_list.json' # A simple list of keyboard targets + keyboard_aliases_file = v1_dir / 'keyboard_aliases.json' # A list of historical keyboard names and their new name + keyboard_metadata_file = v1_dir / 'keyboard_metadata.json' # All the data configurator/via needs for initialization + usb_file = v1_dir / 'usb.json' # A mapping of USB VID/PID -> keyboard target if not api_data_dir.exists(): api_data_dir.mkdir() - kb_all = {'last_updated': current_datetime(), 'keyboards': {}} - usb_list = {'last_updated': current_datetime(), 'devices': {}} + kb_all = {} + usb_list = {} # Generate and write keyboard specific JSON files for keyboard_name in list_keyboards(): - kb_all['keyboards'][keyboard_name] = info_json(keyboard_name) + kb_all[keyboard_name] = info_json(keyboard_name) keyboard_dir = v1_dir / 'keyboards' / keyboard_name keyboard_info = keyboard_dir / 'info.json' keyboard_readme = keyboard_dir / 'readme.md' keyboard_readme_src = Path('keyboards') / keyboard_name / 'readme.md' keyboard_dir.mkdir(parents=True, exist_ok=True) - keyboard_info.write_text(json.dumps({'last_updated': current_datetime(), 'keyboards': {keyboard_name: kb_all['keyboards'][keyboard_name]}})) + keyboard_info.write_text(json.dumps({'last_updated': current_datetime(), 'keyboards': {keyboard_name: kb_all[keyboard_name]}})) if keyboard_readme_src.exists(): copyfile(keyboard_readme_src, keyboard_readme) - if 'usb' in kb_all['keyboards'][keyboard_name]: - usb = kb_all['keyboards'][keyboard_name]['usb'] + if 'usb' in kb_all[keyboard_name]: + usb = kb_all[keyboard_name]['usb'] - if 'vid' in usb and usb['vid'] not in usb_list['devices']: - usb_list['devices'][usb['vid']] = {} + if 'vid' in usb and usb['vid'] not in usb_list: + usb_list[usb['vid']] = {} - if 'pid' in usb and usb['pid'] not in usb_list['devices'][usb['vid']]: - usb_list['devices'][usb['vid']][usb['pid']] = {} + if 'pid' in usb and usb['pid'] not in usb_list[usb['vid']]: + usb_list[usb['vid']][usb['pid']] = {} if 'vid' in usb and 'pid' in usb: - usb_list['devices'][usb['vid']][usb['pid']][keyboard_name] = usb + usb_list[usb['vid']][usb['pid']][keyboard_name] = usb # Write the global JSON files - keyboard_list.write_text(json.dumps({'last_updated': current_datetime(), 'keyboards': sorted(kb_all['keyboards'])}, cls=InfoJSONEncoder)) - keyboard_all.write_text(json.dumps(kb_all, cls=InfoJSONEncoder)) - usb_file.write_text(json.dumps(usb_list, cls=InfoJSONEncoder)) + keyboard_all_file.write_text(json.dumps({'last_updated': current_datetime(), 'keyboards': kb_all}, cls=InfoJSONEncoder)) + usb_file.write_text(json.dumps({'last_updated': current_datetime(), 'usb': usb_list}, cls=InfoJSONEncoder)) + + keyboard_list = sorted(kb_all) + keyboard_list_file.write_text(json.dumps({'last_updated': current_datetime(), 'keyboards': keyboard_list}, cls=InfoJSONEncoder)) + + keyboard_aliases = json_load(Path('data/mappings/keyboard_aliases.json')) + keyboard_aliases_file.write_text(json.dumps({'last_updated': current_datetime(), 'keyboard_aliases': keyboard_aliases}, cls=InfoJSONEncoder)) + + keyboard_metadata = { + 'last_updated': current_datetime(), + 'keyboards': keyboard_list, + 'keyboard_aliases': keyboard_aliases, + 'usb': usb_list + } + keyboard_metadata_file.write_text(json.dumps(keyboard_metadata, cls=InfoJSONEncoder)) diff --git a/lib/python/qmk/cli/generate/config_h.py b/lib/python/qmk/cli/generate/config_h.py index e6d49ea4d5..ccea6d7a05 100755 --- a/lib/python/qmk/cli/generate/config_h.py +++ b/lib/python/qmk/cli/generate/config_h.py @@ -6,7 +6,9 @@ from dotty_dict import dotty from milc import cli from qmk.decorators import automagic_keyboard, automagic_keymap -from qmk.info import _json_load, info_json +from qmk.info import info_json +from qmk.json_schema import json_load +from qmk.keyboard import keyboard_folder from qmk.path import is_keyboard, normpath @@ -73,7 +75,7 @@ def matrix_pins(matrix_pins): @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") -@cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.') +@cli.argument('-kb', '--keyboard', type=keyboard_folder, help='Keyboard to generate config.h for.') @cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True) @automagic_keyboard @automagic_keymap @@ -92,7 +94,7 @@ def generate_config_h(cli): # Build the info_config.h file. kb_info_json = dotty(info_json(cli.config.generate_config_h.keyboard)) - info_config_map = _json_load(Path('data/mappings/info_config.json')) + info_config_map = json_load(Path('data/mappings/info_config.json')) config_h_lines = ['/* This file was generated by `qmk generate-config-h`. Do not edit or copy.' ' */', '', '#pragma once'] diff --git a/lib/python/qmk/cli/generate/info_json.py b/lib/python/qmk/cli/generate/info_json.py index f3fc54ddcf..6c00ba7d8a 100755 --- a/lib/python/qmk/cli/generate/info_json.py +++ b/lib/python/qmk/cli/generate/info_json.py @@ -8,8 +8,10 @@ from jsonschema import Draft7Validator, validators from milc import cli from qmk.decorators import automagic_keyboard, automagic_keymap -from qmk.info import info_json, _jsonschema +from qmk.info import info_json from qmk.info_json_encoder import InfoJSONEncoder +from qmk.json_schema import load_jsonschema +from qmk.keyboard import keyboard_folder from qmk.path import is_keyboard @@ -33,13 +35,13 @@ 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') + schema = load_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('-kb', '--keyboard', type=keyboard_folder, 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 diff --git a/lib/python/qmk/cli/generate/layouts.py b/lib/python/qmk/cli/generate/layouts.py index a738edfe64..7b4394291f 100755 --- a/lib/python/qmk/cli/generate/layouts.py +++ b/lib/python/qmk/cli/generate/layouts.py @@ -5,6 +5,7 @@ from milc import cli from qmk.constants import COL_LETTERS, ROW_LETTERS from qmk.decorators import automagic_keyboard, automagic_keymap from qmk.info import info_json +from qmk.keyboard import keyboard_folder from qmk.path import is_keyboard, normpath usb_properties = { @@ -16,7 +17,7 @@ usb_properties = { @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") -@cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.') +@cli.argument('-kb', '--keyboard', type=keyboard_folder, help='Keyboard to generate config.h for.') @cli.subcommand('Used by the make system to generate layouts.h from info.json', hidden=True) @automagic_keyboard @automagic_keymap diff --git a/lib/python/qmk/cli/generate/rules_mk.py b/lib/python/qmk/cli/generate/rules_mk.py index 15917987b9..91759d26c6 100755 --- a/lib/python/qmk/cli/generate/rules_mk.py +++ b/lib/python/qmk/cli/generate/rules_mk.py @@ -6,7 +6,9 @@ from dotty_dict import dotty from milc import cli from qmk.decorators import automagic_keyboard, automagic_keymap -from qmk.info import _json_load, info_json +from qmk.info import info_json +from qmk.json_schema import json_load +from qmk.keyboard import keyboard_folder from qmk.path import is_keyboard, normpath @@ -37,7 +39,7 @@ def process_mapping_rule(kb_info_json, rules_key, info_dict): @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") @cli.argument('-e', '--escape', arg_only=True, action='store_true', help="Escape spaces in quiet mode") -@cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.') +@cli.argument('-kb', '--keyboard', type=keyboard_folder, help='Keyboard to generate config.h for.') @cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True) @automagic_keyboard @automagic_keymap @@ -54,7 +56,7 @@ def generate_rules_mk(cli): return False kb_info_json = dotty(info_json(cli.config.generate_rules_mk.keyboard)) - info_rules_map = _json_load(Path('data/mappings/info_rules.json')) + info_rules_map = json_load(Path('data/mappings/info_rules.json')) rules_mk_lines = ['# This file was generated by `qmk generate-rules-mk`. Do not edit or copy.', ''] # Iterate through the info_rules map to generate basic rules diff --git a/lib/python/qmk/cli/info.py b/lib/python/qmk/cli/info.py index a7ce8abf03..88b65686f5 100755 --- a/lib/python/qmk/cli/info.py +++ b/lib/python/qmk/cli/info.py @@ -10,7 +10,7 @@ from milc import cli from qmk.info_json_encoder import InfoJSONEncoder from qmk.constants import COL_LETTERS, ROW_LETTERS from qmk.decorators import automagic_keyboard, automagic_keymap -from qmk.keyboard import render_layouts, render_layout +from qmk.keyboard import keyboard_folder, render_layouts, render_layout from qmk.keymap import locate_keymap from qmk.info import info_json from qmk.path import is_keyboard @@ -124,7 +124,7 @@ def print_text_output(kb_info_json): show_keymap(kb_info_json, False) -@cli.argument('-kb', '--keyboard', help='Keyboard to show info for.') +@cli.argument('-kb', '--keyboard', type=keyboard_folder, help='Keyboard to show info for.') @cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.') @cli.argument('-l', '--layouts', action='store_true', help='Render the layouts.') @cli.argument('-m', '--matrix', action='store_true', help='Render the layouts with matrix information.') diff --git a/lib/python/qmk/cli/list/keymaps.py b/lib/python/qmk/cli/list/keymaps.py index 49bc84b2ce..7c0ad43997 100644 --- a/lib/python/qmk/cli/list/keymaps.py +++ b/lib/python/qmk/cli/list/keymaps.py @@ -4,18 +4,14 @@ from milc import cli import qmk.keymap from qmk.decorators import automagic_keyboard -from qmk.path import is_keyboard +from qmk.keyboard import keyboard_folder -@cli.argument("-kb", "--keyboard", help="Specify keyboard name. Example: 1upkeyboards/1up60hse") +@cli.argument("-kb", "--keyboard", type=keyboard_folder, help="Specify keyboard name. Example: 1upkeyboards/1up60hse") @cli.subcommand("List the keymaps for a specific keyboard") @automagic_keyboard def list_keymaps(cli): """List the keymaps for a specific keyboard """ - if not is_keyboard(cli.config.list_keymaps.keyboard): - cli.log.error('Keyboard %s does not exist!', cli.config.list_keymaps.keyboard) - return False - for name in qmk.keymap.list_keymaps(cli.config.list_keymaps.keyboard): print(name) diff --git a/lib/python/qmk/cli/new/keymap.py b/lib/python/qmk/cli/new/keymap.py index 52c564997b..ea98a287c1 100755 --- a/lib/python/qmk/cli/new/keymap.py +++ b/lib/python/qmk/cli/new/keymap.py @@ -5,10 +5,11 @@ from pathlib import Path import qmk.path from qmk.decorators import automagic_keyboard, automagic_keymap +from qmk.keyboard import keyboard_folder from milc import cli -@cli.argument('-kb', '--keyboard', help='Specify keyboard name. Example: 1upkeyboards/1up60hse') +@cli.argument('-kb', '--keyboard', type=keyboard_folder, help='Specify keyboard name. Example: 1upkeyboards/1up60hse') @cli.argument('-km', '--keymap', help='Specify the name for the new keymap directory') @cli.subcommand('Creates a new keymap for the keyboard of your choosing') @automagic_keyboard diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index 6a57c1ff5d..d742f67560 100644 --- a/lib/python/qmk/commands.py +++ b/lib/python/qmk/commands.py @@ -13,6 +13,7 @@ from milc import cli import qmk.keymap from qmk.constants import KEYBOARD_OUTPUT_PREFIX +from qmk.json_schema import json_load time_fmt = '%Y-%m-%d-%H:%M:%S' @@ -190,6 +191,15 @@ def parse_configurator_json(configurator_file): """ # FIXME(skullydazed/anyone): Add validation here user_keymap = json.load(configurator_file) + orig_keyboard = user_keymap['keyboard'] + aliases = json_load(Path('data/mappings/keyboard_aliases.json')) + + if orig_keyboard in aliases: + if 'target' in aliases[orig_keyboard]: + user_keymap['keyboard'] = aliases[orig_keyboard]['target'] + + if 'layouts' in aliases[orig_keyboard] and user_keymap['layout'] in aliases[orig_keyboard]['layouts']: + user_keymap['layout'] = aliases[orig_keyboard]['layouts'][user_keymap['layout']] return user_keymap diff --git a/lib/python/qmk/info.py b/lib/python/qmk/info.py index 60d3a0132a..e2350b7f72 100644 --- a/lib/python/qmk/info.py +++ b/lib/python/qmk/info.py @@ -1,17 +1,15 @@ """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 -import hjson import jsonschema from dotty_dict import dotty from milc import cli from qmk.constants import CHIBIOS_PROCESSORS, LUFA_PROCESSORS, VUSB_PROCESSORS from qmk.c_parse import find_layouts +from qmk.json_schema import deep_update, json_load, keyboard_validate, keyboard_api_validate from qmk.keyboard import config_h, rules_mk from qmk.keymap import list_keymaps from qmk.makefile import parse_rules_mk_file @@ -82,52 +80,6 @@ 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 hjson.load(json_file.open(encoding='utf-8')) - - 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. - - FIXME(skullydazed/anyone): Refactor to make this a public function. - """ - 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_features(info_data, rules): """Find all the features enabled in rules.mk. """ @@ -258,7 +210,7 @@ def _extract_config_h(info_data): # Pull in data from the json map dotty_info = dotty(info_data) - info_config_map = _json_load(Path('data/mappings/info_config.json')) + info_config_map = json_load(Path('data/mappings/info_config.json')) for config_key, info_dict in info_config_map.items(): info_key = info_dict['info_key'] @@ -326,7 +278,7 @@ def _extract_rules_mk(info_data): # Pull in data from the json map dotty_info = dotty(info_data) - info_rules_map = _json_load(Path('data/mappings/info_rules.json')) + info_rules_map = json_load(Path('data/mappings/info_rules.json')) for rules_key, info_dict in info_rules_map.items(): info_key = info_dict['info_key'] @@ -516,25 +468,12 @@ 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) + 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),)) diff --git a/lib/python/qmk/json_schema.py b/lib/python/qmk/json_schema.py new file mode 100644 index 0000000000..b4cd1776b2 --- /dev/null +++ b/lib/python/qmk/json_schema.py @@ -0,0 +1,68 @@ +"""Functions that help us generate and use info.json files. +""" +import json +from collections.abc import Mapping +from pathlib import Path + +import hjson +import jsonschema +from milc import cli + + +def json_load(json_file): + """Load a json file from disk. + + Note: file must be a Path object. + """ + try: + return hjson.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 load_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') + + 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 = load_jsonschema('keyboard') + validator = jsonschema.Draft7Validator(schema).validate + + return validator(data) + + +def keyboard_api_validate(data): + """Validates data against the api_keyboard jsonschema. + """ + base = load_jsonschema('keyboard') + relative = load_jsonschema('api_keyboard') + resolver = jsonschema.RefResolver.from_schema(base) + validator = jsonschema.Draft7Validator(relative, resolver=resolver).validate + + return validator(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 diff --git a/lib/python/qmk/keyboard.py b/lib/python/qmk/keyboard.py index a4c2873757..89f9346c40 100644 --- a/lib/python/qmk/keyboard.py +++ b/lib/python/qmk/keyboard.py @@ -7,7 +7,9 @@ import os from glob import glob from qmk.c_parse import parse_config_h_file +from qmk.json_schema import json_load from qmk.makefile import parse_rules_mk_file +from qmk.path import is_keyboard BOX_DRAWING_CHARACTERS = { "unicode": { @@ -31,6 +33,28 @@ BOX_DRAWING_CHARACTERS = { base_path = os.path.join(os.getcwd(), "keyboards") + os.path.sep +def keyboard_folder(keyboard): + """Returns the actual keyboard folder. + + This checks aliases and DEFAULT_FOLDER to resolve the actual path for a keyboard. + """ + aliases = json_load(Path('data/mappings/keyboard_aliases.json')) + + if keyboard in aliases: + keyboard = aliases[keyboard].get('target', keyboard) + + rules_mk_file = Path(base_path, keyboard, 'rules.mk') + + if rules_mk_file.exists(): + rules_mk = parse_rules_mk_file(rules_mk_file) + keyboard = rules_mk.get('DEFAULT_FOLDER', keyboard) + + if not is_keyboard(keyboard): + raise ValueError(f'Invalid keyboard: {keyboard}') + + return keyboard + + def _find_name(path): """Determine the keyboard name by stripping off the base_path and rules.mk. """ diff --git a/lib/python/qmk/path.py b/lib/python/qmk/path.py index 2aa1916f55..72bae59273 100644 --- a/lib/python/qmk/path.py +++ b/lib/python/qmk/path.py @@ -15,6 +15,7 @@ def is_keyboard(keyboard_name): if keyboard_name: keyboard_path = QMK_FIRMWARE / 'keyboards' / keyboard_name rules_mk = keyboard_path / 'rules.mk' + return rules_mk.exists() diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index 82c42a20e8..a97472e6be 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py @@ -134,8 +134,8 @@ def test_list_keymaps_vendor_kb_rev(): def test_list_keymaps_no_keyboard_found(): result = check_subcommand('list-keymaps', '-kb', 'asdfghjkl') - check_returncode(result, [1]) - assert 'does not exist' in result.stdout + check_returncode(result, [2]) + assert 'invalid keyboard_folder value' in result.stdout def test_json2c(): |