diff --git a/lib/python/qmk/cli/generate/api.py b/lib/python/qmk/cli/generate/api.py index 0596b3f22b5..d49e40c591c 100755 --- a/lib/python/qmk/cli/generate/api.py +++ b/lib/python/qmk/cli/generate/api.py @@ -2,6 +2,7 @@ """ from pathlib import Path import shutil +import hjson import json from milc import cli @@ -11,30 +12,16 @@ from qmk.info import info_json from qmk.json_encoders import InfoJSONEncoder from qmk.json_schema import json_load from qmk.keyboard import find_readme, list_keyboards +from qmk.xap.common import get_xap_definition_files, update_xap_definitions -TEMPLATE_PATH = Path('data/templates/api/') +DATA_PATH = Path('data') +TEMPLATE_PATH = DATA_PATH / 'templates/api/' BUILD_API_PATH = Path('.build/api_data/') -@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't write the data to disk.") -@cli.argument('-f', '--filter', arg_only=True, action='append', default=[], help="Filter the list of keyboards based on partial name matches the supplied value. May be passed multiple times.") -@cli.subcommand('Creates a new keymap for the keyboard of your choosing', hidden=False if cli.config.user.developer else True) -def generate_api(cli): - """Generates the QMK API data. +def _filtered_keyboard_list(): + """Perform basic filtering of list_keyboards """ - if BUILD_API_PATH.exists(): - shutil.rmtree(BUILD_API_PATH) - - shutil.copytree(TEMPLATE_PATH, BUILD_API_PATH) - - v1_dir = BUILD_API_PATH / 'v1' - 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 - - # Filter down when required keyboard_list = list_keyboards() if cli.args.filter: kb_list = [] @@ -42,6 +29,45 @@ def generate_api(cli): if any(i in keyboard_name for i in cli.args.filter): kb_list.append(keyboard_name) keyboard_list = kb_list + return keyboard_list + + +def _resolve_xap_specs(output_folder): + """To make it easier for consumers, replace specs with pre-merged versions + """ + overall = None + for file in get_xap_definition_files(): + overall = update_xap_definitions(overall, hjson.load(file.open(encoding='utf-8'))) + + # Inject dummy bits for unspecified response flags + for n in range(0, 8): + if str(n) not in overall['response_flags']['bits']: + overall['response_flags']['bits'][str(n)] = {'name': '', 'description': '', 'define': '-'} + + hjson.dump(overall, (output_folder / file.name).open(mode='w', encoding='utf-8')) + + +@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't write the data to disk.") +@cli.argument('-f', '--filter', arg_only=True, action='append', default=[], help="Filter the list of keyboards based on partial name matches the supplied value. May be passed multiple times.") +@cli.subcommand('Generate QMK API data', hidden=False if cli.config.user.developer else True) +def generate_api(cli): + """Generates the QMK API data. + """ + v1_dir = BUILD_API_PATH / 'v1' + 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 BUILD_API_PATH.exists(): + shutil.rmtree(BUILD_API_PATH) + + shutil.copytree(TEMPLATE_PATH, BUILD_API_PATH) + shutil.copytree(DATA_PATH, v1_dir) + + # Filter down when required + keyboard_list = _filtered_keyboard_list() kb_all = {} usb_list = {} @@ -86,6 +112,9 @@ def generate_api(cli): 'usb': usb_list, } + # Feature specific handling + _resolve_xap_specs(v1_dir / 'xap') + # Write the global JSON files keyboard_all_json = json.dumps({'last_updated': current_datetime(), 'keyboards': kb_all}, cls=InfoJSONEncoder) usb_json = json.dumps({'last_updated': current_datetime(), 'usb': usb_list}, cls=InfoJSONEncoder)