Correctly handle 'latest'

This commit is contained in:
zvecr 2022-10-13 02:21:15 +01:00
parent d1e3036ce2
commit 384bb7ddc7
2 changed files with 38 additions and 26 deletions

View File

@ -1,5 +1,6 @@
"""This script handles the XAP protocol data files. """This script handles the XAP protocol data files.
""" """
import re
import os import os
import hjson import hjson
import jsonschema import jsonschema
@ -17,6 +18,39 @@ from qmk.path import keyboard
XAP_SPEC = 'xap.hjson' XAP_SPEC = 'xap.hjson'
def list_lighting_versions(feature):
"""Return available versions - sorted newest first
"""
ret = []
for file in Path('data/constants/').glob(f'{feature}_[0-9].[0-9].[0-9].json'):
ret.append(file.stem.split('_')[-1])
ret.sort(reverse=True)
return ret
def load_lighting_spec(feature, version='latest'):
"""Build lighting data from the requested spec file
"""
if version == 'latest':
version = list_lighting_versions(feature)[0]
spec = json_load(Path(f'data/constants/{feature}_{version}.json'))
# preprocess for gross rgblight "mode + n"
for obj in spec.get('effects', {}).values():
define = obj['key']
offset = 0
found = re.match('(.*)_(\\d+)$', define)
if found:
define = found.group(1)
offset = int(found.group(2)) - 1
obj['define'] = define
obj['offset'] = offset
return spec
def _get_jinja2_env(data_templates_xap_subdir: str): def _get_jinja2_env(data_templates_xap_subdir: str):
templates_dir = os.path.join(QMK_FIRMWARE, 'data', 'templates', 'xap', data_templates_xap_subdir) templates_dir = os.path.join(QMK_FIRMWARE, 'data', 'templates', 'xap', data_templates_xap_subdir)
j2 = Environment(loader=FileSystemLoader(templates_dir), autoescape=select_autoescape()) j2 = Environment(loader=FileSystemLoader(templates_dir), autoescape=select_autoescape())
@ -29,8 +63,8 @@ def render_xap_output(data_templates_xap_subdir, file_to_render, defs):
j2.globals['to_snake'] = to_snake j2.globals['to_snake'] = to_snake
constants = {} constants = {}
for feature in ['rgblight', 'rgb_matrix']: for feature in ['rgblight', 'rgb_matrix', 'led_matrix']:
constants[feature] = json_load(Path(f'data/constants/{feature}_0.0.1.json')) constants[feature] = load_lighting_spec(feature)
return j2.get_template(file_to_render).render(xap=defs, xap_str=hjson.dumps(defs), constants=constants) return j2.get_template(file_to_render).render(xap=defs, xap_str=hjson.dumps(defs), constants=constants)

View File

@ -1,13 +1,9 @@
"""This script generates the XAP protocol generated header to be compiled into QMK. """This script generates the XAP protocol generated header to be compiled into QMK.
""" """
import re
from pathlib import Path
from qmk.casing import to_snake from qmk.casing import to_snake
from qmk.commands import dump_lines from qmk.commands import dump_lines
from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
from qmk.xap.common import merge_xap_defs, route_conditions from qmk.xap.common import merge_xap_defs, route_conditions, load_lighting_spec
from qmk.json_schema import json_load
PREFIX_MAP = { PREFIX_MAP = {
'rgblight': { 'rgblight': {
@ -25,24 +21,6 @@ PREFIX_MAP = {
} }
def _get_lighting_spec(feature):
version = '0.0.1'
spec = json_load(Path(f'data/constants/{feature}_{version}.json'))
# preprocess for gross rgblight "mode + n"
for obj in spec.get('effects', {}).values():
define = obj['key']
offset = 0
found = re.match('(.*)_(\\d+)$', define)
if found:
define = found.group(1)
offset = int(found.group(2)) - 1
obj['define'] = define
obj['offset'] = offset
return spec
def _get_c_type(xap_type): def _get_c_type(xap_type):
if xap_type == 'bool': if xap_type == 'bool':
return 'bool' return 'bool'
@ -407,7 +385,7 @@ def _append_lighting_mapping(lines, xap_defs):
''') ''')
for feature in PREFIX_MAP.keys(): for feature in PREFIX_MAP.keys():
spec = _get_lighting_spec(feature) spec = load_lighting_spec(feature)
lines.append(f'#ifdef {feature.upper()}_ENABLE') lines.append(f'#ifdef {feature.upper()}_ENABLE')
_append_lighting_map(lines, feature, spec) _append_lighting_map(lines, feature, spec)