diff options
Diffstat (limited to 'lib/python/qmk')
27 files changed, 347 insertions, 139 deletions
diff --git a/lib/python/qmk/c_parse.py b/lib/python/qmk/c_parse.py index b8e5f6a3c9..7dd464bd34 100644 --- a/lib/python/qmk/c_parse.py +++ b/lib/python/qmk/c_parse.py @@ -11,7 +11,7 @@ from milc import cli from qmk.comment_remover import comment_remover -default_key_entry = {'x': -1, 'y': 0, 'w': 1} +default_key_entry = {'x': -1, 'y': 0} single_comment_regex = re.compile(r'\s+/[/*].*$') multi_comment_regex = re.compile(r'/\*(.|\n)*?\*/', re.MULTILINE) layout_macro_define_regex = re.compile(r'^#\s*define') @@ -217,10 +217,13 @@ def _coerce_led_token(_type, value): return value_map[value] -def _validate_led_config(matrix, matrix_rows, matrix_indexes, position, position_raw, flags): +def _validate_led_config(matrix, matrix_rows, matrix_cols, matrix_indexes, position, position_raw, flags): # TODO: Improve crude parsing/validation if len(matrix) != matrix_rows and len(matrix) != (matrix_rows / 2): raise ValueError("Unable to parse g_led_config matrix data") + for index, row in enumerate(matrix): + if len(row) != matrix_cols: + raise ValueError(f"Number of columns in row {index} ({len(row)}) does not match matrix ({matrix_cols})") if len(position) != len(flags): raise ValueError(f"Number of g_led_config physical positions ({len(position)}) does not match number of flags ({len(flags)})") if len(matrix_indexes) and (max(matrix_indexes) >= len(flags)): @@ -234,13 +237,16 @@ def _validate_led_config(matrix, matrix_rows, matrix_indexes, position, position def _parse_led_config(file, matrix_cols, matrix_rows): """Return any 'raw' led/rgb matrix config """ - matrix_raw = [] + matrix = [] position_raw = [] flags = [] found_led_config = False bracket_count = 0 section = 0 + current_row_index = 0 + current_row = [] + for _type, value in lex(_preprocess_c_file(file), CLexer()): # Assume g_led_config..stuff..; if value == 'g_led_config': @@ -254,12 +260,16 @@ def _parse_led_config(file, matrix_cols, matrix_rows): if bracket_count == 2: section += 1 elif value == '}': + if section == 1 and bracket_count == 3: + matrix.append(current_row) + current_row = [] + current_row_index += 1 bracket_count -= 1 else: # Assume any non whitespace value here is important enough to stash if _type in [Token.Literal.Number.Integer, Token.Literal.Number.Float, Token.Literal.Number.Hex, Token.Name]: if section == 1 and bracket_count == 3: - matrix_raw.append(_coerce_led_token(_type, value)) + current_row.append(_coerce_led_token(_type, value)) if section == 2 and bracket_count == 3: position_raw.append(_coerce_led_token(_type, value)) if section == 3 and bracket_count == 2: @@ -269,16 +279,15 @@ def _parse_led_config(file, matrix_cols, matrix_rows): return None # Slightly better intrim format - matrix = list(_get_chunks(matrix_raw, matrix_cols)) position = list(_get_chunks(position_raw, 2)) - matrix_indexes = list(filter(lambda x: x is not None, matrix_raw)) + matrix_indexes = list(filter(lambda x: x is not None, sum(matrix, []))) # If we have not found anything - bail with no error if not section: return None # Throw any validation errors - _validate_led_config(matrix, matrix_rows, matrix_indexes, position, position_raw, flags) + _validate_led_config(matrix, matrix_rows, matrix_cols, matrix_indexes, position, position_raw, flags) return (matrix, position, flags) diff --git a/lib/python/qmk/cli/c2json.py b/lib/python/qmk/cli/c2json.py index 43110a9387..7f6aca070a 100644 --- a/lib/python/qmk/cli/c2json.py +++ b/lib/python/qmk/cli/c2json.py @@ -57,7 +57,7 @@ def c2json(cli): cli.args.output.parent.mkdir(parents=True, exist_ok=True) if cli.args.output.exists(): cli.args.output.replace(cli.args.output.parent / (cli.args.output.name + '.bak')) - cli.args.output.write_text(json.dumps(keymap_json, cls=InfoJSONEncoder)) + cli.args.output.write_text(json.dumps(keymap_json, cls=InfoJSONEncoder, sort_keys=True)) if not cli.args.quiet: cli.log.info('Wrote keymap to %s.', cli.args.output) 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/cli/format/json.py b/lib/python/qmk/cli/format/json.py index 19d504491f..058b613294 100755 --- a/lib/python/qmk/cli/format/json.py +++ b/lib/python/qmk/cli/format/json.py @@ -62,4 +62,4 @@ def format_json(cli): json_file['layers'][layer_num] = current_layer # Display the results - print(json.dumps(json_file, cls=json_encoder)) + print(json.dumps(json_file, cls=json_encoder, sort_keys=True)) diff --git a/lib/python/qmk/cli/generate/api.py b/lib/python/qmk/cli/generate/api.py index 580e080eeb..61a2f9f732 100755 --- a/lib/python/qmk/cli/generate/api.py +++ b/lib/python/qmk/cli/generate/api.py @@ -66,6 +66,12 @@ def _filtered_copy(src, dst): dst.write_text(json.dumps(data, separators=(',', ':')), encoding='utf-8') return dst + if dst.suffix == '.jsonschema': + data = json_load(src) + + dst.write_text(json.dumps(data), encoding='utf-8') + return dst + return shutil.copy2(src, dst) diff --git a/lib/python/qmk/cli/generate/autocorrect_data.py b/lib/python/qmk/cli/generate/autocorrect_data.py index 5b70e0cb4e..b11c66d95d 100644 --- a/lib/python/qmk/cli/generate/autocorrect_data.py +++ b/lib/python/qmk/cli/generate/autocorrect_data.py @@ -63,7 +63,13 @@ def parse_file(file_name: str) -> List[Tuple[str, str]]: """ try: + import english_words + correct_words = english_words.get_english_words_set(['web2'], lower=True, alpha=True) + except AttributeError: from english_words import english_words_lower_alpha_set as correct_words + if not cli.args.quiet: + cli.echo('The english_words package is outdated, update by running:') + cli.echo(' {fg_cyan}python3 -m pip install english_words --upgrade') except ImportError: if not cli.args.quiet: cli.echo('Autocorrection will falsely trigger when a typo is a substring of a correctly spelled word.') diff --git a/lib/python/qmk/cli/generate/info_json.py b/lib/python/qmk/cli/generate/info_json.py index 0dc80f10cc..08c294146b 100755 --- a/lib/python/qmk/cli/generate/info_json.py +++ b/lib/python/qmk/cli/generate/info_json.py @@ -76,7 +76,7 @@ def generate_info_json(cli): # Build the info.json file kb_info_json = info_json(cli.config.generate_info_json.keyboard) strip_info_json(kb_info_json) - info_json_text = json.dumps(kb_info_json, indent=4, cls=InfoJSONEncoder) + info_json_text = json.dumps(kb_info_json, indent=4, cls=InfoJSONEncoder, sort_keys=True) if cli.args.output: # Write to a file diff --git a/lib/python/qmk/cli/generate/keyboard_h.py b/lib/python/qmk/cli/generate/keyboard_h.py index 152921bdce..fa4036e39a 100755 --- a/lib/python/qmk/cli/generate/keyboard_h.py +++ b/lib/python/qmk/cli/generate/keyboard_h.py @@ -11,12 +11,9 @@ from qmk.keyboard import keyboard_completer, keyboard_folder from qmk.constants import COL_LETTERS, ROW_LETTERS, GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE -def _generate_layouts(keyboard): - """Generates the layouts.h file. +def _generate_layouts(keyboard, kb_info_json): + """Generates the layouts macros. """ - # Build the info.json file - kb_info_json = info_json(keyboard) - if 'matrix_size' not in kb_info_json: cli.log.error(f'{keyboard}: Invalid matrix config.') return [] @@ -65,6 +62,32 @@ def _generate_layouts(keyboard): return lines +def _generate_keycodes(kb_info_json): + """Generates keyboard level keycodes. + """ + if 'keycodes' not in kb_info_json: + return [] + + lines = [] + lines.append('enum keyboard_keycodes {') + + for index, item in enumerate(kb_info_json.get('keycodes')): + key = item["key"] + if index == 0: + lines.append(f' {key} = QK_KB_0,') + else: + lines.append(f' {key},') + + lines.append('};') + + for item in kb_info_json.get('keycodes', []): + key = item["key"] + for alias in item.get("aliases", []): + lines.append(f'#define {alias} {key}') + + return lines + + @cli.argument('-i', '--include', nargs='?', arg_only=True, help='Optional file to include') @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") @@ -73,8 +96,12 @@ def _generate_layouts(keyboard): def generate_keyboard_h(cli): """Generates the keyboard.h file. """ + # Build the info.json file + kb_info_json = info_json(cli.args.keyboard) + keyboard_h = cli.args.include - dd_layouts = _generate_layouts(cli.args.keyboard) + dd_layouts = _generate_layouts(cli.args.keyboard, kb_info_json) + dd_keycodes = _generate_keycodes(kb_info_json) valid_config = dd_layouts or keyboard_h # Build the layouts.h file. @@ -87,6 +114,11 @@ def generate_keyboard_h(cli): if keyboard_h: keyboard_h_lines.append(f'#include "{Path(keyboard_h).name}"') + keyboard_h_lines.append('') + keyboard_h_lines.append('// Keycode content') + if dd_keycodes: + keyboard_h_lines.extend(dd_keycodes) + # Protect against poorly configured keyboards if not valid_config: keyboard_h_lines.append('#error("<keyboard>.h is required unless your keyboard uses data-driven configuration. Please rename your keyboard\'s header file to <keyboard>.h")') diff --git a/lib/python/qmk/cli/generate/keycodes.py b/lib/python/qmk/cli/generate/keycodes.py index 17503bac63..3b69b17ed1 100644 --- a/lib/python/qmk/cli/generate/keycodes.py +++ b/lib/python/qmk/cli/generate/keycodes.py @@ -143,7 +143,7 @@ def generate_keycode_extras(cli): """ # Build the header file. - keycodes_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once', '#include "keymap.h"', '// clang-format off'] + keycodes_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once', '#include "keycodes.h"', '// clang-format off'] keycodes = load_spec(cli.args.version, cli.args.lang) diff --git a/lib/python/qmk/cli/git/submodule.py b/lib/python/qmk/cli/git/submodule.py index 9f354c021e..ef116ea124 100644 --- a/lib/python/qmk/cli/git/submodule.py +++ b/lib/python/qmk/cli/git/submodule.py @@ -7,14 +7,21 @@ from qmk import submodules REMOVE_DIRS = [ 'lib/ugfx', - 'lib/pico-sdk', 'lib/chibios-contrib/ext/mcux-sdk', - 'lib/lvgl', +] + +IGNORE_DIRS = [ + 'lib/arm_atsam', + 'lib/fnv', + 'lib/lib8tion', + 'lib/python', + 'lib/usbhost', ] @cli.argument('--check', arg_only=True, action='store_true', help='Check if the submodules are dirty, and display a warning if they are.') @cli.argument('--sync', arg_only=True, action='store_true', help='Shallow clone any missing submodules.') +@cli.argument('-f', '--force', action='store_true', help='Flag to remove unexpected directories') @cli.subcommand('Git Submodule actions.') def git_submodule(cli): """Git Submodule actions @@ -29,7 +36,15 @@ def git_submodule(cli): cli.run(['git', 'submodule', 'update', '--depth=50', '--init', name], capture_output=False) return True - for folder in REMOVE_DIRS: + # can be the default behavior with: qmk config git_submodule.force=True + remove_dirs = REMOVE_DIRS + if cli.config.git_submodule.force: + # Also trash everything that isnt marked as "safe" + for path in normpath('lib').iterdir(): + if not any(ignore in path.as_posix() for ignore in IGNORE_DIRS): + remove_dirs.append(path) + + for folder in map(normpath, remove_dirs): if normpath(folder).is_dir(): print(f"Removing '{folder}'") shutil.rmtree(folder) diff --git a/lib/python/qmk/cli/info.py b/lib/python/qmk/cli/info.py index fa5729bcc9..02cc6051ec 100755 --- a/lib/python/qmk/cli/info.py +++ b/lib/python/qmk/cli/info.py @@ -18,6 +18,29 @@ from qmk.path import is_keyboard UNICODE_SUPPORT = sys.stdout.encoding.lower().startswith('utf') +def _strip_api_content(info_json): + # Ideally this would only be added in the API pathway. + info_json.pop('platform', None) + info_json.pop('platform_key', None) + info_json.pop('processor_type', None) + info_json.pop('protocol', None) + info_json.pop('config_h_features', None) + info_json.pop('keymaps', None) + info_json.pop('keyboard_folder', None) + info_json.pop('parse_errors', None) + info_json.pop('parse_warnings', None) + + for layout in info_json.get('layouts', {}).values(): + layout.pop('filename', None) + layout.pop('c_macro', None) + layout.pop('json_layout', None) + + if 'matrix_pins' in info_json: + info_json.pop('matrix_size', None) + + return info_json + + def show_keymap(kb_info_json, title_caps=True): """Render the keymap in ascii art. """ @@ -81,7 +104,6 @@ def print_friendly_output(kb_info_json): cli.echo('{fg_blue}Maintainer{fg_reset}: QMK Community') else: cli.echo('{fg_blue}Maintainer{fg_reset}: %s', kb_info_json['maintainer']) - cli.echo('{fg_blue}Keyboard Folder{fg_reset}: %s', kb_info_json.get('keyboard_folder', 'Unknown')) cli.echo('{fg_blue}Layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys()))) cli.echo('{fg_blue}Processor{fg_reset}: %s', kb_info_json.get('processor', 'Unknown')) cli.echo('{fg_blue}Bootloader{fg_reset}: %s', kb_info_json.get('bootloader', 'Unknown')) @@ -141,6 +163,7 @@ def print_parsed_rules_mk(keyboard_name): @cli.argument('-f', '--format', default='friendly', arg_only=True, help='Format to display the data in (friendly, text, json) (Default: friendly).') @cli.argument('--ascii', action='store_true', default=not UNICODE_SUPPORT, help='Render layout box drawings in ASCII only.') @cli.argument('-r', '--rules-mk', action='store_true', help='Render the parsed values of the keyboard\'s rules.mk file.') +@cli.argument('-a', '--api', action='store_true', help='Show fully processed info intended for API consumption.') @cli.subcommand('Keyboard information.') @automagic_keyboard @automagic_keymap @@ -171,9 +194,12 @@ def info(cli): else: kb_info_json = info_json(cli.config.info.keyboard) + if not cli.args.api: + kb_info_json = _strip_api_content(kb_info_json) + # Output in the requested format if cli.args.format == 'json': - print(json.dumps(kb_info_json, cls=InfoJSONEncoder)) + print(json.dumps(kb_info_json, cls=InfoJSONEncoder, sort_keys=True)) return True elif cli.args.format == 'text': print_dotted_output(kb_info_json) diff --git a/lib/python/qmk/cli/json2c.py b/lib/python/qmk/cli/json2c.py index 2873a9bfd3..a2db314947 100755 --- a/lib/python/qmk/cli/json2c.py +++ b/lib/python/qmk/cli/json2c.py @@ -5,7 +5,7 @@ from milc import cli import qmk.keymap import qmk.path -from qmk.commands import parse_configurator_json +from qmk.commands import dump_lines, parse_configurator_json @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') @@ -21,21 +21,8 @@ def json2c(cli): # Parse the configurator from json file (or stdin) user_keymap = parse_configurator_json(cli.args.filename) - # Environment processing - if cli.args.output and cli.args.output.name == '-': - cli.args.output = None - # Generate the keymap keymap_c = qmk.keymap.generate_c(user_keymap) - if cli.args.output: - cli.args.output.parent.mkdir(parents=True, exist_ok=True) - if cli.args.output.exists(): - cli.args.output.replace(cli.args.output.parent / (cli.args.output.name + '.bak')) - cli.args.output.write_text(keymap_c) - - if not cli.args.quiet: - cli.log.info('Wrote keymap to %s.', cli.args.output) - - else: - print(keymap_c) + # Show the results + dump_lines(cli.args.output, keymap_c.split('\n'), cli.args.quiet) diff --git a/lib/python/qmk/cli/migrate.py b/lib/python/qmk/cli/migrate.py index 4164f9c8ad..c1b1ad1ea9 100644 --- a/lib/python/qmk/cli/migrate.py +++ b/lib/python/qmk/cli/migrate.py @@ -75,7 +75,7 @@ def migrate(cli): # Finally write out updated info.json cli.log.info(f' Updating {target_info}') - target_info.write_text(json.dumps(info_data.to_dict(), cls=InfoJSONEncoder)) + target_info.write_text(json.dumps(info_data.to_dict(), cls=InfoJSONEncoder, sort_keys=True)) cli.log.info(f'{{fg_green}}Migration of keyboard {{fg_cyan}}{cli.args.keyboard}{{fg_green}} complete!{{fg_reset}}') cli.log.info(f"Verify build with {{fg_yellow}}qmk compile -kb {cli.args.keyboard} -km default{{fg_reset}}.") diff --git a/lib/python/qmk/cli/new/keyboard.py b/lib/python/qmk/cli/new/keyboard.py index cdd3919168..ce956d0ce1 100644 --- a/lib/python/qmk/cli/new/keyboard.py +++ b/lib/python/qmk/cli/new/keyboard.py @@ -102,7 +102,7 @@ def augment_community_info(src, dest): item["matrix"] = [int(item["y"]), int(item["x"])] # finally write out the updated info.json - dest.write_text(json.dumps(info, cls=InfoJSONEncoder)) + dest.write_text(json.dumps(info, cls=InfoJSONEncoder, sort_keys=True)) def _question(*args, **kwargs): diff --git a/lib/python/qmk/cli/new/keymap.py b/lib/python/qmk/cli/new/keymap.py index e7823bc46d..9b0ac221a4 100755 --- a/lib/python/qmk/cli/new/keymap.py +++ b/lib/python/qmk/cli/new/keymap.py @@ -5,7 +5,7 @@ import shutil from milc import cli from milc.questions import question -from qmk.path import is_keyboard, keymap +from qmk.path import is_keyboard, keymaps, keymap from qmk.git import git_get_username from qmk.decorators import automagic_keyboard, automagic_keymap from qmk.keyboard import keyboard_completer, keyboard_folder @@ -50,9 +50,9 @@ def new_keymap(cli): return False # generate keymap paths - km_path = keymap(kb_name) - keymap_path_default = km_path / 'default' - keymap_path_new = km_path / user_name + keymaps_dirs = keymaps(kb_name) + keymap_path_default = keymap(kb_name, 'default') + keymap_path_new = keymaps_dirs[0] / user_name if not keymap_path_default.exists(): cli.log.error(f'Default keymap {{fg_cyan}}{keymap_path_default}{{fg_reset}} does not exist!') diff --git a/lib/python/qmk/cli/via2json.py b/lib/python/qmk/cli/via2json.py index 6edc9dfbe5..77823b5d9d 100755 --- a/lib/python/qmk/cli/via2json.py +++ b/lib/python/qmk/cli/via2json.py @@ -141,5 +141,5 @@ def via2json(cli): # Generate the keymap.json keymap_json = generate_json(cli.args.keymap, cli.args.keyboard, keymap_layout, keymap_data, macro_data) - keymap_lines = [json.dumps(keymap_json, cls=KeymapJSONEncoder)] + keymap_lines = [json.dumps(keymap_json, cls=KeymapJSONEncoder, sort_keys=True)] dump_lines(cli.args.output, keymap_lines, cli.args.quiet) diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index 5561a354c5..b1eac13957 100644 --- a/lib/python/qmk/commands.py +++ b/lib/python/qmk/commands.py @@ -51,6 +51,9 @@ def create_make_target(target, dry_run=False, parallel=1, **env_vars): for key, value in env_vars.items(): env.append(f'{key}={value}') + if cli.config.general.verbose: + env.append('VERBOSE=true') + return [make_cmd, *(['-n'] if dry_run else []), *get_make_parallel_args(parallel), *env, target] @@ -175,9 +178,6 @@ def compile_configurator_json(user_keymap, bootloader=None, parallel=1, clean=Fa if bootloader: make_command.append(bootloader) - for key, value in env_vars.items(): - make_command.append(f'{key}={value}') - make_command.extend([ f'KEYBOARD={user_keymap["keyboard"]}', f'KEYMAP={user_keymap["keymap"]}', @@ -198,6 +198,9 @@ def compile_configurator_json(user_keymap, bootloader=None, parallel=1, clean=Fa 'QMK_BIN="qmk"', ]) + for key, value in env_vars.items(): + make_command.append(f'{key}={value}') + return make_command diff --git a/lib/python/qmk/importers.py b/lib/python/qmk/importers.py index 307c66ee3c..8c449a7194 100644 --- a/lib/python/qmk/importers.py +++ b/lib/python/qmk/importers.py @@ -5,7 +5,7 @@ import json from qmk.git import git_get_username from qmk.json_schema import validate -from qmk.path import keyboard, keymap +from qmk.path import keyboard, keymaps from qmk.constants import MCU2BOOTLOADER, LEGACY_KEYCODES from qmk.json_encoders import InfoJSONEncoder, KeymapJSONEncoder from qmk.json_schema import deep_update, json_load @@ -84,14 +84,14 @@ def import_keymap(keymap_data): kb_name = keymap_data['keyboard'] km_name = keymap_data['keymap'] - km_folder = keymap(kb_name) / km_name + km_folder = keymaps(kb_name)[0] / km_name keyboard_keymap = km_folder / 'keymap.json' # This is the deepest folder in the expected tree keyboard_keymap.parent.mkdir(parents=True, exist_ok=True) # Dump out all those lovely files - keyboard_keymap.write_text(json.dumps(keymap_data, cls=KeymapJSONEncoder)) + keyboard_keymap.write_text(json.dumps(keymap_data, cls=KeymapJSONEncoder, sort_keys=True)) return (kb_name, km_name) @@ -139,8 +139,8 @@ def import_keyboard(info_data, keymap_data=None): temp = json_load(keyboard_info) deep_update(temp, info_data) - keyboard_info.write_text(json.dumps(temp, cls=InfoJSONEncoder)) - keyboard_keymap.write_text(json.dumps(keymap_data, cls=KeymapJSONEncoder)) + keyboard_info.write_text(json.dumps(temp, cls=InfoJSONEncoder, sort_keys=True)) + keyboard_keymap.write_text(json.dumps(keymap_data, cls=KeymapJSONEncoder, sort_keys=True)) return kb_name diff --git a/lib/python/qmk/info.py b/lib/python/qmk/info.py index b7ee055eef..dbd26153d8 100644 --- a/lib/python/qmk/info.py +++ b/lib/python/qmk/info.py @@ -50,18 +50,14 @@ def _valid_community_layout(layout): return (Path('layouts/default') / layout).exists() -def _validate(keyboard, info_data): - """Perform various validation on the provided info.json data - """ - # First validate against the jsonschema - try: - validate(info_data, 'qmk.api.keyboard.v1') +def _get_key_left_position(key): + # Special case for ISO enter + return key['x'] - 0.25 if key.get('h', 1) == 2 and key.get('w', 1) == 1.25 else key['x'] - except jsonschema.ValidationError as e: - json_path = '.'.join([str(p) for p in e.absolute_path]) - cli.log.error('Invalid API data: %s: %s: %s', keyboard, json_path, e.message) - exit(1) +def _additional_validation(keyboard, info_data): + """Non schema checks + """ layouts = info_data.get('layouts', {}) layout_aliases = info_data.get('layout_aliases', {}) community_layouts = info_data.get('community_layouts', []) @@ -73,7 +69,7 @@ def _validate(keyboard, info_data): # Warn if physical positions are offset (at least one key should be at x=0, and at least one key at y=0) for layout_name, layout_data in layouts.items(): - offset_x = min([k['x'] for k in layout_data['layout']]) + offset_x = min([_get_key_left_position(k) for k in layout_data['layout']]) if offset_x > 0: _log_warning(info_data, f'Layout "{layout_name}" is offset on X axis by {offset_x}') @@ -103,6 +99,27 @@ def _validate(keyboard, info_data): if layout_name not in layouts and layout_name not in layout_aliases: _log_error(info_data, 'Claims to support community layout %s but no %s() macro found' % (layout, layout_name)) + # keycodes with length > 7 must have short forms for visualisation purposes + for decl in info_data.get('keycodes', []): + if len(decl["key"]) > 7: + if not decl.get("aliases", []): + _log_error(info_data, f'Keycode {decl["key"]} has no short form alias') + + +def _validate(keyboard, info_data): + """Perform various validation on the provided info.json data + """ + # First validate against the jsonschema + try: + validate(info_data, 'qmk.api.keyboard.v1') + + _additional_validation(keyboard, info_data) + + except jsonschema.ValidationError as e: + json_path = '.'.join([str(p) for p in e.absolute_path]) + cli.log.error('Invalid API data: %s: %s: %s', keyboard, json_path, e.message) + exit(1) + def info_json(keyboard): """Generate the info.json data for a specific keyboard. diff --git a/lib/python/qmk/json_encoders.py b/lib/python/qmk/json_encoders.py index f968b3dbb2..1e90f6a288 100755 --- a/lib/python/qmk/json_encoders.py +++ b/lib/python/qmk/json_encoders.py @@ -27,30 +27,56 @@ class QMKJSONEncoder(json.JSONEncoder): return float(obj) - def encode_list(self, obj): + def encode_dict(self, obj, path): + """Encode a dict-like object. + """ + if obj: + self.indentation_level += 1 + + items = sorted(obj.items(), key=self.sort_dict) if self.sort_keys else obj.items() + output = [self.indent_str + f"{json.dumps(key)}: {self.encode(value, path + [key])}" for key, value in items] + + self.indentation_level -= 1 + + return "{\n" + ",\n".join(output) + "\n" + self.indent_str + "}" + else: + return "{}" + + def encode_dict_single_line(self, obj, path): + """Encode a dict-like object onto a single line. + """ + return "{" + ", ".join(f"{json.dumps(key)}: {self.encode(value, path + [key])}" for key, value in sorted(obj.items(), key=self.sort_layout)) + "}" + + def encode_list(self, obj, path): """Encode a list-like object. """ if self.primitives_only(obj): - return "[" + ", ".join(self.encode(element) for element in obj) + "]" + return "[" + ", ".join(self.encode(value, path + [index]) for index, value in enumerate(obj)) + "]" else: self.indentation_level += 1 - output = [self.indent_str + self.encode(element) for element in obj] + + if path[-1] in ('layout', 'rotary'): + # These are part of a LED layout or encoder config, put them on a single line + output = [self.indent_str + self.encode_dict_single_line(value, path + [index]) for index, value in enumerate(obj)] + else: + output = [self.indent_str + self.encode(value, path + [index]) for index, value in enumerate(obj)] + self.indentation_level -= 1 return "[\n" + ",\n".join(output) + "\n" + self.indent_str + "]" - def encode(self, obj): - """Encode keymap.json objects for QMK. + def encode(self, obj, path=[]): + """Encode JSON objects for QMK. """ |