From 6d668c05b4b328c7b0e8961075d734d343ed163a Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 19 May 2023 16:05:43 +1000 Subject: [PATCH] CLI: Improve keymap folder resolution (#20981) --- lib/python/qmk/cli/new/keymap.py | 8 ++++---- lib/python/qmk/importers.py | 4 ++-- lib/python/qmk/keymap.py | 4 ++-- lib/python/qmk/path.py | 25 ++++++++++++++++++++++--- lib/python/qmk/tests/test_qmk_path.py | 4 ++-- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/lib/python/qmk/cli/new/keymap.py b/lib/python/qmk/cli/new/keymap.py index e7823bc46dd2..9b0ac221a4ae 100755 --- a/lib/python/qmk/cli/new/keymap.py +++ b/lib/python/qmk/cli/new/keymap.py @@ -5,7 +5,7 @@ from milc import cli from milc.questions import question -from qmk.path import is_keyboard, keymap +from qmk.path import is_keyboard, keymaps, keymap from qmk.git import git_get_username from qmk.decorators import automagic_keyboard, automagic_keymap from qmk.keyboard import keyboard_completer, keyboard_folder @@ -50,9 +50,9 @@ def new_keymap(cli): return False # generate keymap paths - km_path = keymap(kb_name) - keymap_path_default = km_path / 'default' - keymap_path_new = km_path / user_name + keymaps_dirs = keymaps(kb_name) + keymap_path_default = keymap(kb_name, 'default') + keymap_path_new = keymaps_dirs[0] / user_name if not keymap_path_default.exists(): cli.log.error(f'Default keymap {{fg_cyan}}{keymap_path_default}{{fg_reset}} does not exist!') diff --git a/lib/python/qmk/importers.py b/lib/python/qmk/importers.py index 307c66ee3ccd..edc1f940daa6 100644 --- a/lib/python/qmk/importers.py +++ b/lib/python/qmk/importers.py @@ -5,7 +5,7 @@ from qmk.git import git_get_username from qmk.json_schema import validate -from qmk.path import keyboard, keymap +from qmk.path import keyboard, keymaps from qmk.constants import MCU2BOOTLOADER, LEGACY_KEYCODES from qmk.json_encoders import InfoJSONEncoder, KeymapJSONEncoder from qmk.json_schema import deep_update, json_load @@ -84,7 +84,7 @@ def import_keymap(keymap_data): kb_name = keymap_data['keyboard'] km_name = keymap_data['keymap'] - km_folder = keymap(kb_name) / km_name + km_folder = keymaps(kb_name)[0] / km_name keyboard_keymap = km_folder / 'keymap.json' # This is the deepest folder in the expected tree diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index 825267a1fe71..11e8d39dadf5 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py @@ -379,7 +379,7 @@ def write_json(keyboard, keymap, layout, layers, macros=None): """ keymap_json = generate_json(keyboard, keymap, layout, layers, macros=None) keymap_content = json.dumps(keymap_json) - keymap_file = qmk.path.keymap(keyboard) / keymap / 'keymap.json' + keymap_file = qmk.path.keymaps(keyboard)[0] / keymap / 'keymap.json' return write_file(keymap_file, keymap_content) @@ -406,7 +406,7 @@ def write(keymap_json): A list of macros for this keymap. """ keymap_content = generate_c(keymap_json) - keymap_file = qmk.path.keymap(keymap_json['keyboard']) / keymap_json['keymap'] / 'keymap.c' + keymap_file = qmk.path.keymaps(keymap_json['keyboard'])[0] / keymap_json['keymap'] / 'keymap.c' return write_file(keymap_file, keymap_content) diff --git a/lib/python/qmk/path.py b/lib/python/qmk/path.py index 556d0eefc858..9d248451b86f 100644 --- a/lib/python/qmk/path.py +++ b/lib/python/qmk/path.py @@ -36,8 +36,8 @@ def keyboard(keyboard_name): return Path('keyboards') / keyboard_name -def keymap(keyboard_name): - """Locate the correct directory for storing a keymap. +def keymaps(keyboard_name): + """Returns all of the `keymaps/` directories for a given keyboard. Args: @@ -45,17 +45,36 @@ def keymap(keyboard_name): The name of the keyboard. Example: clueboard/66/rev3 """ keyboard_folder = keyboard(keyboard_name) + found_dirs = [] for _ in range(MAX_KEYBOARD_SUBFOLDERS): if (keyboard_folder / 'keymaps').exists(): - return (keyboard_folder / 'keymaps').resolve() + found_dirs.append((keyboard_folder / 'keymaps').resolve()) keyboard_folder = keyboard_folder.parent + if len(found_dirs) > 0: + return found_dirs + logging.error('Could not find the keymaps directory!') raise NoSuchKeyboardError('Could not find keymaps directory for: %s' % keyboard_name) +def keymap(keyboard_name, keymap_name): + """Locate the directory of a given keymap. + + Args: + + keyboard_name + The name of the keyboard. Example: clueboard/66/rev3 + keymap_name + The name of the keymap. Example: default + """ + for keymap_dir in keymaps(keyboard_name): + if (keymap_dir / keymap_name).exists(): + return (keymap_dir / keymap_name).resolve() + + def normpath(path): """Returns a `pathlib.Path()` object for a given path. diff --git a/lib/python/qmk/tests/test_qmk_path.py b/lib/python/qmk/tests/test_qmk_path.py index 4b5132f13d32..cc068e39dad8 100644 --- a/lib/python/qmk/tests/test_qmk_path.py +++ b/lib/python/qmk/tests/test_qmk_path.py @@ -5,8 +5,8 @@ def test_keymap_pytest_basic(): - path = qmk.path.keymap('handwired/pytest/basic') - assert path.samefile('keyboards/handwired/pytest/basic/keymaps') + path = qmk.path.keymap('handwired/pytest/basic', 'default') + assert path.samefile('keyboards/handwired/pytest/basic/keymaps/default') def test_normpath():