Skip to content

Commit

Permalink
Bring qmk generate-info-json inline with other generate commands
Browse files Browse the repository at this point in the history
  • Loading branch information
skullydazed committed Jul 23, 2021
1 parent f9665c1 commit 4444661
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
46 changes: 31 additions & 15 deletions lib/python/qmk/cli/generate/info_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
"""
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, load_jsonschema
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):
Expand All @@ -35,16 +36,19 @@ def remove_additional_properties(validator, properties, instance, schema):
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('qmk.keyboard.v1')
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', action='store_true', help='Write the output to a file.')
@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
Expand All @@ -61,17 +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)

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

# Write to a file if --output flag was specified
if cli.args.output:
output_path = Path('keyboards') / cli.config.generate_info_json.keyboard / 'info.json'
with open(output_path, 'w+') as json_file:
json_object = json.dumps(kb_info_json, indent=4, cls=InfoJSONEncoder)
json_file.write(json_object)
cli.log.info('Wrote file to %s.', output_path)
# 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)

else:
# Display the results
print(info_json_text)
12 changes: 10 additions & 2 deletions lib/python/qmk/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def load_jsonschema(schema_name):


@lru_cache(maxsize=0)
def create_validator(schema):
"""Creates a validator for the given schema id.
def compile_schema_store():
"""Compile all our schemas into a schema store.
"""
schema_store = {}

Expand All @@ -51,6 +51,14 @@ def create_validator(schema):
continue
schema_store[schema_data['$id']] = schema_data

return schema_store


@lru_cache(maxsize=0)
def create_validator(schema):
"""Creates a validator for the given schema id.
"""
schema_store = compile_schema_store()
resolver = jsonschema.RefResolver.from_schema(schema_store['qmk.keyboard.v1'], store=schema_store)

return jsonschema.Draft7Validator(schema_store[schema], resolver=resolver).validate
Expand Down

0 comments on commit 4444661

Please sign in to comment.