diff options
Diffstat (limited to 'lib/python/qmk/path.py')
-rw-r--r-- | lib/python/qmk/path.py | 57 |
1 files changed, 46 insertions, 11 deletions
diff --git a/lib/python/qmk/path.py b/lib/python/qmk/path.py index cf087265f..d16928afb 100644 --- a/lib/python/qmk/path.py +++ b/lib/python/qmk/path.py | |||
@@ -2,34 +2,69 @@ | |||
2 | """ | 2 | """ |
3 | import logging | 3 | import logging |
4 | import os | 4 | import os |
5 | from pathlib import Path | ||
5 | 6 | ||
7 | from qmk.constants import QMK_FIRMWARE, MAX_KEYBOARD_SUBFOLDERS | ||
6 | from qmk.errors import NoSuchKeyboardError | 8 | from qmk.errors import NoSuchKeyboardError |
7 | 9 | ||
8 | 10 | ||
11 | def is_keymap_dir(keymap_path): | ||
12 | """Returns True if `keymap_path` is a valid keymap directory. | ||
13 | """ | ||
14 | keymap_path = Path(keymap_path) | ||
15 | keymap_c = keymap_path / 'keymap.c' | ||
16 | keymap_json = keymap_path / 'keymap.json' | ||
17 | |||
18 | return any((keymap_c.exists(), keymap_json.exists())) | ||
19 | |||
20 | |||
21 | def is_keyboard(keyboard_name): | ||
22 | """Returns True if `keyboard_name` is a keyboard we can compile. | ||
23 | """ | ||
24 | keyboard_path = QMK_FIRMWARE / 'keyboards' / keyboard_name | ||
25 | rules_mk = keyboard_path / 'rules.mk' | ||
26 | return rules_mk.exists() | ||
27 | |||
28 | |||
29 | def under_qmk_firmware(): | ||
30 | """Returns a Path object representing the relative path under qmk_firmware, or None. | ||
31 | """ | ||
32 | cwd = Path(os.environ['ORIG_CWD']) | ||
33 | |||
34 | try: | ||
35 | return cwd.relative_to(QMK_FIRMWARE) | ||
36 | except ValueError: | ||
37 | return None | ||
38 | |||
39 | |||
9 | def keymap(keyboard): | 40 | def keymap(keyboard): |
10 | """Locate the correct directory for storing a keymap. | 41 | """Locate the correct directory for storing a keymap. |
11 | 42 | ||
12 | Args: | 43 | Args: |
44 | |||
13 | keyboard | 45 | keyboard |
14 | The name of the keyboard. Example: clueboard/66/rev3 | 46 | The name of the keyboard. Example: clueboard/66/rev3 |
15 | """ | 47 | """ |
16 | for directory in ['.', '..', '../..', '../../..', '../../../..', '../../../../..']: | 48 | keyboard_folder = Path('keyboards') / keyboard |
17 | basepath = os.path.normpath(os.path.join('keyboards', keyboard, directory, 'keymaps')) | 49 | |
50 | for i in range(MAX_KEYBOARD_SUBFOLDERS): | ||
51 | if (keyboard_folder / 'keymaps').exists(): | ||
52 | return (keyboard_folder / 'keymaps').resolve() | ||
18 | 53 | ||
19 | if os.path.exists(basepath): | 54 | keyboard_folder = keyboard_folder.parent |
20 | return basepath | ||
21 | 55 | ||
22 | logging.error('Could not find keymaps directory!') | 56 | logging.error('Could not find the keymaps directory!') |
23 | raise NoSuchKeyboardError('Could not find keymaps directory for: %s' % keyboard) | 57 | raise NoSuchKeyboardError('Could not find keymaps directory for: %s' % keyboard) |
24 | 58 | ||
25 | 59 | ||
26 | def normpath(path): | 60 | def normpath(path): |
27 | """Returns the fully resolved absolute path to a file. | 61 | """Returns a `pathlib.Path()` object for a given path. |
28 | 62 | ||
29 | This function will return the absolute path to a file as seen from the | 63 | This will use the path to a file as seen from the directory the script was called from. You should use this to normalize filenames supplied from the command line. |
30 | directory the script was called from. | ||
31 | """ | 64 | """ |
32 | if path and path[0] == '/': | 65 | path = Path(path) |
33 | return os.path.normpath(path) | 66 | |
67 | if path.is_absolute(): | ||
68 | return Path(path) | ||
34 | 69 | ||
35 | return os.path.normpath(os.path.join(os.environ['ORIG_CWD'], path)) | 70 | return Path(os.environ['ORIG_CWD']) / path |