2020-05-26 22:05:41 +02:00
|
|
|
"""Keyboard information script.
|
|
|
|
|
|
|
|
Compile an info.json for a particular keyboard and pretty-print it.
|
|
|
|
"""
|
2021-05-09 15:53:18 +02:00
|
|
|
import sys
|
2020-05-26 22:05:41 +02:00
|
|
|
import json
|
|
|
|
|
|
|
|
from milc import cli
|
|
|
|
|
2021-03-25 12:38:10 +01:00
|
|
|
from qmk.json_encoders import InfoJSONEncoder
|
2020-12-30 19:27:37 +01:00
|
|
|
from qmk.constants import COL_LETTERS, ROW_LETTERS
|
2020-05-26 22:05:41 +02:00
|
|
|
from qmk.decorators import automagic_keyboard, automagic_keymap
|
2021-05-09 17:06:44 +02:00
|
|
|
from qmk.keyboard import keyboard_completer, keyboard_folder, render_layouts, render_layout, rules_mk
|
2022-03-21 11:57:41 +01:00
|
|
|
from qmk.info import info_json, keymap_json
|
2020-05-26 22:05:41 +02:00
|
|
|
from qmk.keymap import locate_keymap
|
|
|
|
from qmk.path import is_keyboard
|
|
|
|
|
2021-05-09 15:53:18 +02:00
|
|
|
UNICODE_SUPPORT = sys.stdout.encoding.lower().startswith('utf')
|
2020-11-02 09:41:01 +01:00
|
|
|
|
2020-05-26 22:05:41 +02:00
|
|
|
|
2023-03-23 12:19:30 +01:00
|
|
|
def _strip_api_content(info_json):
|
|
|
|
# Ideally this would only be added in the API pathway.
|
|
|
|
info_json.pop('platform', None)
|
|
|
|
info_json.pop('platform_key', None)
|
|
|
|
info_json.pop('processor_type', None)
|
|
|
|
info_json.pop('protocol', None)
|
|
|
|
info_json.pop('config_h_features', None)
|
|
|
|
info_json.pop('keymaps', None)
|
|
|
|
info_json.pop('keyboard_folder', None)
|
|
|
|
info_json.pop('parse_errors', None)
|
|
|
|
info_json.pop('parse_warnings', None)
|
|
|
|
|
|
|
|
for layout in info_json.get('layouts', {}).values():
|
|
|
|
layout.pop('filename', None)
|
|
|
|
layout.pop('c_macro', None)
|
|
|
|
layout.pop('json_layout', None)
|
|
|
|
|
|
|
|
if 'matrix_pins' in info_json:
|
|
|
|
info_json.pop('matrix_size', None)
|
|
|
|
|
2023-08-27 05:00:14 +02:00
|
|
|
for feature in ['rgb_matrix', 'led_matrix']:
|
|
|
|
if info_json.get(feature, {}).get("layout", None):
|
|
|
|
info_json[feature].pop('led_count', None)
|
|
|
|
|
2023-03-23 12:19:30 +01:00
|
|
|
return info_json
|
|
|
|
|
|
|
|
|
2020-10-25 22:48:44 +01:00
|
|
|
def show_keymap(kb_info_json, title_caps=True):
|
2020-05-26 22:05:41 +02:00
|
|
|
"""Render the keymap in ascii art.
|
|
|
|
"""
|
|
|
|
keymap_path = locate_keymap(cli.config.info.keyboard, cli.config.info.keymap)
|
|
|
|
|
|
|
|
if keymap_path and keymap_path.suffix == '.json':
|
2021-02-27 21:00:50 +01:00
|
|
|
keymap_data = json.load(keymap_path.open(encoding='utf-8'))
|
2020-05-26 22:05:41 +02:00
|
|
|
layout_name = keymap_data['layout']
|
2021-08-17 00:33:30 +02:00
|
|
|
layout_name = kb_info_json.get('layout_aliases', {}).get(layout_name, layout_name) # Resolve alias names
|
2020-05-26 22:05:41 +02:00
|
|
|
|
|
|
|
for layer_num, layer in enumerate(keymap_data['layers']):
|
|
|
|
if title_caps:
|
2021-08-17 00:33:30 +02:00
|
|
|
cli.echo('{fg_cyan}Keymap %s Layer %s{fg_reset}:', cli.config.info.keymap, layer_num)
|
2020-05-26 22:05:41 +02:00
|
|
|
else:
|
2021-08-17 00:33:30 +02:00
|
|
|
cli.echo('{fg_cyan}keymap.%s.layer.%s{fg_reset}:', cli.config.info.keymap, layer_num)
|
2020-05-26 22:05:41 +02:00
|
|
|
|
2020-11-02 09:41:01 +01:00
|
|
|
print(render_layout(kb_info_json['layouts'][layout_name]['layout'], cli.config.info.ascii, layer))
|
2020-05-26 22:05:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
def show_layouts(kb_info_json, title_caps=True):
|
|
|
|
"""Render the layouts with info.json labels.
|
|
|
|
"""
|
2020-11-02 09:41:01 +01:00
|
|
|
for layout_name, layout_art in render_layouts(kb_info_json, cli.config.info.ascii).items():
|
2021-08-17 00:33:30 +02:00
|
|
|
title = f'Layout {layout_name.title()}' if title_caps else f'layouts.{layout_name}'
|
2020-05-26 22:05:41 +02:00
|
|
|
cli.echo('{fg_cyan}%s{fg_reset}:', title)
|
|
|
|
print(layout_art) # Avoid passing dirty data to cli.echo()
|
|
|
|
|
|
|
|
|
2020-10-25 22:48:44 +01:00
|
|
|
def show_matrix(kb_info_json, title_caps=True):
|
2020-05-26 22:05:41 +02:00
|
|
|
"""Render the layout with matrix labels in ascii art.
|
|
|
|
"""
|
2020-10-25 22:48:44 +01:00
|
|
|
for layout_name, layout in kb_info_json['layouts'].items():
|
2020-05-26 22:05:41 +02:00
|
|
|
# Build our label list
|
|
|
|
labels = []
|
|
|
|
for key in layout['layout']:
|
2021-02-27 21:00:50 +01:00
|
|
|
if 'matrix' in key:
|
2020-05-26 22:05:41 +02:00
|
|
|
row = ROW_LETTERS[key['matrix'][0]]
|
|
|
|
col = COL_LETTERS[key['matrix'][1]]
|
|
|
|
|
|
|
|
labels.append(row + col)
|
|
|
|
else:
|
|
|
|
labels.append('')
|
|
|
|
|
|
|
|
# Print the header
|
|
|
|
if title_caps:
|
|
|
|
cli.echo('{fg_blue}Matrix for "%s"{fg_reset}:', layout_name)
|
|
|
|
else:
|
|
|
|
cli.echo('{fg_blue}matrix_%s{fg_reset}:', layout_name)
|
|
|
|
|
2020-11-02 09:41:01 +01:00
|
|
|
print(render_layout(kb_info_json['layouts'][layout_name]['layout'], cli.config.info.ascii, labels))
|
2020-05-26 22:05:41 +02:00
|
|
|
|
|
|
|
|
2020-10-25 22:48:44 +01:00
|
|
|
def print_friendly_output(kb_info_json):
|
2020-05-27 09:43:22 +02:00
|
|
|
"""Print the info.json in a friendly text format.
|
|
|
|
"""
|
2020-10-25 22:48:44 +01:00
|
|
|
cli.echo('{fg_blue}Keyboard Name{fg_reset}: %s', kb_info_json.get('keyboard_name', 'Unknown'))
|
|
|
|
cli.echo('{fg_blue}Manufacturer{fg_reset}: %s', kb_info_json.get('manufacturer', 'Unknown'))
|
|
|
|
if 'url' in kb_info_json:
|
|
|
|
cli.echo('{fg_blue}Website{fg_reset}: %s', kb_info_json.get('url', ''))
|
|
|
|
if kb_info_json.get('maintainer', 'qmk') == 'qmk':
|
2020-05-27 09:43:22 +02:00
|
|
|
cli.echo('{fg_blue}Maintainer{fg_reset}: QMK Community')
|
|
|
|
else:
|
2020-10-25 22:48:44 +01:00
|
|
|
cli.echo('{fg_blue}Maintainer{fg_reset}: %s', kb_info_json['maintainer'])
|
|
|
|
cli.echo('{fg_blue}Layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys())))
|
|
|
|
cli.echo('{fg_blue}Processor{fg_reset}: %s', kb_info_json.get('processor', 'Unknown'))
|
|
|
|
cli.echo('{fg_blue}Bootloader{fg_reset}: %s', kb_info_json.get('bootloader', 'Unknown'))
|
2021-02-27 21:00:50 +01:00
|
|
|
if 'layout_aliases' in kb_info_json:
|
|
|
|
aliases = [f'{key}={value}' for key, value in kb_info_json['layout_aliases'].items()]
|
|
|
|
cli.echo('{fg_blue}Layout aliases:{fg_reset} %s' % (', '.join(aliases),))
|
2020-05-27 09:43:22 +02:00
|
|
|
|
|
|
|
|
2020-10-25 22:48:44 +01:00
|
|
|
def print_text_output(kb_info_json):
|
2020-05-27 09:43:22 +02:00
|
|
|
"""Print the info.json in a plain text format.
|
|
|
|
"""
|
2020-10-25 22:48:44 +01:00
|
|
|
for key in sorted(kb_info_json):
|
2020-05-27 09:43:22 +02:00
|
|
|
if key == 'layouts':
|
2020-10-25 22:48:44 +01:00
|
|
|
cli.echo('{fg_blue}layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys())))
|
2020-05-27 09:43:22 +02:00
|
|
|
else:
|
2020-10-25 22:48:44 +01:00
|
|
|
cli.echo('{fg_blue}%s{fg_reset}: %s', key, kb_info_json[key])
|
2020-05-27 09:43:22 +02:00
|
|
|
|
|
|
|
if cli.config.info.layouts:
|
2020-10-25 22:48:44 +01:00
|
|
|
show_layouts(kb_info_json, False)
|
2020-05-27 09:43:22 +02:00
|
|
|
|
|
|
|
if cli.config.info.matrix:
|
2020-10-25 22:48:44 +01:00
|
|
|
show_matrix(kb_info_json, False)
|
2020-05-27 09:43:22 +02:00
|
|
|
|
|
|
|
if cli.config_source.info.keymap and cli.config_source.info.keymap != 'config_file':
|
2020-10-25 22:48:44 +01:00
|
|
|
show_keymap(kb_info_json, False)
|
2020-05-27 09:43:22 +02:00
|
|
|
|
|
|
|
|
2021-08-17 00:33:30 +02:00
|
|
|
def print_dotted_output(kb_info_json, prefix=''):
|
|
|
|
"""Print the info.json in a plain text format with dot-joined keys.
|
|
|
|
"""
|
|
|
|
for key in sorted(kb_info_json):
|
|
|
|
new_prefix = f'{prefix}.{key}' if prefix else key
|
|
|
|
|
|
|
|
if key in ['parse_errors', 'parse_warnings']:
|
|
|
|
continue
|
|
|
|
elif key == 'layouts' and prefix == '':
|
|
|
|
cli.echo('{fg_blue}layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys())))
|
|
|
|
elif isinstance(kb_info_json[key], dict):
|
|
|
|
print_dotted_output(kb_info_json[key], new_prefix)
|
|
|
|
elif isinstance(kb_info_json[key], list):
|
|
|
|
cli.echo('{fg_blue}%s{fg_reset}: %s', new_prefix, ', '.join(map(str, sorted(kb_info_json[key]))))
|
|
|
|
else:
|
|
|
|
cli.echo('{fg_blue}%s{fg_reset}: %s', new_prefix, kb_info_json[key])
|
|
|
|
|
|
|
|
|
2021-05-09 17:06:44 +02:00
|
|
|
def print_parsed_rules_mk(keyboard_name):
|
|
|
|
rules = rules_mk(keyboard_name)
|
|
|
|
for k in sorted(rules.keys()):
|
|
|
|
print('%s = %s' % (k, rules[k]))
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2021-04-15 04:00:22 +02:00
|
|
|
@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='Keyboard to show info for.')
|
2022-03-21 11:57:41 +01:00
|
|
|
@cli.argument('-km', '--keymap', help='Keymap to show info for (Optional).')
|
2020-05-26 22:05:41 +02:00
|
|
|
@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).')
|
2021-05-09 15:53:18 +02:00
|
|
|
@cli.argument('--ascii', action='store_true', default=not UNICODE_SUPPORT, help='Render layout box drawings in ASCII only.')
|
2021-05-09 17:06:44 +02:00
|
|
|
@cli.argument('-r', '--rules-mk', action='store_true', help='Render the parsed values of the keyboard\'s rules.mk file.')
|
2023-03-23 12:19:30 +01:00
|
|
|
@cli.argument('-a', '--api', action='store_true', help='Show fully processed info intended for API consumption.')
|
2020-05-26 22:05:41 +02:00
|
|
|
@cli.subcommand('Keyboard information.')
|
|
|
|
@automagic_keyboard
|
|
|
|
@automagic_keymap
|
|
|
|
def info(cli):
|
|
|
|
"""Compile an info.json for a particular keyboard and pretty-print it.
|
|
|
|
"""
|
|
|
|
# Determine our keyboard(s)
|
2020-05-27 09:43:22 +02:00
|
|
|
if not cli.config.info.keyboard:
|
2020-11-02 09:41:01 +01:00
|
|
|
cli.log.error('Missing parameter: --keyboard')
|
2020-05-27 09:43:22 +02:00
|
|
|
cli.subcommands['info'].print_help()
|
2020-10-07 01:46:10 +02:00
|
|
|
return False
|
2020-05-27 09:43:22 +02:00
|
|
|
|
2020-05-26 22:05:41 +02:00
|
|
|
if not is_keyboard(cli.config.info.keyboard):
|
2020-05-27 09:43:22 +02:00
|
|
|
cli.log.error('Invalid keyboard: "%s"', cli.config.info.keyboard)
|
2020-10-07 01:46:10 +02:00
|
|
|
return False
|
2020-05-26 22:05:41 +02:00
|
|
|
|
2021-05-09 17:06:44 +02:00
|
|
|
if bool(cli.args.rules_mk):
|
|
|
|
print_parsed_rules_mk(cli.config.info.keyboard)
|
|
|
|
return False
|
|
|
|
|
2022-03-21 11:57:41 +01:00
|
|
|
# default keymap stored in config file should be ignored
|
|
|
|
if cli.config_source.info.keymap == 'config_file':
|
|
|
|
cli.config_source.info.keymap = None
|
|
|
|
|
2020-05-26 22:05:41 +02:00
|
|
|
# Build the info.json file
|
2022-03-21 11:57:41 +01:00
|
|
|
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)
|
2020-05-26 22:05:41 +02:00
|
|
|
|
2023-03-23 12:19:30 +01:00
|
|
|
if not cli.args.api:
|
|
|
|
kb_info_json = _strip_api_content(kb_info_json)
|
|
|
|
|
2020-05-26 22:05:41 +02:00
|
|
|
# Output in the requested format
|
|
|
|
if cli.args.format == 'json':
|
2023-05-20 14:15:05 +02:00
|
|
|
print(json.dumps(kb_info_json, cls=InfoJSONEncoder, sort_keys=True))
|
2021-08-17 00:33:30 +02:00
|
|
|
return True
|
2020-10-07 01:46:10 +02:00
|
|
|
elif cli.args.format == 'text':
|
2021-08-17 00:33:30 +02:00
|
|
|
print_dotted_output(kb_info_json)
|
|
|
|
title_caps = False
|
2020-05-26 22:05:41 +02:00
|
|
|
elif cli.args.format == 'friendly':
|
2020-05-27 09:43:22 +02:00
|
|
|
print_friendly_output(kb_info_json)
|
2021-08-17 00:33:30 +02:00
|
|
|
title_caps = True
|
2020-05-26 22:05:41 +02:00
|
|
|
else:
|
|
|
|
cli.log.error('Unknown format: %s', cli.args.format)
|
2020-10-07 01:46:10 +02:00
|
|
|
return False
|
2021-08-17 00:33:30 +02:00
|
|
|
|
2022-03-21 11:57:41 +01:00
|
|
|
# Output requested extras
|
2021-08-17 00:33:30 +02:00
|
|
|
if cli.config.info.layouts:
|
|
|
|
show_layouts(kb_info_json, title_caps)
|
|
|
|
|
|
|
|
if cli.config.info.matrix:
|
|
|
|
show_matrix(kb_info_json, title_caps)
|
|
|
|
|
2022-03-21 11:57:41 +01:00
|
|
|
if cli.config.info.keymap:
|
2021-08-17 00:33:30 +02:00
|
|
|
show_keymap(kb_info_json, title_caps)
|