Extend 'qmk info' to handle keymap level overrides (#16702)

master
Joel Challis 2022-03-21 10:57:41 +00:00 committed by GitHub
parent aea9005236
commit 4f8cc32cf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 27 deletions

View File

@ -5,10 +5,9 @@ from pathlib import Path
from dotty_dict import dotty
from milc import cli
from qmk.info import info_json
from qmk.json_schema import json_load, validate
from qmk.info import info_json, keymap_json_config
from qmk.json_schema import json_load
from qmk.keyboard import keyboard_completer, keyboard_folder
from qmk.keymap import locate_keymap
from qmk.commands import dump_lines
from qmk.path import normpath
from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
@ -180,10 +179,7 @@ def generate_config_h(cli):
"""
# Determine our keyboard/keymap
if cli.args.keymap:
km = locate_keymap(cli.args.keyboard, cli.args.keymap)
km_json = json_load(km)
validate(km_json, 'qmk.keymap.v1')
kb_info_json = dotty(km_json.get('config', {}))
kb_info_json = dotty(keymap_json_config(cli.args.keyboard, cli.args.keymap))
else:
kb_info_json = dotty(info_json(cli.args.keyboard))

View File

@ -5,10 +5,9 @@ from pathlib import Path
from dotty_dict import dotty
from milc import cli
from qmk.info import info_json
from qmk.json_schema import json_load, validate
from qmk.info import info_json, keymap_json_config
from qmk.json_schema import json_load
from qmk.keyboard import keyboard_completer, keyboard_folder
from qmk.keymap import locate_keymap
from qmk.commands import dump_lines
from qmk.path import normpath
from qmk.constants import GPL2_HEADER_SH_LIKE, GENERATED_HEADER_SH_LIKE
@ -51,10 +50,7 @@ def generate_rules_mk(cli):
"""
# Determine our keyboard/keymap
if cli.args.keymap:
km = locate_keymap(cli.args.keyboard, cli.args.keymap)
km_json = json_load(km)
validate(km_json, 'qmk.keymap.v1')
kb_info_json = dotty(km_json.get('config', {}))
kb_info_json = dotty(keymap_json_config(cli.args.keyboard, cli.args.keymap))
else:
kb_info_json = dotty(info_json(cli.args.keyboard))

View File

@ -11,8 +11,8 @@ from qmk.json_encoders import InfoJSONEncoder
from qmk.constants import COL_LETTERS, ROW_LETTERS
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.keyboard import keyboard_completer, keyboard_folder, render_layouts, render_layout, rules_mk
from qmk.info import info_json, keymap_json
from qmk.keymap import locate_keymap
from qmk.info import info_json
from qmk.path import is_keyboard
UNICODE_SUPPORT = sys.stdout.encoding.lower().startswith('utf')
@ -135,7 +135,7 @@ def print_parsed_rules_mk(keyboard_name):
@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='Keyboard to show info for.')
@cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.')
@cli.argument('-km', '--keymap', help='Keymap to show info for (Optional).')
@cli.argument('-l', '--layouts', action='store_true', help='Render the layouts.')
@cli.argument('-m', '--matrix', action='store_true', help='Render the layouts with matrix information.')
@cli.argument('-f', '--format', default='friendly', arg_only=True, help='Format to display the data in (friendly, text, json) (Default: friendly).')
@ -161,7 +161,14 @@ def info(cli):
print_parsed_rules_mk(cli.config.info.keyboard)
return False
# default keymap stored in config file should be ignored
if cli.config_source.info.keymap == 'config_file':
cli.config_source.info.keymap = None
# Build the info.json file
if cli.config.info.keymap:
kb_info_json = keymap_json(cli.config.info.keyboard, cli.config.info.keymap)
else:
kb_info_json = info_json(cli.config.info.keyboard)
# Output in the requested format
@ -178,11 +185,12 @@ def info(cli):
cli.log.error('Unknown format: %s', cli.args.format)
return False
# Output requested extras
if cli.config.info.layouts:
show_layouts(kb_info_json, title_caps)
if cli.config.info.matrix:
show_matrix(kb_info_json, title_caps)
if cli.config_source.info.keymap and cli.config_source.info.keymap != 'config_file':
if cli.config.info.keymap:
show_keymap(kb_info_json, title_caps)

View File

@ -8,10 +8,11 @@ from dotty_dict import dotty
from milc import cli
from qmk.constants import CHIBIOS_PROCESSORS, LUFA_PROCESSORS, VUSB_PROCESSORS
from qmk.c_parse import find_layouts
from qmk.c_parse import find_layouts, parse_config_h_file
from qmk.json_schema import deep_update, json_load, validate
from qmk.keyboard import config_h, rules_mk
from qmk.keymap import list_keymaps
from qmk.keymap import list_keymaps, locate_keymap
from qmk.commands import parse_configurator_json
from qmk.makefile import parse_rules_mk_file
from qmk.math import compute
@ -68,8 +69,8 @@ def info_json(keyboard):
# Merge in the data from info.json, config.h, and rules.mk
info_data = merge_info_jsons(keyboard, info_data)
info_data = _extract_rules_mk(info_data)
info_data = _extract_config_h(info_data)
info_data = _extract_rules_mk(info_data, rules_mk(str(keyboard)))
info_data = _extract_config_h(info_data, config_h(str(keyboard)))
# Ensure that we have matrix row and column counts
info_data = _matrix_size(info_data)
@ -400,11 +401,9 @@ def _extract_device_version(info_data):
info_data['usb']['device_version'] = f'{major}.{minor}.{revision}'
def _extract_config_h(info_data):
def _extract_config_h(info_data, config_c):
"""Pull some keyboard information from existing config.h files
"""
config_c = config_h(info_data['keyboard_folder'])
# Pull in data from the json map
dotty_info = dotty(info_data)
info_config_map = json_load(Path('data/mappings/info_config.json'))
@ -472,10 +471,9 @@ def _extract_config_h(info_data):
return info_data
def _extract_rules_mk(info_data):
def _extract_rules_mk(info_data, rules):
"""Pull some keyboard information from existing rules.mk files
"""
rules = rules_mk(info_data['keyboard_folder'])
info_data['processor'] = rules.get('MCU', info_data.get('processor', 'atmega32u4'))
if info_data['processor'] in CHIBIOS_PROCESSORS:
@ -766,3 +764,36 @@ def find_info_json(keyboard):
# Return a list of the info.json files that actually exist
return [info_json for info_json in info_jsons if info_json.exists()]
def keymap_json_config(keyboard, keymap):
"""Extract keymap level config
"""
keymap_folder = locate_keymap(keyboard, keymap).parent
km_info_json = parse_configurator_json(keymap_folder / 'keymap.json')
return km_info_json.get('config', {})
def keymap_json(keyboard, keymap):
"""Generate the info.json data for a specific keymap.
"""
keymap_folder = locate_keymap(keyboard, keymap).parent
# Files to scan
keymap_config = keymap_folder / 'config.h'
keymap_rules = keymap_folder / 'rules.mk'
keymap_file = keymap_folder / 'keymap.json'
# Build the info.json file
kb_info_json = info_json(keyboard)
# Merge in the data from keymap.json
km_info_json = keymap_json_config(keyboard, keymap) if keymap_file.exists() else {}
deep_update(kb_info_json, km_info_json)
# Merge in the data from config.h, and rules.mk
_extract_rules_mk(kb_info_json, parse_rules_mk_file(keymap_rules))
_extract_config_h(kb_info_json, parse_config_h_file(keymap_config))
return kb_info_json