diff options
Diffstat (limited to 'lib/python/qmk/cli')
| -rw-r--r-- | lib/python/qmk/cli/__init__.py | 23 | ||||
| -rw-r--r-- | lib/python/qmk/cli/c2json.py | 3 | ||||
| -rw-r--r-- | lib/python/qmk/cli/chibios/confmigrate.py | 9 | ||||
| -rw-r--r-- | lib/python/qmk/cli/generate/__init__.py | 4 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/generate/api.py | 14 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/generate/config_h.py | 152 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/generate/info_json.py | 65 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/generate/layouts.py | 98 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/generate/rules_mk.py | 91 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/info.py | 7 | ||||
| -rw-r--r-- | lib/python/qmk/cli/json/__init__.py | 5 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/json/keymap.py | 16 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/kle2json.py | 51 |
13 files changed, 466 insertions, 72 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index 372c40921..a5f1f4767 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py | |||
| @@ -19,7 +19,6 @@ from . import flash | |||
| 19 | from . import generate | 19 | from . import generate |
| 20 | from . import hello | 20 | from . import hello |
| 21 | from . import info | 21 | from . import info |
| 22 | from . import json | ||
| 23 | from . import json2c | 22 | from . import json2c |
| 24 | from . import lint | 23 | from . import lint |
| 25 | from . import list | 24 | from . import list |
| @@ -28,6 +27,24 @@ from . import new | |||
| 28 | from . import pyformat | 27 | from . import pyformat |
| 29 | from . import pytest | 28 | from . import pytest |
| 30 | 29 | ||
| 31 | if sys.version_info[0] != 3 or sys.version_info[1] < 6: | 30 | # Supported version information |
| 32 | cli.log.error('Your Python is too old! Please upgrade to Python 3.6 or later.') | 31 | # |
| 32 | # Based on the OSes we support these are the minimum python version available by default. | ||
| 33 | # Last update: 2021 Jan 02 | ||
| 34 | # | ||
| 35 | # Arch: 3.9 | ||
| 36 | # Debian: 3.7 | ||
| 37 | # Fedora 31: 3.7 | ||
| 38 | # Fedora 32: 3.8 | ||
| 39 | # Fedora 33: 3.9 | ||
| 40 | # FreeBSD: 3.7 | ||
| 41 | # Gentoo: 3.7 | ||
| 42 | # macOS: 3.9 (from homebrew) | ||
| 43 | # msys2: 3.8 | ||
| 44 | # Slackware: 3.7 | ||
| 45 | # solus: 3.7 | ||
| 46 | # void: 3.9 | ||
| 47 | |||
| 48 | if sys.version_info[0] != 3 or sys.version_info[1] < 7: | ||
| 49 | cli.log.error('Your Python is too old! Please upgrade to Python 3.7 or later.') | ||
| 33 | exit(127) | 50 | exit(127) |
diff --git a/lib/python/qmk/cli/c2json.py b/lib/python/qmk/cli/c2json.py index 2b3bb774f..8f9d8dc38 100644 --- a/lib/python/qmk/cli/c2json.py +++ b/lib/python/qmk/cli/c2json.py | |||
| @@ -6,6 +6,7 @@ from milc import cli | |||
| 6 | 6 | ||
| 7 | import qmk.keymap | 7 | import qmk.keymap |
| 8 | import qmk.path | 8 | import qmk.path |
| 9 | from qmk.info_json_encoder import InfoJSONEncoder | ||
| 9 | 10 | ||
| 10 | 11 | ||
| 11 | @cli.argument('--no-cpp', arg_only=True, action='store_false', help='Do not use \'cpp\' on keymap.c') | 12 | @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): | |||
| 47 | cli.args.output.parent.mkdir(parents=True, exist_ok=True) | 48 | cli.args.output.parent.mkdir(parents=True, exist_ok=True) |
| 48 | if cli.args.output.exists(): | 49 | if cli.args.output.exists(): |
| 49 | cli.args.output.replace(cli.args.output.name + '.bak') | 50 | cli.args.output.replace(cli.args.output.name + '.bak') |
| 50 | cli.args.output.write_text(json.dumps(keymap_json)) | 51 | cli.args.output.write_text(json.dumps(keymap_json, cls=InfoJSONEncoder)) |
| 51 | 52 | ||
| 52 | if not cli.args.quiet: | 53 | if not cli.args.quiet: |
| 53 | cli.log.info('Wrote keymap to %s.', cli.args.output) | 54 | 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 b9cfda961..3e348b2b0 100644 --- a/lib/python/qmk/cli/chibios/confmigrate.py +++ b/lib/python/qmk/cli/chibios/confmigrate.py | |||
| @@ -32,7 +32,7 @@ file_header = """\ | |||
| 32 | 32 | ||
| 33 | /* | 33 | /* |
| 34 | * This file was auto-generated by: | 34 | * This file was auto-generated by: |
| 35 | * `qmk chibios-confupdate -i {0} -r {1}` | 35 | * `qmk chibios-confmigrate -i {0} -r {1}` |
| 36 | */ | 36 | */ |
| 37 | 37 | ||
| 38 | #pragma once | 38 | #pragma once |
| @@ -111,6 +111,7 @@ def migrate_mcuconf_h(to_override, outfile): | |||
| 111 | @cli.argument('-r', '--reference', type=normpath, arg_only=True, help='Specify the reference file to compare against') | 111 | @cli.argument('-r', '--reference', type=normpath, arg_only=True, help='Specify the reference file to compare against') |
| 112 | @cli.argument('-o', '--overwrite', arg_only=True, action='store_true', help='Overwrites the input file during migration.') | 112 | @cli.argument('-o', '--overwrite', arg_only=True, action='store_true', help='Overwrites the input file during migration.') |
| 113 | @cli.argument('-d', '--delete', arg_only=True, action='store_true', help='If the file has no overrides, migration will delete the input file.') | 113 | @cli.argument('-d', '--delete', arg_only=True, action='store_true', help='If the file has no overrides, migration will delete the input file.') |
| 114 | @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.') | ||
| 114 | @cli.subcommand('Generates a migrated ChibiOS configuration file, as a result of comparing the input against a reference') | 115 | @cli.subcommand('Generates a migrated ChibiOS configuration file, as a result of comparing the input against a reference') |
| 115 | def chibios_confmigrate(cli): | 116 | def chibios_confmigrate(cli): |
| 116 | """Generates a usable ChibiOS replacement configuration file, based on a fully-defined conf and a reference config. | 117 | """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): | |||
| 142 | 143 | ||
| 143 | eprint('--------------------------------------') | 144 | eprint('--------------------------------------') |
| 144 | 145 | ||
| 145 | if "CHCONF_H" in input_defs["dict"] or "_CHCONF_H_" in input_defs["dict"]: | 146 | 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): |
| 146 | migrate_chconf_h(to_override, outfile=sys.stdout) | 147 | migrate_chconf_h(to_override, outfile=sys.stdout) |
| 147 | if cli.args.overwrite: | 148 | if cli.args.overwrite: |
| 148 | with open(cli.args.input, "w") as out_file: | 149 | with open(cli.args.input, "w") as out_file: |
| 149 | migrate_chconf_h(to_override, outfile=out_file) | 150 | migrate_chconf_h(to_override, outfile=out_file) |
| 150 | 151 | ||
| 151 | elif "HALCONF_H" in input_defs["dict"] or "_HALCONF_H_" in input_defs["dict"]: | 152 | 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): |
| 152 | migrate_halconf_h(to_override, outfile=sys.stdout) | 153 | migrate_halconf_h(to_override, outfile=sys.stdout) |
| 153 | if cli.args.overwrite: | 154 | if cli.args.overwrite: |
| 154 | with open(cli.args.input, "w") as out_file: | 155 | with open(cli.args.input, "w") as out_file: |
| 155 | migrate_halconf_h(to_override, outfile=out_file) | 156 | migrate_halconf_h(to_override, outfile=out_file) |
| 156 | 157 | ||
| 157 | elif "MCUCONF_H" in input_defs["dict"] or "_MCUCONF_H_" in input_defs["dict"]: | 158 | 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): |
| 158 | migrate_mcuconf_h(to_override, outfile=sys.stdout) | 159 | migrate_mcuconf_h(to_override, outfile=sys.stdout) |
| 159 | if cli.args.overwrite: | 160 | if cli.args.overwrite: |
| 160 | with open(cli.args.input, "w") as out_file: | 161 | with open(cli.args.input, "w") as out_file: |
diff --git a/lib/python/qmk/cli/generate/__init__.py b/lib/python/qmk/cli/generate/__init__.py index f9585bfb5..bd75b044c 100644 --- a/lib/python/qmk/cli/generate/__init__.py +++ b/lib/python/qmk/cli/generate/__init__.py | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | from . import api | 1 | from . import api |
| 2 | from . import config_h | ||
| 2 | from . import docs | 3 | from . import docs |
| 4 | from . import info_json | ||
| 5 | from . import layouts | ||
| 3 | from . import rgb_breathe_table | 6 | from . import rgb_breathe_table |
| 7 | from . import rules_mk | ||
diff --git a/lib/python/qmk/cli/generate/api.py b/lib/python/qmk/cli/generate/api.py index 66db37cb5..6d111f244 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 | |||
| 8 | 8 | ||
| 9 | from qmk.datetime import current_datetime | 9 | from qmk.datetime import current_datetime |
| 10 | from qmk.info import info_json | 10 | from qmk.info import info_json |
| 11 | from qmk.info_json_encoder import InfoJSONEncoder | ||
| 11 | from qmk.keyboard import list_keyboards | 12 | from qmk.keyboard import list_keyboards |
| 12 | 13 | ||
| 13 | 14 | ||
| @@ -44,15 +45,16 @@ def generate_api(cli): | |||
| 44 | if 'usb' in kb_all['keyboards'][keyboard_name]: | 45 | if 'usb' in kb_all['keyboards'][keyboard_name]: |
| 45 | usb = kb_all['keyboards'][keyboard_name]['usb'] | 46 | usb = kb_all['keyboards'][keyboard_name]['usb'] |
| 46 | 47 | ||
| 47 | if usb['vid'] not in usb_list['devices']: | 48 | if 'vid' in usb and usb['vid'] not in usb_list['devices']: |
| 48 | usb_list['devices'][usb['vid']] = {} | 49 | usb_list['devices'][usb['vid']] = {} |
| 49 | 50 | ||
| 50 | if usb['pid'] not in usb_list['devices'][usb['vid']]: | 51 | if 'pid' in usb and usb['pid'] not in usb_list['devices'][usb['vid']]: |
| 51 | usb_list['devices'][usb['vid']][usb['pid']] = {} | 52 | usb_list['devices'][usb['vid']][usb['pid']] = {} |
| 52 | 53 | ||
| 53 | usb_list['devices'][usb['vid']][usb['pid']][keyboard_name] = usb | 54 | if 'vid' in usb and 'pid' in usb: |
| 55 | usb_list['devices'][usb['vid']][usb['pid']][keyboard_name] = usb | ||
| 54 | 56 | ||
| 55 | # Write the global JSON files | 57 | # Write the global JSON files |
| 56 | keyboard_list.write_text(json.dumps({'last_updated': current_datetime(), 'keyboards': sorted(kb_all['keyboards'])})) | 58 | keyboard_list.write_text(json.dumps({'last_updated': current_datetime(), 'keyboards': sorted(kb_all['keyboards'])}, cls=InfoJSONEncoder)) |
| 57 | keyboard_all.write_text(json.dumps(kb_all)) | 59 | keyboard_all.write_text(json.dumps(kb_all, cls=InfoJSONEncoder)) |
| 58 | usb_file.write_text(json.dumps(usb_list)) | 60 | 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 000000000..7ddad745d --- /dev/null +++ b/lib/python/qmk/cli/generate/config_h.py | |||
| @@ -0,0 +1,152 @@ | |||
| 1 | """Used by the make system to generate info_config.h from info.json. | ||
| 2 | """ | ||
| 3 | from pathlib import Path | ||
| 4 | |||
| 5 | from dotty_dict import dotty | ||
| 6 | from milc import cli | ||
| 7 | |||
| 8 | from qmk.decorators import automagic_keyboard, automagic_keymap | ||
| 9 | from qmk.info import _json_load, info_json | ||
| 10 | from qmk.path import is_keyboard, normpath | ||
| 11 | |||
| 12 | |||
| 13 | def direct_pins(direct_pins): | ||
| 14 | """Return the config.h lines that set the direct pins. | ||
| 15 | """ | ||
| 16 | rows = [] | ||
| 17 | |||
| 18 | for row in direct_pins: | ||
| 19 | cols = ','.join(map(str, [col or 'NO_PIN' for col in row])) | ||
| 20 | rows.append('{' + cols + '}') | ||
| 21 | |||
| 22 | col_count = len(direct_pins[0]) | ||
| 23 | row_count = len(direct_pins) | ||
| 24 | |||
| 25 | return """ | ||
| 26 | #ifndef MATRIX_COLS | ||
| 27 | # define MATRIX_COLS %s | ||
| 28 | #endif // MATRIX_COLS | ||
| 29 | |||
| 30 | #ifndef MATRIX_ROWS | ||
| 31 | # define MATRIX_ROWS %s | ||
| 32 | #endif // MATRIX_ROWS | ||
| 33 | |||
| 34 | #ifndef DIRECT_PINS | ||
| 35 | # define DIRECT_PINS {%s} | ||
| 36 | #endif // DIRECT_PINS | ||
| 37 | """ % (col_count, row_count, ','.join(rows)) | ||
| 38 | |||
| 39 | |||
| 40 | def pin_array(define, pins): | ||
| 41 | """Return the config.h lines that set a pin array. | ||
| 42 | """ | ||
| 43 | pin_num = len(pins) | ||
| 44 | pin_array = ', '.join(map(str, [pin or 'NO_PIN' for pin in pins])) | ||
| 45 | |||
| 46 | return f""" | ||
| 47 | #ifndef {define}S | ||
| 48 | # define {define}S {pin_num} | ||
| 49 | #endif // {define}S | ||
| 50 | |||
| 51 | #ifndef {define}_PINS | ||
| 52 | # define {define}_PINS {{ {pin_array} }} | ||
| 53 | #endif // {define}_PINS | ||
| 54 | """ | ||
| 55 | |||
| 56 | |||
| 57 | def matrix_pins(matrix_pins): | ||
| 58 | """Add the matrix config to the config.h. | ||
| 59 | """ | ||
| 60 | pins = [] | ||
| 61 | |||
| 62 | if 'direct' in matrix_pins: | ||
| 63 | pins.append(direct_pins(matrix_pins['direct'])) | ||
| 64 | |||
| 65 | if 'cols' in matrix_pins: | ||
| 66 | pins.append(pin_array('MATRIX_COL', matrix_pins['cols'])) | ||
| 67 | |||
| 68 | if 'rows' in matrix_pins: | ||
| 69 | pins.append(pin_array('MATRIX_ROW', matrix_pins['rows'])) | ||
| 70 | |||
| 71 | return '\n'.join(pins) | ||
| 72 | |||
| 73 | |||
| 74 | @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') | ||
| 75 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") | ||
| 76 | @cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.') | ||
| 77 | @cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True) | ||
| 78 | @automagic_keyboard | ||
| 79 | @automagic_keymap | ||
| 80 | def generate_config_h(cli): | ||
| 81 | """Generates the info_config.h file. | ||
| 82 | """ | ||
| 83 | # Determine our keyboard(s) | ||
| 84 | if not cli.config.generate_config_h.keyboard: | ||
| 85 | cli.log.error('Missing paramater: --keyboard') | ||
| 86 | cli.subcommands['info'].print_help() | ||
| 87 | return False | ||
| 88 | |||
| 89 | if not is_keyboard(cli.config.generate_config_h.keyboard): | ||
| 90 | cli.log.error('Invalid keyboard: "%s"', cli.config.generate_config_h.keyboard) | ||
| 91 | return False | ||
| 92 | |||
| 93 | # Build the info_config.h file. | ||
| 94 | kb_info_json = dotty(info_json(cli.config.generate_config_h.keyboard)) | ||
| 95 | info_config_map = _json_load(Path('data/mappings/info_config.json')) | ||
| 96 | |||
| 97 | config_h_lines = ['/* This file was generated by `qmk generate-config-h`. Do not edit or copy.' ' */', '', '#pragma once'] | ||
| 98 | |||
| 99 | # Iterate through the info_config map to generate basic things | ||
| 100 | for config_key, info_dict in info_config_map.items(): | ||
| 101 | info_key = info_dict['info_key'] | ||
| 102 | key_type = info_dict.get('value_type', 'str') | ||
| 103 | to_config = info_dict.get('to_config', True) | ||
| 104 | |||
| 105 | if not to_config: | ||
| 106 | continue | ||
| 107 | |||
| 108 | try: | ||
| 109 | config_value = kb_info_json[info_key] | ||
| 110 | except KeyError: | ||
| 111 | continue | ||
| 112 | |||
| 113 | if key_type.startswith('array'): | ||
| 114 | config_h_lines.append('') | ||
| 115 | config_h_lines.append(f'#ifndef {config_key}') | ||
| 116 | config_h_lines.append(f'# define {config_key} {{ {", ".join(map(str, config_value))} }}') | ||
| 117 | config_h_lines.append(f'#endif // {config_key}') | ||
| 118 | elif key_type == 'bool': | ||
| 119 | if config_value: | ||
| 120 | config_h_lines.append('') | ||
| 121 | config_h_lines.append(f'#ifndef {config_key}') | ||
| 122 | config_h_lines.append(f'# define {config_key}') | ||
| 123 | config_h_lines.append(f'#endif // {config_key}') | ||
| 124 | elif key_type == 'mapping': | ||
| 125 | for key, value in config_value.items(): | ||
| 126 | config_h_lines.append('') | ||
| 127 | config_h_lines.append(f'#ifndef {key}') | ||
| 128 | config_h_lines.append(f'# define {key} {value}') | ||
| 129 | config_h_lines.append(f'#endif // {key}') | ||
| 130 | else: | ||
| 131 | config_h_lines.append('') | ||
| 132 | config_h_lines.append(f'#ifndef {config_key}') | ||
| 133 | config_h_lines.append(f'# define {config_key} {config_value}') | ||
| 134 | config_h_lines.append(f'#endif // {config_key}') | ||
| 135 | |||
| 136 | if 'matrix_pins' in kb_info_json: | ||
| 137 | config_h_lines.append(matrix_pins(kb_info_json['matrix_pins'])) | ||
| 138 | |||
| 139 | # Show the results | ||
| 140 | config_h = '\n'.join(config_h_lines) | ||
| 141 | |||
| 142 | if cli.args.output: | ||
| 143 | cli.args.output.parent.mkdir(parents=True, exist_ok=True) | ||
| 144 | if cli.args.output.exists(): | ||
| 145 | cli.args.output.replace(cli.args.output.name + '.bak') | ||
| 146 | cli.args.output.write_text(config_h) | ||
| 147 | |||
| 148 | if not cli.args.quiet: | ||
| 149 | cli.log.info('Wrote info_config.h to %s.', cli.args.output) | ||
| 150 | |||
| 151 | else: | ||
| 152 | 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 000000000..f3fc54ddc --- /dev/null +++ b/lib/python/qmk/cli/generate/info_json.py | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | """Keyboard information script. | ||
| 2 | |||
| 3 | Compile an info.json for a particular keyboard and pretty-print it. | ||
| 4 | """ | ||
| 5 | import json | ||
| 6 | |||
| 7 | from jsonschema import Draft7Validator, validators | ||
| 8 | from milc import cli | ||
| 9 | |||
| 10 | from qmk.decorators import automagic_keyboard, automagic_keymap | ||
| 11 | from qmk.info import info_json, _jsonschema | ||
| 12 | from qmk.info_json_encoder import InfoJSONEncoder | ||
| 13 | from qmk.path import is_keyboard | ||
| 14 | |||
| 15 | |||
| 16 | def pruning_validator(validator_class): | ||
| 17 | """Extends Draft7Validator to remove properties that aren't specified in the schema. | ||
| 18 | """ | ||
| 19 | validate_properties = validator_class.VALIDATORS["properties"] | ||
| 20 | |||
| 21 | def remove_additional_properties(validator, properties, instance, schema): | ||
| 22 | for prop in list(instance.keys()): | ||
| 23 | if prop not in properties: | ||
| 24 | del instance[prop] | ||
| 25 | |||
| 26 | for error in validate_properties(validator, properties, instance, schema): | ||
| 27 | yield error | ||
| 28 | |||
| 29 | return validators.extend(validator_class, {"properties": remove_additional_properties}) | ||
| 30 | |||
| 31 | |||
| 32 | def strip_info_json(kb_info_json): | ||
| 33 | """Remove the API-only properties from the info.json. | ||
| 34 | """ | ||
| 35 | pruning_draft_7_validator = pruning_validator(Draft7Validator) | ||
| 36 | schema = _jsonschema('keyboard') | ||
| 37 | validator = pruning_draft_7_validator(schema).validate | ||
| 38 | |||
| 39 | return validator(kb_info_json) | ||
| 40 | |||
| 41 | |||
| 42 | @cli.argument('-kb', '--keyboard', help='Keyboard to show info for.') | ||
| 43 | @cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.') | ||
| 44 | @cli.subcommand('Generate an info.json file for a keyboard.', hidden=False if cli.config.user.developer else True) | ||
| 45 | @automagic_keyboard | ||
| 46 | @automagic_keymap | ||
| 47 | def generate_info_json(cli): | ||
| 48 | """Generate an info.json file for a keyboard | ||
| 49 | """ | ||
| 50 | # Determine our keyboard(s) | ||
| 51 | if not cli.config.generate_info_json.keyboard: | ||
| 52 | cli.log.error('Missing parameter: --keyboard') | ||
| 53 | cli.subcommands['info'].print_help() | ||
| 54 | return False | ||
| 55 | |||
| 56 | if not is_keyboard(cli.config.generate_info_json.keyboard): | ||
| 57 | cli.log.error('Invalid keyboard: "%s"', cli.config.generate_info_json.keyboard) | ||
| 58 | return False | ||
| 59 | |||
| 60 | # Build the info.json file | ||
| 61 | kb_info_json = info_json(cli.config.generate_info_json.keyboard) | ||
| 62 | strip_info_json(kb_info_json) | ||
| 63 | |||
| 64 | # Display the results | ||
| 65 | 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 new file mode 100755 index 000000000..b7baae065 --- /dev/null +++ b/lib/python/qmk/cli/generate/layouts.py | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | """Used by the make system to generate layouts.h from info.json. | ||
| 2 | """ | ||
| 3 | from milc import cli | ||
| 4 | |||
| 5 | from qmk.constants import COL_LETTERS, ROW_LETTERS | ||
| 6 | from qmk.decorators import automagic_keyboard, automagic_keymap | ||
| 7 | from qmk.info import info_json | ||
| 8 | from qmk.path import is_keyboard, normpath | ||
| 9 | |||
| 10 | usb_properties = { | ||
| 11 | 'vid': 'VENDOR_ID', | ||
| 12 | 'pid': 'PRODUCT_ID', | ||
| 13 | 'device_ver': 'DEVICE_VER', | ||
| 14 | } | ||
| 15 | |||
| 16 | |||
| 17 | @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') | ||
| 18 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") | ||
| 19 | @cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.') | ||
| 20 | @cli.subcommand('Used by the make system to generate layouts.h from info.json', hidden=True) | ||
| 21 | @automagic_keyboard | ||
| 22 | @automagic_keymap | ||
| 23 | def generate_layouts(cli): | ||
| 24 | """Generates the layouts.h file. | ||
| 25 | """ | ||
| 26 | # Determine our keyboard(s) | ||
| 27 | if not cli.config.generate_layouts.keyboard: | ||
| 28 | cli.log.error('Missing paramater: --keyboard') | ||
| 29 | cli.subcommands['info'].print_help() | ||
| 30 | return False | ||
| 31 | |||
| 32 | if not is_keyboard(cli.config.generate_layouts.keyboard): | ||
| 33 | cli.log.error('Invalid keyboard: "%s"', cli.config.generate_layouts.keyboard) | ||
| 34 | return False | ||
| 35 | |||
| 36 | # Build the info.json file | ||
| 37 | kb_info_json = info_json(cli.config.generate_layouts.keyboard) | ||
| 38 | |||
| 39 | # Build the layouts.h file. | ||
| 40 | layouts_h_lines = ['/* This file was generated by `qmk generate-layouts`. Do not edit or copy.' ' */', '', '#pragma once'] | ||
| 41 | |||
| 42 | if 'matrix_pins' in kb_info_json: | ||
| 43 | if 'direct' in kb_info_json['matrix_pins']: | ||
| 44 | col_num = len(kb_info_json['matrix_pins']['direct'][0]) | ||
| 45 | row_num = len(kb_info_json['matrix_pins']['direct']) | ||
| 46 | elif 'cols' in kb_info_json['matrix_pins'] and 'rows' in kb_info_json['matrix_pins']: | ||
| 47 | col_num = len(kb_info_json['matrix_pins']['cols']) | ||
| 48 | row_num = len(kb_info_json['matrix_pins']['rows']) | ||
| 49 | else: | ||
| 50 | cli.log.error('%s: Invalid matrix config.', cli.config.generate_layouts.keyboard) | ||
| 51 | return False | ||
| 52 | |||
| 53 | for layout_name in kb_info_json['layouts']: | ||
| 54 | if kb_info_json['layouts'][layout_name]['c_macro']: | ||
| 55 | continue | ||
| 56 | |||
| 57 | if 'matrix' not in kb_info_json['layouts'][layout_name]['layout'][0]: | ||
| 58 | cli.log.debug('%s/%s: No matrix data!', cli.config.generate_layouts.keyboard, layout_name) | ||
| 59 | continue | ||
| 60 | |||
| 61 | layout_keys = [] | ||
| 62 | layout_matrix = [['KC_NO' for i in range(col_num)] for i in range(row_num)] | ||
| 63 | |||
| 64 | for i, key in enumerate(kb_info_json['layouts'][layout_name]['layout']): | ||
| 65 | row = key['matrix'][0] | ||
| 66 | col = key['matrix'][1] | ||
| 67 | identifier = 'k%s%s' % (ROW_LETTERS[row], COL_LETTERS[col]) | ||
| 68 | |||
| 69 | try: | ||
| 70 | layout_matrix[row][col] = identifier | ||
| 71 | layout_keys.append(identifier) | ||
| 72 | except IndexError: | ||
| 73 | key_name = key.get('label', identifier) | ||
| 74 | cli.log.error('Matrix data out of bounds for layout %s at index %s (%s): %s, %s', layout_name, i, key_name, row, col) | ||
| 75 | return False | ||
| 76 | |||
| 77 | layouts_h_lines.append('') | ||
| 78 | layouts_h_lines.append('#define %s(%s) {\\' % (layout_name, ', '.join(layout_keys))) | ||
| 79 | |||
| 80 | rows = ', \\\n'.join(['\t {' + ', '.join(row) + '}' for row in layout_matrix]) | ||
| 81 | rows += ' \\' | ||
| 82 | layouts_h_lines.append(rows) | ||
| 83 | layouts_h_lines.append('}') | ||
| 84 | |||
| 85 | # Show the results | ||
| 86 | layouts_h = '\n'.join(layouts_h_lines) + '\n' | ||
| 87 | |||
| 88 | if cli.args.output: | ||
| 89 | cli.args.output.parent.mkdir(parents=True, exist_ok=True) | ||
| 90 | if cli.args.output.exists(): | ||
| 91 | cli.args.output.replace(cli.args.output.name + '.bak') | ||
| 92 | cli.args.output.write_text(layouts_h) | ||
| 93 | |||
| 94 | if not cli.args.quiet: | ||
| 95 | cli.log.info('Wrote info_config.h to %s.', cli.args.output) | ||
| 96 | |||
| 97 | else: | ||
| 98 | 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 000000000..af740f341 --- /dev/null +++ b/lib/python/qmk/cli/generate/rules_mk.py | |||
| @@ -0,0 +1,91 @@ | |||
| 1 | """Used by the make system to generate a rules.mk | ||
| 2 | """ | ||
| 3 | from pathlib import Path | ||
| 4 | |||
| 5 | from dotty_dict import dotty | ||
| 6 | from milc import cli | ||
| 7 | |||
| 8 | from qmk.decorators import automagic_keyboard, automagic_keymap | ||
| 9 | from qmk.info import _json_load, info_json | ||
| 10 | from qmk.path import is_keyboard, normpath | ||
| 11 | |||
| 12 | |||
| 13 | def process_mapping_rule(kb_info_json, rules_key, info_dict): | ||
| 14 | """Return the rules.mk line(s) for a mapping rule. | ||
| 15 | """ | ||
| 16 | if not info_dict.get('to_c', True): | ||
| 17 | return None | ||
| 18 | |||
| 19 | info_key = info_dict['info_key'] | ||
| 20 | key_type = info_dict.get('value_type', 'str') | ||
| 21 | |||
| 22 | try: | ||
| 23 | rules_value = kb_info_json[info_key] | ||
| 24 | except KeyError: | ||
| 25 | return None | ||
| 26 | |||
| 27 | if key_type == 'array': | ||
| 28 | return f'{rules_key} ?= {" ".join(rules_value)}' | ||
| 29 | elif key_type == 'bool': | ||
| 30 | return f'{rules_key} ?= {"on" if rules_value else "off"}' | ||
| 31 | elif key_type == 'mapping': | ||
| 32 | return '\n'.join([f'{key} ?= {value}' for key, value in rules_value.items()]) | ||
| 33 | |||
| 34 | return f'{rules_key} ?= {rules_value}' | ||
| 35 | |||
| 36 | |||
| 37 | @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') | ||
| 38 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") | ||
| 39 | @cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.') | ||
| 40 | @cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True) | ||
| 41 | @automagic_keyboard | ||
| 42 | @automagic_keymap | ||
| 43 | def generate_rules_mk(cli): | ||
| 44 | """Generates a rules.mk file from info.json. | ||
| 45 | """ | ||
| 46 | if not cli.config.generate_rules_mk.keyboard: | ||
| 47 | cli.log.error('Missing paramater: --keyboard') | ||
| 48 | cli.subcommands['info'].print_help() | ||
| 49 | return False | ||
| 50 | |||
| 51 | if not is_keyboard(cli.config.generate_rules_mk.keyboard): | ||
| 52 | cli.log.error('Invalid keyboard: "%s"', cli.config.generate_rules_mk.keyboard) | ||
| 53 | return False | ||
| 54 | |||
| 55 | kb_info_json = dotty(info_json(cli.config.generate_rules_mk.keyboard)) | ||
| 56 | info_rules_map = _json_load(Path('data/mappings/info_rules.json')) | ||
| 57 | rules_mk_lines = ['# This file was generated by `qmk generate-rules-mk`. Do not edit or copy.', ''] | ||
| 58 | |||
| 59 | # Iterate through the info_rules map to generate basic rules | ||
| 60 | for rules_key, info_dict in info_rules_map.items(): | ||
| 61 | new_entry = process_mapping_rule(kb_info_json, rules_key, info_dict) | ||
| 62 | |||
| 63 | if new_entry: | ||
| 64 | rules_mk_lines.append(new_entry) | ||
| 65 | |||
| 66 | # Iterate through features to enable/disable them | ||
| 67 | if 'features' in kb_info_json: | ||
| 68 | for feature, enabled in kb_info_json['features'].items(): | ||
| 69 | if feature == 'bootmagic_lite' and enabled: | ||
| 70 | rules_mk_lines.append('BOOTMAGIC_ENABLE ?= lite') | ||
| 71 | else: | ||
| 72 | feature = feature.upper() | ||
| 73 | enabled = 'yes' if enabled else 'no' | ||
| 74 | rules_mk_lines.append(f'{feature}_ENABLE ?= {enabled}') | ||
| 75 | |||
| 76 | # Show the results | ||
| 77 | rules_mk = '\n'.join(rules_mk_lines) + '\n' | ||
| 78 | |||
| 79 | if cli.args.output: | ||
| 80 | cli.args.output.parent.mkdir(parents=True, exist_ok=True) | ||
| 81 | if cli.args.output.exists(): | ||
| 82 | cli.args.output.replace(cli.args.output.name + '.bak') | ||
| 83 | cli.args.output.write_text(rules_mk) | ||
| 84 | |||
| 85 | if cli.args.quiet: | ||
| 86 | print(cli.args.output) | ||
| 87 | else: | ||
| 88 | cli.log.info('Wrote rules.mk to %s.', cli.args.output) | ||
| 89 | |||
| 90 | else: | ||
| 91 | print(rules_mk) | ||
diff --git a/lib/python/qmk/cli/info.py b/lib/python/qmk/cli/info.py index 9ab299a21..87d7253d4 100755 --- a/lib/python/qmk/cli/info.py +++ b/lib/python/qmk/cli/info.py | |||
| @@ -7,6 +7,8 @@ import platform | |||
| 7 | 7 | ||
| 8 | from milc import cli | 8 | from milc import cli |
| 9 | 9 | ||
| 10 | from qmk.info_json_encoder import InfoJSONEncoder | ||
| 11 | from qmk.constants import COL_LETTERS, ROW_LETTERS | ||
| 10 | from qmk.decorators import automagic_keyboard, automagic_keymap | 12 | from qmk.decorators import automagic_keyboard, automagic_keymap |
| 11 | from qmk.keyboard import render_layouts, render_layout | 13 | from qmk.keyboard import render_layouts, render_layout |
| 12 | from qmk.keymap import locate_keymap | 14 | from qmk.keymap import locate_keymap |
| @@ -15,9 +17,6 @@ from qmk.path import is_keyboard | |||
| 15 | 17 | ||
| 16 | platform_id = platform.platform().lower() | 18 | platform_id = platform.platform().lower() |
| 17 | 19 | ||
| 18 | ROW_LETTERS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop' | ||
| 19 | COL_LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijilmnopqrstuvwxyz' | ||
| 20 | |||
| 21 | 20 | ||
| 22 | def show_keymap(kb_info_json, title_caps=True): | 21 | def show_keymap(kb_info_json, title_caps=True): |
| 23 | """Render the keymap in ascii art. | 22 | """Render the keymap in ascii art. |
| @@ -149,7 +148,7 @@ def info(cli): | |||
| 149 | 148 | ||
| 150 | # Output in the requested format | 149 | # Output in the requested format |
| 151 | if cli.args.format == 'json': | 150 | if cli.args.format == 'json': |
| 152 | print(json.dumps(kb_info_json)) | 151 | print(json.dumps(kb_info_json, cls=InfoJSONEncoder)) |
| 153 | elif cli.args.format == 'text': | 152 | elif cli.args.format == 'text': |
| 154 | print_text_output(kb_info_json) | 153 | print_text_output(kb_info_json) |
| 155 | elif cli.args.format == 'friendly': | 154 | elif cli.args.format == 'friendly': |
diff --git a/lib/python/qmk/cli/json/__init__.py b/lib/python/qmk/cli/json/__init__.py deleted file mode 100644 index f4ebfc45b..000000000 --- a/lib/python/qmk/cli/json/__init__.py +++ /dev/null | |||
| @@ -1,5 +0,0 @@ | |||
| 1 | """QMK CLI JSON Subcommands | ||
| 2 | |||
| 3 | We list each subcommand here explicitly because all the reliable ways of searching for modules are slow and delay startup. | ||
| 4 | """ | ||
| 5 | 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 2af9faaa7..000000000 --- a/lib/python/qmk/cli/json/keymap.py +++ /dev/null | |||
| @@ -1,16 +0,0 @@ | |||
| 1 | """Generate a keymap.c from a configurator export. | ||
| 2 | """ | ||
| 3 | from pathlib import Path | ||
| 4 | |||
| 5 | from milc import cli | ||
| 6 | |||
| 7 | |||
| 8 | @cli.argument('-o', '--output', arg_only=True, type=Path, help='File to write to') | ||
| 9 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") | ||
| 10 | @cli.argument('filename', arg_only=True, help='Configurator JSON file') | ||
| 11 | @cli.subcommand('Creates a keymap.c from a QMK Configurator export.', hidden=True) | ||
| 12 | def json_keymap(cli): | ||
| 13 | """Renamed to `qmk json2c`. | ||
| 14 | """ | ||
| 15 | cli.log.error('This command has been renamed to `qmk json2c`.') | ||
| 16 | return False | ||
diff --git a/lib/python/qmk/cli/kle2json.py b/lib/python/qmk/cli/kle2json.py index 3d1bb8c43..66d504bfc 100755 --- a/lib/python/qmk/cli/kle2json.py +++ b/lib/python/qmk/cli/kle2json.py | |||
| @@ -3,25 +3,12 @@ | |||
| 3 | import json | 3 | import json |
| 4 | import os | 4 | import os |
| 5 | from pathlib import Path | 5 | from pathlib import Path |
| 6 | from decimal import Decimal | ||
| 7 | from collections import OrderedDict | ||
| 8 | 6 | ||
| 9 | from milc import cli | 7 | from milc import cli |
| 10 | from kle2xy import KLE2xy | 8 | from kle2xy import KLE2xy |
| 11 | 9 | ||
| 12 | from qmk.converter import kle2qmk | 10 | from qmk.converter import kle2qmk |
| 13 | 11 | from qmk.info_json_encoder import InfoJSONEncoder | |
| 14 | |||
| 15 | class CustomJSONEncoder(json.JSONEncoder): | ||
| 16 | def default(self, obj): | ||
| 17 | try: | ||
| 18 | if isinstance(obj, Decimal): | ||
| 19 | if obj % 2 in (Decimal(0), Decimal(1)): | ||
| 20 | return int(obj) | ||
| 21 | return float(obj) | ||
| 22 | except TypeError: | ||
| 23 | pass | ||
| 24 | return json.JSONEncoder.default(self, obj) | ||
| 25 | 12 | ||
| 26 | 13 | ||
| 27 | @cli.argument('filename', help='The KLE raw txt to convert') | 14 | @cli.argument('filename', help='The KLE raw txt to convert') |
| @@ -52,24 +39,22 @@ def kle2json(cli): | |||
| 52 | cli.log.error('Could not parse KLE raw data: %s', raw_code) | 39 | cli.log.error('Could not parse KLE raw data: %s', raw_code) |
| 53 | cli.log.exception(e) | 40 | cli.log.exception(e) |
| 54 | return False | 41 | return False |
| 55 | keyboard = OrderedDict( | 42 | keyboard = { |
| 56 | keyboard_name=kle.name, | 43 | 'keyboard_name': kle.name, |
| 57 | url='', | 44 | 'url': '', |
| 58 | maintainer='qmk', | 45 | 'maintainer': 'qmk', |
| 59 | width=kle.columns, | 46 | 'width': kle.columns, |
| 60 | height=kle.rows, | 47 | 'height': kle.rows, |
| 61 | layouts={'LAYOUT': { | 48 | 'layouts': { |
| 62 | 'layout': 'LAYOUT_JSON_HERE' | 49 | 'LAYOUT': { |
| 63 | }}, | 50 | 'layout': kle2qmk(kle) |
| 64 | ) | 51 | } |
| 65 | # Initialize keyboard with json encoded from ordered dict | 52 | }, |
| 66 | keyboard = json.dumps(keyboard, indent=4, separators=(', ', ': '), sort_keys=False, cls=CustomJSONEncoder) | 53 | } |
| 67 | # Initialize layout with kle2qmk from converter module | 54 | |
| 68 | layout = json.dumps(kle2qmk(kle), separators=(', ', ':'), cls=CustomJSONEncoder) | ||
| 69 | # Replace layout in keyboard json | ||
| 70 | keyboard = keyboard.replace('"LAYOUT_JSON_HERE"', layout) | ||
| 71 | # Write our info.json | 55 | # Write our info.json |
| 72 | file = open(out_path / "info.json", "w") | 56 | keyboard = json.dumps(keyboard, indent=4, separators=(', ', ': '), sort_keys=False, cls=InfoJSONEncoder) |
| 73 | file.write(keyboard) | 57 | info_json_file = out_path / 'info.json' |
| 74 | file.close() | 58 | |
| 59 | info_json_file.write_text(keyboard) | ||
| 75 | cli.log.info('Wrote out {fg_cyan}%s/info.json', out_path) | 60 | cli.log.info('Wrote out {fg_cyan}%s/info.json', out_path) |
