diff options
Diffstat (limited to 'lib/python/qmk/cli')
| -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 | 277 | ||||
| -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 | 78 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/info.py | 7 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/kle2json.py | 51 |
10 files changed, 558 insertions, 48 deletions
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..1de84de7a --- /dev/null +++ b/lib/python/qmk/cli/generate/config_h.py | |||
| @@ -0,0 +1,277 @@ | |||
| 1 | """Used by the make system to generate info_config.h from info.json. | ||
| 2 | """ | ||
| 3 | from milc import cli | ||
| 4 | |||
| 5 | from qmk.constants import LED_INDICATORS | ||
| 6 | from qmk.decorators import automagic_keyboard, automagic_keymap | ||
| 7 | from qmk.info import info_json, rgblight_animations, rgblight_properties, rgblight_toggles | ||
| 8 | from qmk.path import is_keyboard, normpath | ||
| 9 | |||
| 10 | usb_prop_map = { | ||
| 11 | 'vid': 'VENDOR_ID', | ||
| 12 | 'pid': 'PRODUCT_ID', | ||
| 13 | 'device_ver': 'DEVICE_VER', | ||
| 14 | } | ||
| 15 | |||
| 16 | |||
| 17 | def debounce(debounce): | ||
| 18 | """Return the config.h lines that set debounce | ||
| 19 | """ | ||
| 20 | return """ | ||
| 21 | #ifndef DEBOUNCE | ||
| 22 | # define DEBOUNCE %s | ||
| 23 | #endif // DEBOUNCE | ||
| 24 | """ % debounce | ||
| 25 | |||
| 26 | |||
| 27 | def diode_direction(diode_direction): | ||
| 28 | """Return the config.h lines that set diode direction | ||
| 29 | """ | ||
| 30 | return """ | ||
| 31 | #ifndef DIODE_DIRECTION | ||
| 32 | # define DIODE_DIRECTION %s | ||
| 33 | #endif // DIODE_DIRECTION | ||
| 34 | """ % diode_direction | ||
| 35 | |||
| 36 | |||
| 37 | def keyboard_name(keyboard_name): | ||
| 38 | """Return the config.h lines that set the keyboard's name. | ||
| 39 | """ | ||
| 40 | return """ | ||
| 41 | #ifndef DESCRIPTION | ||
| 42 | # define DESCRIPTION %s | ||
| 43 | #endif // DESCRIPTION | ||
| 44 | |||
| 45 | #ifndef PRODUCT | ||
| 46 | # define PRODUCT %s | ||
| 47 | #endif // PRODUCT | ||
| 48 | """ % (keyboard_name.replace("'", ""), keyboard_name.replace("'", "")) | ||
| 49 | |||
| 50 | |||
| 51 | def manufacturer(manufacturer): | ||
| 52 | """Return the config.h lines that set the manufacturer. | ||
| 53 | """ | ||
| 54 | return """ | ||
| 55 | #ifndef MANUFACTURER | ||
| 56 | # define MANUFACTURER %s | ||
| 57 | #endif // MANUFACTURER | ||
| 58 | """ % (manufacturer.replace("'", "")) | ||
| 59 | |||
| 60 | |||
| 61 | def direct_pins(direct_pins): | ||
| 62 | """Return the config.h lines that set the direct pins. | ||
| 63 | """ | ||
| 64 | rows = [] | ||
| 65 | |||
| 66 | for row in direct_pins: | ||
| 67 | cols = ','.join(map(str, [col or 'NO_PIN' for col in row])) | ||
| 68 | rows.append('{' + cols + '}') | ||
| 69 | |||
| 70 | col_count = len(direct_pins[0]) | ||
| 71 | row_count = len(direct_pins) | ||
| 72 | |||
| 73 | return """ | ||
| 74 | #ifndef MATRIX_COLS | ||
| 75 | # define MATRIX_COLS %s | ||
| 76 | #endif // MATRIX_COLS | ||
| 77 | |||
| 78 | #ifndef MATRIX_ROWS | ||
| 79 | # define MATRIX_ROWS %s | ||
| 80 | #endif // MATRIX_ROWS | ||
| 81 | |||
| 82 | #ifndef DIRECT_PINS | ||
| 83 | # define DIRECT_PINS {%s} | ||
| 84 | #endif // DIRECT_PINS | ||
| 85 | """ % (col_count, row_count, ','.join(rows)) | ||
| 86 | |||
| 87 | |||
| 88 | def col_pins(col_pins): | ||
| 89 | """Return the config.h lines that set the column pins. | ||
| 90 | """ | ||
| 91 | cols = ','.join(map(str, [pin or 'NO_PIN' for pin in col_pins])) | ||
| 92 | col_num = len(col_pins) | ||
| 93 | |||
| 94 | return """ | ||
| 95 | #ifndef MATRIX_COLS | ||
| 96 | # define MATRIX_COLS %s | ||
| 97 | #endif // MATRIX_COLS | ||
| 98 | |||
| 99 | #ifndef MATRIX_COL_PINS | ||
| 100 | # define MATRIX_COL_PINS {%s} | ||
| 101 | #endif // MATRIX_COL_PINS | ||
| 102 | """ % (col_num, cols) | ||
| 103 | |||
| 104 | |||
| 105 | def row_pins(row_pins): | ||
| 106 | """Return the config.h lines that set the row pins. | ||
| 107 | """ | ||
| 108 | rows = ','.join(map(str, [pin or 'NO_PIN' for pin in row_pins])) | ||
| 109 | row_num = len(row_pins) | ||
| 110 | |||
| 111 | return """ | ||
| 112 | #ifndef MATRIX_ROWS | ||
| 113 | # define MATRIX_ROWS %s | ||
| 114 | #endif // MATRIX_ROWS | ||
| 115 | |||
| 116 | #ifndef MATRIX_ROW_PINS | ||
| 117 | # define MATRIX_ROW_PINS {%s} | ||
| 118 | #endif // MATRIX_ROW_PINS | ||
| 119 | """ % (row_num, rows) | ||
| 120 | |||
| 121 | |||
| 122 | def indicators(config): | ||
| 123 | """Return the config.h lines that setup LED indicators. | ||
| 124 | """ | ||
| 125 | defines = [] | ||
| 126 | |||
| 127 | for led, define in LED_INDICATORS.items(): | ||
| 128 | if led in config: | ||
| 129 | defines.append('') | ||
| 130 | defines.append('#ifndef %s' % (define,)) | ||
| 131 | defines.append('# define %s %s' % (define, config[led])) | ||
| 132 | defines.append('#endif // %s' % (define,)) | ||
| 133 | |||
| 134 | return '\n'.join(defines) | ||
| 135 | |||
| 136 | |||
| 137 | def layout_aliases(layout_aliases): | ||
| 138 | """Return the config.h lines that setup layout aliases. | ||
| 139 | """ | ||
| 140 | defines = [] | ||
| 141 | |||
| 142 | for alias, layout in layout_aliases.items(): | ||
| 143 | defines.append('') | ||
| 144 | defines.append('#ifndef %s' % (alias,)) | ||
| 145 | defines.append('# define %s %s' % (alias, layout)) | ||
| 146 | defines.append('#endif // %s' % (alias,)) | ||
| 147 | |||
| 148 | return '\n'.join(defines) | ||
| 149 | |||
| 150 | |||
| 151 | def matrix_pins(matrix_pins): | ||
| 152 | """Add the matrix config to the config.h. | ||
| 153 | """ | ||
| 154 | pins = [] | ||
| 155 | |||
| 156 | if 'direct' in matrix_pins: | ||
| 157 | pins.append(direct_pins(matrix_pins['direct'])) | ||
| 158 | |||
| 159 | if 'cols' in matrix_pins: | ||
| 160 | pins.append(col_pins(matrix_pins['cols'])) | ||
| 161 | |||
| 162 | if 'rows' in matrix_pins: | ||
| 163 | pins.append(row_pins(matrix_pins['rows'])) | ||
| 164 | |||
| 165 | return '\n'.join(pins) | ||
| 166 | |||
| 167 | |||
| 168 | def rgblight(config): | ||
| 169 | """Return the config.h lines that setup rgblight. | ||
| 170 | """ | ||
| 171 | rgblight_config = [] | ||
| 172 | |||
| 173 | for json_key, config_key in rgblight_properties.items(): | ||
| 174 | if json_key in config: | ||
| 175 | rgblight_config.append('') | ||
| 176 | rgblight_config.append('#ifndef %s' % (config_key[0],)) | ||
| 177 | rgblight_config.append('# define %s %s' % (config_key[0], config[json_key])) | ||
| 178 | rgblight_config.append('#endif // %s' % (config_key[0],)) | ||
| 179 | |||
| 180 | for json_key, config_key in rgblight_toggles.items(): | ||
| 181 | if config.get(json_key): | ||
| 182 | rgblight_config.append('') | ||
| 183 | rgblight_config.append('#ifndef %s' % (config_key,)) | ||
| 184 | rgblight_config.append('# define %s' % (config_key,)) | ||
| 185 | rgblight_config.append('#endif // %s' % (config_key,)) | ||
| 186 | |||
| 187 | for json_key, config_key in rgblight_animations.items(): | ||
| 188 | if 'animations' in config and config['animations'].get(json_key): | ||
| 189 | rgblight_config.append('') | ||
| 190 | rgblight_config.append('#ifndef %s' % (config_key,)) | ||
| 191 | rgblight_config.append('# define %s' % (config_key,)) | ||
| 192 | rgblight_config.append('#endif // %s' % (config_key,)) | ||
| 193 | |||
| 194 | return '\n'.join(rgblight_config) | ||
| 195 | |||
| 196 | |||
| 197 | def usb_properties(usb_props): | ||
| 198 | """Return the config.h lines that setup USB params. | ||
| 199 | """ | ||
| 200 | usb_lines = [] | ||
| 201 | |||
| 202 | for info_name, config_name in usb_prop_map.items(): | ||
| 203 | if info_name in usb_props: | ||
| 204 | usb_lines.append('') | ||
| 205 | usb_lines.append('#ifndef ' + config_name) | ||
| 206 | usb_lines.append('# define %s %s' % (config_name, usb_props[info_name])) | ||
| 207 | usb_lines.append('#endif // ' + config_name) | ||
| 208 | |||
| 209 | return '\n'.join(usb_lines) | ||
| 210 | |||
| 211 | |||
| 212 | @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') | ||
| 213 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") | ||
| 214 | @cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.') | ||
| 215 | @cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True) | ||
| 216 | @automagic_keyboard | ||
| 217 | @automagic_keymap | ||
| 218 | def generate_config_h(cli): | ||
| 219 | """Generates the info_config.h file. | ||
| 220 | """ | ||
| 221 | # Determine our keyboard(s) | ||
| 222 | if not cli.config.generate_config_h.keyboard: | ||
| 223 | cli.log.error('Missing paramater: --keyboard') | ||
| 224 | cli.subcommands['info'].print_help() | ||
| 225 | return False | ||
| 226 | |||
| 227 | if not is_keyboard(cli.config.generate_config_h.keyboard): | ||
| 228 | cli.log.error('Invalid keyboard: "%s"', cli.config.generate_config_h.keyboard) | ||
| 229 | return False | ||
| 230 | |||
| 231 | # Build the info.json file | ||
| 232 | kb_info_json = info_json(cli.config.generate_config_h.keyboard) | ||
| 233 | |||
| 234 | # Build the info_config.h file. | ||
| 235 | config_h_lines = ['/* This file was generated by `qmk generate-config-h`. Do not edit or copy.' ' */', '', '#pragma once'] | ||
| 236 | |||
| 237 | if 'debounce' in kb_info_json: | ||
| 238 | config_h_lines.append(debounce(kb_info_json['debounce'])) | ||
| 239 | |||
| 240 | if 'diode_direction' in kb_info_json: | ||
| 241 | config_h_lines.append(diode_direction(kb_info_json['diode_direction'])) | ||
| 242 | |||
| 243 | if 'indicators' in kb_info_json: | ||
| 244 | config_h_lines.append(indicators(kb_info_json['indicators'])) | ||
| 245 | |||
| 246 | if 'keyboard_name' in kb_info_json: | ||
| 247 | config_h_lines.append(keyboard_name(kb_info_json['keyboard_name'])) | ||
| 248 | |||
| 249 | if 'layout_aliases' in kb_info_json: | ||
| 250 | config_h_lines.append(layout_aliases(kb_info_json['layout_aliases'])) | ||
| 251 | |||
| 252 | if 'manufacturer' in kb_info_json: | ||
| 253 | config_h_lines.append(manufacturer(kb_info_json['manufacturer'])) | ||
| 254 | |||
| 255 | if 'rgblight' in kb_info_json: | ||
| 256 | config_h_lines.append(rgblight(kb_info_json['rgblight'])) | ||
| 257 | |||
| 258 | if 'matrix_pins' in kb_info_json: | ||
| 259 | config_h_lines.append(matrix_pins(kb_info_json['matrix_pins'])) | ||
| 260 | |||
| 261 | if 'usb' in kb_info_json: | ||
| 262 | config_h_lines.append(usb_properties(kb_info_json['usb'])) | ||
| 263 | |||
| 264 | # Show the results | ||
| 265 | config_h = '\n'.join(config_h_lines) | ||
| 266 | |||
| 267 | if cli.args.output: | ||
| 268 | cli.args.output.parent.mkdir(parents=True, exist_ok=True) | ||
| 269 | if cli.args.output.exists(): | ||
| 270 | cli.args.output.replace(cli.args.output.name + '.bak') | ||
| 271 | cli.args.output.write_text(config_h) | ||
| 272 | |||
| 273 | if not cli.args.quiet: | ||
| 274 | cli.log.info('Wrote info_config.h to %s.', cli.args.output) | ||
| 275 | |||
| 276 | else: | ||
| 277 | 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..b262e3c66 --- /dev/null +++ b/lib/python/qmk/cli/generate/rules_mk.py | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | """Used by the make system to generate a rules.mk | ||
| 2 | """ | ||
| 3 | from milc import cli | ||
| 4 | |||
| 5 | from qmk.decorators import automagic_keyboard, automagic_keymap | ||
| 6 | from qmk.info import info_json | ||
| 7 | from qmk.path import is_keyboard, normpath | ||
| 8 | |||
| 9 | info_to_rules = { | ||
| 10 | 'board': 'BOARD', | ||
| 11 | 'bootloader': 'BOOTLOADER', | ||
| 12 | 'processor': 'MCU', | ||
| 13 | } | ||
| 14 | |||
| 15 | |||
| 16 | @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') | ||
| 17 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") | ||
| 18 | @cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.') | ||
| 19 | @cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True) | ||
| 20 | @automagic_keyboard | ||
| 21 | @automagic_keymap | ||
| 22 | def generate_rules_mk(cli): | ||
| 23 | """Generates a rules.mk file from info.json. | ||
| 24 | """ | ||
| 25 | # Determine our keyboard(s) | ||
| 26 | if not cli.config.generate_rules_mk.keyboard: | ||
| 27 | cli.log.error('Missing paramater: --keyboard') | ||
| 28 | cli.subcommands['info'].print_help() | ||
| 29 | return False | ||
| 30 | |||
| 31 | if not is_keyboard(cli.config.generate_rules_mk.keyboard): | ||
| 32 | cli.log.error('Invalid keyboard: "%s"', cli.config.generate_rules_mk.keyboard) | ||
| 33 | return False | ||
| 34 | |||
| 35 | # Build the info.json file | ||
| 36 | kb_info_json = info_json(cli.config.generate_rules_mk.keyboard) | ||
| 37 | rules_mk_lines = ['# This file was generated by `qmk generate-rules-mk`. Do not edit or copy.', ''] | ||
| 38 | |||
| 39 | # Bring in settings | ||
| 40 | for info_key, rule_key in info_to_rules.items(): | ||
| 41 | if info_key in kb_info_json: | ||
| 42 | rules_mk_lines.append(f'{rule_key} ?= {kb_info_json[info_key]}') | ||
| 43 | |||
| 44 | # Find features that should be enabled | ||
| 45 | if 'features' in kb_info_json: | ||
| 46 | for feature, enabled in kb_info_json['features'].items(): | ||
| 47 | if feature == 'bootmagic_lite' and enabled: | ||
| 48 | rules_mk_lines.append('BOOTMAGIC_ENABLE ?= lite') | ||
| 49 | else: | ||
| 50 | feature = feature.upper() | ||
| 51 | enabled = 'yes' if enabled else 'no' | ||
| 52 | rules_mk_lines.append(f'{feature}_ENABLE ?= {enabled}') | ||
| 53 | |||
| 54 | # Set the LED driver | ||
| 55 | if 'led_matrix' in kb_info_json and 'driver' in kb_info_json['led_matrix']: | ||
| 56 | driver = kb_info_json['led_matrix']['driver'] | ||
| 57 | rules_mk_lines.append(f'LED_MATRIX_DRIVER ?= {driver}') | ||
| 58 | |||
| 59 | # Add community layouts | ||
| 60 | if 'community_layouts' in kb_info_json: | ||
| 61 | rules_mk_lines.append(f'LAYOUTS ?= {" ".join(kb_info_json["community_layouts"])}') | ||
| 62 | |||
| 63 | # Show the results | ||
| 64 | rules_mk = '\n'.join(rules_mk_lines) + '\n' | ||
| 65 | |||
| 66 | if cli.args.output: | ||
| 67 | cli.args.output.parent.mkdir(parents=True, exist_ok=True) | ||
| 68 | if cli.args.output.exists(): | ||
| 69 | cli.args.output.replace(cli.args.output.name + '.bak') | ||
| 70 | cli.args.output.write_text(rules_mk) | ||
| 71 | |||
| 72 | if cli.args.quiet: | ||
| 73 | print(cli.args.output) | ||
| 74 | else: | ||
| 75 | cli.log.info('Wrote info_config.h to %s.', cli.args.output) | ||
| 76 | |||
| 77 | else: | ||
| 78 | 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/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) |
