diff options
| author | Zach White <skullydazed@gmail.com> | 2021-03-25 04:38:10 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-25 22:38:10 +1100 |
| commit | 3e60997edba46544557b3a775bdb1538e07c3edf (patch) | |
| tree | 165923c2cd6b5eeee150c0285ce8ed9eff44eaee /lib | |
| parent | a74846a0dbd5efbc7b993918f7f3122e1d620c24 (diff) | |
| download | qmk_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')
| -rw-r--r-- | lib/python/qmk/cli/__init__.py | 1 | ||||
| -rw-r--r-- | lib/python/qmk/cli/c2json.py | 2 | ||||
| -rw-r--r-- | lib/python/qmk/cli/format/__init__.py | 1 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/format/json.py | 66 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/generate/api.py | 2 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/generate/info_json.py | 2 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/info.py | 2 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/kle2json.py | 2 | ||||
| -rwxr-xr-x | lib/python/qmk/info_json_encoder.py | 96 | ||||
| -rwxr-xr-x | lib/python/qmk/json_encoders.py | 192 | ||||
| -rw-r--r-- | lib/python/qmk/tests/minimal_info.json | 13 | ||||
| -rw-r--r-- | lib/python/qmk/tests/minimal_keymap.json | 7 | ||||
| -rw-r--r-- | lib/python/qmk/tests/pytest_export.json | 6 | ||||
| -rw-r--r-- | lib/python/qmk/tests/test_cli_commands.py | 24 |
14 files changed, 309 insertions, 107 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index a5f1f4767..1349e68a9 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py | |||
| @@ -16,6 +16,7 @@ from . import docs | |||
| 16 | from . import doctor | 16 | from . import doctor |
| 17 | from . import fileformat | 17 | from . import fileformat |
| 18 | from . import flash | 18 | from . import flash |
| 19 | from . import format | ||
| 19 | from . import generate | 20 | from . import generate |
| 20 | from . import hello | 21 | from . import hello |
| 21 | from . import info | 22 | from . import info |
diff --git a/lib/python/qmk/cli/c2json.py b/lib/python/qmk/cli/c2json.py index a97e21222..1fa833b64 100644 --- a/lib/python/qmk/cli/c2json.py +++ b/lib/python/qmk/cli/c2json.py | |||
| @@ -6,7 +6,7 @@ from milc import cli | |||
| 6 | 6 | ||
| 7 | import qmk.keymap | 7 | import qmk.keymap |
| 8 | import qmk.path | 8 | import qmk.path |
| 9 | from qmk.info_json_encoder import InfoJSONEncoder | 9 | from qmk.json_encoders import InfoJSONEncoder |
| 10 | from qmk.keyboard import keyboard_folder | 10 | from qmk.keyboard import keyboard_folder |
| 11 | 11 | ||
| 12 | 12 | ||
diff --git a/lib/python/qmk/cli/format/__init__.py b/lib/python/qmk/cli/format/__init__.py new file mode 100644 index 000000000..741ec778b --- /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 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)) | ||
diff --git a/lib/python/qmk/cli/generate/api.py b/lib/python/qmk/cli/generate/api.py index 290590834..70019428f 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 | |||
| 8 | 8 | ||
| 9 | from qmk.datetime import current_datetime | 9 | from qmk.datetime import current_datetime |
| 10 | from qmk.info import info_json | 10 | from qmk.info import info_json |
| 11 | from qmk.info_json_encoder import InfoJSONEncoder | 11 | from qmk.json_encoders import InfoJSONEncoder |
| 12 | from qmk.json_schema import json_load | 12 | from qmk.json_schema import json_load |
| 13 | from qmk.keyboard import list_keyboards | 13 | from qmk.keyboard import list_keyboards |
| 14 | 14 | ||
diff --git a/lib/python/qmk/cli/generate/info_json.py b/lib/python/qmk/cli/generate/info_json.py index 6c00ba7d8..1af7f0439 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 | |||
| 9 | 9 | ||
| 10 | from qmk.decorators import automagic_keyboard, automagic_keymap | 10 | from qmk.decorators import automagic_keyboard, automagic_keymap |
| 11 | from qmk.info import info_json | 11 | from qmk.info import info_json |
| 12 | from qmk.info_json_encoder import InfoJSONEncoder | 12 | from qmk.json_encoders import InfoJSONEncoder |
| 13 | from qmk.json_schema import load_jsonschema | 13 | from qmk.json_schema import load_jsonschema |
| 14 | from qmk.keyboard import keyboard_folder | 14 | from qmk.keyboard import keyboard_folder |
| 15 | from qmk.path import is_keyboard | 15 | from qmk.path import is_keyboard |
diff --git a/lib/python/qmk/cli/info.py b/lib/python/qmk/cli/info.py index 88b65686f..aac507c1a 100755 --- a/lib/python/qmk/cli/info.py +++ b/lib/python/qmk/cli/info.py | |||
| @@ -7,7 +7,7 @@ import platform | |||
| 7 | 7 | ||
| 8 | from milc import cli | 8 | from milc import cli |
| 9 | 9 | ||
| 10 | from qmk.info_json_encoder import InfoJSONEncoder | 10 | from qmk.json_encoders import InfoJSONEncoder |
| 11 | from qmk.constants import COL_LETTERS, ROW_LETTERS | 11 | from qmk.constants import COL_LETTERS, ROW_LETTERS |
| 12 | from qmk.decorators import automagic_keyboard, automagic_keymap | 12 | from qmk.decorators import automagic_keyboard, automagic_keymap |
| 13 | from qmk.keyboard import keyboard_folder, render_layouts, render_layout | 13 | 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 3bb744358..91499c9af 100755 --- a/lib/python/qmk/cli/kle2json.py +++ b/lib/python/qmk/cli/kle2json.py | |||
| @@ -8,7 +8,7 @@ from milc import cli | |||
| 8 | from kle2xy import KLE2xy | 8 | from kle2xy import KLE2xy |
| 9 | 9 | ||
| 10 | from qmk.converter import kle2qmk | 10 | from qmk.converter import kle2qmk |
| 11 | from qmk.info_json_encoder import InfoJSONEncoder | 11 | from qmk.json_encoders import InfoJSONEncoder |
| 12 | 12 | ||
| 13 | 13 | ||
| 14 | @cli.argument('filename', help='The KLE raw txt to convert') | 14 | @cli.argument('filename', help='The KLE raw txt to convert') |
diff --git a/lib/python/qmk/info_json_encoder.py b/lib/python/qmk/info_json_encoder.py deleted file mode 100755 index 60dae7247..000000000 --- a/lib/python/qmk/info_json_encoder.py +++ /dev/null | |||
| @@ -1,96 +0,0 @@ | |||
| 1 | """Class that pretty-prints QMK info.json files. | ||
| 2 | """ | ||
| 3 | import json | ||
| 4 | from decimal import Decimal | ||
| 5 | |||
| 6 | |||
| 7 | class InfoJSONEncoder(json.JSONEncoder): | ||
| 8 | """Custom encoder to make info.json's a little nicer to work with. | ||
| 9 | """ | ||
| 10 | container_types = (list, tuple, dict) | ||
| 11 | indentation_char = " " | ||
| 12 | |||
| 13 | def __init__(self, *args, **kwargs): | ||
| 14 | super().__init__(*args, **kwargs) | ||
| 15 | self.indentation_level = 0 | ||
| 16 | |||
| 17 | if not self.indent: | ||
| 18 | self.indent = 4 | ||
| 19 | |||
| 20 | def encode(self, obj): | ||
| 21 | """Encode JSON objects for QMK. | ||
| 22 | """ | ||
| 23 | if isinstance(obj, Decimal): | ||
| 24 | if obj == int(obj): # I can't believe Decimal objects don't have .is_integer() | ||
| 25 | return int(obj) | ||
| 26 | return float(obj) | ||
| 27 | |||
| 28 | elif isinstance(obj, (list, tuple)): | ||
| 29 | if self._primitives_only(obj): | ||
| 30 | return "[" + ", ".join(self.encode(element) for element in obj) + "]" | ||
| 31 | |||
| 32 | else: | ||
| 33 | self.indentation_level += 1 | ||
| 34 | output = [self.indent_str + self.encode(element) for element in obj] | ||
| 35 | self.indentation_level -= 1 | ||
| 36 | return "[\n" + ",\n".join(output) + "\n" + self.indent_str + "]" | ||
| 37 | |||
| 38 | elif isinstance(obj, dict): | ||
| 39 | if obj: | ||
| 40 | if self.indentation_level == 4: | ||
| 41 | # These are part of a layout, put them on a single line. | ||
| 42 | return "{ " + ", ".join(f"{self.encode(key)}: {self.encode(element)}" for key, element in sorted(obj.items())) + " }" | ||
| 43 | |||
| 44 | else: | ||
| 45 | self.indentation_level += 1 | ||
| 46 | output = [self.indent_str + f"{json.dumps(key)}: {self.encode(value)}" for key, value in sorted(obj.items(), key=self.sort_root_dict)] | ||
| 47 | self.indentation_level -= 1 | ||
| 48 | return "{\n" + ",\n".join(output) + "\n" + self.indent_str + "}" | ||
| 49 | else: | ||
| 50 | return "{}" | ||
| 51 | else: | ||
| 52 | return super().encode(obj) | ||
| 53 | |||
| 54 | def _primitives_only(self, obj): | ||
| 55 | """Returns true if the object doesn't have any container type objects (list, tuple, dict). | ||
| 56 | """ | ||
| 57 | if isinstance(obj, dict): | ||
| 58 | obj = obj.values() | ||
| 59 | |||
| 60 | return not any(isinstance(element, self.container_types) for element in obj) | ||
| 61 | |||
| 62 | def sort_root_dict(self, key): | ||
| 63 | """Forces layout to the back of the sort order. | ||
| 64 | """ | ||
| 65 | key = key[0] | ||
| 66 | |||
| 67 | if self.indentation_level == 1: | ||
| 68 | if key == 'manufacturer': | ||
| 69 | return '10keyboard_name' | ||
| 70 | |||
| 71 | elif key == 'keyboard_name': | ||
| 72 | return '11keyboard_name' | ||
| 73 | |||
| 74 | elif key == 'maintainer': | ||
| 75 | return '12maintainer' | ||
| 76 | |||
| 77 | elif key in ('height', 'width'): | ||
| 78 | return '40' + str(key) | ||
| 79 | |||
| 80 | elif key == 'community_layouts': | ||
| 81 | return '97community_layouts' | ||
| 82 | |||
| 83 | elif key == 'layout_aliases': | ||
| 84 | return '98layout_aliases' | ||
| 85 | |||
| 86 | elif key == 'layouts': | ||
| 87 | return '99layouts' | ||
| 88 | |||
| 89 | else: | ||
| 90 | return '50' + str(key) | ||
| 91 | |||
| 92 | return key | ||
| 93 | |||
| 94 | @property | ||
| 95 | def indent_str(self): | ||
| 96 | return self.indentation_char * (self.indentation_level * self.indent) | ||
diff --git a/lib/python/qmk/json_encoders.py b/lib/python/qmk/json_encoders.py new file mode 100755 index 000000000..9f3da022b --- /dev/null +++ b/lib/python/qmk/json_encoders.py | |||
| @@ -0,0 +1,192 @@ | |||
| 1 | """Class that pretty-prints QMK info.json files. | ||
| 2 | """ | ||
| 3 | import json | ||
| 4 | from decimal import Decimal | ||
| 5 | |||
| 6 | newline = '\n' | ||
| 7 | |||
| 8 | |||
| 9 | class QMKJSONEncoder(json.JSONEncoder): | ||
| 10 | """Base class for all QMK JSON encoders. | ||
| 11 | """ | ||
| 12 | container_types = (list, tuple, dict) | ||
| 13 | indentation_char = " " | ||
| 14 | |||
| 15 | def __init__(self, *args, **kwargs): | ||
| 16 | super().__init__(*args, **kwargs) | ||
| 17 | self.indentation_level = 0 | ||
| 18 | |||
| 19 | if not self.indent: | ||
| 20 | self.indent = 4 | ||
| 21 | |||
| 22 | def encode_decimal(self, obj): | ||
| 23 | """Encode a decimal object. | ||
| 24 | """ | ||
| 25 | if obj == int(obj): # I can't believe Decimal objects don't have .is_integer() | ||
| 26 | return int(obj) | ||
| 27 | |||
| 28 | return float(obj) | ||
| 29 | |||
| 30 | def encode_list(self, obj): | ||
| 31 | """Encode a list-like object. | ||
| 32 | """ | ||
| 33 | if self.primitives_only(obj): | ||
| 34 | return "[" + ", ".join(self.encode(element) for element in obj) + "]" | ||
| 35 | |||
| 36 | else: | ||
| 37 | self.indentation_level += 1 | ||
| 38 | output = [self.indent_str + self.encode(element) for element in obj] | ||
| 39 | self.indentation_level -= 1 | ||
| 40 | |||
| 41 | return "[\n" + ",\n".join(output) + "\n" + self.indent_str + "]" | ||
| 42 | |||
| 43 | def encode(self, obj): | ||
| 44 | """Encode keymap.json objects for QMK. | ||
| 45 | """ | ||
| 46 | if isinstance(obj, Decimal): | ||
| 47 | return self.encode_decimal(obj) | ||
| 48 | |||
| 49 | elif isinstance(obj, (list, tuple)): | ||
| 50 | return self.encode_list(obj) | ||
| 51 | |||
| 52 | elif isinstance(obj, dict): | ||
| 53 | return self.encode_dict(obj) | ||
| 54 | |||
| 55 | else: | ||
| 56 | return super().encode(obj) | ||
| 57 | |||
| 58 | def primitives_only(self, obj): | ||
| 59 | """Returns true if the object doesn't have any container type objects (list, tuple, dict). | ||
| 60 | """ | ||
| 61 | if isinstance(obj, dict): | ||
| 62 | obj = obj.values() | ||
| 63 | |||
| 64 | return not any(isinstance(element, self.container_types) for element in obj) | ||
| 65 | |||
| 66 | @property | ||
| 67 | def indent_str(self): | ||
| 68 | return self.indentation_char * (self.indentation_level * self.indent) | ||
| 69 | |||
| 70 | |||
| 71 | class InfoJSONEncoder(QMKJSONEncoder): | ||
| 72 | """Custom encoder to make info.json's a little nicer to work with. | ||
| 73 | """ | ||
| 74 | def encode_dict(self, obj): | ||
| 75 | """Encode info.json dictionaries. | ||
| 76 | """ | ||
| 77 | if obj: | ||
| 78 | if self.indentation_level == 4: | ||
| 79 | # These are part of a layout, put them on a single line. | ||
| 80 | return "{ " + ", ".join(f"{self.encode(key)}: {self.encode(element)}" for key, element in sorted(obj.items())) + " }" | ||
| 81 | |||
| 82 | else: | ||
| 83 | self.indentation_level += 1 | ||
| 84 | output = [self.indent_str + f"{json.dumps(key)}: {self.encode(value)}" for key, value in sorted(obj.items(), key=self.sort_dict)] | ||
| 85 | self.indentation_level -= 1 | ||
| 86 | return "{\n" + ",\n".join(output) + "\n" + self.indent_str + "}" | ||
| 87 | else: | ||
| 88 | return "{}" | ||
| 89 | |||
| 90 | def sort_dict(self, key): | ||
| 91 | """Forces layout to the back of the sort order. | ||
| 92 | """ | ||
| 93 | key = key[0] | ||
| 94 | |||
| 95 | if self.indentation_level == 1: | ||
| 96 | if key == 'manufacturer': | ||
| 97 | return '10keyboard_name' | ||
| 98 | |||
| 99 | elif key == 'keyboard_name': | ||
| 100 | return '11keyboard_name' | ||
| 101 | |||
| 102 | elif key == 'maintainer': | ||
| 103 | return '12maintainer' | ||
| 104 | |||
| 105 | elif key in ('height', 'width'): | ||
| 106 | return '40' + str(key) | ||
| 107 | |||
| 108 | elif key == 'community_layouts': | ||
| 109 | return '97community_layouts' | ||
| 110 | |||
| 111 | elif key == 'layout_aliases': | ||
| 112 | return '98layout_aliases' | ||
| 113 | |||
| 114 | elif key == 'layouts': | ||
| 115 | return '99layouts' | ||
| 116 | |||
| 117 | else: | ||
| 118 | return '50' + str(key) | ||
| 119 | |||
| 120 | return key | ||
| 121 | |||
| 122 | |||
| 123 | class KeymapJSONEncoder(QMKJSONEncoder): | ||
| 124 | """Custom encoder to make keymap.json's a little nicer to work with. | ||
| 125 | """ | ||
| 126 | def encode_dict(self, obj): | ||
| 127 | """Encode dictionary objects for keymap.json. | ||
| 128 | """ | ||
| 129 | if obj: | ||
| 130 | self.indentation_level += 1 | ||
| 131 | output_lines = [f"{self.indent_str}{json.dumps(key)}: {self.encode(value)}" for key, value in sorted(obj.items(), key=self.sort_dict)] | ||
| 132 | output = ',\n'.join(output_lines) | ||
| 133 | self.indentation_level -= 1 | ||
| 134 | |||
| 135 | return f"{{\n{output}\n{self.indent_str}}}" | ||
| 136 | |||
| 137 | else: | ||
| 138 | return "{}" | ||
| 139 | |||
| 140 | def encode_list(self, obj): | ||
| 141 | """Encode a list-like object. | ||
| 142 | """ | ||
| 143 | if self.indentation_level == 2: | ||
| 144 | indent_level = self.indentation_level + 1 | ||
| 145 | # We have a list of keycodes | ||
| 146 | layer = [[]] | ||
| 147 | |||
| 148 | for key in obj: | ||
| 149 | if key == 'JSON_NEWLINE': | ||
| 150 | layer.append([]) | ||
| 151 | else: | ||
| 152 | layer[-1].append(f'"{key}"') | ||
| 153 | |||
| 154 | layer = [f"{self.indent_str*indent_level}{', '.join(row)}" for row in layer] | ||
| 155 | |||
| 156 | return f"{self.indent_str}[\n{newline.join(layer)}\n{self.indent_str*self.indentation_level}]" | ||
| 157 | |||
| 158 | elif self.primitives_only(obj): | ||
| 159 | return "[" + ", ".join(self.encode(element) for element in obj) + "]" | ||
| 160 | |||
| 161 | else: | ||
| 162 | self.indentation_level += 1 | ||
| 163 | output = [self.indent_str + self.encode(element) for element in obj] | ||
| 164 | self.indentation_level -= 1 | ||
| 165 | |||
| 166 | return "[\n" + ",\n".join(output) + "\n" + self.indent_str + "]" | ||
| 167 | |||
| 168 | def sort_dict(self, key): | ||
| 169 | """Sorts the hashes in a nice way. | ||
| 170 | """ | ||
| 171 | key = key[0] | ||
| 172 | |||
| 173 | if self.indentation_level == 1: | ||
| 174 | if key == 'version': | ||
| 175 | return '00version' | ||
| 176 | |||
| 177 | elif key == 'author': | ||
| 178 | return '01author' | ||
| 179 | |||
| 180 | elif key == 'notes': | ||
| 181 | return '02notes' | ||
| 182 | |||
| 183 | elif key == 'layers': | ||
| 184 | return '98layers' | ||
| 185 | |||
| 186 | elif key == 'documentation': | ||
| 187 | return '99documentation' | ||
| 188 | |||
| 189 | else: | ||
| 190 | return '50' + str(key) | ||
| 191 | |||
| 192 | return key | ||
diff --git a/lib/python/qmk/tests/minimal_info.json b/lib/python/qmk/tests/minimal_info.json new file mode 100644 index 000000000..b91c23bd3 --- /dev/null +++ b/lib/python/qmk/tests/minimal_info.json | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | { | ||
| 2 | "keyboard_name": "tester", | ||
| 3 | "maintainer": "qmk", | ||
| 4 | "height": 5, | ||
| 5 | "width": 15, | ||
| 6 | "layouts": { | ||
| 7 | "LAYOUT": { | ||
| 8 | "layout": [ | ||
| 9 | { "label": "KC_A", "x": 0, "y": 0, "matrix": [0, 0] } | ||
| 10 | ] | ||
| 11 | } | ||
| 12 | } | ||
| 13 | } | ||
diff --git a/lib/python/qmk/tests/minimal_keymap.json b/lib/python/qmk/tests/minimal_keymap.json new file mode 100644 index 000000000..258f9e8a9 --- /dev/null +++ b/lib/python/qmk/tests/minimal_keymap.json | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | { | ||
| 2 | "keyboard": "handwired/pytest/basic", | ||
| 3 | "keymap": "test", | ||
| 4 | "layers": [["KC_A"]], | ||
| 5 | "layout": "LAYOUT_ortho_1x1", | ||
| 6 | "version": 1 | ||
| 7 | } | ||
diff --git a/lib/python/qmk/tests/pytest_export.json b/lib/python/qmk/tests/pytest_export.json deleted file mode 100644 index 5fb0d624f..000000000 --- a/lib/python/qmk/tests/pytest_export.json +++ /dev/null | |||
| @@ -1,6 +0,0 @@ | |||
| 1 | { | ||
| 2 | "keyboard":"handwired/pytest/basic", | ||
| 3 | "keymap":"pytest_unittest", | ||
| 4 | "layout":"LAYOUT", | ||
| 5 | "layers":[["KC_A"]] | ||
| 6 | } | ||
diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index a97472e6b..c57d2b7fc 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py | |||
| @@ -259,3 +259,27 @@ def test_generate_layouts(): | |||
| 259 | result = check_subcommand('generate-layouts', '-kb', 'handwired/pytest/basic') | 259 | result = check_subcommand('generate-layouts', '-kb', 'handwired/pytest/basic') |
| 260 | check_returncode(result) | 260 | check_returncode(result) |
| 261 | assert '#define LAYOUT_custom(k0A) {' in result.stdout | 261 | assert '#define LAYOUT_custom(k0A) {' in result.stdout |
| 262 | |||
| 263 | |||
| 264 | def test_format_json_keyboard(): | ||
| 265 | result = check_subcommand('format-json', '--format', 'keyboard', 'lib/python/qmk/tests/minimal_info.json') | ||
| 266 | check_returncode(result) | ||
| 267 | assert result.stdout == '{\n "keyboard_name": "tester",\n "maintainer": "qmk",\n "height": 5,\n "width": 15,\n "layouts": {\n "LAYOUT": {\n "layout": [\n { "label": "KC_A", "matrix": [0, 0], "x": 0, "y": 0 }\n ]\n }\n }\n}\n' | ||
| 268 | |||
| 269 | |||
| 270 | def test_format_json_keymap(): | ||
| 271 | result = check_subcommand('format-json', '--format', 'keymap', 'lib/python/qmk/tests/minimal_keymap.json') | ||
| 272 | check_returncode(result) | ||
| 273 | assert result.stdout == '{\n "version": 1,\n "keyboard": "handwired/pytest/basic",\n "keymap": "test",\n "layout": "LAYOUT_ortho_1x1",\n "layers": [\n [\n "KC_A"\n ]\n ]\n}\n' | ||
| 274 | |||
| 275 | |||
| 276 | def test_format_json_keyboard_auto(): | ||
| 277 | result = check_subcommand('format-json', '--format', 'auto', 'lib/python/qmk/tests/minimal_info.json') | ||
| 278 | check_returncode(result) | ||
| 279 | assert result.stdout == '{\n "keyboard_name": "tester",\n "maintainer": "qmk",\n "height": 5,\n "width": 15,\n "layouts": {\n "LAYOUT": {\n "layout": [\n { "label": "KC_A", "matrix": [0, 0], "x": 0, "y": 0 }\n ]\n }\n }\n}\n' | ||
| 280 | |||
| 281 | |||
| 282 | def test_format_json_keymap_auto(): | ||
| 283 | result = check_subcommand('format-json', '--format', 'auto', 'lib/python/qmk/tests/minimal_keymap.json') | ||
| 284 | check_returncode(result) | ||
| 285 | assert result.stdout == '{\n "keyboard": "handwired/pytest/basic",\n "keymap": "test",\n "layers": [\n ["KC_A"]\n ],\n "layout": "LAYOUT_ortho_1x1",\n "version": 1\n}\n' | ||
