summaryrefslogtreecommitdiffstats
path: root/lib/python/qmk/cli/generate/info_json.py
blob: 7e6654e45d47db198ab38bad4674e2765391b96a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Keyboard information script.

Compile an info.json for a particular keyboard and pretty-print it.
"""
import json

from milc import cli

from qmk.info_json_encoder import InfoJSONEncoder
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.info import info_json
from qmk.path import is_keyboard


@cli.argument('-kb', '--keyboard', help='Keyboard to show info for.')
@cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.')
@cli.subcommand('Generate an info.json file for a keyboard.', hidden=False if cli.config.user.developer else True)
@automagic_keyboard
@automagic_keymap
def generate_info_json(cli):
    """Generate an info.json file for a keyboard
    """
    # Determine our keyboard(s)
    if not cli.config.generate_info_json.keyboard:
        cli.log.error('Missing paramater: --keyboard')
        cli.subcommands['info'].print_help()
        return False

    if not is_keyboard(cli.config.generate_info_json.keyboard):
        cli.log.error('Invalid keyboard: "%s"', cli.config.generate_info_json.keyboard)
        return False

    # Build the info.json file
    kb_info_json = info_json(cli.config.generate_info_json.keyboard)
    pared_down_json = {}

    for key in ('manufacturer', 'maintainer', 'usb', 'keyboard_name', 'width', 'height', 'debounce', 'diode_direction', 'features', 'community_layouts', 'layout_aliases', 'matrix_pins', 'rgblight', 'url'):
        if key in kb_info_json:
            pared_down_json[key] = kb_info_json[key]

    pared_down_json['layouts'] = {}
    if 'layouts' in pared_down_json:
        for layout_name, layout in kb_info_json['layouts'].items():
            pared_down_json['layouts'][layout_name] = {}
            pared_down_json['layouts'][layout_name]['key_count'] = layout.get('key_count', len(layout['layout']))
            pared_down_json['layouts'][layout_name]['layout'] = layout['layout']

    # Display the results
    print(json.dumps(pared_down_json, indent=2, cls=InfoJSONEncoder))