forked from zykrah/firmware-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_encoders.py
281 lines (213 loc) · 8.53 KB
/
json_encoders.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"""Class that pretty-prints QMK info.json files.
"""
import json
from decimal import Decimal
newline = '\n'
class QMKJSONEncoder(json.JSONEncoder):
"""Base class for all QMK JSON encoders.
"""
container_types = (list, tuple, dict)
indentation_char = " "
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.indentation_level = 0
if not self.indent:
self.indent = 4
def encode_decimal(self, obj):
"""Encode a decimal object.
"""
if obj == int(obj): # I can't believe Decimal objects don't have .is_integer()
return int(obj)
return float(obj)
def encode_list(self, obj):
"""Encode a list-like object.
"""
if self.primitives_only(obj):
return "[" + ", ".join(self.encode(element) for element in obj) + "]"
else:
self.indentation_level += 1
output = [self.indent_str + self.encode(element) for element in obj]
self.indentation_level -= 1
return "[\n" + ",\n".join(output) + "\n" + self.indent_str + "]"
def encode(self, obj):
"""Encode keymap.json objects for QMK.
"""
if isinstance(obj, Decimal):
return self.encode_decimal(obj)
elif isinstance(obj, (list, tuple)):
return self.encode_list(obj)
elif isinstance(obj, dict):
return self.encode_dict(obj)
else:
return super().encode(obj)
def primitives_only(self, obj):
"""Returns true if the object doesn't have any container type objects (list, tuple, dict).
"""
if isinstance(obj, dict):
obj = obj.values()
return not any(isinstance(element, self.container_types) for element in obj)
@property
def indent_str(self):
return self.indentation_char * (self.indentation_level * self.indent)
class InfoJSONEncoder(QMKJSONEncoder):
"""Custom encoder to make info.json's a little nicer to work with.
"""
def encode_dict(self, obj):
"""Encode info.json dictionaries.
"""
if obj:
if set(("x", "y")).issubset(obj.keys()) or ('pin_a' in obj.keys() and 'pin_b' in obj.keys()):
# These are part of a encoder/layout/led_config, put them on a single line.
return "{ " + ", ".join(f"{self.encode(key)}: {self.encode(element)}" for key, element in sorted(obj.items())) + " }"
else:
self.indentation_level += 1
output = [self.indent_str + f"{json.dumps(key)}: {self.encode(value)}" for key, value 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_dict(self, key):
"""Forces layout to the back of the sort order.
"""
key = key[0]
if self.indentation_level == 1:
if key == 'manufacturer':
return '10keyboard_name'
elif key == 'keyboard_name':
return '11keyboard_name'
elif key == 'maintainer':
return '12maintainer'
elif key == 'processor':
return '13processor'
elif key == 'bootloader':
return '14bootloader'
elif key == 'board':
return '15board'
elif key == 'usb':
return '16usb'
elif key == 'features':
return '17features'
elif key == 'encoder':
return '18encoder'
elif key == 'community_layouts':
return '97community_layouts'
elif key == 'layout_aliases':
return '98layout_aliases'
elif key == 'layouts':
return '99layouts'
else:
return '50' + str(key)
# Sorting USB
elif self.indentation_level == 2:
if key == 'vid':
return '10vid'
elif key == 'pid':
return '11pid'
elif key == 'device_ver':
return '12device_ver'
elif key == 'rows':
return '1rows'
elif key == 'cols':
return '2col'
return key
class KeymapJSONEncoder(QMKJSONEncoder):
"""Custom encoder to make keymap.json's a little nicer to work with.
"""
def encode_dict(self, obj):
"""Encode dictionary objects for keymap.json.
"""
if obj:
self.indentation_level += 1
output_lines = [f"{self.indent_str}{json.dumps(key)}: {self.encode(value)}" for key, value in sorted(obj.items(), key=self.sort_dict)]
output = ',\n'.join(output_lines)
self.indentation_level -= 1
return f"{{\n{output}\n{self.indent_str}}}"
else:
return "{}"
def encode_list(self, obj):
"""Encode a list-like object.
"""
if self.indentation_level == 2:
indent_level = self.indentation_level + 1
# We have a list of keycodes
layer = [[]]
for key in obj:
if key == 'JSON_NEWLINE':
layer.append([])
else:
if isinstance(key, dict):
# We have a macro
# TODO: Add proper support for nicely formatting keymap.json macros
layer[-1].append(f'{self.encode(key)}')
else:
layer[-1].append(f'"{key}"')
layer = [f"{self.indent_str*indent_level}{', '.join(row)}" for row in layer]
return f"{self.indent_str}[\n{newline.join(layer)}\n{self.indent_str*self.indentation_level}]"
elif self.primitives_only(obj):
return "[" + ", ".join(self.encode(element) for element in obj) + "]"
else:
self.indentation_level += 1
output = [self.indent_str + self.encode(element) for element in obj]
self.indentation_level -= 1
return "[\n" + ",\n".join(output) + "\n" + self.indent_str + "]"
def sort_dict(self, key):
"""Sorts the hashes in a nice way.
"""
key = key[0]
if self.indentation_level == 1:
if key == 'version':
return '00version'
elif key == 'author':
return '01author'
elif key == 'notes':
return '02notes'
elif key == 'layers':
return '98layers'
elif key == 'documentation':
return '99documentation'
else:
return '50' + str(key)
return key
from json.encoder import encode_basestring_ascii, encode_basestring, INFINITY, c_make_encoder, _make_iterencode
class KLEJSONEncoder(json.JSONEncoder):
"""Modified the stock encoder to just turn float values that are whole numbers into integers. E.g. 1.0 -> 1
"""
def iterencode(self, o, _one_shot=False):
if self.check_circular:
markers = {}
else:
markers = None
if self.ensure_ascii:
_encoder = encode_basestring_ascii
else:
_encoder = encode_basestring
def floatstr(o, allow_nan=self.allow_nan,
_repr=float.__repr__, _inf=INFINITY, _neginf=-INFINITY):
if o != o:
text = 'NaN'
elif o == _inf:
text = 'Infinity'
elif o == _neginf:
text = '-Infinity'
# 2 below lines are the only lines changed from original function
elif o == int(o):
return int(o).__repr__()
else:
return _repr(o)
if not allow_nan:
raise ValueError(
"Out of range float values are not JSON compliant: " +
repr(o))
return text
if (_one_shot and c_make_encoder is not None
and self.indent is None):
_iterencode = c_make_encoder(
markers, self.default, _encoder, self.indent,
self.key_separator, self.item_separator, self.sort_keys,
self.skipkeys, self.allow_nan)
else:
_iterencode = _make_iterencode(
markers, self.default, _encoder, self.indent, floatstr,
self.key_separator, self.item_separator, self.sort_keys,
self.skipkeys, _one_shot)
return _iterencode(o, 0)