summaryrefslogtreecommitdiff
path: root/lib/python/qmk/cli/generate/info_json.py
blob: 8931b68b6ff4fca2f7233f0de3c7f122a77fe34e (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Keyboard information script.

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

from jsonschema import Draft7Validator, validators
from milc import cli

from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.info import info_json
from qmk.json_encoders import InfoJSONEncoder
from qmk.json_schema import load_jsonschema
from qmk.keyboard import keyboard_completer, keyboard_folder
from qmk.path import is_keyboard


def pruning_validator(validator_class):
    """Extends Draft7Validator to remove properties that aren't specified in the schema.
    """
    validate_properties = validator_class.VALIDATORS["properties"]

    def remove_additional_properties(validator, properties, instance, schema):
        for prop in list(instance.keys()):
            if prop not in properties:
                del instance[prop]

        for error in validate_properties(validator, properties, instance, schema):
            yield error

    return validators.extend(validator_class, {"properties": remove_additional_properties})


def strip_info_json(kb_info_json):
    """Remove the API-only properties from the info.json.
    """
    pruning_draft_7_validator = pruning_validator(Draft7Validator)
    schema = load_jsonschema('keyboard')
    validator = pruning_draft_7_validator(schema).validate

    return validator(kb_info_json)


@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.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 parameter: --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)
    strip_info_json(kb_info_json)

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