decorators.py 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """Helpful decorators that subcommands can use.
  2. """
  3. import functools
  4. from pathlib import Path
  5. from milc import cli
  6. from qmk.keymap import is_keymap_dir
  7. from qmk.path import is_keyboard, under_qmk_firmware
  8. def automagic_keyboard(func):
  9. """Sets `cli.config.<subcommand>.keyboard` based on environment.
  10. This will rewrite cli.config.<subcommand>.keyboard if the user did not pass `--keyboard` and the directory they are currently in is a keyboard or keymap directory.
  11. """
  12. @functools.wraps(func)
  13. def wrapper(*args, **kwargs):
  14. # Check to make sure their copy of MILC supports config_source
  15. if not hasattr(cli, 'config_source'):
  16. cli.log.error("This subcommand requires a newer version of the QMK CLI. Please upgrade using `pip3 install --upgrade qmk` or your package manager.")
  17. exit(1)
  18. # Ensure that `--keyboard` was not passed and CWD is under `qmk_firmware/keyboards`
  19. if cli.config_source[cli._entrypoint.__name__]['keyboard'] != 'argument':
  20. relative_cwd = under_qmk_firmware()
  21. if relative_cwd and len(relative_cwd.parts) > 1 and relative_cwd.parts[0] == 'keyboards':
  22. # Attempt to extract the keyboard name from the current directory
  23. current_path = Path('/'.join(relative_cwd.parts[1:]))
  24. if 'keymaps' in current_path.parts:
  25. # Strip current_path of anything after `keymaps`
  26. keymap_index = len(current_path.parts) - current_path.parts.index('keymaps') - 1
  27. current_path = current_path.parents[keymap_index]
  28. if is_keyboard(current_path):
  29. cli.config[cli._entrypoint.__name__]['keyboard'] = str(current_path)
  30. cli.config_source[cli._entrypoint.__name__]['keyboard'] = 'keyboard_directory'
  31. return func(*args, **kwargs)
  32. return wrapper
  33. def automagic_keymap(func):
  34. """Sets `cli.config.<subcommand>.keymap` based on environment.
  35. This will rewrite cli.config.<subcommand>.keymap if the user did not pass `--keymap` and the directory they are currently in is a keymap, layout, or user directory.
  36. """
  37. @functools.wraps(func)
  38. def wrapper(*args, **kwargs):
  39. # Check to make sure their copy of MILC supports config_source
  40. if not hasattr(cli, 'config_source'):
  41. cli.log.error("This subcommand requires a newer version of the QMK CLI. Please upgrade using `pip3 install --upgrade qmk` or your package manager.")
  42. exit(1)
  43. # Ensure that `--keymap` was not passed and that we're under `qmk_firmware`
  44. if cli.config_source[cli._entrypoint.__name__]['keymap'] != 'argument':
  45. relative_cwd = under_qmk_firmware()
  46. if relative_cwd and len(relative_cwd.parts) > 1:
  47. # If we're in `qmk_firmware/keyboards` and `keymaps` is in our path, try to find the keyboard name.
  48. if relative_cwd.parts[0] == 'keyboards' and 'keymaps' in relative_cwd.parts:
  49. current_path = Path('/'.join(relative_cwd.parts[1:])) # Strip 'keyboards' from the front
  50. if 'keymaps' in current_path.parts and current_path.name != 'keymaps':
  51. while current_path.parent.name != 'keymaps':
  52. current_path = current_path.parent
  53. cli.config[cli._entrypoint.__name__]['keymap'] = current_path.name
  54. cli.config_source[cli._entrypoint.__name__]['keymap'] = 'keymap_directory'
  55. # If we're in `qmk_firmware/layouts` guess the name from the community keymap they're in
  56. elif relative_cwd.parts[0] == 'layouts' and is_keymap_dir(relative_cwd):
  57. cli.config[cli._entrypoint.__name__]['keymap'] = relative_cwd.name
  58. cli.config_source[cli._entrypoint.__name__]['keymap'] = 'layouts_directory'
  59. # If we're in `qmk_firmware/users` guess the name from the userspace they're in
  60. elif relative_cwd.parts[0] == 'users':
  61. # Guess the keymap name based on which userspace they're in
  62. cli.config[cli._entrypoint.__name__]['keymap'] = relative_cwd.parts[1]
  63. cli.config_source[cli._entrypoint.__name__]['keymap'] = 'users_directory'
  64. return func(*args, **kwargs)
  65. return wrapper