1
0
Fork 0

Improve argument handling of c2json (#22170)

* Improve argument handling of c2json

* Add automagic
This commit is contained in:
Joel Challis 2023-11-01 22:37:05 +00:00 committed by GitHub
parent a27bc60703
commit 8ea955c72f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 42 deletions

View file

@ -191,16 +191,15 @@ def _strip_any(keycode):
return keycode
def find_keymap_from_dir():
"""Returns `(keymap_name, source)` for the directory we're currently in.
def find_keymap_from_dir(*args):
"""Returns `(keymap_name, source)` for the directory provided (or cwd if not specified).
"""
relative_cwd = qmk.path.under_qmk_firmware()
relative_path = qmk.path.under_qmk_firmware(*args)
if relative_cwd and len(relative_cwd.parts) > 1:
if relative_path and len(relative_path.parts) > 1:
# If we're in `qmk_firmware/keyboards` and `keymaps` is in our path, try to find the keyboard name.
if relative_cwd.parts[0] == 'keyboards' and 'keymaps' in relative_cwd.parts:
current_path = Path('/'.join(relative_cwd.parts[1:])) # Strip 'keyboards' from the front
if relative_path.parts[0] == 'keyboards' and 'keymaps' in relative_path.parts:
current_path = Path('/'.join(relative_path.parts[1:])) # Strip 'keyboards' from the front
if 'keymaps' in current_path.parts and current_path.name != 'keymaps':
while current_path.parent.name != 'keymaps':
@ -209,13 +208,13 @@ def find_keymap_from_dir():
return current_path.name, 'keymap_directory'
# If we're in `qmk_firmware/layouts` guess the name from the community keymap they're in
elif relative_cwd.parts[0] == 'layouts' and is_keymap_dir(relative_cwd):
return relative_cwd.name, 'layouts_directory'
elif relative_path.parts[0] == 'layouts' and is_keymap_dir(relative_path):
return relative_path.name, 'layouts_directory'
# If we're in `qmk_firmware/users` guess the name from the userspace they're in
elif relative_cwd.parts[0] == 'users':
elif relative_path.parts[0] == 'users':
# Guess the keymap name based on which userspace they're in
return relative_cwd.parts[1], 'users_directory'
return relative_path.parts[1], 'users_directory'
return None, None
@ -657,7 +656,7 @@ def parse_keymap_c(keymap_file, use_cpp=True):
Returns:
a dictionary containing the parsed keymap
"""
if keymap_file == '-':
if not isinstance(keymap_file, (Path, str)) or keymap_file == '-':
if use_cpp:
keymap_file = _c_preprocess(None, sys.stdin)
else: