Browse Source
Generate api data on each push (#10609)
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>master
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 397 additions and 125 deletions
-
35.github/workflows/api.yml
-
1.gitignore
-
1api_data/_config.yml
-
5api_data/readme.md
-
1lib/python/qmk/cli/__init__.py
-
2lib/python/qmk/cli/c2json.py
-
1lib/python/qmk/cli/generate/__init__.py
-
58lib/python/qmk/cli/generate/api.py
-
56lib/python/qmk/cli/info.py
-
2lib/python/qmk/cli/json2c.py
-
19lib/python/qmk/cli/list/keyboards.py
-
5lib/python/qmk/constants.py
-
29lib/python/qmk/datetime.py
-
36lib/python/qmk/decorators.py
-
8lib/python/qmk/info.py
-
20lib/python/qmk/keyboard.py
-
219lib/python/qmk/keymap.py
-
24lib/python/qmk/tests/test_qmk_keymap.py
@ -0,0 +1,35 @@ |
|||
name: Update API Data |
|||
|
|||
on: |
|||
push: |
|||
branches: |
|||
- master |
|||
paths: |
|||
- 'keyboards/**' |
|||
- 'layouts/community/**' |
|||
|
|||
jobs: |
|||
api_data: |
|||
runs-on: ubuntu-latest |
|||
container: qmkfm/base_container |
|||
|
|||
steps: |
|||
- uses: actions/checkout@v2 |
|||
with: |
|||
fetch-depth: 1 |
|||
persist-credentials: false |
|||
|
|||
- name: Generate API Data |
|||
run: qmk generate-api |
|||
|
|||
- name: Upload API Data |
|||
uses: JamesIves/github-pages-deploy-action@3.7.1 |
|||
with: |
|||
ACCESS_TOKEN: ${{ secrets.API_TOKEN_GITHUB }} |
|||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
|||
BRANCH: main |
|||
FOLDER: api_data/v1 |
|||
CLEAN: true |
|||
GIT_CONFIG_EMAIL: hello@qmk.fm |
|||
REPOSITORY_NAME: qmk/qmk_keyboards |
|||
TARGET_FOLDER: v1 |
@ -0,0 +1 @@ |
|||
theme: jekyll-theme-cayman |
@ -0,0 +1,5 @@ |
|||
# QMK Keyboard Metadata |
|||
|
|||
This directory contains machine parsable data about keyboards supported by QMK. The latest version is always available online at <https://keyboards.qmk.fm>. |
|||
|
|||
Do not edit anything here by hand. It is generated with the `qmk generate-api` command. |
@ -0,0 +1 @@ |
|||
from . import api |
@ -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)) |
@ -1,28 +1,13 @@ |
|||
"""List the keyboards currently defined within QMK |
|||
""" |
|||
# We avoid pathlib here because this is performance critical code. |
|||
import os |
|||
import glob |
|||
|
|||
from milc import cli |
|||
|
|||
BASE_PATH = os.path.join(os.getcwd(), "keyboards") + os.path.sep |
|||
KB_WILDCARD = os.path.join(BASE_PATH, "**", "rules.mk") |
|||
|
|||
|
|||
def find_name(path): |
|||
"""Determine the keyboard name by stripping off the base_path and rules.mk. |
|||
""" |
|||
return path.replace(BASE_PATH, "").replace(os.path.sep + "rules.mk", "") |
|||
import qmk.keyboard |
|||
|
|||
|
|||
@cli.subcommand("List the keyboards currently defined within QMK") |
|||
def list_keyboards(cli): |
|||
"""List the keyboards currently defined within QMK |
|||
""" |
|||
# find everywhere we have rules.mk where keymaps isn't in the path |
|||
paths = [path for path in glob.iglob(KB_WILDCARD, recursive=True) if 'keymaps' not in path] |
|||
|
|||
# Extract the keyboard name from the path and print it |
|||
for keyboard_name in sorted(map(find_name, paths)): |
|||
for keyboard_name in qmk.keyboard.list_keyboards(): |
|||
print(keyboard_name) |
@ -0,0 +1,29 @@ |
|||
"""Functions to work with dates and times in a uniform way. |
|||
|
|||
The results of these functions are cached for 5 seconds to provide uniform time strings across short running processes. Long running processes that need more precise timekeeping should not use these functions. |
|||
""" |
|||
from time import gmtime, strftime |
|||
|
|||
from qmk.constants import DATE_FORMAT, DATETIME_FORMAT, TIME_FORMAT |
|||
from qmk.decorators import lru_cache |
|||
|
|||
|
|||
@lru_cache(timeout=5) |
|||
def current_date(): |
|||
"""Returns the current time in UTZ as a formatted string. |
|||
""" |
|||
return strftime(DATE_FORMAT, gmtime()) |
|||
|
|||
|
|||
@lru_cache(timeout=5) |
|||
def current_datetime(): |
|||
"""Returns the current time in UTZ as a formatted string. |
|||
""" |
|||
return strftime(DATETIME_FORMAT, gmtime()) |
|||
|
|||
|
|||
@lru_cache(timeout=5) |
|||
def current_time(): |
|||
"""Returns the current time in UTZ as a formatted string. |
|||
""" |
|||
return strftime(TIME_FORMAT, gmtime()) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue