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 | |
| 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')
| -rw-r--r-- | lib/python/qmk/cli/__init__.py | 5 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/compile.py | 37 | ||||
| -rw-r--r-- | lib/python/qmk/cli/flash.py | 47 | ||||
| -rw-r--r-- | lib/python/qmk/cli/list/keymaps.py | 3 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/new/keymap.py | 3 |
5 files changed, 66 insertions, 29 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index 5149a6215..eb524217c 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py | |||
| @@ -2,6 +2,8 @@ | |||
| 2 | 2 | ||
| 3 | We list each subcommand here explicitly because all the reliable ways of searching for modules are slow and delay startup. | 3 | We list each subcommand here explicitly because all the reliable ways of searching for modules are slow and delay startup. |
| 4 | """ | 4 | """ |
| 5 | from milc import cli | ||
| 6 | |||
| 5 | from . import cformat | 7 | from . import cformat |
| 6 | from . import compile | 8 | from . import compile |
| 7 | from . import config | 9 | from . import config |
| @@ -16,3 +18,6 @@ from . import kle2json | |||
| 16 | from . import new | 18 | from . import new |
| 17 | from . import pyformat | 19 | from . import pyformat |
| 18 | from . import pytest | 20 | from . import pytest |
| 21 | |||
| 22 | if not hasattr(cli, 'config_source'): | ||
| 23 | cli.log.warning("Your QMK CLI is out of date. Please upgrade with `pip3 install --upgrade qmk` or by using your package manager.") | ||
diff --git a/lib/python/qmk/cli/compile.py b/lib/python/qmk/cli/compile.py index 3068c97d8..6480d624b 100755 --- a/lib/python/qmk/cli/compile.py +++ b/lib/python/qmk/cli/compile.py | |||
| @@ -8,13 +8,17 @@ from argparse import FileType | |||
| 8 | from milc import cli | 8 | from milc import cli |
| 9 | 9 | ||
| 10 | import qmk.path | 10 | import qmk.path |
| 11 | from qmk.commands import compile_configurator_json, create_make_command, find_keyboard_keymap, parse_configurator_json | 11 | from qmk.decorators import automagic_keyboard, automagic_keymap |
| 12 | from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json | ||
| 12 | 13 | ||
| 13 | 14 | ||
| 14 | @cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), help='The configurator export to compile') | 15 | @cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), help='The configurator export to compile') |
| 15 | @cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.') | 16 | @cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.') |
| 16 | @cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Ignored when a configurator export is supplied.') | 17 | @cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Ignored when a configurator export is supplied.') |
| 18 | @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.") | ||
| 17 | @cli.subcommand('Compile a QMK Firmware.') | 19 | @cli.subcommand('Compile a QMK Firmware.') |
| 20 | @automagic_keyboard | ||
| 21 | @automagic_keymap | ||
| 18 | def compile(cli): | 22 | def compile(cli): |
| 19 | """Compile a QMK Firmware. | 23 | """Compile a QMK Firmware. |
| 20 | 24 | ||
| @@ -22,8 +26,10 @@ def compile(cli): | |||
| 22 | 26 | ||
| 23 | If a keyboard and keymap are provided this command will build a firmware based on that. | 27 | If a keyboard and keymap are provided this command will build a firmware based on that. |
| 24 | """ | 28 | """ |
| 29 | command = None | ||
| 30 | |||
| 25 | if cli.args.filename: | 31 | if cli.args.filename: |
| 26 | # If a configurator JSON was provided skip straight to compiling it | 32 | # If a configurator JSON was provided generate a keymap and compile it |
| 27 | # FIXME(skullydazed): add code to check and warn if the keymap already exists when compiling a json keymap. | 33 | # FIXME(skullydazed): add code to check and warn if the keymap already exists when compiling a json keymap. |
| 28 | user_keymap = parse_configurator_json(cli.args.filename) | 34 | user_keymap = parse_configurator_json(cli.args.filename) |
| 29 | keymap_path = qmk.path.keymap(user_keymap['keyboard']) | 35 | keymap_path = qmk.path.keymap(user_keymap['keyboard']) |
| @@ -32,16 +38,23 @@ def compile(cli): | |||
| 32 | cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) | 38 | cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) |
| 33 | 39 | ||
| 34 | else: | 40 | else: |
| 35 | # Perform the action the user specified | 41 | if cli.config.compile.keyboard and cli.config.compile.keymap: |
| 36 | user_keyboard, user_keymap = find_keyboard_keymap() | ||
| 37 | if user_keyboard and user_keymap: | ||
| 38 | # Generate the make command for a specific keyboard/keymap. | 42 | # Generate the make command for a specific keyboard/keymap. |
| 39 | command = create_make_command(user_keyboard, user_keymap) | 43 | command = create_make_command(cli.config.compile.keyboard, cli.config.compile.keymap) |
| 44 | |||
| 45 | elif not cli.config.compile.keyboard: | ||
| 46 | cli.log.error('Could not determine keyboard!') | ||
| 47 | elif not cli.config.compile.keymap: | ||
| 48 | cli.log.error('Could not determine keymap!') | ||
| 40 | 49 | ||
| 41 | else: | 50 | # Compile the firmware, if we're able to |
| 42 | cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.') | 51 | if command: |
| 43 | cli.echo('usage: qmk compile [-h] [-b] [-kb KEYBOARD] [-km KEYMAP] [filename]') | 52 | cli.log.info('Compiling keymap with {fg_cyan}%s', ' '.join(command)) |
| 44 | return False | 53 | if not cli.args.dry_run: |
| 54 | cli.echo('\n') | ||
| 55 | subprocess.run(command) | ||
| 45 | 56 | ||
| 46 | cli.log.info('Compiling keymap with {fg_cyan}%s\n\n', ' '.join(command)) | 57 | else: |
| 47 | subprocess.run(command) | 58 | cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.') |
| 59 | cli.echo('usage: qmk compile [-h] [-b] [-kb KEYBOARD] [-km KEYMAP] [filename]') | ||
| 60 | return False | ||
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 | ||
diff --git a/lib/python/qmk/cli/list/keymaps.py b/lib/python/qmk/cli/list/keymaps.py index d199d29bc..cec9ca022 100644 --- a/lib/python/qmk/cli/list/keymaps.py +++ b/lib/python/qmk/cli/list/keymaps.py | |||
| @@ -1,12 +1,15 @@ | |||
| 1 | """List the keymaps for a specific keyboard | 1 | """List the keymaps for a specific keyboard |
| 2 | """ | 2 | """ |
| 3 | from milc import cli | 3 | from milc import cli |
| 4 | |||
| 4 | import qmk.keymap | 5 | import qmk.keymap |
| 6 | from qmk.decorators import automagic_keyboard | ||
| 5 | from qmk.errors import NoSuchKeyboardError | 7 | from qmk.errors import NoSuchKeyboardError |
| 6 | 8 | ||
| 7 | 9 | ||
| 8 | @cli.argument("-kb", "--keyboard", help="Specify keyboard name. Example: 1upkeyboards/1up60hse") | 10 | @cli.argument("-kb", "--keyboard", help="Specify keyboard name. Example: 1upkeyboards/1up60hse") |
| 9 | @cli.subcommand("List the keymaps for a specific keyboard") | 11 | @cli.subcommand("List the keymaps for a specific keyboard") |
| 12 | @automagic_keyboard | ||
| 10 | def list_keymaps(cli): | 13 | def list_keymaps(cli): |
| 11 | """List the keymaps for a specific keyboard | 14 | """List the keymaps for a specific keyboard |
| 12 | """ | 15 | """ |
diff --git a/lib/python/qmk/cli/new/keymap.py b/lib/python/qmk/cli/new/keymap.py index cbe50692e..5ae262856 100755 --- a/lib/python/qmk/cli/new/keymap.py +++ b/lib/python/qmk/cli/new/keymap.py | |||
| @@ -4,12 +4,15 @@ import shutil | |||
| 4 | from pathlib import Path | 4 | from pathlib import Path |
| 5 | 5 | ||
| 6 | import qmk.path | 6 | import qmk.path |
| 7 | from qmk.decorators import automagic_keyboard, automagic_keymap | ||
| 7 | from milc import cli | 8 | from milc import cli |
| 8 | 9 | ||
| 9 | 10 | ||
| 10 | @cli.argument('-kb', '--keyboard', help='Specify keyboard name. Example: 1upkeyboards/1up60hse') | 11 | @cli.argument('-kb', '--keyboard', help='Specify keyboard name. Example: 1upkeyboards/1up60hse') |
| 11 | @cli.argument('-km', '--keymap', help='Specify the name for the new keymap directory') | 12 | @cli.argument('-km', '--keymap', help='Specify the name for the new keymap directory') |
| 12 | @cli.subcommand('Creates a new keymap for the keyboard of your choosing') | 13 | @cli.subcommand('Creates a new keymap for the keyboard of your choosing') |
| 14 | @automagic_keyboard | ||
| 15 | @automagic_keymap | ||
| 13 | def new_keymap(cli): | 16 | def new_keymap(cli): |
| 14 | """Creates a new keymap for the keyboard of your choosing. | 17 | """Creates a new keymap for the keyboard of your choosing. |
| 15 | """ | 18 | """ |
