diff options
Diffstat (limited to 'lib/python/qmk/cli/format/json.py')
-rwxr-xr-x | lib/python/qmk/cli/format/json.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/lib/python/qmk/cli/format/json.py b/lib/python/qmk/cli/format/json.py new file mode 100755 index 000000000..1358c70e7 --- /dev/null +++ b/lib/python/qmk/cli/format/json.py | |||
@@ -0,0 +1,66 @@ | |||
1 | """JSON Formatting Script | ||
2 | |||
3 | Spits out a JSON file formatted with one of QMK's formatters. | ||
4 | """ | ||
5 | import json | ||
6 | |||
7 | from jsonschema import ValidationError | ||
8 | from milc import cli | ||
9 | |||
10 | from qmk.info import info_json | ||
11 | from qmk.json_schema import json_load, keyboard_validate | ||
12 | from qmk.json_encoders import InfoJSONEncoder, KeymapJSONEncoder | ||
13 | from qmk.path import normpath | ||
14 | |||
15 | |||
16 | @cli.argument('json_file', arg_only=True, type=normpath, help='JSON file to format') | ||
17 | @cli.argument('-f', '--format', choices=['auto', 'keyboard', 'keymap'], default='auto', arg_only=True, help='JSON formatter to use (Default: autodetect)') | ||
18 | @cli.subcommand('Generate an info.json file for a keyboard.', hidden=False if cli.config.user.developer else True) | ||
19 | def format_json(cli): | ||
20 | """Format a json file. | ||
21 | """ | ||
22 | json_file = json_load(cli.args.json_file) | ||
23 | |||
24 | if cli.args.format == 'auto': | ||
25 | try: | ||
26 | keyboard_validate(json_file) | ||
27 | json_encoder = InfoJSONEncoder | ||
28 | |||
29 | except ValidationError as e: | ||
30 | cli.log.warning('File %s did not validate as a keyboard:\n\t%s', cli.args.json_file, e) | ||
31 | cli.log.info('Treating %s as a keymap file.', cli.args.json_file) | ||
32 | json_encoder = KeymapJSONEncoder | ||
33 | |||
34 | elif cli.args.format == 'keyboard': | ||
35 | json_encoder = InfoJSONEncoder | ||
36 | elif cli.args.format == 'keymap': | ||
37 | json_encoder = KeymapJSONEncoder | ||
38 | else: | ||
39 | # This should be impossible | ||
40 | cli.log.error('Unknown format: %s', cli.args.format) | ||
41 | return False | ||
42 | |||
43 | if json_encoder == KeymapJSONEncoder and 'layout' in json_file: | ||
44 | # Attempt to format the keycodes. | ||
45 | layout = json_file['layout'] | ||
46 | info_data = info_json(json_file['keyboard']) | ||
47 | |||
48 | if layout in info_data.get('layout_aliases', {}): | ||
49 | layout = json_file['layout'] = info_data['layout_aliases'][layout] | ||
50 | |||
51 | if layout in info_data.get('layouts'): | ||
52 | for layer_num, layer in enumerate(json_file['layers']): | ||
53 | current_layer = [] | ||
54 | last_row = 0 | ||
55 | |||
56 | for keymap_key, info_key in zip(layer, info_data['layouts'][layout]['layout']): | ||
57 | if last_row != info_key['y']: | ||
58 | current_layer.append('JSON_NEWLINE') | ||
59 | last_row = info_key['y'] | ||
60 | |||
61 | current_layer.append(keymap_key) | ||
62 | |||
63 | json_file['layers'][layer_num] = current_layer | ||
64 | |||
65 | # Display the results | ||
66 | print(json.dumps(json_file, cls=json_encoder)) | ||