diff options
author | Ivan Blinkov <ivan@blinkov.ru> | 2022-02-10 16:47:11 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:11 +0300 |
commit | 5b283123c882433dafbaf6b338adeea16c1a0ea0 (patch) | |
tree | 339adc63bce23800021202ae4a8328a843dc447a /contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/registry.py | |
parent | 1aeb9a455974457866f78722ad98114bafc84e8a (diff) | |
download | ydb-5b283123c882433dafbaf6b338adeea16c1a0ea0.tar.gz |
Restoring authorship annotation for Ivan Blinkov <ivan@blinkov.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/registry.py')
-rw-r--r-- | contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/registry.py | 200 |
1 files changed, 100 insertions, 100 deletions
diff --git a/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/registry.py b/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/registry.py index 5df089b20c..24d0e729a1 100644 --- a/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/registry.py +++ b/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/registry.py @@ -23,110 +23,110 @@ others contain the Vi bindings. They are merged together using a We also have a `ConditionalRegistry` object that can enable/disable a group of key bindings at once. """ -from __future__ import unicode_literals +from __future__ import unicode_literals from abc import ABCMeta, abstractmethod from prompt_toolkit.cache import SimpleCache from prompt_toolkit.filters import CLIFilter, to_cli_filter, Never from prompt_toolkit.keys import Key, Keys - + from six import text_type, with_metaclass - -__all__ = ( + +__all__ = ( 'BaseRegistry', - 'Registry', + 'Registry', 'ConditionalRegistry', 'MergedRegistry', -) - - -class _Binding(object): - """ - (Immutable binding class.) - """ +) + + +class _Binding(object): + """ + (Immutable binding class.) + """ def __init__(self, keys, handler, filter=None, eager=None, save_before=None): - assert isinstance(keys, tuple) - assert callable(handler) - assert isinstance(filter, CLIFilter) - assert isinstance(eager, CLIFilter) + assert isinstance(keys, tuple) + assert callable(handler) + assert isinstance(filter, CLIFilter) + assert isinstance(eager, CLIFilter) assert callable(save_before) - - self.keys = keys - self.handler = handler - self.filter = filter - self.eager = eager + + self.keys = keys + self.handler = handler + self.filter = filter + self.eager = eager self.save_before = save_before - - def call(self, event): - return self.handler(event) - - def __repr__(self): - return '%s(keys=%r, handler=%r)' % ( - self.__class__.__name__, self.keys, self.handler) - - + + def call(self, event): + return self.handler(event) + + def __repr__(self): + return '%s(keys=%r, handler=%r)' % ( + self.__class__.__name__, self.keys, self.handler) + + class BaseRegistry(with_metaclass(ABCMeta, object)): - """ + """ Interface for a Registry. """ _version = 0 # For cache invalidation. - + @abstractmethod def get_bindings_for_keys(self, keys): pass - + @abstractmethod def get_bindings_starting_with_keys(self, keys): pass - + # `add_binding` and `remove_binding` don't have to be part of this # interface. class Registry(BaseRegistry): - """ + """ Key binding registry. """ - def __init__(self): - self.key_bindings = [] + def __init__(self): + self.key_bindings = [] self._get_bindings_for_keys_cache = SimpleCache(maxsize=10000) self._get_bindings_starting_with_keys_cache = SimpleCache(maxsize=1000) self._version = 0 # For cache invalidation. - + def _clear_cache(self): self._version += 1 self._get_bindings_for_keys_cache.clear() self._get_bindings_starting_with_keys_cache.clear() - - def add_binding(self, *keys, **kwargs): - """ - Decorator for annotating key bindings. - - :param filter: :class:`~prompt_toolkit.filters.CLIFilter` to determine - when this key binding is active. - :param eager: :class:`~prompt_toolkit.filters.CLIFilter` or `bool`. - When True, ignore potential longer matches when this key binding is - hit. E.g. when there is an active eager key binding for Ctrl-X, - execute the handler immediately and ignore the key binding for - Ctrl-X Ctrl-E of which it is a prefix. + + def add_binding(self, *keys, **kwargs): + """ + Decorator for annotating key bindings. + + :param filter: :class:`~prompt_toolkit.filters.CLIFilter` to determine + when this key binding is active. + :param eager: :class:`~prompt_toolkit.filters.CLIFilter` or `bool`. + When True, ignore potential longer matches when this key binding is + hit. E.g. when there is an active eager key binding for Ctrl-X, + execute the handler immediately and ignore the key binding for + Ctrl-X Ctrl-E of which it is a prefix. :param save_before: Callable that takes an `Event` and returns True if we should save the current buffer, before handling the event. (That's the default.) - """ - filter = to_cli_filter(kwargs.pop('filter', True)) - eager = to_cli_filter(kwargs.pop('eager', False)) + """ + filter = to_cli_filter(kwargs.pop('filter', True)) + eager = to_cli_filter(kwargs.pop('eager', False)) save_before = kwargs.pop('save_before', lambda e: True) to_cli_filter(kwargs.pop('invalidate_ui', True)) # Deprecated! (ignored.) - - assert not kwargs - assert keys - assert all(isinstance(k, (Key, text_type)) for k in keys), \ - 'Key bindings should consist of Key and string (unicode) instances.' + + assert not kwargs + assert keys + assert all(isinstance(k, (Key, text_type)) for k in keys), \ + 'Key bindings should consist of Key and string (unicode) instances.' assert callable(save_before) - + if isinstance(filter, Never): - # When a filter is Never, it will always stay disabled, so in that case - # don't bother putting it in the registry. It will slow down every key + # When a filter is Never, it will always stay disabled, so in that case + # don't bother putting it in the registry. It will slow down every key # press otherwise. def decorator(func): return func @@ -136,44 +136,44 @@ class Registry(BaseRegistry): _Binding(keys, func, filter=filter, eager=eager, save_before=save_before)) self._clear_cache() - + return func - return decorator - - def remove_binding(self, function): - """ - Remove a key binding. - - This expects a function that was given to `add_binding` method as - parameter. Raises `ValueError` when the given function was not - registered before. - """ - assert callable(function) - - for b in self.key_bindings: - if b.handler == function: - self.key_bindings.remove(b) + return decorator + + def remove_binding(self, function): + """ + Remove a key binding. + + This expects a function that was given to `add_binding` method as + parameter. Raises `ValueError` when the given function was not + registered before. + """ + assert callable(function) + + for b in self.key_bindings: + if b.handler == function: + self.key_bindings.remove(b) self._clear_cache() - return - - # No key binding found for this function. Raise ValueError. - raise ValueError('Binding not found: %r' % (function, )) - - def get_bindings_for_keys(self, keys): - """ - Return a list of key bindings that can handle this key. - (This return also inactive bindings, so the `filter` still has to be - called, for checking it.) - - :param keys: tuple of keys. - """ + return + + # No key binding found for this function. Raise ValueError. + raise ValueError('Binding not found: %r' % (function, )) + + def get_bindings_for_keys(self, keys): + """ + Return a list of key bindings that can handle this key. + (This return also inactive bindings, so the `filter` still has to be + called, for checking it.) + + :param keys: tuple of keys. + """ def get(): result = [] for b in self.key_bindings: if len(keys) == len(b.keys): match = True any_count = 0 - + for i, j in zip(b.keys, keys): if i != j and i != Keys.Any: match = False @@ -192,15 +192,15 @@ class Registry(BaseRegistry): return self._get_bindings_for_keys_cache.get(keys, get) - def get_bindings_starting_with_keys(self, keys): - """ - Return a list of key bindings that handle a key sequence starting with - `keys`. (It does only return bindings for which the sequences are - longer than `keys`. And like `get_bindings_for_keys`, it also includes - inactive bindings.) - - :param keys: tuple of keys. - """ + def get_bindings_starting_with_keys(self, keys): + """ + Return a list of key bindings that handle a key sequence starting with + `keys`. (It does only return bindings for which the sequences are + longer than `keys`. And like `get_bindings_for_keys`, it also includes + inactive bindings.) + + :param keys: tuple of keys. + """ def get(): result = [] for b in self.key_bindings: |