2015-03-09 01:01:39 +03:00
|
|
|
"""Injections module."""
|
2015-01-10 12:24:25 +03:00
|
|
|
|
2015-09-01 00:30:38 +03:00
|
|
|
import six
|
2015-08-03 12:57:42 +03:00
|
|
|
|
2016-04-03 17:27:53 +03:00
|
|
|
from dependency_injector.utils import (
|
|
|
|
is_provider,
|
|
|
|
is_delegated_provider,
|
|
|
|
is_injection,
|
|
|
|
is_arg_injection,
|
|
|
|
is_kwarg_injection,
|
|
|
|
fetch_cls_init,
|
|
|
|
)
|
|
|
|
|
|
|
|
from dependency_injector.errors import Error
|
2015-09-28 14:19:22 +03:00
|
|
|
|
2015-01-10 12:24:25 +03:00
|
|
|
|
2015-12-07 15:31:51 +03:00
|
|
|
@six.python_2_unicode_compatible
|
2015-01-10 12:24:25 +03:00
|
|
|
class Injection(object):
|
2015-11-24 11:33:10 +03:00
|
|
|
"""Base injection class.
|
|
|
|
|
|
|
|
All injections extend this class.
|
2015-12-14 11:22:15 +03:00
|
|
|
|
|
|
|
.. py:attribute:: injectable
|
|
|
|
|
|
|
|
Injectable value, could be provider or any other object.
|
|
|
|
|
|
|
|
:type: object | :py:class:`dependency_injector.providers.Provider`
|
|
|
|
|
2015-12-28 18:25:25 +03:00
|
|
|
.. py:attribute:: call_injectable
|
2015-12-14 11:22:15 +03:00
|
|
|
|
2015-12-28 18:25:25 +03:00
|
|
|
Flag that is set to ``True`` if it is needed to call injectable.
|
|
|
|
|
|
|
|
Injectable needs to be called if it is not delegated provider.
|
2015-12-14 11:22:15 +03:00
|
|
|
|
|
|
|
:type: bool
|
2015-11-24 11:33:10 +03:00
|
|
|
"""
|
2015-01-10 12:24:25 +03:00
|
|
|
|
2015-07-22 10:53:16 +03:00
|
|
|
__IS_INJECTION__ = True
|
2015-12-28 18:25:25 +03:00
|
|
|
__slots__ = ('injectable', 'call_injectable')
|
2015-03-11 16:18:42 +03:00
|
|
|
|
2015-10-14 14:30:01 +03:00
|
|
|
def __init__(self, injectable):
|
2015-11-24 11:33:10 +03:00
|
|
|
"""Initializer.
|
|
|
|
|
|
|
|
:param injectable: Injectable value, could be provider or any
|
|
|
|
other object.
|
|
|
|
:type injectable: object |
|
|
|
|
:py:class:`dependency_injector.providers.Provider`
|
|
|
|
"""
|
2015-01-10 12:24:25 +03:00
|
|
|
self.injectable = injectable
|
2015-12-28 18:25:25 +03:00
|
|
|
self.call_injectable = (is_provider(injectable) and
|
|
|
|
not is_delegated_provider(injectable))
|
2015-11-24 11:33:10 +03:00
|
|
|
super(Injection, self).__init__()
|
2015-01-10 12:24:25 +03:00
|
|
|
|
2015-01-11 16:03:45 +03:00
|
|
|
@property
|
|
|
|
def value(self):
|
2015-11-24 11:33:10 +03:00
|
|
|
"""Read-only property that represents injectable value.
|
|
|
|
|
2015-12-28 18:25:25 +03:00
|
|
|
Injectable values and delegated providers are provided "as is".
|
|
|
|
Other providers will be called every time, when injection needs to
|
|
|
|
be done.
|
2015-11-24 11:33:10 +03:00
|
|
|
|
|
|
|
:rtype: object
|
|
|
|
"""
|
2015-12-28 18:25:25 +03:00
|
|
|
if self.call_injectable:
|
2016-03-13 23:46:32 +03:00
|
|
|
return self.injectable.provide()
|
2015-01-11 16:03:45 +03:00
|
|
|
return self.injectable
|
2015-01-10 12:24:25 +03:00
|
|
|
|
2015-12-09 20:28:52 +03:00
|
|
|
def __str__(self):
|
2015-12-07 15:31:51 +03:00
|
|
|
"""Return string representation of provider.
|
2015-01-10 12:24:25 +03:00
|
|
|
|
2015-12-07 15:31:51 +03:00
|
|
|
:rtype: str
|
|
|
|
"""
|
2015-12-09 20:28:52 +03:00
|
|
|
return '<{injection}({injectable}) at {address}>'.format(
|
|
|
|
injection='.'.join((self.__class__.__module__,
|
|
|
|
self.__class__.__name__)),
|
|
|
|
injectable=repr(self.injectable),
|
|
|
|
address=hex(id(self)))
|
2015-12-07 15:31:51 +03:00
|
|
|
|
|
|
|
__repr__ = __str__
|
|
|
|
|
|
|
|
|
|
|
|
class Arg(Injection):
|
|
|
|
"""Positional argument injection."""
|
|
|
|
|
|
|
|
__IS_ARG_INJECTION__ = True
|
|
|
|
|
|
|
|
|
|
|
|
@six.python_2_unicode_compatible
|
2015-11-16 14:28:27 +03:00
|
|
|
class _NamedInjection(Injection):
|
2015-12-14 11:22:15 +03:00
|
|
|
"""Base class of named injections.
|
|
|
|
|
|
|
|
.. py:attribute:: name
|
|
|
|
|
2016-05-17 22:37:50 +03:00
|
|
|
Injection target's name (keyword argument, attribute).
|
2015-12-14 11:22:15 +03:00
|
|
|
|
|
|
|
:type: str
|
|
|
|
"""
|
2015-10-14 14:30:01 +03:00
|
|
|
|
|
|
|
__slots__ = ('name',)
|
|
|
|
|
|
|
|
def __init__(self, name, injectable):
|
2015-12-11 12:35:48 +03:00
|
|
|
"""Initializer.
|
|
|
|
|
|
|
|
:param name: Injection target's name.
|
|
|
|
:type name: str
|
|
|
|
|
|
|
|
:param injectable: Injectable value, could be provider or any
|
|
|
|
other object.
|
|
|
|
:type injectable: object |
|
|
|
|
:py:class:`dependency_injector.providers.Provider`
|
|
|
|
"""
|
2015-10-14 14:30:01 +03:00
|
|
|
self.name = name
|
2015-11-16 14:28:27 +03:00
|
|
|
super(_NamedInjection, self).__init__(injectable)
|
2015-10-14 14:30:01 +03:00
|
|
|
|
2015-12-09 20:28:52 +03:00
|
|
|
def __str__(self):
|
2015-12-07 15:31:51 +03:00
|
|
|
"""Return string representation of provider.
|
2015-10-14 14:30:01 +03:00
|
|
|
|
2015-12-07 15:31:51 +03:00
|
|
|
:rtype: str
|
|
|
|
"""
|
2015-12-09 20:28:52 +03:00
|
|
|
return '<{injection}({name}, {injectable}) at {address}>'.format(
|
2015-12-07 15:31:51 +03:00
|
|
|
name=repr(self.name),
|
2015-12-09 20:28:52 +03:00
|
|
|
injection='.'.join((self.__class__.__module__,
|
|
|
|
self.__class__.__name__)),
|
|
|
|
injectable=repr(self.injectable),
|
|
|
|
address=hex(id(self)))
|
2015-12-07 15:31:51 +03:00
|
|
|
|
|
|
|
__repr__ = __str__
|
2015-10-14 14:30:01 +03:00
|
|
|
|
|
|
|
|
2015-11-16 14:28:27 +03:00
|
|
|
class KwArg(_NamedInjection):
|
2015-12-14 11:22:15 +03:00
|
|
|
"""Keyword argument injection.
|
|
|
|
|
|
|
|
.. py:attribute:: name
|
2015-01-10 12:24:25 +03:00
|
|
|
|
2015-12-14 11:22:15 +03:00
|
|
|
Keyword argument's name.
|
2015-12-11 12:35:48 +03:00
|
|
|
|
2015-12-14 11:22:15 +03:00
|
|
|
:type: str
|
2015-12-11 12:35:48 +03:00
|
|
|
"""
|
|
|
|
|
2015-07-22 10:53:16 +03:00
|
|
|
__IS_KWARG_INJECTION__ = True
|
2015-03-11 16:18:42 +03:00
|
|
|
|
2015-01-10 12:24:25 +03:00
|
|
|
|
2015-11-16 14:28:27 +03:00
|
|
|
class Attribute(_NamedInjection):
|
2015-12-14 11:22:15 +03:00
|
|
|
"""Attribute injection.
|
|
|
|
|
|
|
|
.. py:attribute:: name
|
2015-01-10 12:24:25 +03:00
|
|
|
|
2015-12-14 11:22:15 +03:00
|
|
|
Attribute's name.
|
2015-12-11 12:35:48 +03:00
|
|
|
|
2015-12-14 11:22:15 +03:00
|
|
|
:type: str
|
2015-12-11 12:35:48 +03:00
|
|
|
"""
|
|
|
|
|
2015-07-22 10:53:16 +03:00
|
|
|
__IS_ATTRIBUTE_INJECTION__ = True
|
2015-03-11 16:18:42 +03:00
|
|
|
|
2015-01-10 12:24:25 +03:00
|
|
|
|
2015-09-01 00:30:38 +03:00
|
|
|
def inject(*args, **kwargs):
|
2015-08-03 12:57:42 +03:00
|
|
|
"""Dependency injection decorator.
|
|
|
|
|
2015-11-24 11:33:10 +03:00
|
|
|
:py:func:`inject` decorator can be used for making inline dependency
|
|
|
|
injections. It patches decorated callable in such way that dependency
|
|
|
|
injection will be done during every call of decorated callable.
|
|
|
|
|
|
|
|
:py:func:`inject` decorator supports different syntaxes of passing
|
|
|
|
injections:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# Positional arguments injections (simplified syntax):
|
|
|
|
@inject(1, 2)
|
|
|
|
def some_function(arg1, arg2):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Keyword arguments injections (simplified syntax):
|
|
|
|
@inject(arg1=1)
|
|
|
|
@inject(arg2=2)
|
|
|
|
def some_function(arg1, arg2):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Keyword arguments injections (extended (full) syntax):
|
|
|
|
@inject(KwArg('arg1', 1))
|
|
|
|
@inject(KwArg('arg2', 2))
|
|
|
|
def some_function(arg1, arg2):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Keyword arguments injections into class init (simplified syntax):
|
|
|
|
@inject(arg1=1)
|
|
|
|
@inject(arg2=2)
|
|
|
|
class SomeClass(object):
|
|
|
|
|
|
|
|
def __init__(self, arg1, arg2):
|
|
|
|
pass
|
|
|
|
|
2015-11-25 16:02:20 +03:00
|
|
|
:param args: Tuple of context positional arguments.
|
|
|
|
:type args: tuple[object]
|
|
|
|
|
|
|
|
:param kwargs: Dictionary of context keyword arguments.
|
|
|
|
:type kwargs: dict[str, object]
|
|
|
|
|
2015-11-24 11:33:10 +03:00
|
|
|
:return: Class / callable decorator
|
|
|
|
:rtype: (callable) -> (type | callable)
|
2015-08-03 12:57:42 +03:00
|
|
|
"""
|
2015-10-23 09:53:53 +03:00
|
|
|
arg_injections = _parse_args_injections(args)
|
|
|
|
kwarg_injections = _parse_kwargs_injections(args, kwargs)
|
2015-08-03 12:57:42 +03:00
|
|
|
|
2015-09-28 22:10:17 +03:00
|
|
|
def decorator(callback_or_cls):
|
2015-08-03 12:57:42 +03:00
|
|
|
"""Dependency injection decorator."""
|
2015-09-28 22:10:17 +03:00
|
|
|
if isinstance(callback_or_cls, six.class_types):
|
|
|
|
cls = callback_or_cls
|
2016-03-01 16:42:06 +03:00
|
|
|
cls_init = fetch_cls_init(cls)
|
|
|
|
if not cls_init:
|
|
|
|
raise Error(
|
|
|
|
'Class {0}.{1} has no __init__() '.format(cls.__module__,
|
|
|
|
cls.__name__) +
|
|
|
|
'method and could not be decorated with @inject decorator')
|
|
|
|
cls.__init__ = decorator(cls_init)
|
2015-09-28 14:19:22 +03:00
|
|
|
return cls
|
|
|
|
|
2015-09-28 22:10:17 +03:00
|
|
|
callback = callback_or_cls
|
2016-03-01 13:25:54 +03:00
|
|
|
|
2016-03-01 17:28:05 +03:00
|
|
|
if hasattr(callback, '__INJECT_DECORATED__'):
|
2015-10-23 09:53:53 +03:00
|
|
|
callback.args += arg_injections
|
|
|
|
callback.kwargs += kwarg_injections
|
|
|
|
callback.injections += arg_injections + kwarg_injections
|
2015-09-01 00:30:38 +03:00
|
|
|
return callback
|
2015-08-03 12:57:42 +03:00
|
|
|
|
2015-09-01 00:30:38 +03:00
|
|
|
@six.wraps(callback)
|
2015-08-03 12:57:42 +03:00
|
|
|
def decorated(*args, **kwargs):
|
|
|
|
"""Decorated with dependency injection callback."""
|
2016-02-08 01:29:41 +03:00
|
|
|
if decorated.args:
|
|
|
|
args = tuple(arg.value for arg in decorated.args) + args
|
|
|
|
|
|
|
|
for kwarg in decorated.kwargs:
|
|
|
|
if kwarg.name not in kwargs:
|
|
|
|
kwargs[kwarg.name] = kwarg.value
|
|
|
|
|
|
|
|
return callback(*args, **kwargs)
|
2015-08-03 12:57:42 +03:00
|
|
|
|
2016-03-01 17:28:05 +03:00
|
|
|
decorated.__INJECT_DECORATED__ = True
|
|
|
|
decorated.origin = callback
|
2015-10-23 09:53:53 +03:00
|
|
|
decorated.args = arg_injections
|
|
|
|
decorated.kwargs = kwarg_injections
|
|
|
|
decorated.injections = arg_injections + kwarg_injections
|
2015-08-03 12:57:42 +03:00
|
|
|
|
|
|
|
return decorated
|
|
|
|
return decorator
|
2015-10-21 11:44:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
def _parse_args_injections(args):
|
|
|
|
"""Parse positional argument injections according to current syntax."""
|
|
|
|
return tuple(Arg(arg) if not is_injection(arg) else arg
|
|
|
|
for arg in args
|
|
|
|
if not is_injection(arg) or is_arg_injection(arg))
|
|
|
|
|
|
|
|
|
|
|
|
def _parse_kwargs_injections(args, kwargs):
|
|
|
|
"""Parse keyword argument injections according to current syntax."""
|
|
|
|
kwarg_injections = tuple(injection
|
|
|
|
for injection in args
|
|
|
|
if is_kwarg_injection(injection))
|
|
|
|
if kwargs:
|
|
|
|
kwarg_injections += tuple(KwArg(name, value)
|
|
|
|
for name, value in six.iteritems(kwargs))
|
|
|
|
return kwarg_injections
|