mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 19:14:00 +03:00
Refactoring of @inject decorator
This commit is contained in:
parent
05b65f432c
commit
f67343c45c
|
@ -15,7 +15,7 @@ def override(catalog):
|
||||||
|
|
||||||
|
|
||||||
def inject(injection):
|
def inject(injection):
|
||||||
"""Inject decorator.
|
"""Dependency injection decorator.
|
||||||
|
|
||||||
:type injection: Injection
|
:type injection: Injection
|
||||||
:return: (callable) -> (callable)
|
:return: (callable) -> (callable)
|
||||||
|
@ -23,12 +23,19 @@ def inject(injection):
|
||||||
injection = ensure_is_injection(injection)
|
injection = ensure_is_injection(injection)
|
||||||
|
|
||||||
def decorator(callback):
|
def decorator(callback):
|
||||||
"""Decorator."""
|
"""Dependency injection decorator."""
|
||||||
|
if hasattr(callback, '__injections__'):
|
||||||
|
callback.__injections__ += (injection,)
|
||||||
|
|
||||||
@wraps(callback)
|
@wraps(callback)
|
||||||
def decorated(*args, **kwargs):
|
def decorated(*args, **kwargs):
|
||||||
"""Decorated."""
|
"""Decorated with dependency injection callback."""
|
||||||
|
for injection in getattr(decorated, '__injections__'):
|
||||||
if injection.name not in kwargs:
|
if injection.name not in kwargs:
|
||||||
kwargs[injection.name] = injection.value
|
kwargs[injection.name] = injection.value
|
||||||
return callback(*args, **kwargs)
|
return callback(*args, **kwargs)
|
||||||
|
|
||||||
|
setattr(decorated, '__injections__', (injection,))
|
||||||
|
|
||||||
return decorated
|
return decorated
|
||||||
return decorator
|
return decorator
|
||||||
|
|
Loading…
Reference in New Issue
Block a user