summaryrefslogtreecommitdiffstats
path: root/lib/python/qmk/cli/generate/info_json.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python/qmk/cli/generate/info_json.py')
-rwxr-xr-xlib/python/qmk/cli/generate/info_json.py40
1 files changed, 33 insertions, 7 deletions
diff --git a/lib/python/qmk/cli/generate/info_json.py b/lib/python/qmk/cli/generate/info_json.py
index 8931b68b6f..284d1a8510 100755
--- a/lib/python/qmk/cli/generate/info_json.py
+++ b/lib/python/qmk/cli/generate/info_json.py
@@ -4,15 +4,17 @@ Compile an info.json for a particular keyboard and pretty-print it.
"""
import json
-from jsonschema import Draft7Validator, validators
+from argcomplete.completers import FilesCompleter
+from jsonschema import Draft7Validator, RefResolver, validators
from milc import cli
+from pathlib import Path
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.json_schema import compile_schema_store
from qmk.keyboard import keyboard_completer, keyboard_folder
-from qmk.path import is_keyboard
+from qmk.path import is_keyboard, normpath
def pruning_validator(validator_class):
@@ -34,15 +36,19 @@ def pruning_validator(validator_class):
def strip_info_json(kb_info_json):
"""Remove the API-only properties from the info.json.
"""
+ schema_store = compile_schema_store()
pruning_draft_7_validator = pruning_validator(Draft7Validator)
- schema = load_jsonschema('keyboard')
- validator = pruning_draft_7_validator(schema).validate
+ schema = schema_store['qmk.keyboard.v1']
+ resolver = RefResolver.from_schema(schema_store['qmk.keyboard.v1'], store=schema_store)
+ validator = pruning_draft_7_validator(schema, resolver=resolver).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.argument('-o', '--output', arg_only=True, completer=FilesCompleter, help='Write the output the specified file, overwriting if necessary.')
+@cli.argument('-ow', '--overwrite', arg_only=True, action='store_true', help='Overwrite the existing info.json. (Overrides the location of --output)')
@cli.subcommand('Generate an info.json file for a keyboard.', hidden=False if cli.config.user.developer else True)
@automagic_keyboard
@automagic_keymap
@@ -59,9 +65,29 @@ def generate_info_json(cli):
cli.log.error('Invalid keyboard: "%s"', cli.config.generate_info_json.keyboard)
return False
+ if cli.args.overwrite:
+ output_path = (Path('keyboards') / cli.config.generate_info_json.keyboard / 'info.json').resolve()
+
+ if cli.args.output:
+ cli.log.warning('Overwriting user supplied --output with %s', output_path)
+
+ cli.args.output = output_path
+
# Build the info.json file
kb_info_json = info_json(cli.config.generate_info_json.keyboard)
strip_info_json(kb_info_json)
+ info_json_text = json.dumps(kb_info_json, indent=4, cls=InfoJSONEncoder)
+
+ if cli.args.output:
+ # Write to a file
+ output_path = normpath(cli.args.output)
+
+ if output_path.exists():
+ cli.log.warning('Overwriting output file %s', output_path)
+
+ output_path.write_text(info_json_text + '\n')
+ cli.log.info('Wrote info.json to %s.', output_path)
- # Display the results
- print(json.dumps(kb_info_json, indent=2, cls=InfoJSONEncoder))
+ else:
+ # Display the results
+ print(info_json_text)