2020-05-27 02:05:41 +06:00
|
|
|
"""Keyboard information script.
|
|
|
|
|
|
|
|
Compile an info.json for a particular keyboard and pretty-print it.
|
|
|
|
"""
|
|
|
|
import json
|
2020-11-02 14:41:01 +06:00
|
|
|
import platform
|
2020-05-27 02:05:41 +06:00
|
|
|
|
|
|
|
from milc import cli
|
|
|
|
|
2020-12-31 00:27:37 +06:00
|
|
|
from qmk.info_json_encoder import InfoJSONEncoder
|
|
|
|
from qmk.constants import COL_LETTERS, ROW_LETTERS
|
2020-05-27 02:05:41 +06:00
|
|
|
from qmk.decorators import automagic_keyboard, automagic_keymap
|
|
|
|
from qmk.keyboard import render_layouts, render_layout
|
|
|
|
from qmk.keymap import locate_keymap
|
|
|
|
from qmk.info import info_json
|
|
|
|
from qmk.path import is_keyboard
|
|
|
|
|
2020-11-02 14:41:01 +06:00
|
|
|
platform_id = platform.platform().lower()
|
|
|
|
|
2020-05-27 02:05:41 +06:00
|
|
|
|
2020-10-26 03:48:44 +06:00
|
|
|
def show_keymap(kb_info_json, title_caps=True):
|
2020-05-27 02:05:41 +06: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':
|
|
|
|
if title_caps:
|
|
|
|
cli.echo('{fg_blue}Keymap "%s"{fg_reset}:', cli.config.info.keymap)
|
|
|
|
else:
|
|
|
|
cli.echo('{fg_blue}keymap_%s{fg_reset}:', cli.config.info.keymap)
|
|
|
|
|
|
|
|
keymap_data = json.load(keymap_path.open())
|
|
|
|
layout_name = keymap_data['layout']
|
|
|
|
|
|
|
|
for layer_num, layer in enumerate(keymap_data['layers']):
|
|
|
|
if title_caps:
|
|
|
|
cli.echo('{fg_cyan}Layer %s{fg_reset}:', layer_num)
|
|
|
|
else:
|
|
|
|
cli.echo('{fg_cyan}layer_%s{fg_reset}:', layer_num)
|
|
|
|
|
2020-11-02 14:41:01 +06:00
|
|
|
print(render_layout(kb_info_json['layouts'][layout_name]['layout'], cli.config.info.ascii, layer))
|
2020-05-27 02:05:41 +06:00
|
|
|
|
|
|
|
|
|
|
|
def show_layouts(kb_info_json, title_caps=True):
|
|
|
|
"""Render the layouts with info.json labels.
|
|
|
|
"""
|
2020-11-02 14:41:01 +06:00
|
|
|
for layout_name, layout_art in render_layouts(kb_info_json, cli.config.info.ascii).items():
|
2020-05-27 02:05:41 +06:00
|
|
|
title = layout_name.title() if title_caps else layout_name
|
|
|
|
cli.echo('{fg_cyan}%s{fg_reset}:', title)
|
|
|
|
print(layout_art) # Avoid passing dirty data to cli.echo()
|
|
|
|
|
|
|
|
|
2020-10-26 03:48:44 +06:00
|
|
|
def show_matrix(kb_info_json, title_caps=True):
|
2020-05-27 02:05:41 +06:00
|
|
|
"""Render the layout with matrix labels in ascii art.
|
|
|
|
"""
|
2020-10-26 03:48:44 +06:00
|
|
|
for layout_name, layout in kb_info_json['layouts'].items():
|
2020-05-27 02:05:41 +06:00
|
|
|
# Build our label list
|
|
|
|
labels = []
|
|
|
|
for key in layout['layout']:
|
|
|
|
if key['matrix']:
|
|
|
|
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 14:41:01 +06:00
|
|
|
print(render_layout(kb_info_json['layouts'][layout_name]['layout'], cli.config.info.ascii, labels))
|
2020-05-27 02:05:41 +06:00
|
|
|
|
|
|
|
|
2020-10-26 03:48:44 +06:00
|
|
|
def print_friendly_output(kb_info_json):
|
2020-05-27 13:43:22 +06:00
|
|
|
"""Print the info.json in a friendly text format.
|
|
|
|
"""
|
2020-10-26 03:48:44 +06: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 13:43:22 +06:00
|
|
|
cli.echo('{fg_blue}Maintainer{fg_reset}: QMK Community')
|
|
|
|
else:
|
2020-10-26 03:48:44 +06:00
|
|
|
cli.echo('{fg_blue}Maintainer{fg_reset}: %s', kb_info_json['maintainer'])
|
|
|
|
cli.echo('{fg_blue}Keyboard Folder{fg_reset}: %s', kb_info_json.get('keyboard_folder', 'Unknown'))
|
|
|
|
cli.echo('{fg_blue}Layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys())))
|
|
|
|
if 'width' in kb_info_json and 'height' in kb_info_json:
|
|
|
|
cli.echo('{fg_blue}Size{fg_reset}: %s x %s' % (kb_info_json['width'], kb_info_json['height']))
|
|
|
|
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'))
|
2020-05-27 13:43:22 +06:00
|
|
|
|
|
|
|
if cli.config.info.layouts:
|
2020-10-26 03:48:44 +06:00
|
|
|
show_layouts(kb_info_json, True)
|
2020-05-27 13:43:22 +06:00
|
|
|
|
|
|
|
if cli.config.info.matrix:
|
2020-10-26 03:48:44 +06:00
|
|
|
show_matrix(kb_info_json, True)
|
2020-05-27 13:43:22 +06:00
|
|
|
|
|
|
|
if cli.config_source.info.keymap and cli.config_source.info.keymap != 'config_file':
|
2020-10-26 03:48:44 +06:00
|
|
|
show_keymap(kb_info_json, True)
|
2020-05-27 13:43:22 +06:00
|
|
|
|
|
|
|
|
2020-10-26 03:48:44 +06:00
|
|
|
def print_text_output(kb_info_json):
|
2020-05-27 13:43:22 +06:00
|
|
|
"""Print the info.json in a plain text format.
|
|
|
|
"""
|
2020-10-26 03:48:44 +06:00
|
|
|
for key in sorted(kb_info_json):
|
2020-05-27 13:43:22 +06:00
|
|
|
if key == 'layouts':
|
2020-10-26 03:48:44 +06:00
|
|
|
cli.echo('{fg_blue}layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys())))
|
2020-05-27 13:43:22 +06:00
|
|
|
else:
|
2020-10-26 03:48:44 +06:00
|
|
|
cli.echo('{fg_blue}%s{fg_reset}: %s', key, kb_info_json[key])
|
2020-05-27 13:43:22 +06:00
|
|
|
|
|
|
|
if cli.config.info.layouts:
|
2020-10-26 03:48:44 +06:00
|
|
|
show_layouts(kb_info_json, False)
|
2020-05-27 13:43:22 +06:00
|
|
|
|
|
|
|
if cli.config.info.matrix:
|
2020-10-26 03:48:44 +06:00
|
|
|
show_matrix(kb_info_json, False)
|
2020-05-27 13:43:22 +06:00
|
|
|
|
|
|
|
if cli.config_source.info.keymap and cli.config_source.info.keymap != 'config_file':
|
2020-10-26 03:48:44 +06:00
|
|
|
show_keymap(kb_info_json, False)
|
2020-05-27 13:43:22 +06:00
|
|
|
|
|
|
|
|
2020-05-27 02:05:41 +06:00
|
|
|
@cli.argument('-kb', '--keyboard', help='Keyboard to show info for.')
|
|
|
|
@cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.')
|
|
|
|
@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).')
|
2020-11-02 14:41:01 +06:00
|
|
|
@cli.argument('--ascii', action='store_true', default='windows' in platform_id, help='Render layout box drawings in ASCII only.')
|
2020-05-27 02:05:41 +06: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 13:43:22 +06:00
|
|
|
if not cli.config.info.keyboard:
|
2020-11-02 14:41:01 +06:00
|
|
|
cli.log.error('Missing parameter: --keyboard')
|
2020-05-27 13:43:22 +06:00
|
|
|
cli.subcommands['info'].print_help()
|
2020-10-07 05:46:10 +06:00
|
|
|
return False
|
2020-05-27 13:43:22 +06:00
|
|
|
|
2020-05-27 02:05:41 +06:00
|
|
|
if not is_keyboard(cli.config.info.keyboard):
|
2020-05-27 13:43:22 +06:00
|
|
|
cli.log.error('Invalid keyboard: "%s"', cli.config.info.keyboard)
|
2020-10-07 05:46:10 +06:00
|
|
|
return False
|
2020-05-27 02:05:41 +06:00
|
|
|
|
|
|
|
# Build the info.json file
|
|
|
|
kb_info_json = info_json(cli.config.info.keyboard)
|
|
|
|
|
|
|
|
# Output in the requested format
|
|
|
|
if cli.args.format == 'json':
|
2020-12-31 00:27:37 +06:00
|
|
|
print(json.dumps(kb_info_json, cls=InfoJSONEncoder))
|
2020-10-07 05:46:10 +06:00
|
|
|
elif cli.args.format == 'text':
|
2020-05-27 13:43:22 +06:00
|
|
|
print_text_output(kb_info_json)
|
2020-05-27 02:05:41 +06:00
|
|
|
elif cli.args.format == 'friendly':
|
2020-05-27 13:43:22 +06:00
|
|
|
print_friendly_output(kb_info_json)
|
2020-05-27 02:05:41 +06:00
|
|
|
else:
|
|
|
|
cli.log.error('Unknown format: %s', cli.args.format)
|
2020-10-07 05:46:10 +06:00
|
|
|
return False
|