decorators.py 4.3 KB

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