logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

decorators.py (3315B)


  1. """Helpful decorators that subcommands can use.
  2. """
  3. import functools
  4. from time import monotonic
  5. from milc import cli
  6. from qmk.keyboard import find_keyboard_from_dir, keyboard_folder
  7. from qmk.keymap import find_keymap_from_dir
  8. def _get_subcommand_name():
  9. """Handle missing cli.subcommand_name on older versions of milc
  10. """
  11. try:
  12. return cli.subcommand_name
  13. except AttributeError:
  14. return cli._subcommand.__name__
  15. def automagic_keyboard(func):
  16. """Sets `cli.config.<subcommand>.keyboard` based on environment.
  17. 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.
  18. """
  19. @functools.wraps(func)
  20. def wrapper(*args, **kwargs):
  21. cmd = _get_subcommand_name()
  22. # TODO: Workaround for if config file contains "old" keyboard name
  23. # Potential long-term fix needs to be within global cli or milc
  24. if cli.config_source[cmd]['keyboard'] == 'config_file':
  25. cli.config[cmd]['keyboard'] = keyboard_folder(cli.config[cmd]['keyboard'])
  26. # Ensure that `--keyboard` was not passed and CWD is under `qmk_firmware/keyboards`
  27. if cli.config_source[cmd]['keyboard'] != 'argument':
  28. keyboard = find_keyboard_from_dir()
  29. if keyboard:
  30. cli.config[cmd]['keyboard'] = keyboard
  31. cli.config_source[cmd]['keyboard'] = 'keyboard_directory'
  32. return func(*args, **kwargs)
  33. return wrapper
  34. def automagic_keymap(func):
  35. """Sets `cli.config.<subcommand>.keymap` based on environment.
  36. 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.
  37. """
  38. @functools.wraps(func)
  39. def wrapper(*args, **kwargs):
  40. cmd = _get_subcommand_name()
  41. # Ensure that `--keymap` was not passed and that we're under `qmk_firmware`
  42. if cli.config_source[cmd]['keymap'] != 'argument':
  43. keymap_name, keymap_type = find_keymap_from_dir()
  44. if keymap_name:
  45. cli.config[cmd]['keymap'] = keymap_name
  46. cli.config_source[cmd]['keymap'] = keymap_type
  47. return func(*args, **kwargs)
  48. return wrapper
  49. def lru_cache(timeout=10, maxsize=128, typed=False):
  50. """Least Recently Used Cache- cache the result of a function.
  51. Args:
  52. timeout
  53. How many seconds to cache results for.
  54. maxsize
  55. The maximum size of the cache in bytes
  56. typed
  57. When `True` argument types will be taken into consideration, for example `3` and `3.0` will be treated as different keys.
  58. """
  59. def wrapper_cache(func):
  60. func = functools.lru_cache(maxsize=maxsize, typed=typed)(func)
  61. func.expiration = monotonic() + timeout
  62. @functools.wraps(func)
  63. def wrapped_func(*args, **kwargs):
  64. if monotonic() >= func.expiration:
  65. func.expiration = monotonic() + timeout
  66. func.cache_clear()
  67. return func(*args, **kwargs)
  68. wrapped_func.cache_info = func.cache_info
  69. wrapped_func.cache_clear = func.cache_clear
  70. return wrapped_func
  71. return wrapper_cache