diff options
author | skullydazed <[email protected]> | 2020-02-17 11:42:11 -0800 |
---|---|---|
committer | GitHub <[email protected]> | 2020-02-17 11:42:11 -0800 |
commit | c66930445f7d5941eb847568288046d51f853786 (patch) | |
tree | 273f290ca81a27bbbe7bdbf90221c02ac11f42cd /lib/python/qmk/path.py | |
parent | 58724f8dcb9eccb1c132b8ddab422790ddd26be0 (diff) | |
download | qmk_firmware-c66930445f7d5941eb847568288046d51f853786.tar.gz qmk_firmware-c66930445f7d5941eb847568288046d51f853786.zip |
Use pathlib everywhere we can (#7872)0.7.156
* Use pathlib everywhere we can
* Update lib/python/qmk/path.py
Co-Authored-By: Erovia <[email protected]>
* Update lib/python/qmk/path.py
Co-Authored-By: Erovia <[email protected]>
* 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 <[email protected]>
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 cf087265fb..d16928afb5 100644 --- a/lib/python/qmk/path.py +++ b/lib/python/qmk/path.py @@ -2,34 +2,69 @@ """ import logging import os +from pathlib import Path +from qmk.constants import QMK_FIRMWARE, MAX_KEYBOARD_SUBFOLDERS from qmk.errors import NoSuchKeyboardError +def is_keymap_dir(keymap_path): + """Returns True if `keymap_path` is a valid keymap directory. + """ + keymap_path = Path(keymap_path) + keymap_c = keymap_path / 'keymap.c' + keymap_json = keymap_path / 'keymap.json' + + return any((keymap_c.exists(), keymap_json.exists())) + + +def is_keyboard(keyboard_name): + """Returns True if `keyboard_name` is a keyboard we can compile. + """ + keyboard_path = QMK_FIRMWARE / 'keyboards' / keyboard_name + rules_mk = keyboard_path / 'rules.mk' + return rules_mk.exists() + + +def under_qmk_firmware(): + """Returns a Path object representing the relative path under qmk_firmware, or None. + """ + cwd = Path(os.environ['ORIG_CWD']) + + try: + return cwd.relative_to(QMK_FIRMWARE) + except ValueError: + return None + + def keymap(keyboard): """Locate the correct directory for storing a keymap. Args: + keyboard The name of the keyboard. Example: clueboard/66/rev3 """ - for directory in ['.', '..', '../..', '../../..', '../../../..', '../../../../..']: - basepath = os.path.normpath(os.path.join('keyboards', keyboard, directory, 'keymaps')) + keyboard_folder = Path('keyboards') / keyboard + + for i in range(MAX_KEYBOARD_SUBFOLDERS): + if (keyboard_folder / 'keymaps').exists(): + return (keyboard_folder / 'keymaps').resolve() - if os.path.exists(basepath): - return basepath + keyboard_folder = keyboard_folder.parent - logging.error('Could not find keymaps directory!') + logging.error('Could not find the keymaps directory!') raise NoSuchKeyboardError('Could not find keymaps directory for: %s' % keyboard) def normpath(path): - """Returns the fully resolved absolute path to a file. + """Returns a `pathlib.Path()` object for a given path. - This function will return the absolute path to a file as seen from the - directory the script was called from. + 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. """ - if path and path[0] == '/': - return os.path.normpath(path) + path = Path(path) + + if path.is_absolute(): + return Path(path) - return os.path.normpath(os.path.join(os.environ['ORIG_CWD'], path)) + return Path(os.environ['ORIG_CWD']) / path |