2015-04-14 23:17:53 +03:00
|
|
|
"""Decorators module."""
|
|
|
|
|
|
|
|
from six import wraps
|
|
|
|
|
|
|
|
from .utils import ensure_is_injection
|
2015-07-27 01:44:20 +03:00
|
|
|
from .utils import get_injectable_kwargs
|
2015-04-14 23:17:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
def override(catalog):
|
|
|
|
"""Catalog overriding decorator."""
|
|
|
|
def decorator(overriding_catalog):
|
|
|
|
"""Overriding decorator."""
|
|
|
|
catalog.override(overriding_catalog)
|
|
|
|
return overriding_catalog
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
def inject(injection):
|
2015-05-16 22:01:03 +03:00
|
|
|
"""Dependency injection decorator.
|
2015-04-14 23:17:53 +03:00
|
|
|
|
|
|
|
:type injection: Injection
|
|
|
|
:return: (callable) -> (callable)
|
|
|
|
"""
|
|
|
|
injection = ensure_is_injection(injection)
|
|
|
|
|
|
|
|
def decorator(callback):
|
2015-05-16 22:01:03 +03:00
|
|
|
"""Dependency injection decorator."""
|
2015-07-22 16:42:36 +03:00
|
|
|
if hasattr(callback, '_injections'):
|
|
|
|
callback._injections += (injection,)
|
2015-05-16 22:01:03 +03:00
|
|
|
|
2015-04-14 23:17:53 +03:00
|
|
|
@wraps(callback)
|
|
|
|
def decorated(*args, **kwargs):
|
2015-05-16 22:01:03 +03:00
|
|
|
"""Decorated with dependency injection callback."""
|
2015-07-27 01:44:20 +03:00
|
|
|
return callback(*args,
|
|
|
|
**get_injectable_kwargs(kwargs,
|
|
|
|
getattr(decorated,
|
|
|
|
'_injections')))
|
2015-05-16 22:01:03 +03:00
|
|
|
|
2015-07-22 16:42:36 +03:00
|
|
|
setattr(decorated, '_injections', (injection,))
|
2015-05-16 22:01:03 +03:00
|
|
|
|
2015-04-14 23:17:53 +03:00
|
|
|
return decorated
|
|
|
|
return decorator
|