diff options
| author | Zach White <skullydazed@users.noreply.github.com> | 2020-05-26 13:05:41 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-26 13:05:41 -0700 |
| commit | 751316c34465ea77e066c3052729b207f3d62e0c (patch) | |
| tree | cb99656b93c156757e2fd7c84fe716f9c300ca89 /lib/python/qmk/cli/info.py | |
| parent | 5d3bf8a050f3c0beb1f91147dc1ab54de36cbb05 (diff) | |
| download | qmk_firmware-751316c34465ea77e066c3052729b207f3d62e0c.tar.gz qmk_firmware-751316c34465ea77e066c3052729b207f3d62e0c.zip | |
[CLI] Add a subcommand for getting information about a keyboard (#8666)
You can now use `qmk info` to get information about keyboards and keymaps.
Co-authored-by: Erovia <Erovia@users.noreply.github.com>
Diffstat (limited to 'lib/python/qmk/cli/info.py')
| -rwxr-xr-x | lib/python/qmk/cli/info.py | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/lib/python/qmk/cli/info.py b/lib/python/qmk/cli/info.py new file mode 100755 index 000000000..6977673e2 --- /dev/null +++ b/lib/python/qmk/cli/info.py | |||
| @@ -0,0 +1,141 @@ | |||
| 1 | """Keyboard information script. | ||
| 2 | |||
| 3 | Compile an info.json for a particular keyboard and pretty-print it. | ||
| 4 | """ | ||
| 5 | import json | ||
| 6 | |||
| 7 | from milc import cli | ||
| 8 | |||
| 9 | from qmk.decorators import automagic_keyboard, automagic_keymap | ||
| 10 | from qmk.keyboard import render_layouts, render_layout | ||
| 11 | from qmk.keymap import locate_keymap | ||
| 12 | from qmk.info import info_json | ||
| 13 | from qmk.path import is_keyboard | ||
| 14 | |||
| 15 | ROW_LETTERS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop' | ||
| 16 | COL_LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijilmnopqrstuvwxyz' | ||
| 17 | |||
| 18 | |||
| 19 | def show_keymap(info_json, title_caps=True): | ||
| 20 | """Render the keymap in ascii art. | ||
| 21 | """ | ||
| 22 | keymap_path = locate_keymap(cli.config.info.keyboard, cli.config.info.keymap) | ||
| 23 | |||
| 24 | if keymap_path and keymap_path.suffix == '.json': | ||
| 25 | if title_caps: | ||
| 26 | cli.echo('{fg_blue}Keymap "%s"{fg_reset}:', cli.config.info.keymap) | ||
| 27 | else: | ||
| 28 | cli.echo('{fg_blue}keymap_%s{fg_reset}:', cli.config.info.keymap) | ||
| 29 | |||
| 30 | keymap_data = json.load(keymap_path.open()) | ||
| 31 | layout_name = keymap_data['layout'] | ||
| 32 | |||
| 33 | for layer_num, layer in enumerate(keymap_data['layers']): | ||
| 34 | if title_caps: | ||
| 35 | cli.echo('{fg_cyan}Layer %s{fg_reset}:', layer_num) | ||
| 36 | else: | ||
| 37 | cli.echo('{fg_cyan}layer_%s{fg_reset}:', layer_num) | ||
| 38 | |||
| 39 | print(render_layout(info_json['layouts'][layout_name]['layout'], layer)) | ||
| 40 | |||
| 41 | |||
| 42 | def show_layouts(kb_info_json, title_caps=True): | ||
| 43 | """Render the layouts with info.json labels. | ||
| 44 | """ | ||
| 45 | for layout_name, layout_art in render_layouts(kb_info_json).items(): | ||
| 46 | title = layout_name.title() if title_caps else layout_name | ||
| 47 | cli.echo('{fg_cyan}%s{fg_reset}:', title) | ||
| 48 | print(layout_art) # Avoid passing dirty data to cli.echo() | ||
| 49 | |||
| 50 | |||
| 51 | def show_matrix(info_json, title_caps=True): | ||
| 52 | """Render the layout with matrix labels in ascii art. | ||
| 53 | """ | ||
| 54 | for layout_name, layout in info_json['layouts'].items(): | ||
| 55 | # Build our label list | ||
| 56 | labels = [] | ||
| 57 | for key in layout['layout']: | ||
| 58 | if key['matrix']: | ||
| 59 | row = ROW_LETTERS[key['matrix'][0]] | ||
| 60 | col = COL_LETTERS[key['matrix'][1]] | ||
| 61 | |||
| 62 | labels.append(row + col) | ||
| 63 | else: | ||
| 64 | labels.append('') | ||
| 65 | |||
| 66 | # Print the header | ||
| 67 | if title_caps: | ||
| 68 | cli.echo('{fg_blue}Matrix for "%s"{fg_reset}:', layout_name) | ||
| 69 | else: | ||
| 70 | cli.echo('{fg_blue}matrix_%s{fg_reset}:', layout_name) | ||
| 71 | |||
| 72 | print(render_layout(info_json['layouts'][layout_name]['layout'], labels)) | ||
| 73 | |||
| 74 | |||
| 75 | @cli.argument('-kb', '--keyboard', help='Keyboard to show info for.') | ||
| 76 | @cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.') | ||
| 77 | @cli.argument('-l', '--layouts', action='store_true', help='Render the layouts.') | ||
| 78 | @cli.argument('-m', '--matrix', action='store_true', help='Render the layouts with matrix information.') | ||
| 79 | @cli.argument('-f', '--format', default='friendly', arg_only=True, help='Format to display the data in (friendly, text, json) (Default: friendly).') | ||
| 80 | @cli.subcommand('Keyboard information.') | ||
| 81 | @automagic_keyboard | ||
| 82 | @automagic_keymap | ||
| 83 | def info(cli): | ||
| 84 | """Compile an info.json for a particular keyboard and pretty-print it. | ||
| 85 | """ | ||
| 86 | # Determine our keyboard(s) | ||
| 87 | if not is_keyboard(cli.config.info.keyboard): | ||
| 88 | cli.log.error('Invalid keyboard: %s!', cli.config.info.keyboard) | ||
| 89 | exit(1) | ||
| 90 | |||
| 91 | # Build the info.json file | ||
| 92 | kb_info_json = info_json(cli.config.info.keyboard) | ||
| 93 | |||
| 94 | # Output in the requested format | ||
| 95 | if cli.args.format == 'json': | ||
| 96 | print(json.dumps(kb_info_json)) | ||
| 97 | exit() | ||
| 98 | |||
| 99 | if cli.args.format == 'text': | ||
| 100 | for key in sorted(kb_info_json): | ||
| 101 | if key == 'layouts': | ||
| 102 | cli.echo('{fg_blue}layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys()))) | ||
| 103 | else: | ||
| 104 | cli.echo('{fg_blue}%s{fg_reset}: %s', key, kb_info_json[key]) | ||
| 105 | |||
| 106 | if cli.config.info.layouts: | ||
| 107 | show_layouts(kb_info_json, False) | ||
| 108 | |||
| 109 | if cli.config.info.matrix: | ||
| 110 | show_matrix(kb_info_json, False) | ||
| 111 | |||
| 112 | if cli.config_source.info.keymap and cli.config_source.info.keymap != 'config_file': | ||
| 113 | show_keymap(kb_info_json, False) | ||
| 114 | |||
| 115 | elif cli.args.format == 'friendly': | ||
| 116 | cli.echo('{fg_blue}Keyboard Name{fg_reset}: %s', kb_info_json.get('keyboard_name', 'Unknown')) | ||
| 117 | cli.echo('{fg_blue}Manufacturer{fg_reset}: %s', kb_info_json.get('manufacturer', 'Unknown')) | ||
| 118 | if 'url' in kb_info_json: | ||
| 119 | cli.echo('{fg_blue}Website{fg_reset}: %s', kb_info_json['url']) | ||
| 120 | if kb_info_json.get('maintainer') == 'qmk': | ||
| 121 | cli.echo('{fg_blue}Maintainer{fg_reset}: QMK Community') | ||
| 122 | else: | ||
| 123 | cli.echo('{fg_blue}Maintainer{fg_reset}: %s', kb_info_json.get('maintainer', 'qmk')) | ||
| 124 | cli.echo('{fg_blue}Keyboard Folder{fg_reset}: %s', kb_info_json.get('keyboard_folder', 'Unknown')) | ||
| 125 | cli.echo('{fg_blue}Layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys()))) | ||
| 126 | if 'width' in kb_info_json and 'height' in kb_info_json: | ||
| 127 | cli.echo('{fg_blue}Size{fg_reset}: %s x %s' % (kb_info_json['width'], kb_info_json['height'])) | ||
| 128 | cli.echo('{fg_blue}Processor{fg_reset}: %s', kb_info_json.get('processor', 'Unknown')) | ||
| 129 | cli.echo('{fg_blue}Bootloader{fg_reset}: %s', kb_info_json.get('bootloader', 'Unknown')) | ||
| 130 | |||
| 131 | if cli.config.info.layouts: | ||
| 132 | show_layouts(kb_info_json, True) | ||
| 133 | |||
| 134 | if cli.config.info.matrix: | ||
| 135 | show_matrix(kb_info_json, True) | ||
| 136 | |||
| 137 | if cli.config_source.info.keymap and cli.config_source.info.keymap != 'config_file': | ||
| 138 | show_keymap(kb_info_json, True) | ||
| 139 | |||
| 140 | else: | ||
| 141 | cli.log.error('Unknown format: %s', cli.args.format) | ||
