`qmk format-json`: Expose full key path and respect `sort_keys` (#20836)

This commit is contained in:
Ryan 2023-05-20 22:15:05 +10:00 committed by GitHub
parent 102c42b14b
commit 6d90fa2300
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 48 additions and 52 deletions

View File

@ -57,7 +57,7 @@ def c2json(cli):
cli.args.output.parent.mkdir(parents=True, exist_ok=True) cli.args.output.parent.mkdir(parents=True, exist_ok=True)
if cli.args.output.exists(): if cli.args.output.exists():
cli.args.output.replace(cli.args.output.parent / (cli.args.output.name + '.bak')) cli.args.output.replace(cli.args.output.parent / (cli.args.output.name + '.bak'))
cli.args.output.write_text(json.dumps(keymap_json, cls=InfoJSONEncoder)) cli.args.output.write_text(json.dumps(keymap_json, cls=InfoJSONEncoder, sort_keys=True))
if not cli.args.quiet: if not cli.args.quiet:
cli.log.info('Wrote keymap to %s.', cli.args.output) cli.log.info('Wrote keymap to %s.', cli.args.output)

View File

@ -62,4 +62,4 @@ def format_json(cli):
json_file['layers'][layer_num] = current_layer json_file['layers'][layer_num] = current_layer
# Display the results # Display the results
print(json.dumps(json_file, cls=json_encoder)) print(json.dumps(json_file, cls=json_encoder, sort_keys=True))

View File

@ -76,7 +76,7 @@ def generate_info_json(cli):
# Build the info.json file # Build the info.json file
kb_info_json = info_json(cli.config.generate_info_json.keyboard) kb_info_json = info_json(cli.config.generate_info_json.keyboard)
strip_info_json(kb_info_json) strip_info_json(kb_info_json)
info_json_text = json.dumps(kb_info_json, indent=4, cls=InfoJSONEncoder) info_json_text = json.dumps(kb_info_json, indent=4, cls=InfoJSONEncoder, sort_keys=True)
if cli.args.output: if cli.args.output:
# Write to a file # Write to a file

View File

@ -200,7 +200,7 @@ def info(cli):
# Output in the requested format # Output in the requested format
if cli.args.format == 'json': if cli.args.format == 'json':
print(json.dumps(kb_info_json, cls=InfoJSONEncoder)) print(json.dumps(kb_info_json, cls=InfoJSONEncoder, sort_keys=True))
return True return True
elif cli.args.format == 'text': elif cli.args.format == 'text':
print_dotted_output(kb_info_json) print_dotted_output(kb_info_json)

View File

@ -75,7 +75,7 @@ def migrate(cli):
# Finally write out updated info.json # Finally write out updated info.json
cli.log.info(f' Updating {target_info}') cli.log.info(f' Updating {target_info}')
target_info.write_text(json.dumps(info_data.to_dict(), cls=InfoJSONEncoder)) target_info.write_text(json.dumps(info_data.to_dict(), cls=InfoJSONEncoder, sort_keys=True))
cli.log.info(f'{{fg_green}}Migration of keyboard {{fg_cyan}}{cli.args.keyboard}{{fg_green}} complete!{{fg_reset}}') cli.log.info(f'{{fg_green}}Migration of keyboard {{fg_cyan}}{cli.args.keyboard}{{fg_green}} complete!{{fg_reset}}')
cli.log.info(f"Verify build with {{fg_yellow}}qmk compile -kb {cli.args.keyboard} -km default{{fg_reset}}.") cli.log.info(f"Verify build with {{fg_yellow}}qmk compile -kb {cli.args.keyboard} -km default{{fg_reset}}.")

View File

@ -102,7 +102,7 @@ def augment_community_info(src, dest):
item["matrix"] = [int(item["y"]), int(item["x"])] item["matrix"] = [int(item["y"]), int(item["x"])]
# finally write out the updated info.json # finally write out the updated info.json
dest.write_text(json.dumps(info, cls=InfoJSONEncoder)) dest.write_text(json.dumps(info, cls=InfoJSONEncoder, sort_keys=True))
def _question(*args, **kwargs): def _question(*args, **kwargs):

View File

@ -141,5 +141,5 @@ def via2json(cli):
# Generate the keymap.json # Generate the keymap.json
keymap_json = generate_json(cli.args.keymap, cli.args.keyboard, keymap_layout, keymap_data, macro_data) keymap_json = generate_json(cli.args.keymap, cli.args.keyboard, keymap_layout, keymap_data, macro_data)
keymap_lines = [json.dumps(keymap_json, cls=KeymapJSONEncoder)] keymap_lines = [json.dumps(keymap_json, cls=KeymapJSONEncoder, sort_keys=True)]
dump_lines(cli.args.output, keymap_lines, cli.args.quiet) dump_lines(cli.args.output, keymap_lines, cli.args.quiet)

View File

@ -91,7 +91,7 @@ def import_keymap(keymap_data):
keyboard_keymap.parent.mkdir(parents=True, exist_ok=True) keyboard_keymap.parent.mkdir(parents=True, exist_ok=True)
# Dump out all those lovely files # Dump out all those lovely files
keyboard_keymap.write_text(json.dumps(keymap_data, cls=KeymapJSONEncoder)) keyboard_keymap.write_text(json.dumps(keymap_data, cls=KeymapJSONEncoder, sort_keys=True))
return (kb_name, km_name) return (kb_name, km_name)
@ -139,8 +139,8 @@ def import_keyboard(info_data, keymap_data=None):
temp = json_load(keyboard_info) temp = json_load(keyboard_info)
deep_update(temp, info_data) deep_update(temp, info_data)
keyboard_info.write_text(json.dumps(temp, cls=InfoJSONEncoder)) keyboard_info.write_text(json.dumps(temp, cls=InfoJSONEncoder, sort_keys=True))
keyboard_keymap.write_text(json.dumps(keymap_data, cls=KeymapJSONEncoder)) keyboard_keymap.write_text(json.dumps(keymap_data, cls=KeymapJSONEncoder, sort_keys=True))
return kb_name return kb_name

View File

@ -27,39 +27,56 @@ class QMKJSONEncoder(json.JSONEncoder):
return float(obj) return float(obj)
def encode_dict_single_line(self, obj): def encode_dict(self, obj, path):
return "{" + ", ".join(f"{self.encode(key)}: {self.encode(element)}" for key, element in sorted(obj.items(), key=self.sort_layout)) + "}" """Encode a dict-like object.
"""
if obj:
self.indentation_level += 1
def encode_list(self, obj, key=None): items = sorted(obj.items(), key=self.sort_dict) if self.sort_keys else obj.items()
output = [self.indent_str + f"{json.dumps(key)}: {self.encode(value, path + [key])}" for key, value in items]
self.indentation_level -= 1
return "{\n" + ",\n".join(output) + "\n" + self.indent_str + "}"
else:
return "{}"
def encode_dict_single_line(self, obj, path):
"""Encode a dict-like object onto a single line.
"""
return "{" + ", ".join(f"{json.dumps(key)}: {self.encode(value, path + [key])}" for key, value in sorted(obj.items(), key=self.sort_layout)) + "}"
def encode_list(self, obj, path):
"""Encode a list-like object. """Encode a list-like object.
""" """
if self.primitives_only(obj): if self.primitives_only(obj):
return "[" + ", ".join(self.encode(element) for element in obj) + "]" return "[" + ", ".join(self.encode(value, path + [index]) for index, value in enumerate(obj)) + "]"
else: else:
self.indentation_level += 1 self.indentation_level += 1
if key in ('layout', 'rotary'): if path[-1] in ('layout', 'rotary'):
# These are part of a layout or led/encoder config, put them on a single line. # These are part of a LED layout or encoder config, put them on a single line
output = [self.indent_str + self.encode_dict_single_line(element) for element in obj] output = [self.indent_str + self.encode_dict_single_line(value, path + [index]) for index, value in enumerate(obj)]
else: else:
output = [self.indent_str + self.encode(element) for element in obj] output = [self.indent_str + self.encode(value, path + [index]) for index, value in enumerate(obj)]
self.indentation_level -= 1 self.indentation_level -= 1
return "[\n" + ",\n".join(output) + "\n" + self.indent_str + "]" return "[\n" + ",\n".join(output) + "\n" + self.indent_str + "]"
def encode(self, obj, key=None): def encode(self, obj, path=[]):
"""Encode keymap.json objects for QMK. """Encode JSON objects for QMK.
""" """
if isinstance(obj, Decimal): if isinstance(obj, Decimal):
return self.encode_decimal(obj) return self.encode_decimal(obj)
elif isinstance(obj, (list, tuple)): elif isinstance(obj, (list, tuple)):
return self.encode_list(obj, key) return self.encode_list(obj, path)
elif isinstance(obj, dict): elif isinstance(obj, dict):
return self.encode_dict(obj, key) return self.encode_dict(obj, path)
else: else:
return super().encode(obj) return super().encode(obj)
@ -80,19 +97,10 @@ class QMKJSONEncoder(json.JSONEncoder):
class InfoJSONEncoder(QMKJSONEncoder): class InfoJSONEncoder(QMKJSONEncoder):
"""Custom encoder to make info.json's a little nicer to work with. """Custom encoder to make info.json's a little nicer to work with.
""" """
def encode_dict(self, obj, key): def sort_layout(self, item):
"""Encode info.json dictionaries. """Sorts the hashes in a nice way.
""" """
if obj: key = item[0]
self.indentation_level += 1
output = [self.indent_str + f"{json.dumps(k)}: {self.encode(v, k)}" for k, v in sorted(obj.items(), key=self.sort_dict)]
self.indentation_level -= 1
return "{\n" + ",\n".join(output) + "\n" + self.indent_str + "}"
else:
return "{}"
def sort_layout(self, key):
key = key[0]
if key == 'label': if key == 'label':
return '00label' return '00label'
@ -117,14 +125,14 @@ class InfoJSONEncoder(QMKJSONEncoder):
return key return key
def sort_dict(self, key): def sort_dict(self, item):
"""Forces layout to the back of the sort order. """Forces layout to the back of the sort order.
""" """
key = key[0] key = item[0]
if self.indentation_level == 1: if self.indentation_level == 1:
if key == 'manufacturer': if key == 'manufacturer':
return '10keyboard_name' return '10manufacturer'
elif key == 'keyboard_name': elif key == 'keyboard_name':
return '11keyboard_name' return '11keyboard_name'
@ -150,19 +158,7 @@ class InfoJSONEncoder(QMKJSONEncoder):
class KeymapJSONEncoder(QMKJSONEncoder): class KeymapJSONEncoder(QMKJSONEncoder):
"""Custom encoder to make keymap.json's a little nicer to work with. """Custom encoder to make keymap.json's a little nicer to work with.
""" """
def encode_dict(self, obj, key): def encode_list(self, obj, path):
"""Encode dictionary objects for keymap.json.
"""
if obj:
self.indentation_level += 1
output = [self.indent_str + f"{json.dumps(k)}: {self.encode(v, k)}" for k, v in sorted(obj.items(), key=self.sort_dict)]
self.indentation_level -= 1
return "{\n" + ",\n".join(output) + "\n" + self.indent_str + "}"
else:
return "{}"
def encode_list(self, obj, k=None):
"""Encode a list-like object. """Encode a list-like object.
""" """
if self.indentation_level == 2: if self.indentation_level == 2:
@ -196,10 +192,10 @@ class KeymapJSONEncoder(QMKJSONEncoder):
return "[\n" + ",\n".join(output) + "\n" + self.indent_str + "]" return "[\n" + ",\n".join(output) + "\n" + self.indent_str + "]"
def sort_dict(self, key): def sort_dict(self, item):
"""Sorts the hashes in a nice way. """Sorts the hashes in a nice way.
""" """
key = key[0] key = item[0]
if self.indentation_level == 1: if self.indentation_level == 1:
if key == 'version': if key == 'version':