···11"""This script automates the generation of the QMK API data.
22"""
33from pathlib import Path
44-from shutil import copyfile
44+import shutil
55import json
6677from milc import cli
···1212from qmk.json_schema import json_load
1313from qmk.keyboard import find_readme, list_keyboards
14141515+TEMPLATE_PATH = Path('data/templates/api/')
1616+BUILD_API_PATH = Path('.build/api_data/')
1717+15181619@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't write the data to disk.")
2020+@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.")
1721@cli.subcommand('Creates a new keymap for the keyboard of your choosing', hidden=False if cli.config.user.developer else True)
1822def generate_api(cli):
1923 """Generates the QMK API data.
2024 """
2121- api_data_dir = Path('api_data')
2222- v1_dir = api_data_dir / 'v1'
2525+ if BUILD_API_PATH.exists():
2626+ shutil.rmtree(BUILD_API_PATH)
2727+2828+ shutil.copytree(TEMPLATE_PATH, BUILD_API_PATH)
2929+3030+ v1_dir = BUILD_API_PATH / 'v1'
2331 keyboard_all_file = v1_dir / 'keyboards.json' # A massive JSON containing everything
2432 keyboard_list_file = v1_dir / 'keyboard_list.json' # A simple list of keyboard targets
2533 keyboard_aliases_file = v1_dir / 'keyboard_aliases.json' # A list of historical keyboard names and their new name
2634 keyboard_metadata_file = v1_dir / 'keyboard_metadata.json' # All the data configurator/via needs for initialization
2735 usb_file = v1_dir / 'usb.json' # A mapping of USB VID/PID -> keyboard target
28362929- if not api_data_dir.exists():
3030- api_data_dir.mkdir()
3737+ # Filter down when required
3838+ keyboard_list = list_keyboards()
3939+ if cli.args.filter:
4040+ kb_list = []
4141+ for keyboard_name in keyboard_list:
4242+ if any(i in keyboard_name for i in cli.args.filter):
4343+ kb_list.append(keyboard_name)
4444+ keyboard_list = kb_list
31453246 kb_all = {}
3347 usb_list = {}
34483549 # Generate and write keyboard specific JSON files
3636- for keyboard_name in list_keyboards():
5050+ for keyboard_name in keyboard_list:
3751 kb_all[keyboard_name] = info_json(keyboard_name)
3852 keyboard_dir = v1_dir / 'keyboards' / keyboard_name
3953 keyboard_info = keyboard_dir / 'info.json'
···4761 cli.log.debug('Wrote file %s', keyboard_info)
48624963 if keyboard_readme_src:
5050- copyfile(keyboard_readme_src, keyboard_readme)
6464+ shutil.copyfile(keyboard_readme_src, keyboard_readme)
5165 cli.log.debug('Copied %s -> %s', keyboard_readme_src, keyboard_readme)
52665367 if 'usb' in kb_all[keyboard_name]:
+1-1
lib/python/qmk/tests/test_cli_commands.py
···232232233233234234def test_generate_api():
235235- result = check_subcommand('generate-api', '--dry-run')
235235+ result = check_subcommand('generate-api', '--dry-run', '--filter', 'handwired/pytest')
236236 check_returncode(result)
237237238238