diff options
| author | skullydazed <skullydazed@users.noreply.github.com> | 2020-02-17 11:42:11 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-17 11:42:11 -0800 |
| commit | c66930445f7d5941eb847568288046d51f853786 (patch) | |
| tree | 273f290ca81a27bbbe7bdbf90221c02ac11f42cd /lib/python/qmk/commands.py | |
| parent | 58724f8dcb9eccb1c132b8ddab422790ddd26be0 (diff) | |
| download | qmk_firmware-c66930445f7d5941eb847568288046d51f853786.tar.gz qmk_firmware-c66930445f7d5941eb847568288046d51f853786.zip | |
Use pathlib everywhere we can (#7872)
* Use pathlib everywhere we can
* Update lib/python/qmk/path.py
Co-Authored-By: Erovia <Erovia@users.noreply.github.com>
* Update lib/python/qmk/path.py
Co-Authored-By: Erovia <Erovia@users.noreply.github.com>
* 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()
Co-authored-by: Erovia <Erovia@users.noreply.github.com>
Diffstat (limited to 'lib/python/qmk/commands.py')
| -rw-r--r-- | lib/python/qmk/commands.py | 96 |
1 files changed, 87 insertions, 9 deletions
diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index 6067d49ae..cdb8ee037 100644 --- a/lib/python/qmk/commands.py +++ b/lib/python/qmk/commands.py | |||
| @@ -1,13 +1,19 @@ | |||
| 1 | """Functions that build make commands | 1 | """Helper functions for commands. |
| 2 | """ | 2 | """ |
| 3 | import json | 3 | import json |
| 4 | from pathlib import Path | ||
| 5 | |||
| 6 | from milc import cli | ||
| 7 | |||
| 4 | import qmk.keymap | 8 | import qmk.keymap |
| 9 | from qmk.path import is_keyboard, is_keymap_dir, under_qmk_firmware | ||
| 5 | 10 | ||
| 6 | 11 | ||
| 7 | def create_make_command(keyboard, keymap, target=None): | 12 | def create_make_command(keyboard, keymap, target=None): |
| 8 | """Create a make compile command | 13 | """Create a make compile command |
| 9 | 14 | ||
| 10 | Args: | 15 | Args: |
| 16 | |||
| 11 | keyboard | 17 | keyboard |
| 12 | The path of the keyboard, for example 'plank' | 18 | The path of the keyboard, for example 'plank' |
| 13 | 19 | ||
| @@ -18,24 +24,22 @@ def create_make_command(keyboard, keymap, target=None): | |||
| 18 | Usually a bootloader. | 24 | Usually a bootloader. |
| 19 | 25 | ||
| 20 | Returns: | 26 | Returns: |
| 27 | |||
| 21 | A command that can be run to make the specified keyboard and keymap | 28 | A command that can be run to make the specified keyboard and keymap |
| 22 | """ | 29 | """ |
| 23 | if target is None: | 30 | make_args = [keyboard, keymap] |
| 24 | return ['make', ':'.join((keyboard, keymap))] | ||
| 25 | return ['make', ':'.join((keyboard, keymap, target))] | ||
| 26 | 31 | ||
| 32 | if target: | ||
| 33 | make_args.append(target) | ||
| 27 | 34 | ||
| 28 | def parse_configurator_json(configurator_file): | 35 | return ['make', ':'.join(make_args)] |
| 29 | """Open and parse a configurator json export | ||
| 30 | """ | ||
| 31 | user_keymap = json.load(configurator_file) | ||
| 32 | return user_keymap | ||
| 33 | 36 | ||
| 34 | 37 | ||
| 35 | def compile_configurator_json(user_keymap, bootloader=None): | 38 | def compile_configurator_json(user_keymap, bootloader=None): |
| 36 | """Convert a configurator export JSON file into a C file | 39 | """Convert a configurator export JSON file into a C file |
| 37 | 40 | ||
| 38 | Args: | 41 | Args: |
| 42 | |||
| 39 | configurator_filename | 43 | configurator_filename |
| 40 | The configurator JSON export file | 44 | The configurator JSON export file |
| 41 | 45 | ||
| @@ -43,6 +47,7 @@ def compile_configurator_json(user_keymap, bootloader=None): | |||
| 43 | A bootloader to flash | 47 | A bootloader to flash |
| 44 | 48 | ||
| 45 | Returns: | 49 | Returns: |
| 50 | |||
| 46 | A command to run to compile and flash the C file. | 51 | A command to run to compile and flash the C file. |
| 47 | """ | 52 | """ |
| 48 | # Write the keymap C file | 53 | # Write the keymap C file |
| @@ -52,3 +57,76 @@ def compile_configurator_json(user_keymap, bootloader=None): | |||
| 52 | if bootloader is None: | 57 | if bootloader is None: |
| 53 | return create_make_command(user_keymap['keyboard'], user_keymap['keymap']) | 58 | return create_make_command(user_keymap['keyboard'], user_keymap['keymap']) |
| 54 | return create_make_command(user_keymap['keyboard'], user_keymap['keymap'], bootloader) | 59 | return create_make_command(user_keymap['keyboard'], user_keymap['keymap'], bootloader) |
| 60 | |||
| 61 | |||
| 62 | def find_keyboard_keymap(): | ||
| 63 | """Returns `(keyboard_name, keymap_name)` based on the user's current environment. | ||
| 64 | |||
| 65 | This determines the keyboard and keymap name using the following precedence order: | ||
| 66 | |||
| 67 | * Command line flags (--keyboard and --keymap) | ||
| 68 | * Current working directory | ||
| 69 | * `keyboards/<keyboard_name>` | ||
| 70 | * `keyboards/<keyboard_name>/keymaps/<keymap_name>` | ||
| 71 | * `layouts/**/<keymap_name>` | ||
| 72 | * `users/<keymap_name>` | ||
| 73 | * Configuration | ||
| 74 | * cli.config.<subcommand>.keyboard | ||
| 75 | * cli.config.<subcommand>.keymap | ||
| 76 | """ | ||
| 77 | # Check to make sure their copy of MILC supports config_source | ||
| 78 | if not hasattr(cli, 'config_source'): | ||
| 79 | cli.log.error("Your QMK CLI is out of date. Please upgrade using pip3 or your package manager.") | ||
| 80 | exit(1) | ||
| 81 | |||
| 82 | # State variables | ||
| 83 | relative_cwd = under_qmk_firmware() | ||
| 84 | keyboard_name = "" | ||
| 85 | keymap_name = "" | ||
| 86 | |||
| 87 | # If the keyboard or keymap are passed as arguments use that in preference to anything else | ||
| 88 | if cli.config_source[cli._entrypoint.__name__]['keyboard'] == 'argument': | ||
| 89 | keyboard_name = cli.config[cli._entrypoint.__name__]['keyboard'] | ||
| 90 | if cli.config_source[cli._entrypoint.__name__]['keymap'] == 'argument': | ||
| 91 | keymap_name = cli.config[cli._entrypoint.__name__]['keymap'] | ||
| 92 | |||
| 93 | if not keyboard_name or not keymap_name: | ||
| 94 | # If we don't have a keyboard_name and keymap_name from arguments try to derive one or both | ||
| 95 | if relative_cwd and relative_cwd.parts and relative_cwd.parts[0] == 'keyboards': | ||
| 96 | # Try to determine the keyboard and/or keymap name | ||
| 97 | current_path = Path('/'.join(relative_cwd.parts[1:])) | ||
| 98 | |||
| 99 | if current_path.parts[-2] == 'keymaps': | ||
| 100 | if not keymap_name: | ||
| 101 | keymap_name = current_path.parts[-1] | ||
| 102 | if not keyboard_name: | ||
| 103 | keyboard_name = '/'.join(current_path.parts[:-2]) | ||
| 104 | elif not keyboard_name and is_keyboard(current_path): | ||
| 105 | keyboard_name = str(current_path) | ||
| 106 | |||
| 107 | elif relative_cwd and relative_cwd.parts and relative_cwd.parts[0] == 'layouts': | ||
| 108 | # Try to determine the keymap name from the community layout | ||
| 109 | if is_keymap_dir(relative_cwd) and not keymap_name: | ||
| 110 | keymap_name = relative_cwd.name | ||
| 111 | |||
| 112 | elif relative_cwd and relative_cwd.parts and relative_cwd.parts[0] == 'users': | ||
| 113 | # Try to determine the keymap name based on which userspace they're in | ||
| 114 | if not keymap_name and len(relative_cwd.parts) > 1: | ||
| 115 | keymap_name = relative_cwd.parts[1] | ||
| 116 | |||
| 117 | # If we still don't have a keyboard and keymap check the config | ||
| 118 | if not keyboard_name and cli.config[cli._entrypoint.__name__]['keyboard']: | ||
| 119 | keyboard_name = cli.config[cli._entrypoint.__name__]['keyboard'] | ||
| 120 | |||
| 121 | if not keymap_name and cli.config[cli._entrypoint.__name__]['keymap']: | ||
| 122 | keymap_name = cli.config[cli._entrypoint.__name__]['keymap'] | ||
| 123 | |||
| 124 | return (keyboard_name, keymap_name) | ||
| 125 | |||
| 126 | |||
| 127 | def parse_configurator_json(configurator_file): | ||
| 128 | """Open and parse a configurator json export | ||
| 129 | """ | ||
| 130 | user_keymap = json.load(configurator_file) | ||
| 131 | |||
| 132 | return user_keymap | ||
