diff options
| author | Zach White <skullydazed@gmail.com> | 2020-10-25 14:48:44 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-25 14:48:44 -0700 |
| commit | 0c42f91f4ccf98a37f055afb777ed491da56335e (patch) | |
| tree | 547344d80fe7bf75ff3f348eefbc19dbdd346a8a /lib/python/qmk/cli/generate/api.py | |
| parent | 8ef82c466e73e555fd74107d4c57e678d7152ecc (diff) | |
| download | qmk_firmware-0c42f91f4ccf98a37f055afb777ed491da56335e.tar.gz qmk_firmware-0c42f91f4ccf98a37f055afb777ed491da56335e.zip | |
Generate api data on each push (#10609)
* add new qmk generate-api command, to generate a complete set of API data.
* Generate api data and push it to the keyboard repo
* fix typo
* Apply suggestions from code review
Co-authored-by: Joel Challis <git@zvecr.com>
* fixup api workflow
* remove file-changes-action
* use a more mainstream github action
* fix yaml error
* Apply suggestions from code review
Co-authored-by: Erovia <Erovia@users.noreply.github.com>
* more uniform date handling
* make flake8 happy
* Update lib/python/qmk/decorators.py
Co-authored-by: Erovia <Erovia@users.noreply.github.com>
Co-authored-by: Joel Challis <git@zvecr.com>
Co-authored-by: Erovia <Erovia@users.noreply.github.com>
Diffstat (limited to 'lib/python/qmk/cli/generate/api.py')
| -rwxr-xr-x | lib/python/qmk/cli/generate/api.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/python/qmk/cli/generate/api.py b/lib/python/qmk/cli/generate/api.py new file mode 100755 index 000000000..9807a9cd6 --- /dev/null +++ b/lib/python/qmk/cli/generate/api.py | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | """This script automates the generation of the QMK API data. | ||
| 2 | """ | ||
| 3 | from pathlib import Path | ||
| 4 | from shutil import copyfile | ||
| 5 | import json | ||
| 6 | |||
| 7 | from milc import cli | ||
| 8 | |||
| 9 | from qmk.datetime import current_datetime | ||
| 10 | from qmk.info import info_json | ||
| 11 | from qmk.keyboard import list_keyboards | ||
| 12 | |||
| 13 | |||
| 14 | @cli.subcommand('Creates a new keymap for the keyboard of your choosing', hidden=False if cli.config.user.developer else True) | ||
| 15 | def generate_api(cli): | ||
| 16 | """Generates the QMK API data. | ||
| 17 | """ | ||
| 18 | api_data_dir = Path('api_data') | ||
| 19 | v1_dir = api_data_dir / 'v1' | ||
| 20 | keyboard_list = v1_dir / 'keyboard_list.json' | ||
| 21 | keyboard_all = v1_dir / 'keyboards.json' | ||
| 22 | usb_file = v1_dir / 'usb.json' | ||
| 23 | |||
| 24 | if not api_data_dir.exists(): | ||
| 25 | api_data_dir.mkdir() | ||
| 26 | |||
| 27 | kb_all = {'last_updated': current_datetime(), 'keyboards': {}} | ||
| 28 | usb_list = {'last_updated': current_datetime(), 'devices': {}} | ||
| 29 | |||
| 30 | # Generate and write keyboard specific JSON files | ||
| 31 | for keyboard_name in list_keyboards(): | ||
| 32 | kb_all['keyboards'][keyboard_name] = info_json(keyboard_name) | ||
| 33 | keyboard_dir = v1_dir / 'keyboards' / keyboard_name | ||
| 34 | keyboard_info = keyboard_dir / 'info.json' | ||
| 35 | keyboard_readme = keyboard_dir / 'readme.md' | ||
| 36 | keyboard_readme_src = Path('keyboards') / keyboard_name / 'readme.md' | ||
| 37 | |||
| 38 | keyboard_dir.mkdir(parents=True, exist_ok=True) | ||
| 39 | keyboard_info.write_text(json.dumps(kb_all['keyboards'][keyboard_name])) | ||
| 40 | |||
| 41 | if keyboard_readme_src.exists(): | ||
| 42 | copyfile(keyboard_readme_src, keyboard_readme) | ||
| 43 | |||
| 44 | if 'usb' in kb_all['keyboards'][keyboard_name]: | ||
| 45 | usb = kb_all['keyboards'][keyboard_name]['usb'] | ||
| 46 | |||
| 47 | if usb['vid'] not in usb_list['devices']: | ||
| 48 | usb_list['devices'][usb['vid']] = {} | ||
| 49 | |||
| 50 | if usb['pid'] not in usb_list['devices'][usb['vid']]: | ||
| 51 | usb_list['devices'][usb['vid']][usb['pid']] = {} | ||
| 52 | |||
| 53 | usb_list['devices'][usb['vid']][usb['pid']][keyboard_name] = usb | ||
| 54 | |||
| 55 | # Write the global JSON files | ||
| 56 | keyboard_list.write_text(json.dumps({'last_updated': current_datetime(), 'keyboards': sorted(kb_all['keyboards'])})) | ||
| 57 | keyboard_all.write_text(json.dumps(kb_all)) | ||
| 58 | usb_file.write_text(json.dumps(usb_list)) | ||
