2015-03-09 01:01:39 +03:00
|
|
|
"""Injections module."""
|
2015-01-10 12:24:25 +03:00
|
|
|
|
2015-09-28 18:45:15 +03:00
|
|
|
import sys
|
2015-09-01 00:30:38 +03:00
|
|
|
import six
|
2015-08-03 12:57:42 +03:00
|
|
|
|
2015-03-11 16:18:42 +03:00
|
|
|
from .utils import is_provider
|
2015-08-03 12:57:42 +03:00
|
|
|
from .utils import ensure_is_injection
|
|
|
|
from .utils import get_injectable_kwargs
|
2015-03-11 16:18:42 +03:00
|
|
|
|
2015-09-28 14:19:22 +03:00
|
|
|
from .errors import Error
|
|
|
|
|
2015-01-10 12:24:25 +03:00
|
|
|
|
2015-09-28 18:45:15 +03:00
|
|
|
IS_PYPY = '__pypy__' in sys.builtin_module_names
|
|
|
|
if IS_PYPY or six.PY3: # pragma: no cover
|
|
|
|
OBJECT_INIT = six.get_unbound_function(object.__init__)
|
|
|
|
else: # pragma: no cover
|
|
|
|
OBJECT_INIT = None
|
|
|
|
|
2015-01-10 12:24:25 +03:00
|
|
|
|
|
|
|
class Injection(object):
|
2015-03-09 01:01:39 +03:00
|
|
|
"""Base injection class."""
|
2015-01-10 12:24:25 +03:00
|
|
|
|
2015-07-22 10:53:16 +03:00
|
|
|
__IS_INJECTION__ = True
|
2015-07-11 23:34:23 +03:00
|
|
|
__slots__ = ('name', 'injectable')
|
2015-03-11 16:18:42 +03:00
|
|
|
|
2015-07-11 23:34:23 +03:00
|
|
|
def __init__(self, name, injectable):
|
2015-03-09 01:01:39 +03:00
|
|
|
"""Initializer."""
|
2015-01-10 12:24:25 +03:00
|
|
|
self.name = name
|
|
|
|
self.injectable = injectable
|
|
|
|
|
2015-01-11 16:03:45 +03:00
|
|
|
@property
|
|
|
|
def value(self):
|
2015-03-09 01:01:39 +03:00
|
|
|
"""Return injectable value."""
|
2015-07-11 23:34:23 +03:00
|
|
|
if is_provider(self.injectable):
|
2015-01-11 16:03:45 +03:00
|
|
|
return self.injectable()
|
|
|
|
return self.injectable
|
2015-01-10 12:24:25 +03:00
|
|
|
|
|
|
|
|
2015-03-23 02:04:18 +03:00
|
|
|
class KwArg(Injection):
|
|
|
|
"""Keyword argument injection."""
|
2015-01-10 12:24:25 +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
|
|
|
|
|
|
|
class Attribute(Injection):
|
2015-03-09 01:01:39 +03:00
|
|
|
"""Attribute injection."""
|
2015-01-10 12:24:25 +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
|
|
|
|
|
|
|
class Method(Injection):
|
2015-03-09 01:01:39 +03:00
|
|
|
"""Method injection."""
|
2015-03-11 16:18:42 +03:00
|
|
|
|
2015-07-22 10:53:16 +03:00
|
|
|
__IS_METHOD_INJECTION__ = True
|
2015-08-03 12:57:42 +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.
|
|
|
|
|
|
|
|
:type injection: Injection
|
|
|
|
:return: (callable) -> (callable)
|
|
|
|
"""
|
2015-09-03 16:00:23 +03:00
|
|
|
injections = tuple(KwArg(name, value)
|
|
|
|
for name, value in six.iteritems(kwargs))
|
2015-09-01 00:30:38 +03:00
|
|
|
if args:
|
2015-09-03 16:00:23 +03:00
|
|
|
injections += tuple(ensure_is_injection(injection)
|
|
|
|
for injection in args)
|
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
|
2015-09-28 14:19:22 +03:00
|
|
|
try:
|
2015-09-28 14:32:07 +03:00
|
|
|
cls_init = six.get_unbound_function(cls.__init__)
|
2015-09-28 18:45:15 +03:00
|
|
|
assert cls_init is not OBJECT_INIT
|
2015-09-28 14:32:07 +03:00
|
|
|
except (AttributeError, AssertionError):
|
2015-09-28 14:19:22 +03:00
|
|
|
raise Error(
|
2015-09-28 18:45:15 +03:00
|
|
|
'Class {0}.{1} has no __init__() '.format(cls.__module__,
|
|
|
|
cls.__name__) +
|
2015-09-28 14:19:22 +03:00
|
|
|
'method and could not be decorated with @inject decorator')
|
|
|
|
cls.__init__ = decorator(cls_init)
|
|
|
|
return cls
|
|
|
|
|
2015-09-28 22:10:17 +03:00
|
|
|
callback = callback_or_cls
|
2015-09-14 10:53:24 +03:00
|
|
|
if hasattr(callback, 'injections'):
|
|
|
|
callback.injections += 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."""
|
|
|
|
return callback(*args,
|
|
|
|
**get_injectable_kwargs(kwargs,
|
2015-09-14 10:53:24 +03:00
|
|
|
decorated.injections))
|
2015-08-03 12:57:42 +03:00
|
|
|
|
2015-09-14 10:53:24 +03:00
|
|
|
decorated.injections = injections
|
2015-08-03 12:57:42 +03:00
|
|
|
|
|
|
|
return decorated
|
|
|
|
return decorator
|