2017-09-25 16:05:09 +00:00
|
|
|
"""Decorator utility functions."""
|
2018-07-23 08:24:39 +00:00
|
|
|
from typing import Callable, TypeVar
|
2018-07-26 06:55:42 +00:00
|
|
|
|
2019-11-16 09:22:07 +00:00
|
|
|
CALLABLE_T = TypeVar("CALLABLE_T", bound=Callable) # pylint: disable=invalid-name
|
2017-09-25 16:05:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Registry(dict):
|
|
|
|
"""Registry of items."""
|
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
def register(self, name: str) -> Callable[[CALLABLE_T], CALLABLE_T]:
|
2017-09-25 16:05:09 +00:00
|
|
|
"""Return decorator to register item with a specific name."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
def decorator(func: CALLABLE_T) -> CALLABLE_T:
|
2017-09-25 16:05:09 +00:00
|
|
|
"""Register decorated function."""
|
|
|
|
self[name] = func
|
|
|
|
return func
|
|
|
|
|
|
|
|
return decorator
|