diff options
| author | skullydazed <skullydazed@users.noreply.github.com> | 2020-03-13 15:47:04 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-13 15:47:04 -0700 |
| commit | f81b0e35a6a25a9a6e633dc65a4900bed2458cfb (patch) | |
| tree | 707e06f6cd2caeda4278cfd9751ee77bf15aa055 /lib/python/qmk/cli/flash.py | |
| parent | 5e98eaaaff8fde1ce25b9bad6c00a982718cb467 (diff) | |
| download | qmk_firmware-f81b0e35a6a25a9a6e633dc65a4900bed2458cfb.tar.gz qmk_firmware-f81b0e35a6a25a9a6e633dc65a4900bed2458cfb.zip | |
Add decorators for determining keyboard and keymap based on current directory (#8191)
* Use pathlib everywhere we can
* Improvements based on @erovia's feedback
* rework qmk compile and qmk flash to use pathlib
* style
* Remove the subcommand_name argument from find_keyboard_keymap()
* add experimental decorators
* Create decorators for finding keyboard and keymap based on current directory.
Decorators were inspired by @Erovia's brilliant work on the proof of concept.
Diffstat (limited to 'lib/python/qmk/cli/flash.py')
| -rw-r--r-- | lib/python/qmk/cli/flash.py | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/lib/python/qmk/cli/flash.py b/lib/python/qmk/cli/flash.py index f669c3cb7..f8497071e 100644 --- a/lib/python/qmk/cli/flash.py +++ b/lib/python/qmk/cli/flash.py | |||
| @@ -6,9 +6,11 @@ A bootloader must be specified. | |||
| 6 | import subprocess | 6 | import subprocess |
| 7 | from argparse import FileType | 7 | from argparse import FileType |
| 8 | 8 | ||
| 9 | import qmk.path | ||
| 10 | from milc import cli | 9 | from milc import cli |
| 11 | from qmk.commands import compile_configurator_json, create_make_command, find_keyboard_keymap, parse_configurator_json | 10 | |
| 11 | import qmk.path | ||
| 12 | from qmk.decorators import automagic_keyboard, automagic_keymap | ||
| 13 | from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json | ||
| 12 | 14 | ||
| 13 | 15 | ||
| 14 | def print_bootloader_help(): | 16 | def print_bootloader_help(): |
| @@ -28,12 +30,15 @@ def print_bootloader_help(): | |||
| 28 | cli.echo('For more info, visit https://docs.qmk.fm/#/flashing') | 30 | cli.echo('For more info, visit https://docs.qmk.fm/#/flashing') |
| 29 | 31 | ||
| 30 | 32 | ||
| 31 | @cli.argument('-bl', '--bootloader', default='flash', help='The flash command, corresponding to qmk\'s make options of bootloaders.') | ||
| 32 | @cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), help='The configurator export JSON to compile.') | 33 | @cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), help='The configurator export JSON to compile.') |
| 34 | @cli.argument('-b', '--bootloaders', action='store_true', help='List the available bootloaders.') | ||
| 35 | @cli.argument('-bl', '--bootloader', default='flash', help='The flash command, corresponding to qmk\'s make options of bootloaders.') | ||
| 33 | @cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.') | 36 | @cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.') |
| 34 | @cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.') | 37 | @cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.') |
| 35 | @cli.argument('-b', '--bootloaders', action='store_true', help='List the available bootloaders.') | 38 | @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.") |
| 36 | @cli.subcommand('QMK Flash.') | 39 | @cli.subcommand('QMK Flash.') |
| 40 | @automagic_keyboard | ||
| 41 | @automagic_keymap | ||
| 37 | def flash(cli): | 42 | def flash(cli): |
| 38 | """Compile and or flash QMK Firmware or keyboard/layout | 43 | """Compile and or flash QMK Firmware or keyboard/layout |
| 39 | 44 | ||
| @@ -42,12 +47,13 @@ def flash(cli): | |||
| 42 | 47 | ||
| 43 | If no file is supplied, keymap and keyboard are expected. | 48 | If no file is supplied, keymap and keyboard are expected. |
| 44 | 49 | ||
| 45 | If bootloader is omitted, the one according to the rules.mk will be used. | 50 | If bootloader is omitted the make system will use the configured bootloader for that keyboard. |
| 46 | |||
| 47 | """ | 51 | """ |
| 52 | command = '' | ||
| 53 | |||
| 48 | if cli.args.bootloaders: | 54 | if cli.args.bootloaders: |
| 49 | # Provide usage and list bootloaders | 55 | # Provide usage and list bootloaders |
| 50 | cli.echo('usage: qmk flash [-h] [-b] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]') | 56 | cli.echo('usage: qmk flash [-h] [-b] [-n] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]') |
| 51 | print_bootloader_help() | 57 | print_bootloader_help() |
| 52 | return False | 58 | return False |
| 53 | 59 | ||
| @@ -60,16 +66,23 @@ def flash(cli): | |||
| 60 | cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) | 66 | cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) |
| 61 | 67 | ||
| 62 | else: | 68 | else: |
| 63 | # Perform the action the user specified | 69 | if cli.config.flash.keyboard and cli.config.flash.keymap: |
| 64 | user_keyboard, user_keymap = find_keyboard_keymap() | ||
| 65 | if user_keyboard and user_keymap: | ||
| 66 | # Generate the make command for a specific keyboard/keymap. | 70 | # Generate the make command for a specific keyboard/keymap. |
| 67 | command = create_make_command(user_keyboard, user_keymap, cli.args.bootloader) | 71 | command = create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, cli.args.bootloader) |
| 68 | 72 | ||
| 69 | else: | 73 | elif not cli.config.flash.keyboard: |
| 70 | cli.log.error('You must supply a configurator export or both `--keyboard` and `--keymap`.') | 74 | cli.log.error('Could not determine keyboard!') |
| 71 | cli.echo('usage: qmk flash [-h] [-b] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]') | 75 | elif not cli.config.flash.keymap: |
| 72 | return False | 76 | cli.log.error('Could not determine keymap!') |
| 73 | 77 | ||
| 74 | cli.log.info('Flashing keymap with {fg_cyan}%s\n\n', ' '.join(command)) | 78 | # Compile the firmware, if we're able to |
| 75 | subprocess.run(command) | 79 | if command: |
| 80 | cli.log.info('Compiling keymap with {fg_cyan}%s', ' '.join(command)) | ||
| 81 | if not cli.args.dry_run: | ||
| 82 | cli.echo('\n') | ||
| 83 | subprocess.run(command) | ||
| 84 | |||
| 85 | else: | ||
| 86 | cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.') | ||
| 87 | cli.echo('usage: qmk flash [-h] [-b] [-n] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]') | ||
| 88 | return False | ||
