summaryrefslogtreecommitdiff
path: root/lib/python/qmk/cli
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python/qmk/cli')
-rw-r--r--lib/python/qmk/cli/__init__.py1
-rw-r--r--lib/python/qmk/cli/c2json.py2
-rw-r--r--lib/python/qmk/cli/format/__init__.py1
-rwxr-xr-xlib/python/qmk/cli/format/json.py66
-rwxr-xr-xlib/python/qmk/cli/generate/api.py2
-rwxr-xr-xlib/python/qmk/cli/generate/info_json.py2
-rwxr-xr-xlib/python/qmk/cli/info.py2
-rwxr-xr-xlib/python/qmk/cli/kle2json.py2
8 files changed, 73 insertions, 5 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py
index a5f1f47679..1349e68a9b 100644
--- a/lib/python/qmk/cli/__init__.py
+++ b/lib/python/qmk/cli/__init__.py
@@ -16,6 +16,7 @@ from . import docs
from . import doctor
from . import fileformat
from . import flash
+from . import format
from . import generate
from . import hello
from . import info
diff --git a/lib/python/qmk/cli/c2json.py b/lib/python/qmk/cli/c2json.py
index a97e212223..1fa833b647 100644
--- a/lib/python/qmk/cli/c2json.py
+++ b/lib/python/qmk/cli/c2json.py
@@ -6,7 +6,7 @@ from milc import cli
import qmk.keymap
import qmk.path
-from qmk.info_json_encoder import InfoJSONEncoder
+from qmk.json_encoders import InfoJSONEncoder
from qmk.keyboard import keyboard_folder
diff --git a/lib/python/qmk/cli/format/__init__.py b/lib/python/qmk/cli/format/__init__.py
new file mode 100644
index 0000000000..741ec778b1
--- /dev/null
+++ b/lib/python/qmk/cli/format/__init__.py
@@ -0,0 +1 @@
+from . import json
diff --git a/lib/python/qmk/cli/format/json.py b/lib/python/qmk/cli/format/json.py
new file mode 100755
index 0000000000..1358c70e7a
--- /dev/null
+++ b/lib/python/qmk/cli/format/json.py
@@ -0,0 +1,66 @@
+"""JSON Formatting Script
+
+Spits out a JSON file formatted with one of QMK's formatters.
+"""
+import json
+
+from jsonschema import ValidationError
+from milc import cli
+
+from qmk.info import info_json
+from qmk.json_schema import json_load, keyboard_validate
+from qmk.json_encoders import InfoJSONEncoder, KeymapJSONEncoder
+from qmk.path import normpath
+
+
+@cli.argument('json_file', arg_only=True, type=normpath, help='JSON file to format')
+@cli.argument('-f', '--format', choices=['auto', 'keyboard', 'keymap'], default='auto', arg_only=True, help='JSON formatter to use (Default: autodetect)')
+@cli.subcommand('Generate an info.json file for a keyboard.', hidden=False if cli.config.user.developer else True)
+def format_json(cli):
+ """Format a json file.
+ """
+ json_file = json_load(cli.args.json_file)
+
+ if cli.args.format == 'auto':
+ try:
+ keyboard_validate(json_file)
+ json_encoder = InfoJSONEncoder
+
+ except ValidationError as e:
+ cli.log.warning('File %s did not validate as a keyboard:\n\t%s', cli.args.json_file, e)
+ cli.log.info('Treating %s as a keymap file.', cli.args.json_file)
+ json_encoder = KeymapJSONEncoder
+
+ elif cli.args.format == 'keyboard':
+ json_encoder = InfoJSONEncoder
+ elif cli.args.format == 'keymap':
+ json_encoder = KeymapJSONEncoder
+ else:
+ # This should be impossible
+ cli.log.error('Unknown format: %s', cli.args.format)
+ return False
+
+ if json_encoder == KeymapJSONEncoder and 'layout' in json_file:
+ # Attempt to format the keycodes.
+ layout = json_file['layout']
+ info_data = info_json(json_file['keyboard'])
+
+ if layout in info_data.get('layout_aliases', {}):
+ layout = json_file['layout'] = info_data['layout_aliases'][layout]
+
+ if layout in info_data.get('layouts'):
+ for layer_num, layer in enumerate(json_file['layers']):
+ current_layer = []
+ last_row = 0
+
+ for keymap_key, info_key in zip(layer, info_data['layouts'][layout]['layout']):
+ if last_row != info_key['y']:
+ current_layer.append('JSON_NEWLINE')
+ last_row = info_key['y']
+
+ current_layer.append(keymap_key)
+
+ json_file['layers'][layer_num] = current_layer
+
+ # Display the results
+ print(json.dumps(json_file, cls=json_encoder))
diff --git a/lib/python/qmk/cli/generate/api.py b/lib/python/qmk/cli/generate/api.py
index 2905908345..70019428f0 100755
--- a/lib/python/qmk/cli/generate/api.py
+++ b/lib/python/qmk/cli/generate/api.py
@@ -8,7 +8,7 @@ from milc import cli
from qmk.datetime import current_datetime
from qmk.info import info_json
-from qmk.info_json_encoder import InfoJSONEncoder
+from qmk.json_encoders import InfoJSONEncoder
from qmk.json_schema import json_load
from qmk.keyboard import list_keyboards
diff --git a/lib/python/qmk/cli/generate/info_json.py b/lib/python/qmk/cli/generate/info_json.py
index 6c00ba7d8a..1af7f04392 100755
--- a/lib/python/qmk/cli/generate/info_json.py
+++ b/lib/python/qmk/cli/generate/info_json.py
@@ -9,7 +9,7 @@ from milc import cli
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.info import info_json
-from qmk.info_json_encoder import InfoJSONEncoder
+from qmk.json_encoders import InfoJSONEncoder
from qmk.json_schema import load_jsonschema
from qmk.keyboard import keyboard_folder
from qmk.path import is_keyboard
diff --git a/lib/python/qmk/cli/info.py b/lib/python/qmk/cli/info.py
index 88b65686f5..aac507c1a5 100755
--- a/lib/python/qmk/cli/info.py
+++ b/lib/python/qmk/cli/info.py
@@ -7,7 +7,7 @@ import platform
from milc import cli
-from qmk.info_json_encoder import InfoJSONEncoder
+from qmk.json_encoders import InfoJSONEncoder
from qmk.constants import COL_LETTERS, ROW_LETTERS
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.keyboard import keyboard_folder, render_layouts, render_layout
diff --git a/lib/python/qmk/cli/kle2json.py b/lib/python/qmk/cli/kle2json.py
index 3bb7443582..91499c9af3 100755
--- a/lib/python/qmk/cli/kle2json.py
+++ b/lib/python/qmk/cli/kle2json.py
@@ -8,7 +8,7 @@ from milc import cli
from kle2xy import KLE2xy
from qmk.converter import kle2qmk
-from qmk.info_json_encoder import InfoJSONEncoder
+from qmk.json_encoders import InfoJSONEncoder
@cli.argument('filename', help='The KLE raw txt to convert')