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