From 47b9b110097a864d6ab76516b2213afd59948527 Mon Sep 17 00:00:00 2001 From: Zach White Date: Wed, 30 Dec 2020 10:27:37 -0800 Subject: Configure keyboard matrix from info.json (#10817) * Make parameters from info.json available to the build system * move all clueboard settings to info.json * code formatting * make flake8 happy * make flake8 happy * make qmk lint happy * Add support for specifying led indicators in json * move led indicators to the clueboard info.json * Apply suggestions from code review Co-authored-by: Erovia * add missing docstring Co-authored-by: Erovia --- lib/python/qmk/cli/c2json.py | 3 +- lib/python/qmk/cli/chibios/confmigrate.py | 8 +- lib/python/qmk/cli/generate/__init__.py | 4 + lib/python/qmk/cli/generate/api.py | 14 +- lib/python/qmk/cli/generate/config_h.py | 277 ++++++++++++++++++++++++++++++ lib/python/qmk/cli/generate/info_json.py | 49 ++++++ lib/python/qmk/cli/generate/layouts.py | 93 ++++++++++ lib/python/qmk/cli/generate/rules_mk.py | 59 +++++++ lib/python/qmk/cli/info.py | 7 +- lib/python/qmk/cli/kle2json.py | 51 ++---- 10 files changed, 517 insertions(+), 48 deletions(-) create mode 100755 lib/python/qmk/cli/generate/config_h.py create mode 100755 lib/python/qmk/cli/generate/info_json.py create mode 100755 lib/python/qmk/cli/generate/layouts.py create mode 100755 lib/python/qmk/cli/generate/rules_mk.py (limited to 'lib/python/qmk/cli') diff --git a/lib/python/qmk/cli/c2json.py b/lib/python/qmk/cli/c2json.py index 2b3bb774f7..8f9d8dc383 100644 --- a/lib/python/qmk/cli/c2json.py +++ b/lib/python/qmk/cli/c2json.py @@ -6,6 +6,7 @@ from milc import cli import qmk.keymap import qmk.path +from qmk.info_json_encoder import InfoJSONEncoder @cli.argument('--no-cpp', arg_only=True, action='store_false', help='Do not use \'cpp\' on keymap.c') @@ -47,7 +48,7 @@ def c2json(cli): cli.args.output.parent.mkdir(parents=True, exist_ok=True) if cli.args.output.exists(): cli.args.output.replace(cli.args.output.name + '.bak') - cli.args.output.write_text(json.dumps(keymap_json)) + cli.args.output.write_text(json.dumps(keymap_json, cls=InfoJSONEncoder)) if not cli.args.quiet: cli.log.info('Wrote keymap to %s.', cli.args.output) diff --git a/lib/python/qmk/cli/chibios/confmigrate.py b/lib/python/qmk/cli/chibios/confmigrate.py index eae294a0c6..b9cfda9614 100644 --- a/lib/python/qmk/cli/chibios/confmigrate.py +++ b/lib/python/qmk/cli/chibios/confmigrate.py @@ -13,7 +13,7 @@ def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) -fileHeader = """\ +file_header = """\ /* Copyright 2020 QMK * * This program is free software: you can redistribute it and/or modify @@ -77,7 +77,7 @@ def check_diffs(input_defs, reference_defs): def migrate_chconf_h(to_override, outfile): - print(fileHeader.format(cli.args.input.relative_to(QMK_FIRMWARE), cli.args.reference.relative_to(QMK_FIRMWARE)), file=outfile) + print(file_header.format(cli.args.input.relative_to(QMK_FIRMWARE), cli.args.reference.relative_to(QMK_FIRMWARE)), file=outfile) for override in to_override: print("#define %s %s" % (override[0], override[1]), file=outfile) @@ -87,7 +87,7 @@ def migrate_chconf_h(to_override, outfile): def migrate_halconf_h(to_override, outfile): - print(fileHeader.format(cli.args.input.relative_to(QMK_FIRMWARE), cli.args.reference.relative_to(QMK_FIRMWARE)), file=outfile) + print(file_header.format(cli.args.input.relative_to(QMK_FIRMWARE), cli.args.reference.relative_to(QMK_FIRMWARE)), file=outfile) for override in to_override: print("#define %s %s" % (override[0], override[1]), file=outfile) @@ -97,7 +97,7 @@ def migrate_halconf_h(to_override, outfile): def migrate_mcuconf_h(to_override, outfile): - print(fileHeader.format(cli.args.input.relative_to(QMK_FIRMWARE), cli.args.reference.relative_to(QMK_FIRMWARE)), file=outfile) + print(file_header.format(cli.args.input.relative_to(QMK_FIRMWARE), cli.args.reference.relative_to(QMK_FIRMWARE)), file=outfile) print("#include_next \n", file=outfile) diff --git a/lib/python/qmk/cli/generate/__init__.py b/lib/python/qmk/cli/generate/__init__.py index f9585bfb5c..bd75b044c5 100644 --- a/lib/python/qmk/cli/generate/__init__.py +++ b/lib/python/qmk/cli/generate/__init__.py @@ -1,3 +1,7 @@ from . import api +from . import config_h from . import docs +from . import info_json +from . import layouts from . import rgb_breathe_table +from . import rules_mk diff --git a/lib/python/qmk/cli/generate/api.py b/lib/python/qmk/cli/generate/api.py index 66db37cb52..6d111f244c 100755 --- a/lib/python/qmk/cli/generate/api.py +++ b/lib/python/qmk/cli/generate/api.py @@ -8,6 +8,7 @@ from milc import cli from qmk.datetime import current_datetime from qmk.info import info_json +from qmk.info_json_encoder import InfoJSONEncoder from qmk.keyboard import list_keyboards @@ -44,15 +45,16 @@ def generate_api(cli): if 'usb' in kb_all['keyboards'][keyboard_name]: usb = kb_all['keyboards'][keyboard_name]['usb'] - if usb['vid'] not in usb_list['devices']: + if 'vid' in usb and usb['vid'] not in usb_list['devices']: usb_list['devices'][usb['vid']] = {} - if usb['pid'] not in usb_list['devices'][usb['vid']]: + if 'pid' in usb and usb['pid'] not in usb_list['devices'][usb['vid']]: usb_list['devices'][usb['vid']][usb['pid']] = {} - usb_list['devices'][usb['vid']][usb['pid']][keyboard_name] = usb + if 'vid' in usb and 'pid' in usb: + usb_list['devices'][usb['vid']][usb['pid']][keyboard_name] = usb # Write the global JSON files - keyboard_list.write_text(json.dumps({'last_updated': current_datetime(), 'keyboards': sorted(kb_all['keyboards'])})) - keyboard_all.write_text(json.dumps(kb_all)) - usb_file.write_text(json.dumps(usb_list)) + keyboard_list.write_text(json.dumps({'last_updated': current_datetime(), 'keyboards': sorted(kb_all['keyboards'])}, cls=InfoJSONEncoder)) + keyboard_all.write_text(json.dumps(kb_all, cls=InfoJSONEncoder)) + usb_file.write_text(json.dumps(usb_list, cls=InfoJSONEncoder)) diff --git a/lib/python/qmk/cli/generate/config_h.py b/lib/python/qmk/cli/generate/config_h.py new file mode 100755 index 0000000000..4d734017a1 --- /dev/null +++ b/lib/python/qmk/cli/generate/config_h.py @@ -0,0 +1,277 @@ +"""Used by the make system to generate info_config.h from info.json. +""" +from milc import cli + +from qmk.constants import LED_INDICATORS +from qmk.decorators import automagic_keyboard, automagic_keymap +from qmk.info import info_json, rgblight_animations, rgblight_properties, rgblight_toggles +from qmk.path import is_keyboard, normpath + +usb_properties = { + 'vid': 'VENDOR_ID', + 'pid': 'PRODUCT_ID', + 'device_ver': 'DEVICE_VER', +} + + +def debounce(debounce): + """Return the config.h lines that set debounce + """ + return """ +#ifndef DEBOUNCE +# define DEBOUNCE %s +#endif // DEBOUNCE +""" % debounce + + +def diode_direction(diode_direction): + """Return the config.h lines that set diode direction + """ + return """ +#ifndef DIODE_DIRECTION +# define DIODE_DIRECTION %s +#endif // DIODE_DIRECTION +""" % diode_direction + + +def keyboard_name(keyboard_name): + """Return the config.h lines that set the keyboard's name. + """ + return """ +#ifndef DESCRIPTION +# define DESCRIPTION %s +#endif // DESCRIPTION + +#ifndef PRODUCT +# define PRODUCT %s +#endif // PRODUCT +""" % (keyboard_name, keyboard_name) + + +def manufacturer(manufacturer): + """Return the config.h lines that set the manufacturer. + """ + return """ +#ifndef MANUFACTURER +# define MANUFACTURER %s +#endif // MANUFACTURER +""" % (manufacturer) + + +def direct_pins(direct_pins): + """Return the config.h lines that set the direct pins. + """ + rows = [] + + for row in direct_pins: + cols = ','.join([col or 'NO_PIN' for col in row]) + rows.append('{' + cols + '}') + + col_count = len(direct_pins[0]) + row_count = len(direct_pins) + + return """ +#ifndef MATRIX_COLS +# define MATRIX_COLS %s +#endif // MATRIX_COLS + +#ifndef MATRIX_ROWS +# define MATRIX_ROWS %s +#endif // MATRIX_ROWS + +#ifndef DIRECT_PINS +# define DIRECT_PINS {%s} +#endif // DIRECT_PINS +""" % (col_count, row_count, ','.join(rows)) + + +def col_pins(col_pins): + """Return the config.h lines that set the column pins. + """ + cols = ','.join(col_pins) + col_num = len(col_pins) + + return """ +#ifndef MATRIX_COLS +# define MATRIX_COLS %s +#endif // MATRIX_COLS + +#ifndef MATRIX_COL_PINS +# define MATRIX_COL_PINS {%s} +#endif // MATRIX_COL_PINS +""" % (col_num, cols) + + +def row_pins(row_pins): + """Return the config.h lines that set the row pins. + """ + rows = ','.join(row_pins) + row_num = len(row_pins) + + return """ +#ifndef MATRIX_ROWS +# define MATRIX_ROWS %s +#endif // MATRIX_ROWS + +#ifndef MATRIX_ROW_PINS +# define MATRIX_ROW_PINS {%s} +#endif // MATRIX_ROW_PINS +""" % (row_num, rows) + + +def indicators(config): + """Return the config.h lines that setup LED indicators. + """ + defines = [] + + for led, define in LED_INDICATORS.items(): + if led in config: + defines.append('') + defines.append('#ifndef %s' % (define,)) + defines.append('# define %s %s' % (define, config[led])) + defines.append('#endif // %s' % (define,)) + + return '\n'.join(defines) + + +def layout_aliases(layout_aliases): + """Return the config.h lines that setup layout aliases. + """ + defines = [] + + for alias, layout in layout_aliases.items(): + defines.append('') + defines.append('#ifndef %s' % (alias,)) + defines.append('# define %s %s' % (alias, layout)) + defines.append('#endif // %s' % (alias,)) + + return '\n'.join(defines) + + +def matrix_pins(matrix_pins): + """Add the matrix config to the config.h. + """ + pins = [] + + if 'direct' in matrix_pins: + pins.append(direct_pins(matrix_pins['direct'])) + + if 'cols' in matrix_pins: + pins.append(col_pins(matrix_pins['cols'])) + + if 'rows' in matrix_pins: + pins.append(row_pins(matrix_pins['rows'])) + + return '\n'.join(pins) + + +def rgblight(config): + """Return the config.h lines that setup rgblight. + """ + rgblight_config = [] + + for json_key, config_key in rgblight_properties.items(): + if json_key in config: + rgblight_config.append('') + rgblight_config.append('#ifndef %s' % (config_key,)) + rgblight_config.append('# define %s %s' % (config_key, config[json_key])) + rgblight_config.append('#endif // %s' % (config_key,)) + + for json_key, config_key in rgblight_toggles.items(): + if config.get(json_key): + rgblight_config.append('') + rgblight_config.append('#ifndef %s' % (config_key,)) + rgblight_config.append('# define %s' % (config_key,)) + rgblight_config.append('#endif // %s' % (config_key,)) + + for json_key, config_key in rgblight_animations.items(): + if 'animations' in config and config['animations'].get(json_key): + rgblight_config.append('') + rgblight_config.append('#ifndef %s' % (config_key,)) + rgblight_config.append('# define %s' % (config_key,)) + rgblight_config.append('#endif // %s' % (config_key,)) + + return '\n'.join(rgblight_config) + + +def usb_properties(usb_props): + """Return the config.h lines that setup USB params. + """ + usb_lines = [] + + for info_name, config_name in usb_props.items(): + if info_name in usb_props: + usb_lines.append('') + usb_lines.append('#ifndef ' + config_name) + usb_lines.append('# define %s %s' % (config_name, usb_props[info_name])) + usb_lines.append('#endif // ' + config_name) + + return '\n'.join(usb_lines) + + +@cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') +@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") +@cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.') +@cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True) +@automagic_keyboard +@automagic_keymap +def generate_config_h(cli): + """Generates the info_config.h file. + """ + # Determine our keyboard(s) + if not cli.config.generate_config_h.keyboard: + cli.log.error('Missing paramater: --keyboard') + cli.subcommands['info'].print_help() + return False + + if not is_keyboard(cli.config.generate_config_h.keyboard): + cli.log.error('Invalid keyboard: "%s"', cli.config.generate_config_h.keyboard) + return False + + # Build the info.json file + kb_info_json = info_json(cli.config.generate_config_h.keyboard) + + # Build the info_config.h file. + config_h_lines = ['/* This file was generated by `qmk generate-config-h`. Do not edit or copy.' ' */', '', '#pragma once'] + + if 'debounce' in kb_info_json: + config_h_lines.append(debounce(kb_info_json['debounce'])) + + if 'diode_direction' in kb_info_json: + config_h_lines.append(diode_direction(kb_info_json['diode_direction'])) + + if 'indicators' in kb_info_json: + config_h_lines.append(indicators(kb_info_json['indicators'])) + + if 'keyboard_name' in kb_info_json: + config_h_lines.append(keyboard_name(kb_info_json['keyboard_name'])) + + if 'layout_aliases' in kb_info_json: + config_h_lines.append(layout_aliases(kb_info_json['layout_aliases'])) + + if 'manufacturer' in kb_info_json: + config_h_lines.append(manufacturer(kb_info_json['manufacturer'])) + + if 'rgblight' in kb_info_json: + config_h_lines.append(rgblight(kb_info_json['rgblight'])) + + if 'matrix_pins' in kb_info_json: + config_h_lines.append(matrix_pins(kb_info_json['matrix_pins'])) + + if 'usb' in kb_info_json: + config_h_lines.append(usb_properties(kb_info_json['usb'])) + + # Show the results + config_h = '\n'.join(config_h_lines) + + if cli.args.output: + cli.args.output.parent.mkdir(parents=True, exist_ok=True) + if cli.args.output.exists(): + cli.args.output.replace(cli.args.output.name + '.bak') + cli.args.output.write_text(config_h) + + if not cli.args.quiet: + cli.log.info('Wrote info_config.h to %s.', cli.args.output) + + else: + print(config_h) diff --git a/lib/python/qmk/cli/generate/info_json.py b/lib/python/qmk/cli/generate/info_json.py new file mode 100755 index 0000000000..7e6654e45d --- /dev/null +++ b/lib/python/qmk/cli/generate/info_json.py @@ -0,0 +1,49 @@ +"""Keyboard information script. + +Compile an info.json for a particular keyboard and pretty-print it. +""" +import json + +from milc import cli + +from qmk.info_json_encoder import InfoJSONEncoder +from qmk.decorators import automagic_keyboard, automagic_keymap +from qmk.info import info_json +from qmk.path import is_keyboard + + +@cli.argument('-kb', '--keyboard', help='Keyboard to show info for.') +@cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.') +@cli.subcommand('Generate an info.json file for a keyboard.', hidden=False if cli.config.user.developer else True) +@automagic_keyboard +@automagic_keymap +def generate_info_json(cli): + """Generate an info.json file for a keyboard + """ + # Determine our keyboard(s) + if not cli.config.generate_info_json.keyboard: + cli.log.error('Missing paramater: --keyboard') + cli.subcommands['info'].print_help() + return False + + if not is_keyboard(cli.config.generate_info_json.keyboard): + cli.log.error('Invalid keyboard: "%s"', cli.config.generate_info_json.keyboard) + return False + + # Build the info.json file + kb_info_json = info_json(cli.config.generate_info_json.keyboard) + pared_down_json = {} + + for key in ('manufacturer', 'maintainer', 'usb', 'keyboard_name', 'width', 'height', 'debounce', 'diode_direction', 'features', 'community_layouts', 'layout_aliases', 'matrix_pins', 'rgblight', 'url'): + if key in kb_info_json: + pared_down_json[key] = kb_info_json[key] + + pared_down_json['layouts'] = {} + if 'layouts' in pared_down_json: + for layout_name, layout in kb_info_json['layouts'].items(): + pared_down_json['layouts'][layout_name] = {} + pared_down_json['layouts'][layout_name]['key_count'] = layout.get('key_count', len(layout['layout'])) + pared_down_json['layouts'][layout_name]['layout'] = layout['layout'] + + # Display the results + print(json.dumps(pared_down_json, indent=2, cls=InfoJSONEncoder)) diff --git a/lib/python/qmk/cli/generate/layouts.py b/lib/python/qmk/cli/generate/layouts.py new file mode 100755 index 0000000000..809f0ef7ec --- /dev/null +++ b/lib/python/qmk/cli/generate/layouts.py @@ -0,0 +1,93 @@ +"""Used by the make system to generate layouts.h from info.json. +""" +from milc import cli + +from qmk.constants import COL_LETTERS, ROW_LETTERS +from qmk.decorators import automagic_keyboard, automagic_keymap +from qmk.info import info_json +from qmk.path import is_keyboard, normpath + +usb_properties = { + 'vid': 'VENDOR_ID', + 'pid': 'PRODUCT_ID', + 'device_ver': 'DEVICE_VER', +} + + +@cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') +@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") +@cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.') +@cli.subcommand('Used by the make system to generate layouts.h from info.json', hidden=True) +@automagic_keyboard +@automagic_keymap +def generate_layouts(cli): + """Generates the layouts.h file. + """ + # Determine our keyboard(s) + if not cli.config.generate_layouts.keyboard: + cli.log.error('Missing paramater: --keyboard') + cli.subcommands['info'].print_help() + return False + + if not is_keyboard(cli.config.generate_layouts.keyboard): + cli.log.error('Invalid keyboard: "%s"', cli.config.generate_layouts.keyboard) + return False + + # Build the info.json file + kb_info_json = info_json(cli.config.generate_layouts.keyboard) + + # Build the layouts.h file. + layouts_h_lines = ['/* This file was generated by `qmk generate-layouts`. Do not edit or copy.' ' */', '', '#pragma once'] + + if 'direct' in kb_info_json['matrix_pins']: + col_num = len(kb_info_json['matrix_pins']['direct'][0]) + row_num = len(kb_info_json['matrix_pins']['direct']) + elif 'cols' in kb_info_json['matrix_pins'] and 'rows' in kb_info_json['matrix_pins']: + col_num = len(kb_info_json['matrix_pins']['cols']) + row_num = len(kb_info_json['matrix_pins']['rows']) + else: + cli.log.error('%s: Invalid matrix config.', cli.config.generate_layouts.keyboard) + return False + + for layout_name in kb_info_json['layouts']: + if kb_info_json['layouts'][layout_name]['c_macro']: + continue + + layout_keys = [] + layout_matrix = [['KC_NO' for i in range(col_num)] for i in range(row_num)] + + for i, key in enumerate(kb_info_json['layouts'][layout_name]['layout']): + row = key['matrix'][0] + col = key['matrix'][1] + identifier = 'k%s%s' % (ROW_LETTERS[row], COL_LETTERS[col]) + + try: + layout_matrix[row][col] = identifier + layout_keys.append(identifier) + except IndexError: + key_name = key.get('label', identifier) + cli.log.error('Matrix data out of bounds for layout %s at index %s (%s): %s, %s', layout_name, i, key_name, row, col) + return False + + layouts_h_lines.append('') + layouts_h_lines.append('#define %s(%s) {\\' % (layout_name, ', '.join(layout_keys))) + + rows = ', \\\n'.join(['\t {' + ', '.join(row) + '}' for row in layout_matrix]) + rows += ' \\' + layouts_h_lines.append(rows) + layouts_h_lines.append('}') + + # Show the results + layouts_h = '\n'.join(layouts_h_lines) + '\n' + + if cli.args.output: + cli.args.output.parent.mkdir(parents=True, exist_ok=True) + if cli.args.output.exists(): + cli.args.output.replace(cli.args.output.name + '.bak') + cli.args.output.write_text(layouts_h) + + if not cli.args.quiet: + cli.log.info('Wrote info_config.h to %s.', cli.args.output) + + else: + print(layouts_h) diff --git a/lib/python/qmk/cli/generate/rules_mk.py b/lib/python/qmk/cli/generate/rules_mk.py new file mode 100755 index 0000000000..4268ae047b --- /dev/null +++ b/lib/python/qmk/cli/generate/rules_mk.py @@ -0,0 +1,59 @@ +"""Used by the make system to generate a rules.mk +""" +from milc import cli + +from qmk.decorators import automagic_keyboard, automagic_keymap +from qmk.info import info_json +from qmk.path import is_keyboard, normpath + + +@cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') +@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") +@cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.') +@cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True) +@automagic_keyboard +@automagic_keymap +def generate_rules_mk(cli): + """Generates a rules.mk file from info.json. + """ + # Determine our keyboard(s) + if not cli.config.generate_rules_mk.keyboard: + cli.log.error('Missing paramater: --keyboard') + cli.subcommands['info'].print_help() + return False + + if not is_keyboard(cli.config.generate_rules_mk.keyboard): + cli.log.error('Invalid keyboard: "%s"', cli.config.generate_rules_mk.keyboard) + return False + + # Build the info.json file + kb_info_json = info_json(cli.config.generate_rules_mk.keyboard) + rules_mk_lines = ['# This file was generated by `qmk generate-rules-mk`. Do not edit or copy.', ''] + + # Find features that should be enabled + if 'features' in kb_info_json: + for feature, enabled in kb_info_json['features'].items(): + feature = feature.upper() + enabled = 'yes' if enabled else 'no' + rules_mk_lines.append(f'{feature}_ENABLE := {enabled}') + + # Add community layouts + if 'community_layouts' in kb_info_json: + rules_mk_lines.append(f'LAYOUTS = {" ".join(kb_info_json["community_layouts"])}') + + # Show the results + rules_mk = '\n'.join(rules_mk_lines) + '\n' + + if cli.args.output: + cli.args.output.parent.mkdir(parents=True, exist_ok=True) + if cli.args.output.exists(): + cli.args.output.replace(cli.args.output.name + '.bak') + cli.args.output.write_text(rules_mk) + + if cli.args.quiet: + print(cli.args.output) + else: + cli.log.info('Wrote info_config.h to %s.', cli.args.output) + + else: + print(rules_mk) diff --git a/lib/python/qmk/cli/info.py b/lib/python/qmk/cli/info.py index 9ab299a21e..87d7253d4b 100755 --- a/lib/python/qmk/cli/info.py +++ b/lib/python/qmk/cli/info.py @@ -7,6 +7,8 @@ import platform from milc import cli +from qmk.info_json_encoder import InfoJSONEncoder +from qmk.constants import COL_LETTERS, ROW_LETTERS from qmk.decorators import automagic_keyboard, automagic_keymap from qmk.keyboard import render_layouts, render_layout from qmk.keymap import locate_keymap @@ -15,9 +17,6 @@ from qmk.path import is_keyboard platform_id = platform.platform().lower() -ROW_LETTERS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop' -COL_LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijilmnopqrstuvwxyz' - def show_keymap(kb_info_json, title_caps=True): """Render the keymap in ascii art. @@ -149,7 +148,7 @@ def info(cli): # Output in the requested format if cli.args.format == 'json': - print(json.dumps(kb_info_json)) + print(json.dumps(kb_info_json, cls=InfoJSONEncoder)) elif cli.args.format == 'text': print_text_output(kb_info_json) elif cli.args.format == 'friendly': diff --git a/lib/python/qmk/cli/kle2json.py b/lib/python/qmk/cli/kle2json.py index 3d1bb8c43c..66d504bfc2 100755 --- a/lib/python/qmk/cli/kle2json.py +++ b/lib/python/qmk/cli/kle2json.py @@ -3,25 +3,12 @@ import json import os from pathlib import Path -from decimal import Decimal -from collections import OrderedDict from milc import cli from kle2xy import KLE2xy from qmk.converter import kle2qmk - - -class CustomJSONEncoder(json.JSONEncoder): - def default(self, obj): - try: - if isinstance(obj, Decimal): - if obj % 2 in (Decimal(0), Decimal(1)): - return int(obj) - return float(obj) - except TypeError: - pass - return json.JSONEncoder.default(self, obj) +from qmk.info_json_encoder import InfoJSONEncoder @cli.argument('filename', help='The KLE raw txt to convert') @@ -52,24 +39,22 @@ def kle2json(cli): cli.log.error('Could not parse KLE raw data: %s', raw_code) cli.log.exception(e) return False - keyboard = OrderedDict( - keyboard_name=kle.name, - url='', - maintainer='qmk', - width=kle.columns, - height=kle.rows, - layouts={'LAYOUT': { - 'layout': 'LAYOUT_JSON_HERE' - }}, - ) - # Initialize keyboard with json encoded from ordered dict - keyboard = json.dumps(keyboard, indent=4, separators=(', ', ': '), sort_keys=False, cls=CustomJSONEncoder) - # Initialize layout with kle2qmk from converter module - layout = json.dumps(kle2qmk(kle), separators=(', ', ':'), cls=CustomJSONEncoder) - # Replace layout in keyboard json - keyboard = keyboard.replace('"LAYOUT_JSON_HERE"', layout) + keyboard = { + 'keyboard_name': kle.name, + 'url': '', + 'maintainer': 'qmk', + 'width': kle.columns, + 'height': kle.rows, + 'layouts': { + 'LAYOUT': { + 'layout': kle2qmk(kle) + } + }, + } + # Write our info.json - file = open(out_path / "info.json", "w") - file.write(keyboard) - file.close() + keyboard = json.dumps(keyboard, indent=4, separators=(', ', ': '), sort_keys=False, cls=InfoJSONEncoder) + info_json_file = out_path / 'info.json' + + info_json_file.write_text(keyboard) cli.log.info('Wrote out {fg_cyan}%s/info.json', out_path) -- cgit v1.2.3 From f27d8d94489342d4ce7ba9955cd062c725350db9 Mon Sep 17 00:00:00 2001 From: Zach White Date: Sat, 2 Jan 2021 12:05:56 -0800 Subject: Fix compiling on develop (#11409) --- lib/python/qmk/cli/generate/config_h.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/python/qmk/cli') diff --git a/lib/python/qmk/cli/generate/config_h.py b/lib/python/qmk/cli/generate/config_h.py index 4d734017a1..e160ffea83 100755 --- a/lib/python/qmk/cli/generate/config_h.py +++ b/lib/python/qmk/cli/generate/config_h.py @@ -7,7 +7,7 @@ from qmk.decorators import automagic_keyboard, automagic_keymap from qmk.info import info_json, rgblight_animations, rgblight_properties, rgblight_toggles from qmk.path import is_keyboard, normpath -usb_properties = { +usb_prop_map = { 'vid': 'VENDOR_ID', 'pid': 'PRODUCT_ID', 'device_ver': 'DEVICE_VER', @@ -199,7 +199,7 @@ def usb_properties(usb_props): """ usb_lines = [] - for info_name, config_name in usb_props.items(): + for info_name, config_name in usb_prop_map.items(): if info_name in usb_props: usb_lines.append('') usb_lines.append('#ifndef ' + config_name) -- cgit v1.2.3 From 11bd98f684148ed9577b263189121e52027d66d9 Mon Sep 17 00:00:00 2001 From: Zach White Date: Sat, 2 Jan 2021 18:08:17 -0800 Subject: Fix broken keyboards (#11412) * Fix a couple errors * add a dependency for the generated headers --- lib/python/qmk/cli/generate/layouts.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'lib/python/qmk/cli') diff --git a/lib/python/qmk/cli/generate/layouts.py b/lib/python/qmk/cli/generate/layouts.py index 809f0ef7ec..273870e15c 100755 --- a/lib/python/qmk/cli/generate/layouts.py +++ b/lib/python/qmk/cli/generate/layouts.py @@ -39,15 +39,16 @@ def generate_layouts(cli): # Build the layouts.h file. layouts_h_lines = ['/* This file was generated by `qmk generate-layouts`. Do not edit or copy.' ' */', '', '#pragma once'] - if 'direct' in kb_info_json['matrix_pins']: - col_num = len(kb_info_json['matrix_pins']['direct'][0]) - row_num = len(kb_info_json['matrix_pins']['direct']) - elif 'cols' in kb_info_json['matrix_pins'] and 'rows' in kb_info_json['matrix_pins']: - col_num = len(kb_info_json['matrix_pins']['cols']) - row_num = len(kb_info_json['matrix_pins']['rows']) - else: - cli.log.error('%s: Invalid matrix config.', cli.config.generate_layouts.keyboard) - return False + if 'matrix_pins' in kb_info_json: + if 'direct' in kb_info_json['matrix_pins']: + col_num = len(kb_info_json['matrix_pins']['direct'][0]) + row_num = len(kb_info_json['matrix_pins']['direct']) + elif 'cols' in kb_info_json['matrix_pins'] and 'rows' in kb_info_json['matrix_pins']: + col_num = len(kb_info_json['matrix_pins']['cols']) + row_num = len(kb_info_json['matrix_pins']['rows']) + else: + cli.log.error('%s: Invalid matrix config.', cli.config.generate_layouts.keyboard) + return False for layout_name in kb_info_json['layouts']: if kb_info_json['layouts'][layout_name]['c_macro']: -- cgit v1.2.3 From 6daa204363dfaa7f31d935a9913d6b35ec22e971 Mon Sep 17 00:00:00 2001 From: Zach White Date: Sun, 3 Jan 2021 15:45:06 -0800 Subject: fix keyboards with apostrophes in their names --- lib/python/qmk/cli/generate/config_h.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/python/qmk/cli') diff --git a/lib/python/qmk/cli/generate/config_h.py b/lib/python/qmk/cli/generate/config_h.py index e160ffea83..09aa0041f4 100755 --- a/lib/python/qmk/cli/generate/config_h.py +++ b/lib/python/qmk/cli/generate/config_h.py @@ -45,7 +45,7 @@ def keyboard_name(keyboard_name): #ifndef PRODUCT # define PRODUCT %s #endif // PRODUCT -""" % (keyboard_name, keyboard_name) +""" % (keyboard_name.replace("'", ""), keyboard_name.replace("'", "")) def manufacturer(manufacturer): @@ -55,7 +55,7 @@ def manufacturer(manufacturer): #ifndef MANUFACTURER # define MANUFACTURER %s #endif // MANUFACTURER -""" % (manufacturer) +""" % (manufacturer.replace("'", "")) def direct_pins(direct_pins): -- cgit v1.2.3 From ededff8556daff544633cb143cb6d939afd09014 Mon Sep 17 00:00:00 2001 From: Zach White Date: Tue, 1 Dec 2020 12:52:02 -0800 Subject: validate keyboard data with jsonschema --- lib/python/qmk/cli/generate/info_json.py | 2 +- lib/python/qmk/cli/generate/rules_mk.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) (limited to 'lib/python/qmk/cli') diff --git a/lib/python/qmk/cli/generate/info_json.py b/lib/python/qmk/cli/generate/info_json.py index 7e6654e45d..fba4b1c014 100755 --- a/lib/python/qmk/cli/generate/info_json.py +++ b/lib/python/qmk/cli/generate/info_json.py @@ -39,7 +39,7 @@ def generate_info_json(cli): pared_down_json[key] = kb_info_json[key] pared_down_json['layouts'] = {} - if 'layouts' in pared_down_json: + if 'layouts' in kb_info_json: for layout_name, layout in kb_info_json['layouts'].items(): pared_down_json['layouts'][layout_name] = {} pared_down_json['layouts'][layout_name]['key_count'] = layout.get('key_count', len(layout['layout'])) diff --git a/lib/python/qmk/cli/generate/rules_mk.py b/lib/python/qmk/cli/generate/rules_mk.py index 4268ae047b..72ed3c45fa 100755 --- a/lib/python/qmk/cli/generate/rules_mk.py +++ b/lib/python/qmk/cli/generate/rules_mk.py @@ -6,6 +6,10 @@ from qmk.decorators import automagic_keyboard, automagic_keymap from qmk.info import info_json from qmk.path import is_keyboard, normpath +info_to_rules = { + 'bootloader': 'BOOTLOADER', + 'processor': 'MCU' +} @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") @@ -30,6 +34,10 @@ def generate_rules_mk(cli): kb_info_json = info_json(cli.config.generate_rules_mk.keyboard) rules_mk_lines = ['# This file was generated by `qmk generate-rules-mk`. Do not edit or copy.', ''] + # Bring in settings + for info_key, rule_key in info_to_rules.items(): + rules_mk_lines.append(f'{rule_key} := {kb_info_json[info_key]}') + # Find features that should be enabled if 'features' in kb_info_json: for feature, enabled in kb_info_json['features'].items(): @@ -37,6 +45,11 @@ def generate_rules_mk(cli): enabled = 'yes' if enabled else 'no' rules_mk_lines.append(f'{feature}_ENABLE := {enabled}') + # Set the LED driver + if 'led_matrix' in kb_info_json and 'driver' in kb_info_json['led_matrix']: + driver = kb_info_json['led_matrix']['driver'] + rules_mk_lines.append(f'LED_MATRIX_DRIVER = {driver}') + # Add community layouts if 'community_layouts' in kb_info_json: rules_mk_lines.append(f'LAYOUTS = {" ".join(kb_info_json["community_layouts"])}') -- cgit v1.2.3 From b2c26f7cdd4b268e80f98cae7f444956559436ec Mon Sep 17 00:00:00 2001 From: Zach White Date: Tue, 1 Dec 2020 16:04:22 -0800 Subject: get qmk generate-api into a good state --- lib/python/qmk/cli/generate/api.py | 2 +- lib/python/qmk/cli/generate/rules_mk.py | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'lib/python/qmk/cli') diff --git a/lib/python/qmk/cli/generate/api.py b/lib/python/qmk/cli/generate/api.py index 6d111f244c..2fda38fc9a 100755 --- a/lib/python/qmk/cli/generate/api.py +++ b/lib/python/qmk/cli/generate/api.py @@ -48,7 +48,7 @@ def generate_api(cli): if 'vid' in usb and usb['vid'] not in usb_list['devices']: usb_list['devices'][usb['vid']] = {} - if 'pid' in usb and usb['pid'] not in usb_list['devices'][usb['vid']]: + if 'vid' in usb and usb['pid'] not in usb_list['devices'][usb['vid']]: usb_list['devices'][usb['vid']][usb['pid']] = {} if 'vid' in usb and 'pid' in usb: diff --git a/lib/python/qmk/cli/generate/rules_mk.py b/lib/python/qmk/cli/generate/rules_mk.py index 72ed3c45fa..570ef5a0d6 100755 --- a/lib/python/qmk/cli/generate/rules_mk.py +++ b/lib/python/qmk/cli/generate/rules_mk.py @@ -41,9 +41,12 @@ def generate_rules_mk(cli): # Find features that should be enabled if 'features' in kb_info_json: for feature, enabled in kb_info_json['features'].items(): - feature = feature.upper() - enabled = 'yes' if enabled else 'no' - rules_mk_lines.append(f'{feature}_ENABLE := {enabled}') + if feature == 'bootmagic_lite' and enabled: + rules_mk_lines.append(f'BOOTMAGIC_ENABLE := lite') + else: + feature = feature.upper() + enabled = 'yes' if enabled else 'no' + rules_mk_lines.append(f'{feature}_ENABLE := {enabled}') # Set the LED driver if 'led_matrix' in kb_info_json and 'driver' in kb_info_json['led_matrix']: -- cgit v1.2.3 From 56ef80216ae4c67e2a70857c61d1e62eec1ab380 Mon Sep 17 00:00:00 2001 From: Zach White Date: Wed, 30 Dec 2020 11:21:18 -0800 Subject: make flake8 happy --- lib/python/qmk/cli/generate/rules_mk.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lib/python/qmk/cli') diff --git a/lib/python/qmk/cli/generate/rules_mk.py b/lib/python/qmk/cli/generate/rules_mk.py index 570ef5a0d6..2a7e918569 100755 --- a/lib/python/qmk/cli/generate/rules_mk.py +++ b/lib/python/qmk/cli/generate/rules_mk.py @@ -8,9 +8,10 @@ from qmk.path import is_keyboard, normpath info_to_rules = { 'bootloader': 'BOOTLOADER', - 'processor': 'MCU' + 'processor': 'MCU', } + @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") @cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.') @@ -42,7 +43,7 @@ def generate_rules_mk(cli): if 'features' in kb_info_json: for feature, enabled in kb_info_json['features'].items(): if feature == 'bootmagic_lite' and enabled: - rules_mk_lines.append(f'BOOTMAGIC_ENABLE := lite') + rules_mk_lines.append('BOOTMAGIC_ENABLE := lite') else: feature = feature.upper() enabled = 'yes' if enabled else 'no' -- cgit v1.2.3 From 9a178f6826a30007f84983daf1c87ded1525c9cd Mon Sep 17 00:00:00 2001 From: Zach White Date: Wed, 6 Jan 2021 10:13:25 -0800 Subject: fix rgblight properties --- lib/python/qmk/cli/generate/config_h.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/python/qmk/cli') diff --git a/lib/python/qmk/cli/generate/config_h.py b/lib/python/qmk/cli/generate/config_h.py index 09aa0041f4..15d4fbf2dd 100755 --- a/lib/python/qmk/cli/generate/config_h.py +++ b/lib/python/qmk/cli/generate/config_h.py @@ -173,9 +173,9 @@ def rgblight(config): for json_key, config_key in rgblight_properties.items(): if json_key in config: rgblight_config.append('') - rgblight_config.append('#ifndef %s' % (config_key,)) - rgblight_config.append('# define %s %s' % (config_key, config[json_key])) - rgblight_config.append('#endif // %s' % (config_key,)) + rgblight_config.append('#ifndef %s' % (config_key[0],)) + rgblight_config.append('# define %s %s' % (config_key[0], config[json_key])) + rgblight_config.append('#endif // %s' % (config_key[0],)) for json_key, config_key in rgblight_toggles.items(): if config.get(json_key): -- cgit v1.2.3 From 30331b383f9ef4620e47aa07e4f9af7fae9d30b3 Mon Sep 17 00:00:00 2001 From: Zach White Date: Fri, 8 Jan 2021 00:00:15 -0800 Subject: fix bugs triggered by certain boards --- lib/python/qmk/cli/generate/config_h.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/python/qmk/cli') diff --git a/lib/python/qmk/cli/generate/config_h.py b/lib/python/qmk/cli/generate/config_h.py index 15d4fbf2dd..1de84de7a9 100755 --- a/lib/python/qmk/cli/generate/config_h.py +++ b/lib/python/qmk/cli/generate/config_h.py @@ -64,7 +64,7 @@ def direct_pins(direct_pins): rows = [] for row in direct_pins: - cols = ','.join([col or 'NO_PIN' for col in row]) + cols = ','.join(map(str, [col or 'NO_PIN' for col in row])) rows.append('{' + cols + '}') col_count = len(direct_pins[0]) @@ -88,7 +88,7 @@ def direct_pins(direct_pins): def col_pins(col_pins): """Return the config.h lines that set the column pins. """ - cols = ','.join(col_pins) + cols = ','.join(map(str, [pin or 'NO_PIN' for pin in col_pins])) col_num = len(col_pins) return """ @@ -105,7 +105,7 @@ def col_pins(col_pins): def row_pins(row_pins): """Return the config.h lines that set the row pins. """ - rows = ','.join(row_pins) + rows = ','.join(map(str, [pin or 'NO_PIN' for pin in row_pins])) row_num = len(row_pins) return """ -- cgit v1.2.3 From e2c7f893470465f986bd6849c18606be0f2b7842 Mon Sep 17 00:00:00 2001 From: Zach White Date: Fri, 8 Jan 2021 08:46:51 -0800 Subject: typo fix --- lib/python/qmk/cli/generate/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/python/qmk/cli') diff --git a/lib/python/qmk/cli/generate/api.py b/lib/python/qmk/cli/generate/api.py index 2fda38fc9a..6d111f244c 100755 --- a/lib/python/qmk/cli/generate/api.py +++ b/lib/python/qmk/cli/generate/api.py @@ -48,7 +48,7 @@ def generate_api(cli): if 'vid' in usb and usb['vid'] not in usb_list['devices']: usb_list['devices'][usb['vid']] = {} - if 'vid' in usb and usb['pid'] not in usb_list['devices'][usb['vid']]: + if 'pid' in usb and usb['pid'] not in usb_list['devices'][usb['vid']]: usb_list['devices'][usb['vid']][usb['pid']] = {} if 'vid' in usb and 'pid' in usb: -- cgit v1.2.3 From 962bc8d9dd413690dbeadeaac971a5389697210f Mon Sep 17 00:00:00 2001 From: Zach White Date: Sat, 9 Jan 2021 13:34:14 -0800 Subject: Use the schema to eliminate custom code (#11108) * use the schema to eliminate custom code * Update docs/reference_info_json.md Co-authored-by: Ryan * make flake8 happy * bugfix * do not overwrite make vars from json Co-authored-by: Ryan --- lib/python/qmk/cli/generate/info_json.py | 48 +++++++++++++++++++++----------- lib/python/qmk/cli/generate/layouts.py | 4 +++ lib/python/qmk/cli/generate/rules_mk.py | 10 +++---- 3 files changed, 41 insertions(+), 21 deletions(-) (limited to 'lib/python/qmk/cli') diff --git a/lib/python/qmk/cli/generate/info_json.py b/lib/python/qmk/cli/generate/info_json.py index fba4b1c014..f3fc54ddcf 100755 --- a/lib/python/qmk/cli/generate/info_json.py +++ b/lib/python/qmk/cli/generate/info_json.py @@ -4,14 +4,41 @@ Compile an info.json for a particular keyboard and pretty-print it. """ import json +from jsonschema import Draft7Validator, validators from milc import cli -from qmk.info_json_encoder import InfoJSONEncoder from qmk.decorators import automagic_keyboard, automagic_keymap -from qmk.info import info_json +from qmk.info import info_json, _jsonschema +from qmk.info_json_encoder import InfoJSONEncoder from qmk.path import is_keyboard +def pruning_validator(validator_class): + """Extends Draft7Validator to remove properties that aren't specified in the schema. + """ + validate_properties = validator_class.VALIDATORS["properties"] + + def remove_additional_properties(validator, properties, instance, schema): + for prop in list(instance.keys()): + if prop not in properties: + del instance[prop] + + for error in validate_properties(validator, properties, instance, schema): + yield error + + return validators.extend(validator_class, {"properties": remove_additional_properties}) + + +def strip_info_json(kb_info_json): + """Remove the API-only properties from the info.json. + """ + pruning_draft_7_validator = pruning_validator(Draft7Validator) + schema = _jsonschema('keyboard') + validator = pruning_draft_7_validator(schema).validate + + return validator(kb_info_json) + + @cli.argument('-kb', '--keyboard', help='Keyboard to show info for.') @cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.') @cli.subcommand('Generate an info.json file for a keyboard.', hidden=False if cli.config.user.developer else True) @@ -22,7 +49,7 @@ def generate_info_json(cli): """ # Determine our keyboard(s) if not cli.config.generate_info_json.keyboard: - cli.log.error('Missing paramater: --keyboard') + cli.log.error('Missing parameter: --keyboard') cli.subcommands['info'].print_help() return False @@ -32,18 +59,7 @@ def generate_info_json(cli): # Build the info.json file kb_info_json = info_json(cli.config.generate_info_json.keyboard) - pared_down_json = {} - - for key in ('manufacturer', 'maintainer', 'usb', 'keyboard_name', 'width', 'height', 'debounce', 'diode_direction', 'features', 'community_layouts', 'layout_aliases', 'matrix_pins', 'rgblight', 'url'): - if key in kb_info_json: - pared_down_json[key] = kb_info_json[key] - - pared_down_json['layouts'] = {} - if 'layouts' in kb_info_json: - for layout_name, layout in kb_info_json['layouts'].items(): - pared_down_json['layouts'][layout_name] = {} - pared_down_json['layouts'][layout_name]['key_count'] = layout.get('key_count', len(layout['layout'])) - pared_down_json['layouts'][layout_name]['layout'] = layout['layout'] + strip_info_json(kb_info_json) # Display the results - print(json.dumps(pared_down_json, indent=2, cls=InfoJSONEncoder)) + print(json.dumps(kb_info_json, indent=2, cls=InfoJSONEncoder)) diff --git a/lib/python/qmk/cli/generate/layouts.py b/lib/python/qmk/cli/generate/layouts.py index 273870e15c..b7baae0651 100755 --- a/lib/python/qmk/cli/generate/layouts.py +++ b/lib/python/qmk/cli/generate/layouts.py @@ -54,6 +54,10 @@ def generate_layouts(cli): if kb_info_json['layouts'][layout_name]['c_macro']: continue + if 'matrix' not in kb_info_json['layouts'][layout_name]['layout'][0]: + cli.log.debug('%s/%s: No matrix data!', cli.config.generate_layouts.keyboard, layout_name) + continue + layout_keys = [] layout_matrix = [['KC_NO' for i in range(col_num)] for i in range(row_num)] diff --git a/lib/python/qmk/cli/generate/rules_mk.py b/lib/python/qmk/cli/generate/rules_mk.py index 2a7e918569..0fdccb4048 100755 --- a/lib/python/qmk/cli/generate/rules_mk.py +++ b/lib/python/qmk/cli/generate/rules_mk.py @@ -37,26 +37,26 @@ def generate_rules_mk(cli): # Bring in settings for info_key, rule_key in info_to_rules.items(): - rules_mk_lines.append(f'{rule_key} := {kb_info_json[info_key]}') + rules_mk_lines.append(f'{rule_key} ?= {kb_info_json[info_key]}') # Find features that should be enabled if 'features' in kb_info_json: for feature, enabled in kb_info_json['features'].items(): if feature == 'bootmagic_lite' and enabled: - rules_mk_lines.append('BOOTMAGIC_ENABLE := lite') + rules_mk_lines.append('BOOTMAGIC_ENABLE ?= lite') else: feature = feature.upper() enabled = 'yes' if enabled else 'no' - rules_mk_lines.append(f'{feature}_ENABLE := {enabled}') + rules_mk_lines.append(f'{feature}_ENABLE ?= {enabled}') # Set the LED driver if 'led_matrix' in kb_info_json and 'driver' in kb_info_json['led_matrix']: driver = kb_info_json['led_matrix']['driver'] - rules_mk_lines.append(f'LED_MATRIX_DRIVER = {driver}') + rules_mk_lines.append(f'LED_MATRIX_DRIVER ?= {driver}') # Add community layouts if 'community_layouts' in kb_info_json: - rules_mk_lines.append(f'LAYOUTS = {" ".join(kb_info_json["community_layouts"])}') + rules_mk_lines.append(f'LAYOUTS ?= {" ".join(kb_info_json["community_layouts"])}') # Show the results rules_mk = '\n'.join(rules_mk_lines) + '\n' -- cgit v1.2.3 From eaa9106ec74591593e638ac015a5c90d17b30612 Mon Sep 17 00:00:00 2001 From: Zach White Date: Sat, 9 Jan 2021 20:18:47 -0800 Subject: Add support for specifying BOARD in info.json (#11492) * add support for specifying BOARD in info.json * move BOARD from rules.mk to info.json for clueboard * fix keyboards that do not require board * remove out of compliance values --- lib/python/qmk/cli/generate/rules_mk.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib/python/qmk/cli') diff --git a/lib/python/qmk/cli/generate/rules_mk.py b/lib/python/qmk/cli/generate/rules_mk.py index 0fdccb4048..b262e3c666 100755 --- a/lib/python/qmk/cli/generate/rules_mk.py +++ b/lib/python/qmk/cli/generate/rules_mk.py @@ -7,6 +7,7 @@ from qmk.info import info_json from qmk.path import is_keyboard, normpath info_to_rules = { + 'board': 'BOARD', 'bootloader': 'BOOTLOADER', 'processor': 'MCU', } @@ -37,7 +38,8 @@ def generate_rules_mk(cli): # Bring in settings for info_key, rule_key in info_to_rules.items(): - rules_mk_lines.append(f'{rule_key} ?= {kb_info_json[info_key]}') + if info_key in kb_info_json: + rules_mk_lines.append(f'{rule_key} ?= {kb_info_json[info_key]}') # Find features that should be enabled if 'features' in kb_info_json: -- cgit v1.2.3 From 78fe155b7241900988f26479b8e417623b137320 Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Wed, 20 Jan 2021 02:19:36 +1100 Subject: Fix up comments showing how to execute config migration. (#11621) --- lib/python/qmk/cli/chibios/confmigrate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/python/qmk/cli') diff --git a/lib/python/qmk/cli/chibios/confmigrate.py b/lib/python/qmk/cli/chibios/confmigrate.py index b9cfda9614..9d25488586 100644 --- a/lib/python/qmk/cli/chibios/confmigrate.py +++ b/lib/python/qmk/cli/chibios/confmigrate.py @@ -32,7 +32,7 @@ file_header = """\ /* * This file was auto-generated by: - * `qmk chibios-confupdate -i {0} -r {1}` + * `qmk chibios-confmigrate -i {0} -r {1}` */ #pragma once -- cgit v1.2.3 From bdb757e189bf72c0ef382a924328642a629e06d5 Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Thu, 21 Jan 2021 17:00:53 +1100 Subject: ChibiOS conf migrations... take 11 (#11646) * Allow forcing re-migration. * ChibiOS conf upgrade for acheron/arctic acheron/arctic - 2aedbe9103fff6c37e596c33c9ed337957647368 * ChibiOS conf upgrade for acheron/austin acheron/austin - c2f4e3b7fc9f1c3d64f47d139bc58367afeca1b7 * ChibiOS conf upgrade for acheron/keebspcb acheron/keebspcb - 1ba976e409732bfa5c0487dd33e20bec06852ce4 * ChibiOS conf upgrade for acheron/lasgweloth acheron/lasgweloth - 01a2e70d0d86de0ff05d0b898a6e3b4428ee1581 * ChibiOS conf upgrade for aeboards/ext65/rev2 aeboards/ext65/rev2 - 3c9a45cafb4ed6d9672aaff4548b105193633a87 * ChibiOS conf upgrade for ai03/vega ai03/vega - 1bd0dfccb99baa69bacd2d55f2bfa72019b8bf80 * ChibiOS conf upgrade for akegata_denki/device_one akegata_denki/device_one - a013823188660f5fca37c5763f160f8646aed7a7 * ChibiOS conf upgrade for at_at/660m at_at/660m - 9999583e8bec2772046132a22818482d24e18c84 * ChibiOS conf upgrade for box75 box75 - 1126206109a942237eea96f3a9608e3c9ed55f8e * ChibiOS conf upgrade for bt66tech/bt66tech60 bt66tech/bt66tech60 - b69120638a8b2c86c008fd0592be918383d8a454 * ChibiOS conf upgrade for cannonkeys/an_c cannonkeys/an_c - c755f6caaccbe2b30d95661a3d441b836534c29f * ChibiOS conf upgrade for cannonkeys/atlas cannonkeys/atlas - 1a18c55492a834b82715516ea4cd4d3d94508743 * ChibiOS conf upgrade for cannonkeys/chimera65 cannonkeys/chimera65 - 6bf226ef353da9106c381a8ac95a9b90529327e5 * ChibiOS conf upgrade for cannonkeys/db60 cannonkeys/db60 - 07e3246f8e3adc5b6918809e6a5aa1ad064b9a09 * ChibiOS conf upgrade for cannonkeys/devastatingtkl cannonkeys/devastatingtkl - 907d258e29eb7e35b1f868c1ea6148bfd1e3a3f2 * ChibiOS conf upgrade for cannonkeys/instant60 cannonkeys/instant60 - ac876ff6746b08839a3936dd4126b52683b763f4 * ChibiOS conf upgrade for cannonkeys/instant65 cannonkeys/instant65 - 3b8c89ec5fecbdb35cccb00c45e64a798528dbda * ChibiOS conf upgrade for cannonkeys/iron165 cannonkeys/iron165 - 0814ec4067d9857b0134e38384f272cf7172fb03 * ChibiOS conf upgrade for cannonkeys/obliterated75 cannonkeys/obliterated75 - 0f376e5d9a232c62e9c60f8fdc81d12732352ddc * ChibiOS conf upgrade for cannonkeys/onyx cannonkeys/onyx - d35134ebe09046b91ab176035d3742d80b23ed8e * ChibiOS conf upgrade for cannonkeys/ortho48 cannonkeys/ortho48 - 828dbfbb013ff4fefe75044b3c9fd31fec5e95a1 * ChibiOS conf upgrade for cannonkeys/ortho60 cannonkeys/ortho60 - 2aca4dd4234e0dac828f5fa15ae5b3bc3c0605dc * ChibiOS conf upgrade for cannonkeys/ortho75 cannonkeys/ortho75 - 4af8aaaded0ff53e9c01473f5968cc169f268647 * ChibiOS conf upgrade for cannonkeys/practice60 cannonkeys/practice60 - e01df551d9483eeb3f825fcc453317fe65f674b5 * ChibiOS conf upgrade for cannonkeys/practice65 cannonkeys/practice65 - 156163789d4c96cdd4787895788c1d02a2367f5c * ChibiOS conf upgrade for cannonkeys/rekt1800 cannonkeys/rekt1800 - 15882afb6373dea3e5f7ed13c19744ee2463f3bf * ChibiOS conf upgrade for cannonkeys/sagittarius cannonkeys/sagittarius - f49fee611ffcc02cb3dc7f249fc8c83f136907b9 * ChibiOS conf upgrade for cannonkeys/satisfaction75 cannonkeys/satisfaction75 - 2c9fbbea3a38820774f6ff436ef51017b545475a * ChibiOS conf upgrade for cannonkeys/savage65 cannonkeys/savage65 - ddd764be363c46f3e9b1a16151bd02d8f66b2dd4 * ChibiOS conf upgrade for cannonkeys/tmov2 cannonkeys/tmov2 - 7af36cd2a42015838b77697624e90008ffa72778 * ChibiOS conf upgrade for cannonkeys/tsukuyomi cannonkeys/tsukuyomi - a120bea5dacb2cd6f143458cece46d2860ea196d * ChibiOS conf upgrade for cheshire/curiosity cheshire/curiosity - 93d8fe3b7a8e028d4b015941ee71b50afe86bc8a * ChibiOS conf upgrade for coarse/cordillera coarse/cordillera - 7d986d3f534786f302cbf74a5e6b7e51ffe60093 * ChibiOS conf upgrade for converter/siemens_tastatur converter/siemens_tastatur - 3f79f1467bbc56f284aceafd76ab5c6a1cbb68f4 * ChibiOS conf upgrade for cutie_club/borsdorf cutie_club/borsdorf - f8c6015b7aacfd5edef4f22f719eea30660005e7 * ChibiOS conf upgrade for dekunukem/duckypad dekunukem/duckypad - d9c162d0867d3925cca2e653a3a3870eac2d23d2 * ChibiOS conf upgrade for ebastler/isometria_75/rev1 ebastler/isometria_75/rev1 - 2ecdd00b8c6cc794a7014747185b88374d5766c0 * ChibiOS conf upgrade for ergodox_infinity ergodox_infinity - 76736c701db22e890764481c25bd38badf32a1cf * ChibiOS conf upgrade for ergodox_stm32 ergodox_stm32 - a7bdb0b7822617ca0f9d316a082874ac0fef5964 * ChibiOS conf upgrade for function96 function96 - 1b9e394a86dab24b85d160afe9281b5e7652afe2 * ChibiOS conf upgrade for geekboards/macropad_v2 geekboards/macropad_v2 - de261e46a42cc7b7ff793e26200669f94b1dbb18 * ChibiOS conf upgrade for generic_panda/panda65_01 generic_panda/panda65_01 - 592d2443033875e0309506aa8cb5212389054458 * ChibiOS conf upgrade for handwired/ck4x4 handwired/ck4x4 - 9d7d4af7f2d787afd638e976334cd37c490040d7 * ChibiOS conf upgrade for handwired/onekey/blackpill_f401 handwired/onekey/blackpill_f401 - c9257a30203b3a0e5aa18b35e07281fcd043ab5d * ChibiOS conf upgrade for handwired/onekey/blackpill_f411 handwired/onekey/blackpill_f411 - dfccc2c6b245cfb561faba60c9f3fc7af8c3db30 * ChibiOS conf upgrade for handwired/onekey/bluepill handwired/onekey/bluepill - 393f4b94e9a0fbc9cd00de1acde346ad72250b4a * ChibiOS conf upgrade for handwired/onekey/stm32f0_disco handwired/onekey/stm32f0_disco - d827881f9bed09fd41c7c4e9d066e96b6643a099 * ChibiOS conf upgrade for handwired/onekey/teensy_32 handwired/onekey/teensy_32 - dd90f6c823da80ba43afa41249a2bdbbab98b04c * ChibiOS conf upgrade for handwired/onekey/teensy_lc handwired/onekey/teensy_lc - 34596f2eeb62403cce532d20cf893ebd77fe2d05 * ChibiOS conf upgrade for handwired/pill60/blackpill_f401 handwired/pill60/blackpill_f401 - 20bbccb5058085076357f4fa956eede2c0af86e8 * ChibiOS conf upgrade for handwired/pill60/blackpill_f411 handwired/pill60/blackpill_f411 - e03a4c3ec3b9ba65b8067126a039a8eb2cd10e51 * ChibiOS conf upgrade for handwired/pill60/bluepill handwired/pill60/bluepill - 2fb721fb4376cde2067eed1c0c8acbf3bf5851c3 * ChibiOS conf upgrade for handwired/riblee_f401 handwired/riblee_f401 - 2823cd98d64c91b146dae343a32176d51772952b * ChibiOS conf upgrade for handwired/riblee_f411 handwired/riblee_f411 - 5cf7589c9a11c4ed14e7d28676ac836fbb07d013 * ChibiOS conf upgrade for handwired/sono1 handwired/sono1 - d972acee03efb4917fc42f7d72c0a416b67ebd4a * ChibiOS conf upgrade for handwired/t111 handwired/t111 - f947c1d59025fe04e7d7b999d80e20277be4366c * ChibiOS conf upgrade for handwired/twadlee/tp69 handwired/twadlee/tp69 - 48ab62464ba1f2651f4468cbec7c6058e3b8c158 * ChibiOS conf upgrade for handwired/z150 handwired/z150 - 35e85696845e965d7f2dcdb372ed20c17d42c2bb * ChibiOS conf upgrade for hub20 hub20 - f094036a182169b5a73f89f09f2ccaa3e5fc9e7e * ChibiOS conf upgrade for iron180 iron180 - 5efb6f21c848b3c964795d9debddceac7628933d * ChibiOS conf upgrade for jm60 jm60 - dbbdbe8b27b3c1d0a25981b2f75728d163d5d299 * ChibiOS conf upgrade for keebio/bdn9/rev2 keebio/bdn9/rev2 - 0031632523dddfc8f7b3a02cf9c5990ebbb3856d * ChibiOS conf upgrade for keebio/choconum keebio/choconum - 4484ef6936a497610ca5ae4d378bc07b2bc1b1c7 * ChibiOS conf upgrade for matrix/m20add matrix/m20add - 6068aeff5b14c26de8838180f4397800abe9f1aa * ChibiOS conf upgrade for matrix/noah matrix/noah - fcde175fb1d3f5bf0716ac5a880c2cc9ab4bbf7f * ChibiOS conf upgrade for misterknife/knife66 misterknife/knife66 - e7f3ceffb94fb8e680da2af27fa7cb1a0a52b699 * ChibiOS conf upgrade for misterknife/knife66_iso misterknife/knife66_iso - ee027db939c098f908f70f02ffa9cc3952139f70 * ChibiOS conf upgrade for nebula12 nebula12 - 2f3a25b6675dcd0fabe75e7bb63038d3cac19610 * ChibiOS conf upgrade for nemui nemui - 4030faa372512f766fd747876cb28176302453f9 * ChibiOS conf upgrade for nibiria/stream15 nibiria/stream15 - fd7b09affa208403099ef9bff1b756fac0d4f918 * ChibiOS conf upgrade for nightingale_studios/hailey nightingale_studios/hailey - 496a44b2f789cc59a6110c7c1c41d57e59c722be * ChibiOS conf upgrade for planck/rev6 planck/rev6 - 6fb8f0bc21b4ab774ce3d339fdf6f25d96059f10 * ChibiOS conf upgrade for primekb/meridian primekb/meridian - 8021ed99e8bdf92faab806780186cc924dd59e50 * ChibiOS conf upgrade for projectkb/alice projectkb/alice - ad7678d475a14c54f28b62d1c1b15f5b4c4448bc * ChibiOS conf upgrade for projectkb/signature87 projectkb/signature87 - de61338b0a5c477f39c000df8117dd3017c2643d * ChibiOS conf upgrade for ramonimbao/squishy65 ramonimbao/squishy65 - 2ea1a8b18c39d0e193bbbf5836191874d3aafc53 * ChibiOS conf upgrade for ramonimbao/wete ramonimbao/wete - a80b3b309f06566adcdf20234d81c1d5aa599b00 * ChibiOS conf upgrade for rart/rartlice rart/rartlice - c8c80c302428d62bb8b08e3185509a233a94f5f0 * ChibiOS conf upgrade for retro_75 retro_75 - 7434e266bba43ded8ca1ff75d77faa506473ca91 * ChibiOS conf upgrade for rocketboard_16 rocketboard_16 - 1182a8730a84b58eba5a470286b6db6134d36f54 * ChibiOS conf upgrade for satt/vision satt/vision - c85a28a8d7e321511a17a7e7084d0a7876d1779a * ChibiOS conf upgrade for technika technika - 7c59de348f0ec5f6ac9e260806a87aaf16fbafa2 * ChibiOS conf upgrade for tkc/godspeed75 tkc/godspeed75 - e447b39dca393736fbf289049699d5b4748bb8c6 * ChibiOS conf upgrade for tkw/stoutgat/v2/f411 tkw/stoutgat/v2/f411 - 58e9b909379ad107affbff0508e3923264b75426 * ChibiOS conf upgrade for viktus/styrka viktus/styrka - 36bf0921be79a8b037fdc25343584574a312be60 * ChibiOS conf upgrade for westfoxtrot/prophet westfoxtrot/prophet - 80036c8f986c8d3261d6b0748d2057eccab6430b * ChibiOS conf upgrade for whitefox whitefox - c9eccac0196c3e2e2ea6c90d3036283a3eabff2c * ChibiOS conf upgrade for wolfmarkclub/wm1 wolfmarkclub/wm1 - 0d22a426255b829a7805bd6a3ff927198b1e31b4 * ChibiOS conf upgrade for xelus/kangaroo xelus/kangaroo - fdb1ffb66f4841f662e968929f058d4fd403c97e * ChibiOS conf upgrade for xelus/trinityxttkl xelus/trinityxttkl - a9641e5b39b7bb38351b9109f0f381837b2e8e79 * ChibiOS conf upgrade for xelus/valor_frl_tkl xelus/valor_frl_tkl - 6c3b577852736a68727ec319c30712b3088e65cd * ChibiOS conf upgrade for xiaomi/mk02 xiaomi/mk02 - 4c1406ebfee73551a844ef7ab29fc5788d7e875c * ChibiOS conf upgrade for zoo/wampus zoo/wampus - 930c903e85478e220a235f45593c03512c66bc2a * ChibiOS conf upgrade for zvecr/split_blackpill zvecr/split_blackpill - c0e3e1583262e4cb0ebfe2efa6356ed6c5c957ca --- lib/python/qmk/cli/chibios/confmigrate.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'lib/python/qmk/cli') diff --git a/lib/python/qmk/cli/chibios/confmigrate.py b/lib/python/qmk/cli/chibios/confmigrate.py index 9d25488586..3e348b2b07 100644 --- a/lib/python/qmk/cli/chibios/confmigrate.py +++ b/lib/python/qmk/cli/chibios/confmigrate.py @@ -111,6 +111,7 @@ def migrate_mcuconf_h(to_override, outfile): @cli.argument('-r', '--reference', type=normpath, arg_only=True, help='Specify the reference file to compare against') @cli.argument('-o', '--overwrite', arg_only=True, action='store_true', help='Overwrites the input file during migration.') @cli.argument('-d', '--delete', arg_only=True, action='store_true', help='If the file has no overrides, migration will delete the input file.') +@cli.argument('-f', '--force', arg_only=True, action='store_true', help='Re-migrates an already migrated file, even if it doesn\'t detect a full ChibiOS config.') @cli.subcommand('Generates a migrated ChibiOS configuration file, as a result of comparing the input against a reference') def chibios_confmigrate(cli): """Generates a usable ChibiOS replacement configuration file, based on a fully-defined conf and a reference config. @@ -142,19 +143,19 @@ def chibios_confmigrate(cli): eprint('--------------------------------------') - if "CHCONF_H" in input_defs["dict"] or "_CHCONF_H_" in input_defs["dict"]: + if cli.args.input.name == "chconf.h" and ("CHCONF_H" in input_defs["dict"] or "_CHCONF_H_" in input_defs["dict"] or cli.args.force): migrate_chconf_h(to_override, outfile=sys.stdout) if cli.args.overwrite: with open(cli.args.input, "w") as out_file: migrate_chconf_h(to_override, outfile=out_file) - elif "HALCONF_H" in input_defs["dict"] or "_HALCONF_H_" in input_defs["dict"]: + elif cli.args.input.name == "halconf.h" and ("HALCONF_H" in input_defs["dict"] or "_HALCONF_H_" in input_defs["dict"] or cli.args.force): migrate_halconf_h(to_override, outfile=sys.stdout) if cli.args.overwrite: with open(cli.args.input, "w") as out_file: migrate_halconf_h(to_override, outfile=out_file) - elif "MCUCONF_H" in input_defs["dict"] or "_MCUCONF_H_" in input_defs["dict"]: + elif cli.args.input.name == "mcuconf.h" and ("MCUCONF_H" in input_defs["dict"] or "_MCUCONF_H_" in input_defs["dict"] or cli.args.force): migrate_mcuconf_h(to_override, outfile=sys.stdout) if cli.args.overwrite: with open(cli.args.input, "w") as out_file: -- cgit v1.2.3 From ef6329af7c7be77b537fbfc5a5cc7105acc679f7 Mon Sep 17 00:00:00 2001 From: Zach White Date: Sun, 31 Jan 2021 12:46:00 -0800 Subject: Create a system to map between info.json and config.h/rules.mk (#11548) * generate rules.mk from a json mapping * generate rules.mk from a json mapping * support for config.h from json maps * improve the mapping system * document the mapping system * move data/maps to data/mappings * fix flake8 errors * fixup LED_MATRIX_DRIVER * remove product and description from the vision_division keymap level * reduce the complexity of generate-rules-mk * add tests for the generate commands * fix qmk doctor when submodules are not clean --- lib/python/qmk/cli/doctor.py | 4 +- lib/python/qmk/cli/generate/config_h.py | 245 ++++++++------------------------ lib/python/qmk/cli/generate/rules_mk.py | 61 ++++---- 3 files changed, 99 insertions(+), 211 deletions(-) (limited to 'lib/python/qmk/cli') diff --git a/lib/python/qmk/cli/doctor.py b/lib/python/qmk/cli/doctor.py index 70f32911a4..28d480707c 100755 --- a/lib/python/qmk/cli/doctor.py +++ b/lib/python/qmk/cli/doctor.py @@ -107,9 +107,9 @@ def doctor(cli): submodules.update() sub_ok = check_submodules() - if CheckStatus.ERROR in sub_ok: + if sub_ok == CheckStatus.ERROR: status = CheckStatus.ERROR - elif CheckStatus.WARNING in sub_ok and status == CheckStatus.OK: + elif sub_ok == CheckStatus.WARNING and status == CheckStatus.OK: status = CheckStatus.WARNING # Report a summary of our findings to the user diff --git a/lib/python/qmk/cli/generate/config_h.py b/lib/python/qmk/cli/generate/config_h.py index 1de84de7a9..7ddad745d1 100755 --- a/lib/python/qmk/cli/generate/config_h.py +++ b/lib/python/qmk/cli/generate/config_h.py @@ -1,62 +1,14 @@ """Used by the make system to generate info_config.h from info.json. """ +from pathlib import Path + +from dotty_dict import dotty from milc import cli -from qmk.constants import LED_INDICATORS from qmk.decorators import automagic_keyboard, automagic_keymap -from qmk.info import info_json, rgblight_animations, rgblight_properties, rgblight_toggles +from qmk.info import _json_load, info_json from qmk.path import is_keyboard, normpath -usb_prop_map = { - 'vid': 'VENDOR_ID', - 'pid': 'PRODUCT_ID', - 'device_ver': 'DEVICE_VER', -} - - -def debounce(debounce): - """Return the config.h lines that set debounce - """ - return """ -#ifndef DEBOUNCE -# define DEBOUNCE %s -#endif // DEBOUNCE -""" % debounce - - -def diode_direction(diode_direction): - """Return the config.h lines that set diode direction - """ - return """ -#ifndef DIODE_DIRECTION -# define DIODE_DIRECTION %s -#endif // DIODE_DIRECTION -""" % diode_direction - - -def keyboard_name(keyboard_name): - """Return the config.h lines that set the keyboard's name. - """ - return """ -#ifndef DESCRIPTION -# define DESCRIPTION %s -#endif // DESCRIPTION - -#ifndef PRODUCT -# define PRODUCT %s -#endif // PRODUCT -""" % (keyboard_name.replace("'", ""), keyboard_name.replace("'", "")) - - -def manufacturer(manufacturer): - """Return the config.h lines that set the manufacturer. - """ - return """ -#ifndef MANUFACTURER -# define MANUFACTURER %s -#endif // MANUFACTURER -""" % (manufacturer.replace("'", "")) - def direct_pins(direct_pins): """Return the config.h lines that set the direct pins. @@ -72,80 +24,34 @@ def direct_pins(direct_pins): return """ #ifndef MATRIX_COLS -# define MATRIX_COLS %s +# define MATRIX_COLS %s #endif // MATRIX_COLS #ifndef MATRIX_ROWS -# define MATRIX_ROWS %s +# define MATRIX_ROWS %s #endif // MATRIX_ROWS #ifndef DIRECT_PINS -# define DIRECT_PINS {%s} +# define DIRECT_PINS {%s} #endif // DIRECT_PINS """ % (col_count, row_count, ','.join(rows)) -def col_pins(col_pins): - """Return the config.h lines that set the column pins. - """ - cols = ','.join(map(str, [pin or 'NO_PIN' for pin in col_pins])) - col_num = len(col_pins) - - return """ -#ifndef MATRIX_COLS -# define MATRIX_COLS %s -#endif // MATRIX_COLS - -#ifndef MATRIX_COL_PINS -# define MATRIX_COL_PINS {%s} -#endif // MATRIX_COL_PINS -""" % (col_num, cols) - - -def row_pins(row_pins): - """Return the config.h lines that set the row pins. - """ - rows = ','.join(map(str, [pin or 'NO_PIN' for pin in row_pins])) - row_num = len(row_pins) - - return """ -#ifndef MATRIX_ROWS -# define MATRIX_ROWS %s -#endif // MATRIX_ROWS - -#ifndef MATRIX_ROW_PINS -# define MATRIX_ROW_PINS {%s} -#endif // MATRIX_ROW_PINS -""" % (row_num, rows) - - -def indicators(config): - """Return the config.h lines that setup LED indicators. +def pin_array(define, pins): + """Return the config.h lines that set a pin array. """ - defines = [] + pin_num = len(pins) + pin_array = ', '.join(map(str, [pin or 'NO_PIN' for pin in pins])) - for led, define in LED_INDICATORS.items(): - if led in config: - defines.append('') - defines.append('#ifndef %s' % (define,)) - defines.append('# define %s %s' % (define, config[led])) - defines.append('#endif // %s' % (define,)) + return f""" +#ifndef {define}S +# define {define}S {pin_num} +#endif // {define}S - return '\n'.join(defines) - - -def layout_aliases(layout_aliases): - """Return the config.h lines that setup layout aliases. - """ - defines = [] - - for alias, layout in layout_aliases.items(): - defines.append('') - defines.append('#ifndef %s' % (alias,)) - defines.append('# define %s %s' % (alias, layout)) - defines.append('#endif // %s' % (alias,)) - - return '\n'.join(defines) +#ifndef {define}_PINS +# define {define}_PINS {{ {pin_array} }} +#endif // {define}_PINS +""" def matrix_pins(matrix_pins): @@ -157,58 +63,14 @@ def matrix_pins(matrix_pins): pins.append(direct_pins(matrix_pins['direct'])) if 'cols' in matrix_pins: - pins.append(col_pins(matrix_pins['cols'])) + pins.append(pin_array('MATRIX_COL', matrix_pins['cols'])) if 'rows' in matrix_pins: - pins.append(row_pins(matrix_pins['rows'])) + pins.append(pin_array('MATRIX_ROW', matrix_pins['rows'])) return '\n'.join(pins) -def rgblight(config): - """Return the config.h lines that setup rgblight. - """ - rgblight_config = [] - - for json_key, config_key in rgblight_properties.items(): - if json_key in config: - rgblight_config.append('') - rgblight_config.append('#ifndef %s' % (config_key[0],)) - rgblight_config.append('# define %s %s' % (config_key[0], config[json_key])) - rgblight_config.append('#endif // %s' % (config_key[0],)) - - for json_key, config_key in rgblight_toggles.items(): - if config.get(json_key): - rgblight_config.append('') - rgblight_config.append('#ifndef %s' % (config_key,)) - rgblight_config.append('# define %s' % (config_key,)) - rgblight_config.append('#endif // %s' % (config_key,)) - - for json_key, config_key in rgblight_animations.items(): - if 'animations' in config and config['animations'].get(json_key): - rgblight_config.append('') - rgblight_config.append('#ifndef %s' % (config_key,)) - rgblight_config.append('# define %s' % (config_key,)) - rgblight_config.append('#endif // %s' % (config_key,)) - - return '\n'.join(rgblight_config) - - -def usb_properties(usb_props): - """Return the config.h lines that setup USB params. - """ - usb_lines = [] - - for info_name, config_name in usb_prop_map.items(): - if info_name in usb_props: - usb_lines.append('') - usb_lines.append('#ifndef ' + config_name) - usb_lines.append('# define %s %s' % (config_name, usb_props[info_name])) - usb_lines.append('#endif // ' + config_name) - - return '\n'.join(usb_lines) - - @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") @cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.') @@ -228,39 +90,52 @@ def generate_config_h(cli): cli.log.error('Invalid keyboard: "%s"', cli.config.generate_config_h.keyboard) return False - # Build the info.json file - kb_info_json = info_json(cli.config.generate_config_h.keyboard) - # Build the info_config.h file. - config_h_lines = ['/* This file was generated by `qmk generate-config-h`. Do not edit or copy.' ' */', '', '#pragma once'] + kb_info_json = dotty(info_json(cli.config.generate_config_h.keyboard)) + info_config_map = _json_load(Path('data/mappings/info_config.json')) - if 'debounce' in kb_info_json: - config_h_lines.append(debounce(kb_info_json['debounce'])) - - if 'diode_direction' in kb_info_json: - config_h_lines.append(diode_direction(kb_info_json['diode_direction'])) - - if 'indicators' in kb_info_json: - config_h_lines.append(indicators(kb_info_json['indicators'])) - - if 'keyboard_name' in kb_info_json: - config_h_lines.append(keyboard_name(kb_info_json['keyboard_name'])) - - if 'layout_aliases' in kb_info_json: - config_h_lines.append(layout_aliases(kb_info_json['layout_aliases'])) - - if 'manufacturer' in kb_info_json: - config_h_lines.append(manufacturer(kb_info_json['manufacturer'])) + config_h_lines = ['/* This file was generated by `qmk generate-config-h`. Do not edit or copy.' ' */', '', '#pragma once'] - if 'rgblight' in kb_info_json: - config_h_lines.append(rgblight(kb_info_json['rgblight'])) + # Iterate through the info_config map to generate basic things + for config_key, info_dict in info_config_map.items(): + info_key = info_dict['info_key'] + key_type = info_dict.get('value_type', 'str') + to_config = info_dict.get('to_config', True) + + if not to_config: + continue + + try: + config_value = kb_info_json[info_key] + except KeyError: + continue + + if key_type.startswith('array'): + config_h_lines.append('') + config_h_lines.append(f'#ifndef {config_key}') + config_h_lines.append(f'# define {config_key} {{ {", ".join(map(str, config_value))} }}') + config_h_lines.append(f'#endif // {config_key}') + elif key_type == 'bool': + if config_value: + config_h_lines.append('') + config_h_lines.append(f'#ifndef {config_key}') + config_h_lines.append(f'# define {config_key}') + config_h_lines.append(f'#endif // {config_key}') + elif key_type == 'mapping': + for key, value in config_value.items(): + config_h_lines.append('') + config_h_lines.append(f'#ifndef {key}') + config_h_lines.append(f'# define {key} {value}') + config_h_lines.append(f'#endif // {key}') + else: + config_h_lines.append('') + config_h_lines.append(f'#ifndef {config_key}') + config_h_lines.append(f'# define {config_key} {config_value}') + config_h_lines.append(f'#endif // {config_key}') if 'matrix_pins' in kb_info_json: config_h_lines.append(matrix_pins(kb_info_json['matrix_pins'])) - if 'usb' in kb_info_json: - config_h_lines.append(usb_properties(kb_info_json['usb'])) - # Show the results config_h = '\n'.join(config_h_lines) diff --git a/lib/python/qmk/cli/generate/rules_mk.py b/lib/python/qmk/cli/generate/rules_mk.py index b262e3c666..af740f341d 100755 --- a/lib/python/qmk/cli/generate/rules_mk.py +++ b/lib/python/qmk/cli/generate/rules_mk.py @@ -1,16 +1,37 @@ """Used by the make system to generate a rules.mk """ +from pathlib import Path + +from dotty_dict import dotty from milc import cli from qmk.decorators import automagic_keyboard, automagic_keymap -from qmk.info import info_json +from qmk.info import _json_load, info_json from qmk.path import is_keyboard, normpath -info_to_rules = { - 'board': 'BOARD', - 'bootloader': 'BOOTLOADER', - 'processor': 'MCU', -} + +def process_mapping_rule(kb_info_json, rules_key, info_dict): + """Return the rules.mk line(s) for a mapping rule. + """ + if not info_dict.get('to_c', True): + return None + + info_key = info_dict['info_key'] + key_type = info_dict.get('value_type', 'str') + + try: + rules_value = kb_info_json[info_key] + except KeyError: + return None + + if key_type == 'array': + return f'{rules_key} ?= {" ".join(rules_value)}' + elif key_type == 'bool': + return f'{rules_key} ?= {"on" if rules_value else "off"}' + elif key_type == 'mapping': + return '\n'.join([f'{key} ?= {value}' for key, value in rules_value.items()]) + + return f'{rules_key} ?= {rules_value}' @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') @@ -22,7 +43,6 @@ info_to_rules = { def generate_rules_mk(cli): """Generates a rules.mk file from info.json. """ - # Determine our keyboard(s) if not cli.config.generate_rules_mk.keyboard: cli.log.error('Missing paramater: --keyboard') cli.subcommands['info'].print_help() @@ -32,16 +52,18 @@ def generate_rules_mk(cli): cli.log.error('Invalid keyboard: "%s"', cli.config.generate_rules_mk.keyboard) return False - # Build the info.json file - kb_info_json = info_json(cli.config.generate_rules_mk.keyboard) + kb_info_json = dotty(info_json(cli.config.generate_rules_mk.keyboard)) + info_rules_map = _json_load(Path('data/mappings/info_rules.json')) rules_mk_lines = ['# This file was generated by `qmk generate-rules-mk`. Do not edit or copy.', ''] - # Bring in settings - for info_key, rule_key in info_to_rules.items(): - if info_key in kb_info_json: - rules_mk_lines.append(f'{rule_key} ?= {kb_info_json[info_key]}') + # Iterate through the info_rules map to generate basic rules + for rules_key, info_dict in info_rules_map.items(): + new_entry = process_mapping_rule(kb_info_json, rules_key, info_dict) + + if new_entry: + rules_mk_lines.append(new_entry) - # Find features that should be enabled + # Iterate through features to enable/disable them if 'features' in kb_info_json: for feature, enabled in kb_info_json['features'].items(): if feature == 'bootmagic_lite' and enabled: @@ -51,15 +73,6 @@ def generate_rules_mk(cli): enabled = 'yes' if enabled else 'no' rules_mk_lines.append(f'{feature}_ENABLE ?= {enabled}') - # Set the LED driver - if 'led_matrix' in kb_info_json and 'driver' in kb_info_json['led_matrix']: - driver = kb_info_json['led_matrix']['driver'] - rules_mk_lines.append(f'LED_MATRIX_DRIVER ?= {driver}') - - # Add community layouts - if 'community_layouts' in kb_info_json: - rules_mk_lines.append(f'LAYOUTS ?= {" ".join(kb_info_json["community_layouts"])}') - # Show the results rules_mk = '\n'.join(rules_mk_lines) + '\n' @@ -72,7 +85,7 @@ def generate_rules_mk(cli): if cli.args.quiet: print(cli.args.output) else: - cli.log.info('Wrote info_config.h to %s.', cli.args.output) + cli.log.info('Wrote rules.mk to %s.', cli.args.output) else: print(rules_mk) -- cgit v1.2.3 From 448cc1acd5f107f9679a48c605861654523432e7 Mon Sep 17 00:00:00 2001 From: Zach White Date: Mon, 8 Feb 2021 03:03:08 -0800 Subject: remove deprecated qmk json-keymap (#11823) --- lib/python/qmk/cli/__init__.py | 1 - lib/python/qmk/cli/json/__init__.py | 5 ----- lib/python/qmk/cli/json/keymap.py | 16 ---------------- 3 files changed, 22 deletions(-) delete mode 100644 lib/python/qmk/cli/json/__init__.py delete mode 100755 lib/python/qmk/cli/json/keymap.py (limited to 'lib/python/qmk/cli') diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index 372c40921a..1732287da0 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py @@ -19,7 +19,6 @@ from . import flash from . import generate from . import hello from . import info -from . import json from . import json2c from . import lint from . import list diff --git a/lib/python/qmk/cli/json/__init__.py b/lib/python/qmk/cli/json/__init__.py deleted file mode 100644 index f4ebfc45b4..0000000000 --- a/lib/python/qmk/cli/json/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -"""QMK CLI JSON Subcommands - -We list each subcommand here explicitly because all the reliable ways of searching for modules are slow and delay startup. -""" -from . import keymap diff --git a/lib/python/qmk/cli/json/keymap.py b/lib/python/qmk/cli/json/keymap.py deleted file mode 100755 index 2af9faaa72..0000000000 --- a/lib/python/qmk/cli/json/keymap.py +++ /dev/null @@ -1,16 +0,0 @@ -"""Generate a keymap.c from a configurator export. -""" -from pathlib import Path - -from milc import cli - - -@cli.argument('-o', '--output', arg_only=True, type=Path, help='File to write to') -@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") -@cli.argument('filename', arg_only=True, help='Configurator JSON file') -@cli.subcommand('Creates a keymap.c from a QMK Configurator export.', hidden=True) -def json_keymap(cli): - """Renamed to `qmk json2c`. - """ - cli.log.error('This command has been renamed to `qmk json2c`.') - return False -- cgit v1.2.3 From cd336b2b545798269405e8ffef0fe8958d4d27d4 Mon Sep 17 00:00:00 2001 From: Zach White Date: Sat, 13 Feb 2021 10:26:45 -0800 Subject: bump to python 3.7 (#11408) --- lib/python/qmk/cli/__init__.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'lib/python/qmk/cli') diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index 1732287da0..a5f1f47679 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py @@ -27,6 +27,24 @@ from . import new from . import pyformat from . import pytest -if sys.version_info[0] != 3 or sys.version_info[1] < 6: - cli.log.error('Your Python is too old! Please upgrade to Python 3.6 or later.') +# Supported version information +# +# Based on the OSes we support these are the minimum python version available by default. +# Last update: 2021 Jan 02 +# +# Arch: 3.9 +# Debian: 3.7 +# Fedora 31: 3.7 +# Fedora 32: 3.8 +# Fedora 33: 3.9 +# FreeBSD: 3.7 +# Gentoo: 3.7 +# macOS: 3.9 (from homebrew) +# msys2: 3.8 +# Slackware: 3.7 +# solus: 3.7 +# void: 3.9 + +if sys.version_info[0] != 3 or sys.version_info[1] < 7: + cli.log.error('Your Python is too old! Please upgrade to Python 3.7 or later.') exit(127) -- cgit v1.2.3 From 1581ea48dcd48d0d3f42cc09b388c468aedec45d Mon Sep 17 00:00:00 2001 From: Zach White Date: Sat, 27 Feb 2021 12:00:50 -0800 Subject: Fix develop (#12039) Fixes file encoding errors on Windows, and layouts not correctly merging into info.json. * force utf8 encoding * correctly merge layouts and layout aliases * show what aliases point to --- lib/python/qmk/cli/chibios/confmigrate.py | 8 ++++---- lib/python/qmk/cli/generate/layouts.py | 4 ++++ lib/python/qmk/cli/info.py | 7 +++++-- lib/python/qmk/cli/kle2json.py | 2 +- 4 files changed, 14 insertions(+), 7 deletions(-) (limited to 'lib/python/qmk/cli') diff --git a/lib/python/qmk/cli/chibios/confmigrate.py b/lib/python/qmk/cli/chibios/confmigrate.py index 3e348b2b07..89995931a4 100644 --- a/lib/python/qmk/cli/chibios/confmigrate.py +++ b/lib/python/qmk/cli/chibios/confmigrate.py @@ -40,7 +40,7 @@ file_header = """\ def collect_defines(filepath): - with open(filepath, 'r') as f: + with open(filepath, 'r', encoding='utf-8') as f: content = f.read() define_search = re.compile(r'(?m)^#\s*define\s+(?:.*\\\r?\n)*.*$', re.MULTILINE) value_search = re.compile(r'^#\s*define\s+(?P[a-zA-Z0-9_]+(\([^\)]*\))?)\s*(?P.*)', re.DOTALL) @@ -146,17 +146,17 @@ def chibios_confmigrate(cli): if cli.args.input.name == "chconf.h" and ("CHCONF_H" in input_defs["dict"] or "_CHCONF_H_" in input_defs["dict"] or cli.args.force): migrate_chconf_h(to_override, outfile=sys.stdout) if cli.args.overwrite: - with open(cli.args.input, "w") as out_file: + with open(cli.args.input, "w", encoding='utf-8') as out_file: migrate_chconf_h(to_override, outfile=out_file) elif cli.args.input.name == "halconf.h" and ("HALCONF_H" in input_defs["dict"] or "_HALCONF_H_" in input_defs["dict"] or cli.args.force): migrate_halconf_h(to_override, outfile=sys.stdout) if cli.args.overwrite: - with open(cli.args.input, "w") as out_file: + with open(cli.args.input, "w", encoding='utf-8') as out_file: migrate_halconf_h(to_override, outfile=out_file) elif cli.args.input.name == "mcuconf.h" and ("MCUCONF_H" in input_defs["dict"] or "_MCUCONF_H_" in input_defs["dict"] or cli.args.force): migrate_mcuconf_h(to_override, outfile=sys.stdout) if cli.args.overwrite: - with open(cli.args.input, "w") as out_file: + with open(cli.args.input, "w", encoding='utf-8') as out_file: migrate_mcuconf_h(to_override, outfile=out_file) diff --git a/lib/python/qmk/cli/generate/layouts.py b/lib/python/qmk/cli/generate/layouts.py index b7baae0651..15b289522e 100755 --- a/lib/python/qmk/cli/generate/layouts.py +++ b/lib/python/qmk/cli/generate/layouts.py @@ -82,6 +82,10 @@ def generate_layouts(cli): layouts_h_lines.append(rows) layouts_h_lines.append('}') + for alias, target in kb_info_json.get('layout_aliases', {}).items(): + layouts_h_lines.append('') + layouts_h_lines.append('#define %s %s' % (alias, target)) + # Show the results layouts_h = '\n'.join(layouts_h_lines) + '\n' diff --git a/lib/python/qmk/cli/info.py b/lib/python/qmk/cli/info.py index 87d7253d4b..a7ce8abf03 100755 --- a/lib/python/qmk/cli/info.py +++ b/lib/python/qmk/cli/info.py @@ -29,7 +29,7 @@ def show_keymap(kb_info_json, title_caps=True): else: cli.echo('{fg_blue}keymap_%s{fg_reset}:', cli.config.info.keymap) - keymap_data = json.load(keymap_path.open()) + keymap_data = json.load(keymap_path.open(encoding='utf-8')) layout_name = keymap_data['layout'] for layer_num, layer in enumerate(keymap_data['layers']): @@ -57,7 +57,7 @@ def show_matrix(kb_info_json, title_caps=True): # Build our label list labels = [] for key in layout['layout']: - if key['matrix']: + if 'matrix' in key: row = ROW_LETTERS[key['matrix'][0]] col = COL_LETTERS[key['matrix'][1]] @@ -91,6 +91,9 @@ def print_friendly_output(kb_info_json): cli.echo('{fg_blue}Size{fg_reset}: %s x %s' % (kb_info_json['width'], kb_info_json['height'])) cli.echo('{fg_blue}Processor{fg_reset}: %s', kb_info_json.get('processor', 'Unknown')) cli.echo('{fg_blue}Bootloader{fg_reset}: %s', kb_info_json.get('bootloader', 'Unknown')) + if 'layout_aliases' in kb_info_json: + aliases = [f'{key}={value}' for key, value in kb_info_json['layout_aliases'].items()] + cli.echo('{fg_blue}Layout aliases:{fg_reset} %s' % (', '.join(aliases),)) if cli.config.info.layouts: show_layouts(kb_info_json, True) diff --git a/lib/python/qmk/cli/kle2json.py b/lib/python/qmk/cli/kle2json.py index 66d504bfc2..3bb7443582 100755 --- a/lib/python/qmk/cli/kle2json.py +++ b/lib/python/qmk/cli/kle2json.py @@ -27,7 +27,7 @@ def kle2json(cli): cli.log.error('File {fg_cyan}%s{style_reset_all} was not found.', file_path) return False out_path = file_path.parent - raw_code = file_path.open().read() + raw_code = file_path.read_text(encoding='utf-8') # Check if info.json exists, allow overwrite with force if Path(out_path, "info.json").exists() and not cli.args.force: cli.log.error('File {fg_cyan}%s/info.json{style_reset_all} already exists, use -f or --force to overwrite.', out_path) -- cgit v1.2.3