diff options
Diffstat (limited to 'lib/python/qmk/decorators.py')
-rw-r--r-- | lib/python/qmk/decorators.py | 60 |
1 files changed, 11 insertions, 49 deletions
diff --git a/lib/python/qmk/decorators.py b/lib/python/qmk/decorators.py index 629402b09..8d43ae980 100644 --- a/lib/python/qmk/decorators.py +++ b/lib/python/qmk/decorators.py | |||
@@ -1,13 +1,12 @@ | |||
1 | """Helpful decorators that subcommands can use. | 1 | """Helpful decorators that subcommands can use. |
2 | """ | 2 | """ |
3 | import functools | 3 | import functools |
4 | from pathlib import Path | ||
5 | from time import monotonic | 4 | from time import monotonic |
6 | 5 | ||
7 | from milc import cli | 6 | from milc import cli |
8 | 7 | ||
9 | from qmk.keymap import is_keymap_dir | 8 | from qmk.keyboard import find_keyboard_from_dir |
10 | from qmk.path import is_keyboard, under_qmk_firmware | 9 | from qmk.keymap import find_keymap_from_dir |
11 | 10 | ||
12 | 11 | ||
13 | def automagic_keyboard(func): | 12 | def automagic_keyboard(func): |
@@ -17,27 +16,13 @@ def automagic_keyboard(func): | |||
17 | """ | 16 | """ |
18 | @functools.wraps(func) | 17 | @functools.wraps(func) |
19 | def wrapper(*args, **kwargs): | 18 | def wrapper(*args, **kwargs): |
20 | # Check to make sure their copy of MILC supports config_source | ||
21 | if not hasattr(cli, 'config_source'): | ||
22 | cli.log.error("This subcommand requires a newer version of the QMK CLI. Please upgrade using `pip3 install --upgrade qmk` or your package manager.") | ||
23 | exit(1) | ||
24 | |||
25 | # Ensure that `--keyboard` was not passed and CWD is under `qmk_firmware/keyboards` | 19 | # Ensure that `--keyboard` was not passed and CWD is under `qmk_firmware/keyboards` |
26 | if cli.config_source[cli._entrypoint.__name__]['keyboard'] != 'argument': | 20 | if cli.config_source[cli._entrypoint.__name__]['keyboard'] != 'argument': |
27 | relative_cwd = under_qmk_firmware() | 21 | keyboard = find_keyboard_from_dir() |
28 | |||
29 | if relative_cwd and len(relative_cwd.parts) > 1 and relative_cwd.parts[0] == 'keyboards': | ||
30 | # Attempt to extract the keyboard name from the current directory | ||
31 | current_path = Path('/'.join(relative_cwd.parts[1:])) | ||
32 | |||
33 | if 'keymaps' in current_path.parts: | ||
34 | # Strip current_path of anything after `keymaps` | ||
35 | keymap_index = len(current_path.parts) - current_path.parts.index('keymaps') - 1 | ||
36 | current_path = current_path.parents[keymap_index] | ||
37 | 22 | ||
38 | if is_keyboard(current_path): | 23 | if keyboard: |
39 | cli.config[cli._entrypoint.__name__]['keyboard'] = str(current_path) | 24 | cli.config[cli._entrypoint.__name__]['keyboard'] = keyboard |
40 | cli.config_source[cli._entrypoint.__name__]['keyboard'] = 'keyboard_directory' | 25 | cli.config_source[cli._entrypoint.__name__]['keyboard'] = 'keyboard_directory' |
41 | 26 | ||
42 | return func(*args, **kwargs) | 27 | return func(*args, **kwargs) |
43 | 28 | ||
@@ -51,36 +36,13 @@ def automagic_keymap(func): | |||
51 | """ | 36 | """ |
52 | @functools.wraps(func) | 37 | @functools.wraps(func) |
53 | def wrapper(*args, **kwargs): | 38 | def wrapper(*args, **kwargs): |
54 | # Check to make sure their copy of MILC supports config_source | ||
55 | if not hasattr(cli, 'config_source'): | ||
56 | cli.log.error("This subcommand requires a newer version of the QMK CLI. Please upgrade using `pip3 install --upgrade qmk` or your package manager.") | ||
57 | exit(1) | ||
58 | |||
59 | # Ensure that `--keymap` was not passed and that we're under `qmk_firmware` | 39 | # Ensure that `--keymap` was not passed and that we're under `qmk_firmware` |
60 | if cli.config_source[cli._entrypoint.__name__]['keymap'] != 'argument': | 40 | if cli.config_source[cli._entrypoint.__name__]['keymap'] != 'argument': |
61 | relative_cwd = under_qmk_firmware() | 41 | keymap_name, keymap_type = find_keymap_from_dir() |
62 | 42 | ||
63 | if relative_cwd and len(relative_cwd.parts) > 1: | 43 | if keymap_name: |
64 | # If we're in `qmk_firmware/keyboards` and `keymaps` is in our path, try to find the keyboard name. | 44 | cli.config[cli._entrypoint.__name__]['keymap'] = keymap_name |
65 | if relative_cwd.parts[0] == 'keyboards' and 'keymaps' in relative_cwd.parts: | 45 | cli.config_source[cli._entrypoint.__name__]['keymap'] = keymap_type |
66 | current_path = Path('/'.join(relative_cwd.parts[1:])) # Strip 'keyboards' from the front | ||
67 | |||
68 | if 'keymaps' in current_path.parts and current_path.name != 'keymaps': | ||
69 | while current_path.parent.name != 'keymaps': | ||
70 | current_path = current_path.parent | ||
71 | cli.config[cli._entrypoint.__name__]['keymap'] = current_path.name | ||
72 | cli.config_source[cli._entrypoint.__name__]['keymap'] = 'keymap_directory' | ||
73 | |||
74 | # If we're in `qmk_firmware/layouts` guess the name from the community keymap they're in | ||
75 | elif relative_cwd.parts[0] == 'layouts' and is_keymap_dir(relative_cwd): | ||
76 | cli.config[cli._entrypoint.__name__]['keymap'] = relative_cwd.name | ||
77 | cli.config_source[cli._entrypoint.__name__]['keymap'] = 'layouts_directory' | ||
78 | |||
79 | # If we're in `qmk_firmware/users` guess the name from the userspace they're in | ||
80 | elif relative_cwd.parts[0] == 'users': | ||
81 | # Guess the keymap name based on which userspace they're in | ||
82 | cli.config[cli._entrypoint.__name__]['keymap'] = relative_cwd.parts[1] | ||
83 | cli.config_source[cli._entrypoint.__name__]['keymap'] = 'users_directory' | ||
84 | 46 | ||
85 | return func(*args, **kwargs) | 47 | return func(*args, **kwargs) |
86 | 48 | ||