summaryrefslogtreecommitdiff
path: root/lib/python/qmk/cli/generate
diff options
context:
space:
mode:
authorZach White <skullydazed@gmail.com>2020-10-25 14:48:44 -0700
committerGitHub <noreply@github.com>2020-10-25 14:48:44 -0700
commit0c42f91f4ccf98a37f055afb777ed491da56335e (patch)
tree547344d80fe7bf75ff3f348eefbc19dbdd346a8a /lib/python/qmk/cli/generate
parent8ef82c466e73e555fd74107d4c57e678d7152ecc (diff)
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')
-rw-r--r--lib/python/qmk/cli/generate/__init__.py1
-rwxr-xr-xlib/python/qmk/cli/generate/api.py58
2 files changed, 59 insertions, 0 deletions
diff --git a/lib/python/qmk/cli/generate/__init__.py b/lib/python/qmk/cli/generate/__init__.py
new file mode 100644
index 0000000000..4dc7607ef2
--- /dev/null
+++ b/lib/python/qmk/cli/generate/__init__.py
@@ -0,0 +1 @@
+from . import api
diff --git a/lib/python/qmk/cli/generate/api.py b/lib/python/qmk/cli/generate/api.py
new file mode 100755
index 0000000000..9807a9cd68
--- /dev/null
+++ b/lib/python/qmk/cli/generate/api.py
@@ -0,0 +1,58 @@
+"""This script automates the generation of the QMK API data.
+"""
+from pathlib import Path
+from shutil import copyfile
+import json
+
+from milc import cli
+
+from qmk.datetime import current_datetime
+from qmk.info import info_json
+from qmk.keyboard import list_keyboards
+
+
+@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.
+ """
+ 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'
+
+ if not api_data_dir.exists():
+ api_data_dir.mkdir()
+
+ kb_all = {'last_updated': current_datetime(), 'keyboards': {}}
+ usb_list = {'last_updated': current_datetime(), 'devices': {}}
+
+ # Generate and write keyboard specific JSON files
+ for keyboard_name in list_keyboards():
+ kb_all['keyboards'][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(kb_all['keyboards'][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['vid'] not in usb_list['devices']:
+ usb_list['devices'][usb['vid']] = {}
+
+ if usb['pid'] not in usb_list['devices'][usb['vid']]:
+ usb_list['devices'][usb['vid']][usb['pid']] = {}
+
+ usb_list['devices'][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'])}))
+ keyboard_all.write_text(json.dumps(kb_all))
+ usb_file.write_text(json.dumps(usb_list))