utils.py 717 B

12345678910111213141516171819202122232425
  1. from __future__ import unicode_literals
  2. from prompt_toolkit.filters import CLIFilter, Always
  3. __all__ = (
  4. 'create_handle_decorator',
  5. )
  6. def create_handle_decorator(registry, filter=Always()):
  7. """
  8. Create a key handle decorator, which is compatible with `Registry.handle`,
  9. but will chain the given filter to every key binding.
  10. :param filter: `CLIFilter`
  11. """
  12. assert isinstance(filter, CLIFilter)
  13. def handle(*keys, **kw):
  14. # Chain the given filter to the filter of this specific binding.
  15. if 'filter' in kw:
  16. kw['filter'] = kw['filter'] & filter
  17. else:
  18. kw['filter'] = filter
  19. return registry.add_binding(*keys, **kw)
  20. return handle