diff options
Diffstat (limited to 'lib/python/qmk/info.py')
-rw-r--r-- | lib/python/qmk/info.py | 592 |
1 files changed, 510 insertions, 82 deletions
diff --git a/lib/python/qmk/info.py b/lib/python/qmk/info.py index f476dc666..cc81f7a08 100644 --- a/lib/python/qmk/info.py +++ b/lib/python/qmk/info.py | |||
@@ -1,18 +1,64 @@ | |||
1 | """Functions that help us generate and use info.json files. | 1 | """Functions that help us generate and use info.json files. |
2 | """ | 2 | """ |
3 | import json | 3 | import json |
4 | from collections.abc import Mapping | ||
4 | from glob import glob | 5 | from glob import glob |
5 | from pathlib import Path | 6 | from pathlib import Path |
6 | 7 | ||
8 | import jsonschema | ||
7 | from milc import cli | 9 | from milc import cli |
8 | 10 | ||
9 | from qmk.constants import CHIBIOS_PROCESSORS, LUFA_PROCESSORS, VUSB_PROCESSORS | 11 | from qmk.constants import CHIBIOS_PROCESSORS, LUFA_PROCESSORS, VUSB_PROCESSORS, LED_INDICATORS |
10 | from qmk.c_parse import find_layouts | 12 | from qmk.c_parse import find_layouts |
11 | from qmk.keyboard import config_h, rules_mk | 13 | from qmk.keyboard import config_h, rules_mk |
12 | from qmk.keymap import list_keymaps | 14 | from qmk.keymap import list_keymaps |
13 | from qmk.makefile import parse_rules_mk_file | 15 | from qmk.makefile import parse_rules_mk_file |
14 | from qmk.math import compute | 16 | from qmk.math import compute |
15 | 17 | ||
18 | led_matrix_properties = { | ||
19 | 'driver_count': 'LED_DRIVER_COUNT', | ||
20 | 'driver_addr1': 'LED_DRIVER_ADDR_1', | ||
21 | 'driver_addr2': 'LED_DRIVER_ADDR_2', | ||
22 | 'driver_addr3': 'LED_DRIVER_ADDR_3', | ||
23 | 'driver_addr4': 'LED_DRIVER_ADDR_4', | ||
24 | 'led_count': 'LED_DRIVER_LED_COUNT', | ||
25 | 'timeout': 'ISSI_TIMEOUT', | ||
26 | 'persistence': 'ISSI_PERSISTENCE' | ||
27 | } | ||
28 | |||
29 | rgblight_properties = { | ||
30 | 'led_count': ('RGBLED_NUM', int), | ||
31 | 'pin': ('RGB_DI_PIN', str), | ||
32 | 'max_brightness': ('RGBLIGHT_LIMIT_VAL', int), | ||
33 | 'hue_steps': ('RGBLIGHT_HUE_STEP', int), | ||
34 | 'saturation_steps': ('RGBLIGHT_SAT_STEP', int), | ||
35 | 'brightness_steps': ('RGBLIGHT_VAL_STEP', int) | ||
36 | } | ||
37 | |||
38 | rgblight_toggles = { | ||
39 | 'sleep': 'RGBLIGHT_SLEEP', | ||
40 | 'split': 'RGBLIGHT_SPLIT', | ||
41 | } | ||
42 | |||
43 | rgblight_animations = { | ||
44 | 'all': 'RGBLIGHT_ANIMATIONS', | ||
45 | 'alternating': 'RGBLIGHT_EFFECT_ALTERNATING', | ||
46 | 'breathing': 'RGBLIGHT_EFFECT_BREATHING', | ||
47 | 'christmas': 'RGBLIGHT_EFFECT_CHRISTMAS', | ||
48 | 'knight': 'RGBLIGHT_EFFECT_KNIGHT', | ||
49 | 'rainbow_mood': 'RGBLIGHT_EFFECT_RAINBOW_MOOD', | ||
50 | 'rainbow_swirl': 'RGBLIGHT_EFFECT_RAINBOW_SWIRL', | ||
51 | 'rgb_test': 'RGBLIGHT_EFFECT_RGB_TEST', | ||
52 | 'snake': 'RGBLIGHT_EFFECT_SNAKE', | ||
53 | 'static_gradient': 'RGBLIGHT_EFFECT_STATIC_GRADIENT', | ||
54 | 'twinkle': 'RGBLIGHT_EFFECT_TWINKLE' | ||
55 | } | ||
56 | |||
57 | usb_properties = {'vid': 'VENDOR_ID', 'pid': 'PRODUCT_ID', 'device_ver': 'DEVICE_VER'} | ||
58 | |||
59 | true_values = ['1', 'on', 'yes'] | ||
60 | false_values = ['0', 'off', 'no'] | ||
61 | |||
16 | 62 | ||
17 | def info_json(keyboard): | 63 | def info_json(keyboard): |
18 | """Generate the info.json data for a specific keyboard. | 64 | """Generate the info.json data for a specific keyboard. |
@@ -38,8 +84,9 @@ def info_json(keyboard): | |||
38 | info_data['keymaps'][keymap.name] = {'url': f'https://raw.githubusercontent.com/qmk/qmk_firmware/master/{keymap}/keymap.json'} | 84 | info_data['keymaps'][keymap.name] = {'url': f'https://raw.githubusercontent.com/qmk/qmk_firmware/master/{keymap}/keymap.json'} |
39 | 85 | ||
40 | # Populate layout data | 86 | # Populate layout data |
41 | for layout_name, layout_json in _find_all_layouts(info_data, keyboard, rules).items(): | 87 | for layout_name, layout_json in _find_all_layouts(info_data, keyboard).items(): |
42 | if not layout_name.startswith('LAYOUT_kc'): | 88 | if not layout_name.startswith('LAYOUT_kc'): |
89 | layout_json['c_macro'] = True | ||
43 | info_data['layouts'][layout_name] = layout_json | 90 | info_data['layouts'][layout_name] = layout_json |
44 | 91 | ||
45 | # Merge in the data from info.json, config.h, and rules.mk | 92 | # Merge in the data from info.json, config.h, and rules.mk |
@@ -47,54 +94,366 @@ def info_json(keyboard): | |||
47 | info_data = _extract_config_h(info_data) | 94 | info_data = _extract_config_h(info_data) |
48 | info_data = _extract_rules_mk(info_data) | 95 | info_data = _extract_rules_mk(info_data) |
49 | 96 | ||
97 | # Validate against the jsonschema | ||
98 | try: | ||
99 | keyboard_api_validate(info_data) | ||
100 | |||
101 | except jsonschema.ValidationError as e: | ||
102 | json_path = '.'.join([str(p) for p in e.absolute_path]) | ||
103 | cli.log.error('Invalid API data: %s: %s: %s', keyboard, json_path, e.message) | ||
104 | print(dir(e)) | ||
105 | exit() | ||
106 | |||
107 | # Make sure we have at least one layout | ||
108 | if not info_data.get('layouts'): | ||
109 | _log_error(info_data, 'No LAYOUTs defined! Need at least one layout defined in the keyboard.h or info.json.') | ||
110 | |||
111 | # Make sure we supply layout macros for the community layouts we claim to support | ||
112 | # FIXME(skullydazed): This should be populated into info.json and read from there instead | ||
113 | if 'LAYOUTS' in rules and info_data.get('layouts'): | ||
114 | # Match these up against the supplied layouts | ||
115 | supported_layouts = rules['LAYOUTS'].strip().split() | ||
116 | for layout_name in sorted(info_data['layouts']): | ||
117 | layout_name = layout_name[7:] | ||
118 | |||
119 | if layout_name in supported_layouts: | ||
120 | supported_layouts.remove(layout_name) | ||
121 | |||
122 | if supported_layouts: | ||
123 | for supported_layout in supported_layouts: | ||
124 | _log_error(info_data, 'Claims to support community layout %s but no LAYOUT_%s() macro found' % (supported_layout, supported_layout)) | ||
125 | |||
50 | return info_data | 126 | return info_data |
51 | 127 | ||
52 | 128 | ||
53 | def _extract_config_h(info_data): | 129 | def _json_load(json_file): |
54 | """Pull some keyboard information from existing rules.mk files | 130 | """Load a json file from disk. |
131 | |||
132 | Note: file must be a Path object. | ||
133 | """ | ||
134 | try: | ||
135 | return json.load(json_file.open()) | ||
136 | |||
137 | except json.decoder.JSONDecodeError as e: | ||
138 | cli.log.error('Invalid JSON encountered attempting to load {fg_cyan}%s{fg_reset}:\n\t{fg_red}%s', json_file, e) | ||
139 | exit(1) | ||
140 | |||
141 | |||
142 | def _jsonschema(schema_name): | ||
143 | """Read a jsonschema file from disk. | ||
144 | |||
145 | FIXME(skullydazed/anyone): Refactor to make this a public function. | ||
146 | """ | ||
147 | schema_path = Path(f'data/schemas/{schema_name}.jsonschema') | ||
148 | |||
149 | if not schema_path.exists(): | ||
150 | schema_path = Path('data/schemas/false.jsonschema') | ||
151 | |||
152 | return _json_load(schema_path) | ||
153 | |||
154 | |||
155 | def keyboard_validate(data): | ||
156 | """Validates data against the keyboard jsonschema. | ||
157 | """ | ||
158 | schema = _jsonschema('keyboard') | ||
159 | validator = jsonschema.Draft7Validator(schema).validate | ||
160 | |||
161 | return validator(data) | ||
162 | |||
163 | |||
164 | def keyboard_api_validate(data): | ||
165 | """Validates data against the api_keyboard jsonschema. | ||
166 | """ | ||
167 | base = _jsonschema('keyboard') | ||
168 | relative = _jsonschema('api_keyboard') | ||
169 | resolver = jsonschema.RefResolver.from_schema(base) | ||
170 | validator = jsonschema.Draft7Validator(relative, resolver=resolver).validate | ||
171 | |||
172 | return validator(data) | ||
173 | |||
174 | |||
175 | def _extract_debounce(info_data, config_c): | ||
176 | """Handle debounce. | ||
177 | """ | ||
178 | if 'debounce' in info_data and 'DEBOUNCE' in config_c: | ||
179 | _log_warning(info_data, 'Debounce is specified in both info.json and config.h, the config.h value wins.') | ||
180 | |||
181 | if 'DEBOUNCE' in config_c: | ||
182 | info_data['debounce'] = int(config_c['DEBOUNCE']) | ||
183 | |||
184 | return info_data | ||
185 | |||
186 | |||
187 | def _extract_diode_direction(info_data, config_c): | ||
188 | """Handle the diode direction. | ||
189 | """ | ||
190 | if 'diode_direction' in info_data and 'DIODE_DIRECTION' in config_c: | ||
191 | _log_warning(info_data, 'Diode direction is specified in both info.json and config.h, the config.h value wins.') | ||
192 | |||
193 | if 'DIODE_DIRECTION' in config_c: | ||
194 | info_data['diode_direction'] = config_c.get('DIODE_DIRECTION') | ||
195 | |||
196 | return info_data | ||
197 | |||
198 | |||
199 | def _extract_indicators(info_data, config_c): | ||
200 | """Find the LED indicator information. | ||
201 | """ | ||
202 | for json_key, config_key in LED_INDICATORS.items(): | ||
203 | if json_key in info_data.get('indicators', []) and config_key in config_c: | ||
204 | _log_warning(info_data, f'Indicator {json_key} is specified in both info.json and config.h, the config.h value wins.') | ||
205 | |||
206 | if 'indicators' not in info_data: | ||
207 | info_data['indicators'] = {} | ||
208 | |||
209 | if config_key in config_c: | ||
210 | if 'indicators' not in info_data: | ||
211 | info_data['indicators'] = {} | ||
212 | |||
213 | info_data['indicators'][json_key] = config_c.get(config_key) | ||
214 | |||
215 | return info_data | ||
216 | |||
217 | |||
218 | def _extract_community_layouts(info_data, rules): | ||
219 | """Find the community layouts in rules.mk. | ||
220 | """ | ||
221 | community_layouts = rules['LAYOUTS'].split() if 'LAYOUTS' in rules else [] | ||
222 | |||
223 | if 'community_layouts' in info_data: | ||
224 | for layout in community_layouts: | ||
225 | if layout not in info_data['community_layouts']: | ||
226 | community_layouts.append(layout) | ||
227 | |||
228 | else: | ||
229 | info_data['community_layouts'] = community_layouts | ||
230 | |||
231 | return info_data | ||
232 | |||
233 | |||
234 | def _extract_features(info_data, rules): | ||
235 | """Find all the features enabled in rules.mk. | ||
236 | """ | ||
237 | # Special handling for bootmagic which also supports a "lite" mode. | ||
238 | if rules.get('BOOTMAGIC_ENABLE') == 'lite': | ||
239 | rules['BOOTMAGIC_LITE_ENABLE'] = 'on' | ||
240 | del rules['BOOTMAGIC_ENABLE'] | ||
241 | if rules.get('BOOTMAGIC_ENABLE') == 'full': | ||
242 | rules['BOOTMAGIC_ENABLE'] = 'on' | ||
243 | |||
244 | # Skip non-boolean features we haven't implemented special handling for | ||
245 | for feature in 'HAPTIC_ENABLE', 'QWIIC_ENABLE': | ||
246 | if rules.get(feature): | ||
247 | del rules[feature] | ||
248 | |||
249 | # Process the rest of the rules as booleans | ||
250 | for key, value in rules.items(): | ||
251 | if key.endswith('_ENABLE'): | ||
252 | key = '_'.join(key.split('_')[:-1]).lower() | ||
253 | value = True if value.lower() in true_values else False if value.lower() in false_values else value | ||
254 | |||
255 | if 'config_h_features' not in info_data: | ||
256 | info_data['config_h_features'] = {} | ||
257 | |||
258 | if 'features' not in info_data: | ||
259 | info_data['features'] = {} | ||
260 | |||
261 | if key in info_data['features']: | ||
262 | _log_warning(info_data, 'Feature %s is specified in both info.json and rules.mk, the rules.mk value wins.' % (key,)) | ||
263 | |||
264 | info_data['features'][key] = value | ||
265 | info_data['config_h_features'][key] = value | ||
266 | |||
267 | return info_data | ||
268 | |||
269 | |||
270 | def _extract_led_drivers(info_data, rules): | ||
271 | """Find all the LED drivers set in rules.mk. | ||
272 | """ | ||
273 | if 'LED_MATRIX_DRIVER' in rules: | ||
274 | if 'led_matrix' not in info_data: | ||
275 | info_data['led_matrix'] = {} | ||
276 | |||
277 | if info_data['led_matrix'].get('driver'): | ||
278 | _log_warning(info_data, 'LED Matrix driver is specified in both info.json and rules.mk, the rules.mk value wins.') | ||
279 | |||
280 | info_data['led_matrix']['driver'] = rules['LED_MATRIX_DRIVER'] | ||
281 | |||
282 | return info_data | ||
283 | |||
284 | |||
285 | def _extract_led_matrix(info_data, config_c): | ||
286 | """Handle the led_matrix configuration. | ||
287 | """ | ||
288 | led_matrix = info_data.get('led_matrix', {}) | ||
289 | |||
290 | for json_key, config_key in led_matrix_properties.items(): | ||
291 | if config_key in config_c: | ||
292 | if json_key in led_matrix: | ||
293 | _log_warning(info_data, 'LED Matrix: %s is specified in both info.json and config.h, the config.h value wins.' % (json_key,)) | ||
294 | |||
295 | led_matrix[json_key] = config_c[config_key] | ||
296 | |||
297 | |||
298 | def _extract_rgblight(info_data, config_c): | ||
299 | """Handle the rgblight configuration. | ||
300 | """ | ||
301 | rgblight = info_data.get('rgblight', {}) | ||
302 | animations = rgblight.get('animations', {}) | ||
303 | |||
304 | if 'RGBLED_SPLIT' in config_c: | ||
305 | raw_split = config_c.get('RGBLED_SPLIT', '').replace('{', '').replace('}', '').strip() | ||
306 | rgblight['split_count'] = [int(i) for i in raw_split.split(',')] | ||
307 | |||
308 | for json_key, config_key_type in rgblight_properties.items(): | ||
309 | config_key, config_type = config_key_type | ||
310 | |||
311 | if config_key in config_c: | ||
312 | if json_key in rgblight: | ||
313 | _log_warning(info_data, 'RGB Light: %s is specified in both info.json and config.h, the config.h value wins.' % (json_key,)) | ||
314 | |||
315 | try: | ||
316 | rgblight[json_key] = config_type(config_c[config_key]) | ||
317 | except ValueError as e: | ||
318 | cli.log.error('%s: config.h: Could not convert "%s" to %s: %s', info_data['keyboard_folder'], config_c[config_key], config_type.__name__, e) | ||
319 | |||
320 | for json_key, config_key in rgblight_toggles.items(): | ||
321 | if config_key in config_c and json_key in rgblight: | ||
322 | _log_warning(info_data, 'RGB Light: %s is specified in both info.json and config.h, the config.h value wins.', json_key) | ||
323 | |||
324 | rgblight[json_key] = config_key in config_c | ||
325 | |||
326 | for json_key, config_key in rgblight_animations.items(): | ||
327 | if config_key in config_c: | ||
328 | if json_key in animations: | ||
329 | _log_warning(info_data, 'RGB Light: animations: %s is specified in both info.json and config.h, the config.h value wins.' % (json_key,)) | ||
330 | |||
331 | animations[json_key] = config_c[config_key] | ||
332 | |||
333 | if animations: | ||
334 | rgblight['animations'] = animations | ||
335 | |||
336 | if rgblight: | ||
337 | info_data['rgblight'] = rgblight | ||
338 | |||
339 | return info_data | ||
340 | |||
341 | |||
342 | def _pin_name(pin): | ||
343 | """Returns the proper representation for a pin. | ||
344 | """ | ||
345 | pin = pin.strip() | ||
346 | |||
347 | if not pin: | ||
348 | return None | ||
349 | |||
350 | elif pin.isdigit(): | ||
351 | return int(pin) | ||
352 | |||
353 | elif pin == 'NO_PIN': | ||
354 | return None | ||
355 | |||
356 | elif pin[0] in 'ABCDEFGHIJK' and pin[1].isdigit(): | ||
357 | return pin | ||
358 | |||
359 | raise ValueError(f'Invalid pin: {pin}') | ||
360 | |||
361 | |||
362 | def _extract_pins(pins): | ||
363 | """Returns a list of pins from a comma separated string of pins. | ||
364 | """ | ||
365 | return [_pin_name(pin) for pin in pins.split(',')] | ||
366 | |||
367 | |||
368 | def _extract_direct_matrix(info_data, direct_pins): | ||
369 | """ | ||
370 | """ | ||
371 | info_data['matrix_pins'] = {} | ||
372 | direct_pin_array = [] | ||
373 | |||
374 | while direct_pins[-1] != '}': | ||
375 | direct_pins = direct_pins[:-1] | ||
376 | |||
377 | for row in direct_pins.split('},{'): | ||
378 | if row.startswith('{'): | ||
379 | row = row[1:] | ||
380 | |||
381 | if row.endswith('}'): | ||
382 | row = row[:-1] | ||
383 | |||
384 | direct_pin_array.append([]) | ||
385 | |||
386 | for pin in row.split(','): | ||
387 | if pin == 'NO_PIN': | ||
388 | pin = None | ||
389 | |||
390 | direct_pin_array[-1].append(pin) | ||
391 | |||
392 | return direct_pin_array | ||
393 | |||
394 | |||
395 | def _extract_matrix_info(info_data, config_c): | ||
396 | """Populate the matrix information. | ||
55 | """ | 397 | """ |
56 | config_c = config_h(info_data['keyboard_folder']) | ||
57 | row_pins = config_c.get('MATRIX_ROW_PINS', '').replace('{', '').replace('}', '').strip() | 398 | row_pins = config_c.get('MATRIX_ROW_PINS', '').replace('{', '').replace('}', '').strip() |
58 | col_pins = config_c.get('MATRIX_COL_PINS', '').replace('{', '').replace('}', '').strip() | 399 | col_pins = config_c.get('MATRIX_COL_PINS', '').replace('{', '').replace('}', '').strip() |
59 | direct_pins = config_c.get('DIRECT_PINS', '').replace(' ', '')[1:-1] | 400 | direct_pins = config_c.get('DIRECT_PINS', '').replace(' ', '')[1:-1] |
60 | 401 | ||
61 | info_data['diode_direction'] = config_c.get('DIODE_DIRECTION') | 402 | if 'MATRIX_ROWS' in config_c and 'MATRIX_COLS' in config_c: |
62 | info_data['matrix_size'] = { | 403 | if 'matrix_size' in info_data: |
63 | 'rows': compute(config_c.get('MATRIX_ROWS', '0')), | 404 | _log_warning(info_data, 'Matrix size is specified in both info.json and config.h, the config.h values win.') |
64 | 'cols': compute(config_c.get('MATRIX_COLS', '0')), | 405 | |
65 | } | 406 | info_data['matrix_size'] = { |
66 | info_data['matrix_pins'] = {} | 407 | 'cols': compute(config_c.get('MATRIX_COLS', '0')), |
408 | 'rows': compute(config_c.get('MATRIX_ROWS', '0')), | ||
409 | } | ||
410 | |||
411 | if row_pins and col_pins: | ||
412 | if 'matrix_pins' in info_data: | ||
413 | _log_warning(info_data, 'Matrix pins are specified in both info.json and config.h, the config.h values win.') | ||
67 | 414 | ||
68 | if row_pins: | 415 | info_data['matrix_pins'] = { |
69 | info_data['matrix_pins']['rows'] = row_pins.split(',') | 416 | 'cols': _extract_pins(col_pins), |
70 | if col_pins: | 417 | 'rows': _extract_pins(row_pins), |
71 | info_data['matrix_pins']['cols'] = col_pins.split(',') | 418 | } |
72 | 419 | ||
73 | if direct_pins: | 420 | if direct_pins: |
74 | direct_pin_array = [] | 421 | if 'matrix_pins' in info_data: |
75 | for row in direct_pins.split('},{'): | 422 | _log_warning(info_data, 'Direct pins are specified in both info.json and config.h, the config.h values win.') |
76 | if row.startswith('{'): | ||
77 | row = row[1:] | ||
78 | if row.endswith('}'): | ||
79 | row = row[:-1] | ||
80 | 423 | ||
81 | direct_pin_array.append([]) | 424 | info_data['matrix_pins']['direct'] = _extract_direct_matrix(info_data, direct_pins) |
82 | 425 | ||
83 | for pin in row.split(','): | 426 | return info_data |
84 | if pin == 'NO_PIN': | ||
85 | pin = None | ||
86 | 427 | ||
87 | direct_pin_array[-1].append(pin) | ||
88 | 428 | ||
89 | info_data['matrix_pins']['direct'] = direct_pin_array | 429 | def _extract_usb_info(info_data, config_c): |
430 | """Populate the USB information. | ||
431 | """ | ||
432 | if 'usb' not in info_data: | ||
433 | info_data['usb'] = {} | ||
90 | 434 | ||
91 | info_data['usb'] = { | 435 | for info_name, config_name in usb_properties.items(): |
92 | 'vid': config_c.get('VENDOR_ID'), | 436 | if config_name in config_c: |
93 | 'pid': config_c.get('PRODUCT_ID'), | 437 | if info_name in info_data['usb']: |
94 | 'device_ver': config_c.get('DEVICE_VER'), | 438 | _log_warning(info_data, '%s in config.h is overwriting usb.%s in info.json' % (config_name, info_name)) |
95 | 'manufacturer': config_c.get('MANUFACTURER'), | 439 | |
96 | 'product': config_c.get('PRODUCT'), | 440 | info_data['usb'][info_name] = '0x' + config_c[config_name][2:].upper() |
97 | } | 441 | |
442 | return info_data | ||
443 | |||
444 | |||
445 | def _extract_config_h(info_data): | ||
446 | """Pull some keyboard information from existing config.h files | ||
447 | """ | ||
448 | config_c = config_h(info_data['keyboard_folder']) | ||
449 | |||
450 | _extract_debounce(info_data, config_c) | ||
451 | _extract_diode_direction(info_data, config_c) | ||
452 | _extract_indicators(info_data, config_c) | ||
453 | _extract_matrix_info(info_data, config_c) | ||
454 | _extract_usb_info(info_data, config_c) | ||
455 | _extract_led_matrix(info_data, config_c) | ||
456 | _extract_rgblight(info_data, config_c) | ||
98 | 457 | ||
99 | return info_data | 458 | return info_data |
100 | 459 | ||
@@ -103,19 +462,56 @@ def _extract_rules_mk(info_data): | |||
103 | """Pull some keyboard information from existing rules.mk files | 462 | """Pull some keyboard information from existing rules.mk files |
104 | """ | 463 | """ |
105 | rules = rules_mk(info_data['keyboard_folder']) | 464 | rules = rules_mk(info_data['keyboard_folder']) |
106 | mcu = rules.get('MCU') | 465 | mcu = rules.get('MCU', info_data.get('processor')) |
107 | 466 | ||
108 | if mcu in CHIBIOS_PROCESSORS: | 467 | if mcu in CHIBIOS_PROCESSORS: |
109 | return arm_processor_rules(info_data, rules) | 468 | arm_processor_rules(info_data, rules) |
110 | 469 | ||
111 | elif mcu in LUFA_PROCESSORS + VUSB_PROCESSORS: | 470 | elif mcu in LUFA_PROCESSORS + VUSB_PROCESSORS: |
112 | return avr_processor_rules(info_data, rules) | 471 | avr_processor_rules(info_data, rules) |
472 | |||
473 | else: | ||
474 | cli.log.warning("%s: Unknown MCU: %s" % (info_data['keyboard_folder'], mcu)) | ||
475 | unknown_processor_rules(info_data, rules) | ||
476 | |||
477 | _extract_community_layouts(info_data, rules) | ||
478 | _extract_features(info_data, rules) | ||
479 | _extract_led_drivers(info_data, rules) | ||
113 | 480 | ||
114 | msg = "Unknown MCU: " + str(mcu) | 481 | return info_data |
482 | |||
483 | |||
484 | def _merge_layouts(info_data, new_info_data): | ||
485 | """Merge new_info_data into info_data in an intelligent way. | ||
486 | """ | ||
487 | for layout_name, layout_json in new_info_data['layouts'].items(): | ||
488 | if layout_name in info_data['layouts']: | ||
489 | # Pull in layouts we have a macro for | ||
490 | if len(info_data['layouts'][layout_name]['layout']) != len(layout_json['layout']): | ||
491 | msg = '%s: %s: Number of elements in info.json does not match! info.json:%s != %s:%s' | ||
492 | _log_error(info_data, msg % (info_data['keyboard_folder'], layout_name, len(layout_json['layout']), layout_name, len(info_data['layouts'][layout_name]['layout']))) | ||
493 | else: | ||
494 | for i, key in enumerate(info_data['layouts'][layout_name]['layout']): | ||
495 | key.update(layout_json['layout'][i]) | ||
496 | else: | ||
497 | # Pull in layouts that have matrix data | ||
498 | missing_matrix = False | ||
499 | for key in layout_json.get('layout', {}): | ||
500 | if 'matrix' not in key: | ||
501 | missing_matrix = True | ||
502 | |||
503 | if not missing_matrix: | ||
504 | if layout_name in info_data['layouts']: | ||
505 | # Update an existing layout with new data | ||
506 | for i, key in enumerate(info_data['layouts'][layout_name]['layout']): | ||
507 | key.update(layout_json['layout'][i]) | ||
115 | 508 | ||
116 | _log_warning(info_data, msg) | 509 | else: |
510 | # Copy in the new layout wholesale | ||
511 | layout_json['c_macro'] = False | ||
512 | info_data['layouts'][layout_name] = layout_json | ||
117 | 513 | ||
118 | return unknown_processor_rules(info_data, rules) | 514 | return info_data |
119 | 515 | ||
120 | 516 | ||
121 | def _search_keyboard_h(path): | 517 | def _search_keyboard_h(path): |
@@ -131,34 +527,21 @@ def _search_keyboard_h(path): | |||
131 | return layouts | 527 | return layouts |
132 | 528 | ||
133 | 529 | ||
134 | def _find_all_layouts(info_data, keyboard, rules): | 530 | def _find_all_layouts(info_data, keyboard): |
135 | """Looks for layout macros associated with this keyboard. | 531 | """Looks for layout macros associated with this keyboard. |
136 | """ | 532 | """ |
137 | layouts = _search_keyboard_h(Path(keyboard)) | 533 | layouts = _search_keyboard_h(Path(keyboard)) |
138 | 534 | ||
139 | if not layouts: | 535 | if not layouts: |
140 | # If we didn't find any layouts above we widen our search. This is error | 536 | # If we don't find any layouts from info.json or keyboard.h we widen our search. This is error prone which is why we want to encourage people to follow the standard above. |
141 | # prone which is why we want to encourage people to follow the standard above. | 537 | info_data['parse_warnings'].append('%s: Falling back to searching for KEYMAP/LAYOUT macros.' % (keyboard)) |
142 | _log_warning(info_data, 'Falling back to searching for KEYMAP/LAYOUT macros.') | 538 | |
143 | for file in glob('keyboards/%s/*.h' % keyboard): | 539 | for file in glob('keyboards/%s/*.h' % keyboard): |
144 | if file.endswith('.h'): | 540 | if file.endswith('.h'): |
145 | these_layouts = find_layouts(file) | 541 | these_layouts = find_layouts(file) |
146 | if these_layouts: | 542 | if these_layouts: |
147 | layouts.update(these_layouts) | 543 | layouts.update(these_layouts) |
148 | 544 | ||
149 | if 'LAYOUTS' in rules: | ||
150 | # Match these up against the supplied layouts | ||
151 | supported_layouts = rules['LAYOUTS'].strip().split() | ||
152 | for layout_name in sorted(layouts): | ||
153 | if not layout_name.startswith('LAYOUT_'): | ||
154 | continue | ||
155 | layout_name = layout_name[7:] | ||
156 | if layout_name in supported_layouts: | ||
157 | supported_layouts.remove(layout_name) | ||
158 | |||
159 | if supported_layouts: | ||
160 | _log_error(info_data, 'Missing LAYOUT() macro for %s' % (', '.join(supported_layouts))) | ||
161 | |||
162 | return layouts | 545 | return layouts |
163 | 546 | ||
164 | 547 | ||
@@ -180,13 +563,29 @@ def arm_processor_rules(info_data, rules): | |||
180 | """Setup the default info for an ARM board. | 563 | """Setup the default info for an ARM board. |
181 | """ | 564 | """ |
182 | info_data['processor_type'] = 'arm' | 565 | info_data['processor_type'] = 'arm' |
183 | info_data['bootloader'] = rules['BOOTLOADER'] if 'BOOTLOADER' in rules else 'unknown' | ||
184 | info_data['processor'] = rules['MCU'] if 'MCU' in rules else 'unknown' | ||
185 | info_data['protocol'] = 'ChibiOS' | 566 | info_data['protocol'] = 'ChibiOS' |
186 | 567 | ||
187 | if info_data['bootloader'] == 'unknown': | 568 | if 'MCU' in rules: |
569 | if 'processor' in info_data: | ||
570 | _log_warning(info_data, 'Processor/MCU is specified in both info.json and rules.mk, the rules.mk value wins.') | ||
571 | |||
572 | info_data['processor'] = rules['MCU'] | ||
573 | |||
574 | elif 'processor' not in info_data: | ||
575 | info_data['processor'] = 'unknown' | ||
576 | |||
577 | if 'BOOTLOADER' in rules: | ||
578 | # FIXME(skullydazed/anyone): need to remove the massive amounts of duplication first | ||
579 | # if 'bootloader' in info_data: | ||
580 | # _log_warning(info_data, 'Bootloader is specified in both info.json and rules.mk, the rules.mk value wins.') | ||
581 | |||
582 | info_data['bootloader'] = rules['BOOTLOADER'] | ||
583 | |||
584 | else: | ||
188 | if 'STM32' in info_data['processor']: | 585 | if 'STM32' in info_data['processor']: |
189 | info_data['bootloader'] = 'stm32-dfu' | 586 | info_data['bootloader'] = 'stm32-dfu' |
587 | else: | ||
588 | info_data['bootloader'] = 'unknown' | ||
190 | 589 | ||
191 | if 'STM32' in info_data['processor']: | 590 | if 'STM32' in info_data['processor']: |
192 | info_data['platform'] = 'STM32' | 591 | info_data['platform'] = 'STM32' |
@@ -195,6 +594,12 @@ def arm_processor_rules(info_data, rules): | |||
195 | elif 'ARM_ATSAM' in rules: | 594 | elif 'ARM_ATSAM' in rules: |
196 | info_data['platform'] = 'ARM_ATSAM' | 595 | info_data['platform'] = 'ARM_ATSAM' |
197 | 596 | ||
597 | if 'BOARD' in rules: | ||
598 | if 'board' in info_data: | ||
599 | _log_warning(info_data, 'Board is specified in both info.json and rules.mk, the rules.mk value wins.') | ||
600 | |||
601 | info_data['board'] = rules['BOARD'] | ||
602 | |||
198 | return info_data | 603 | return info_data |
199 | 604 | ||
200 | 605 | ||
@@ -204,9 +609,26 @@ def avr_processor_rules(info_data, rules): | |||
204 | info_data['processor_type'] = 'avr' | 609 | info_data['processor_type'] = 'avr' |
205 | info_data['bootloader'] = rules['BOOTLOADER'] if 'BOOTLOADER' in rules else 'atmel-dfu' | 610 | info_data['bootloader'] = rules['BOOTLOADER'] if 'BOOTLOADER' in rules else 'atmel-dfu' |
206 | info_data['platform'] = rules['ARCH'] if 'ARCH' in rules else 'unknown' | 611 | info_data['platform'] = rules['ARCH'] if 'ARCH' in rules else 'unknown' |
207 | info_data['processor'] = rules['MCU'] if 'MCU' in rules else 'unknown' | ||
208 | info_data['protocol'] = 'V-USB' if rules.get('MCU') in VUSB_PROCESSORS else 'LUFA' | 612 | info_data['protocol'] = 'V-USB' if rules.get('MCU') in VUSB_PROCESSORS else 'LUFA' |
209 | 613 | ||
614 | if 'MCU' in rules: | ||
615 | if 'processor' in info_data: | ||
616 | _log_warning(info_data, 'Processor/MCU is specified in both info.json and rules.mk, the rules.mk value wins.') | ||
617 | |||
618 | info_data['processor'] = rules['MCU'] | ||
619 | |||
620 | elif 'processor' not in info_data: | ||
621 | info_data['processor'] = 'unknown' | ||
622 | |||
623 | if 'BOOTLOADER' in rules: | ||
624 | # FIXME(skullydazed/anyone): need to remove the massive amounts of duplication first | ||
625 | # if 'bootloader' in info_data: | ||
626 | # _log_warning(info_data, 'Bootloader is specified in both info.json and rules.mk, the rules.mk value wins.') | ||
627 | |||
628 | info_data['bootloader'] = rules['BOOTLOADER'] | ||
629 | else: | ||
630 | info_data['bootloader'] = 'atmel-dfu' | ||
631 | |||
210 | # FIXME(fauxpark/anyone): Eventually we should detect the protocol by looking at PROTOCOL inherited from mcu_selection.mk: | 632 | # FIXME(fauxpark/anyone): Eventually we should detect the protocol by looking at PROTOCOL inherited from mcu_selection.mk: |
211 | # info_data['protocol'] = 'V-USB' if rules.get('PROTOCOL') == 'VUSB' else 'LUFA' | 633 | # info_data['protocol'] = 'V-USB' if rules.get('PROTOCOL') == 'VUSB' else 'LUFA' |
212 | 634 | ||
@@ -225,38 +647,44 @@ def unknown_processor_rules(info_data, rules): | |||
225 | return info_data | 647 | return info_data |
226 | 648 | ||
227 | 649 | ||
650 | def deep_update(origdict, newdict): | ||
651 | """Update a dictionary in place, recursing to do a deep copy. | ||
652 | """ | ||
653 | for key, value in newdict.items(): | ||
654 | if isinstance(value, Mapping): | ||
655 | origdict[key] = deep_update(origdict.get(key, {}), value) | ||
656 | |||
657 | else: | ||
658 | origdict[key] = value | ||
659 | |||
660 | return origdict | ||
661 | |||
662 | |||
228 | def merge_info_jsons(keyboard, info_data): | 663 | def merge_info_jsons(keyboard, info_data): |
229 | """Return a merged copy of all the info.json files for a keyboard. | 664 | """Return a merged copy of all the info.json files for a keyboard. |
230 | """ | 665 | """ |
231 | for info_file in find_info_json(keyboard): | 666 | for info_file in find_info_json(keyboard): |
232 | # Load and validate the JSON data | 667 | # Load and validate the JSON data |
233 | try: | 668 | new_info_data = _json_load(info_file) |
234 | with info_file.open('r') as info_fd: | ||
235 | new_info_data = json.load(info_fd) | ||
236 | except Exception as e: | ||
237 | _log_error(info_data, "Invalid JSON in file %s: %s: %s" % (str(info_file), e.__class__.__name__, e)) | ||
238 | continue | ||
239 | 669 | ||
240 | if not isinstance(new_info_data, dict): | 670 | if not isinstance(new_info_data, dict): |
241 | _log_error(info_data, "Invalid file %s, root object should be a dictionary." % (str(info_file),)) | 671 | _log_error(info_data, "Invalid file %s, root object should be a dictionary." % (str(info_file),)) |
242 | continue | 672 | continue |
243 | 673 | ||
244 | # Copy whitelisted keys into `info_data` | 674 | try: |
245 | for key in ('keyboard_name', 'manufacturer', 'identifier', 'url', 'maintainer', 'processor', 'bootloader', 'width', 'height'): | 675 | keyboard_validate(new_info_data) |
246 | if key in new_info_data: | 676 | except jsonschema.ValidationError as e: |
247 | info_data[key] = new_info_data[key] | 677 | json_path = '.'.join([str(p) for p in e.absolute_path]) |
678 | cli.log.error('Not including data from file: %s', info_file) | ||
679 | cli.log.error('\t%s: %s', json_path, e.message) | ||
680 | continue | ||
248 | 681 | ||
249 | # Merge the layouts in | 682 | # Mark the layouts as coming from json |
250 | if 'layouts' in new_info_data: | 683 | for layout in new_info_data.get('layouts', {}).values(): |
251 | for layout_name, json_layout in new_info_data['layouts'].items(): | 684 | layout['c_macro'] = False |
252 | # Only pull in layouts we have a macro for | 685 | |
253 | if layout_name in info_data['layouts']: | 686 | # Update info_data with the new data |
254 | if info_data['layouts'][layout_name]['key_count'] != len(json_layout['layout']): | 687 | deep_update(info_data, new_info_data) |
255 | msg = '%s: Number of elements in info.json does not match! info.json:%s != %s:%s' | ||
256 | _log_error(info_data, msg % (layout_name, len(json_layout['layout']), layout_name, len(info_data['layouts'][layout_name]['layout']))) | ||
257 | else: | ||
258 | for i, key in enumerate(info_data['layouts'][layout_name]['layout']): | ||
259 | key.update(json_layout['layout'][i]) | ||
260 | 688 | ||
261 | return info_data | 689 | return info_data |
262 | 690 | ||