aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk/cli/format/json.py
diff options
context:
space:
mode:
authorZach White <skullydazed@gmail.com>2021-03-25 04:38:10 -0700
committerGitHub <noreply@github.com>2021-03-25 22:38:10 +1100
commit3e60997edba46544557b3a775bdb1538e07c3edf (patch)
tree165923c2cd6b5eeee150c0285ce8ed9eff44eaee /lib/python/qmk/cli/format/json.py
parenta74846a0dbd5efbc7b993918f7f3122e1d620c24 (diff)
downloadqmk_firmware-3e60997edba46544557b3a775bdb1538e07c3edf.tar.gz
qmk_firmware-3e60997edba46544557b3a775bdb1538e07c3edf.zip
Add a `qmk format-json` command that will format JSON files (#12372)
* Add a command to format json files * change to work after rebase * add test for qmk format-json * add documentation for qmk format-json * Update lib/python/qmk/cli/format/json.py
Diffstat (limited to 'lib/python/qmk/cli/format/json.py')
-rwxr-xr-xlib/python/qmk/cli/format/json.py66
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
3Spits out a JSON file formatted with one of QMK's formatters.
4"""
5import json
6
7from jsonschema import ValidationError
8from milc import cli
9
10from qmk.info import info_json
11from qmk.json_schema import json_load, keyboard_validate
12from qmk.json_encoders import InfoJSONEncoder, KeymapJSONEncoder
13from 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)
19def 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))