aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk/info_json_encoder.py
diff options
context:
space:
mode:
authorQMK Bot <hello@qmk.fm>2021-03-25 11:38:44 +0000
committerQMK Bot <hello@qmk.fm>2021-03-25 11:38:44 +0000
commita747ef966ba6dc5f52d8e6aa60d7d3e9bcf67758 (patch)
treed1fef064be28adc655a76afff61bc96578ee6239 /lib/python/qmk/info_json_encoder.py
parent87f6df0655486bc372b0205f1261ac28026abaac (diff)
parent3e60997edba46544557b3a775bdb1538e07c3edf (diff)
downloadqmk_firmware-a747ef966ba6dc5f52d8e6aa60d7d3e9bcf67758.tar.gz
qmk_firmware-a747ef966ba6dc5f52d8e6aa60d7d3e9bcf67758.zip
Merge remote-tracking branch 'origin/master' into develop
Diffstat (limited to 'lib/python/qmk/info_json_encoder.py')
-rwxr-xr-xlib/python/qmk/info_json_encoder.py96
1 files changed, 0 insertions, 96 deletions
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"""
3import json
4from decimal import Decimal
5
6
7class 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)