Skip to content

Commit

Permalink
CLI: Improve keymap folder resolution (qmk#20981)
Browse files Browse the repository at this point in the history
  • Loading branch information
fauxpark authored and autoferrit committed Dec 6, 2023
1 parent 051c25a commit 6d668c0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
8 changes: 4 additions & 4 deletions lib/python/qmk/cli/new/keymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!')
Expand Down
4 changes: 2 additions & 2 deletions lib/python/qmk/importers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/python/qmk/keymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down
25 changes: 22 additions & 3 deletions lib/python/qmk/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,45 @@ 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:
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.
Expand Down
4 changes: 2 additions & 2 deletions lib/python/qmk/tests/test_qmk_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 6d668c0

Please sign in to comment.