2016-06-03 12:03:27 +03:00
|
|
|
"""Dependency injector IoC containers module."""
|
2016-05-27 14:37:15 +03:00
|
|
|
|
|
|
|
import six
|
|
|
|
|
2016-05-27 19:12:49 +03:00
|
|
|
from dependency_injector import (
|
2016-05-30 23:34:14 +03:00
|
|
|
providers,
|
2016-05-27 19:12:49 +03:00
|
|
|
utils,
|
|
|
|
errors,
|
|
|
|
)
|
2016-05-27 14:37:15 +03:00
|
|
|
|
|
|
|
|
2016-06-01 18:53:35 +03:00
|
|
|
class DynamicContainer(object):
|
2016-06-06 11:26:53 +03:00
|
|
|
"""Dynamic inversion of control container.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
services = DynamicContainer()
|
|
|
|
services.auth = providers.Factory(AuthService)
|
|
|
|
services.users = providers.Factory(UsersService,
|
|
|
|
auth_service=services.auth)
|
|
|
|
|
|
|
|
.. py:attribute:: providers
|
|
|
|
|
|
|
|
Read-only dictionary of all providers.
|
|
|
|
|
|
|
|
:type: dict[str, :py:class:`dependency_injector.providers.Provider`]
|
|
|
|
|
|
|
|
.. py:attribute:: overridden
|
|
|
|
|
|
|
|
Tuple of overriding containers.
|
|
|
|
|
|
|
|
:type: tuple[:py:class:`DynamicContainer`]
|
|
|
|
|
|
|
|
.. py:attribute:: provider_type
|
|
|
|
|
|
|
|
Type of providers that could be placed in container.
|
|
|
|
|
|
|
|
:type: type
|
|
|
|
"""
|
2016-06-01 18:53:35 +03:00
|
|
|
|
|
|
|
__IS_CONTAINER__ = True
|
|
|
|
|
|
|
|
def __init__(self):
|
2016-06-06 11:26:53 +03:00
|
|
|
"""Initializer.
|
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
2016-06-01 18:53:35 +03:00
|
|
|
self.provider_type = providers.Provider
|
|
|
|
self.providers = dict()
|
2016-06-06 11:26:53 +03:00
|
|
|
self.overridden = tuple()
|
2016-06-01 18:53:35 +03:00
|
|
|
super(DynamicContainer, self).__init__()
|
|
|
|
|
|
|
|
def __setattr__(self, name, value):
|
|
|
|
"""Set instance attribute.
|
|
|
|
|
|
|
|
If value of attribute is provider, it will be added into providers
|
|
|
|
dictionary.
|
2016-06-06 11:26:53 +03:00
|
|
|
|
|
|
|
:param name: Attribute's name
|
|
|
|
:type name: str
|
|
|
|
|
|
|
|
:param value: Attribute's value
|
|
|
|
:type value: object
|
|
|
|
|
|
|
|
:rtype: None
|
2016-06-01 18:53:35 +03:00
|
|
|
"""
|
|
|
|
if utils.is_provider(value):
|
|
|
|
_check_provider_type(self, value)
|
|
|
|
self.providers[name] = value
|
|
|
|
super(DynamicContainer, self).__setattr__(name, value)
|
|
|
|
|
|
|
|
def __delattr__(self, name):
|
|
|
|
"""Delete instance attribute.
|
|
|
|
|
|
|
|
If value of attribute is provider, it will be deleted from providers
|
|
|
|
dictionary.
|
2016-06-06 11:26:53 +03:00
|
|
|
|
|
|
|
:param name: Attribute's name
|
|
|
|
:type name: str
|
|
|
|
|
|
|
|
:rtype: None
|
2016-06-01 18:53:35 +03:00
|
|
|
"""
|
|
|
|
if name in self.providers:
|
|
|
|
del self.providers[name]
|
|
|
|
super(DynamicContainer, self).__delattr__(name)
|
|
|
|
|
|
|
|
def override(self, overriding):
|
|
|
|
"""Override current container by overriding container.
|
|
|
|
|
|
|
|
:param overriding: Overriding container.
|
2016-06-06 11:26:53 +03:00
|
|
|
:type overriding: :py:class:`DynamicContainer`
|
2016-06-01 18:53:35 +03:00
|
|
|
|
|
|
|
:raise: :py:exc:`dependency_injector.errors.Error` if trying to
|
2016-06-06 11:26:53 +03:00
|
|
|
override container by itself
|
2016-06-01 18:53:35 +03:00
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
|
|
|
if overriding is self:
|
|
|
|
raise errors.Error('Container {0} could not be overridden '
|
|
|
|
'with itself'.format(self))
|
|
|
|
|
2016-06-06 11:26:53 +03:00
|
|
|
self.overridden += (overriding,)
|
2016-06-01 18:53:35 +03:00
|
|
|
|
|
|
|
for name, provider in six.iteritems(overriding.providers):
|
|
|
|
try:
|
|
|
|
getattr(self, name).override(provider)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def reset_last_overriding(self):
|
|
|
|
"""Reset last overriding provider for each container providers.
|
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
2016-06-06 11:26:53 +03:00
|
|
|
if not self.overridden:
|
2016-06-01 18:53:35 +03:00
|
|
|
raise errors.Error('Container {0} is not overridden'.format(self))
|
|
|
|
|
2016-06-06 11:26:53 +03:00
|
|
|
self.overridden = self.overridden[:-1]
|
2016-06-01 18:53:35 +03:00
|
|
|
|
|
|
|
for provider in six.itervalues(self.providers):
|
|
|
|
provider.reset_last_overriding()
|
|
|
|
|
|
|
|
def reset_override(self):
|
|
|
|
"""Reset all overridings for each container providers.
|
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
2016-06-06 11:26:53 +03:00
|
|
|
self.overridden = tuple()
|
2016-06-01 18:53:35 +03:00
|
|
|
|
|
|
|
for provider in six.itervalues(self.providers):
|
|
|
|
provider.reset_override()
|
|
|
|
|
|
|
|
|
2016-05-27 14:37:15 +03:00
|
|
|
class DeclarativeContainerMetaClass(type):
|
|
|
|
"""Declarative inversion of control container meta class."""
|
|
|
|
|
|
|
|
def __new__(mcs, class_name, bases, attributes):
|
|
|
|
"""Declarative container class factory."""
|
|
|
|
cls_providers = tuple((name, provider)
|
|
|
|
for name, provider in six.iteritems(attributes)
|
|
|
|
if utils.is_provider(provider))
|
|
|
|
|
|
|
|
inherited_providers = tuple((name, provider)
|
2016-05-30 23:34:14 +03:00
|
|
|
for base in bases if utils.is_container(
|
2016-06-01 18:53:35 +03:00
|
|
|
base) and base is not DynamicContainer
|
2016-05-27 14:37:15 +03:00
|
|
|
for name, provider in six.iteritems(
|
|
|
|
base.cls_providers))
|
|
|
|
|
|
|
|
attributes['cls_providers'] = dict(cls_providers)
|
|
|
|
attributes['inherited_providers'] = dict(inherited_providers)
|
2016-05-27 19:12:49 +03:00
|
|
|
attributes['providers'] = dict(cls_providers + inherited_providers)
|
2016-05-27 14:37:15 +03:00
|
|
|
|
2016-05-29 22:05:43 +03:00
|
|
|
cls = type.__new__(mcs, class_name, bases, attributes)
|
|
|
|
|
2016-05-30 23:34:14 +03:00
|
|
|
for provider in six.itervalues(cls.providers):
|
2016-06-01 16:00:11 +03:00
|
|
|
_check_provider_type(cls, provider)
|
2016-05-29 22:05:43 +03:00
|
|
|
|
|
|
|
return cls
|
2016-05-27 14:37:15 +03:00
|
|
|
|
|
|
|
def __setattr__(cls, name, value):
|
|
|
|
"""Set class attribute.
|
|
|
|
|
|
|
|
If value of attribute is provider, it will be added into providers
|
|
|
|
dictionary.
|
2016-06-06 11:26:53 +03:00
|
|
|
|
|
|
|
:param name: Attribute's name
|
|
|
|
:type name: str
|
|
|
|
|
|
|
|
:param value: Attribute's value
|
|
|
|
:type value: object
|
|
|
|
|
|
|
|
:rtype: None
|
2016-05-27 14:37:15 +03:00
|
|
|
"""
|
|
|
|
if utils.is_provider(value):
|
2016-06-01 16:00:11 +03:00
|
|
|
_check_provider_type(cls, value)
|
2016-05-27 14:37:15 +03:00
|
|
|
cls.providers[name] = value
|
2016-05-27 19:12:49 +03:00
|
|
|
cls.cls_providers[name] = value
|
2016-05-27 14:37:15 +03:00
|
|
|
super(DeclarativeContainerMetaClass, cls).__setattr__(name, value)
|
|
|
|
|
2016-05-27 19:12:49 +03:00
|
|
|
def __delattr__(cls, name):
|
|
|
|
"""Delete class attribute.
|
2016-05-27 14:37:15 +03:00
|
|
|
|
2016-05-27 19:12:49 +03:00
|
|
|
If value of attribute is provider, it will be deleted from providers
|
2016-05-27 14:37:15 +03:00
|
|
|
dictionary.
|
2016-06-06 11:26:53 +03:00
|
|
|
|
|
|
|
:param name: Attribute's name
|
|
|
|
:type name: str
|
|
|
|
|
|
|
|
:rtype: None
|
2016-05-27 14:37:15 +03:00
|
|
|
"""
|
2016-05-27 19:12:49 +03:00
|
|
|
if name in cls.providers and name in cls.cls_providers:
|
|
|
|
del cls.providers[name]
|
|
|
|
del cls.cls_providers[name]
|
|
|
|
super(DeclarativeContainerMetaClass, cls).__delattr__(name)
|
2016-05-27 14:37:15 +03:00
|
|
|
|
|
|
|
|
|
|
|
@six.add_metaclass(DeclarativeContainerMetaClass)
|
|
|
|
class DeclarativeContainer(object):
|
2016-06-06 11:26:53 +03:00
|
|
|
"""Declarative inversion of control container.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
class Services(DeclarativeContainer):
|
|
|
|
auth = providers.Factory(AuthService)
|
|
|
|
users = providers.Factory(UsersService,
|
|
|
|
auth_service=auth)
|
|
|
|
"""
|
2016-05-27 14:37:15 +03:00
|
|
|
|
2016-05-30 23:34:14 +03:00
|
|
|
__IS_CONTAINER__ = True
|
2016-05-27 19:12:49 +03:00
|
|
|
|
2016-05-30 23:34:14 +03:00
|
|
|
provider_type = providers.Provider
|
2016-06-06 11:26:53 +03:00
|
|
|
"""Type of providers that could be placed in container.
|
|
|
|
|
|
|
|
:type: type
|
|
|
|
"""
|
|
|
|
|
|
|
|
instance_type = DynamicContainer
|
|
|
|
"""Type of container that is returned on instantiating declarative
|
|
|
|
container.
|
|
|
|
|
|
|
|
:type: type
|
|
|
|
"""
|
2016-05-29 22:05:43 +03:00
|
|
|
|
2016-05-27 19:12:49 +03:00
|
|
|
providers = dict()
|
2016-06-06 11:26:53 +03:00
|
|
|
"""Read-only dictionary of all providers.
|
|
|
|
|
|
|
|
:type: dict[str, :py:class:`dependency_injector.providers.Provider`]
|
|
|
|
"""
|
|
|
|
|
2016-05-27 14:37:15 +03:00
|
|
|
cls_providers = dict()
|
2016-06-06 11:26:53 +03:00
|
|
|
"""Read-only dictionary of current container providers.
|
|
|
|
|
|
|
|
:type: dict[str, :py:class:`dependency_injector.providers.Provider`]
|
|
|
|
"""
|
|
|
|
|
2016-05-27 14:37:15 +03:00
|
|
|
inherited_providers = dict()
|
2016-06-06 11:26:53 +03:00
|
|
|
"""Read-only dictionary of inherited providers.
|
2016-05-27 19:12:49 +03:00
|
|
|
|
2016-06-06 11:26:53 +03:00
|
|
|
:type: dict[str, :py:class:`dependency_injector.providers.Provider`]
|
|
|
|
"""
|
|
|
|
|
|
|
|
overridden = tuple()
|
|
|
|
"""Tuple of overriding containers.
|
|
|
|
|
|
|
|
:type: tuple[:py:class:`DeclarativeContainer`]
|
|
|
|
"""
|
2016-06-01 18:53:35 +03:00
|
|
|
|
|
|
|
def __new__(cls, *args, **kwargs):
|
2016-06-06 11:26:53 +03:00
|
|
|
"""Constructor.
|
|
|
|
|
|
|
|
:return: Dynamic container with copy of all providers.
|
|
|
|
:rtype: :py:class:`DynamicContainer`
|
|
|
|
"""
|
2016-06-01 18:53:35 +03:00
|
|
|
container = cls.instance_type(*args, **kwargs)
|
|
|
|
container.provider_type = cls.provider_type
|
|
|
|
|
|
|
|
for name, provider in six.iteritems(utils.deepcopy(cls.providers)):
|
|
|
|
setattr(container, name, provider)
|
|
|
|
|
|
|
|
return container
|
|
|
|
|
2016-05-27 19:12:49 +03:00
|
|
|
@classmethod
|
|
|
|
def override(cls, overriding):
|
|
|
|
"""Override current container by overriding container.
|
|
|
|
|
|
|
|
:param overriding: Overriding container.
|
|
|
|
:type overriding: :py:class:`DeclarativeContainer`
|
|
|
|
|
|
|
|
:raise: :py:exc:`dependency_injector.errors.Error` if trying to
|
|
|
|
override container by itself or its subclasses
|
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
|
|
|
if issubclass(cls, overriding):
|
2016-05-30 23:34:14 +03:00
|
|
|
raise errors.Error('Container {0} could not be overridden '
|
2016-05-27 19:12:49 +03:00
|
|
|
'with itself or its subclasses'.format(cls))
|
|
|
|
|
2016-06-06 11:26:53 +03:00
|
|
|
cls.overridden += (overriding,)
|
2016-05-27 19:12:49 +03:00
|
|
|
|
|
|
|
for name, provider in six.iteritems(overriding.cls_providers):
|
|
|
|
try:
|
|
|
|
getattr(cls, name).override(provider)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2016-05-27 14:37:15 +03:00
|
|
|
|
2016-05-30 23:34:14 +03:00
|
|
|
@classmethod
|
|
|
|
def reset_last_overriding(cls):
|
|
|
|
"""Reset last overriding provider for each container providers.
|
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
2016-06-06 11:26:53 +03:00
|
|
|
if not cls.overridden:
|
2016-05-30 23:34:14 +03:00
|
|
|
raise errors.Error('Container {0} is not overridden'.format(cls))
|
|
|
|
|
2016-06-06 11:26:53 +03:00
|
|
|
cls.overridden = cls.overridden[:-1]
|
2016-05-30 23:34:14 +03:00
|
|
|
|
|
|
|
for provider in six.itervalues(cls.providers):
|
|
|
|
provider.reset_last_overriding()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def reset_override(cls):
|
|
|
|
"""Reset all overridings for each container providers.
|
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
2016-06-06 11:26:53 +03:00
|
|
|
cls.overridden = tuple()
|
2016-05-30 23:34:14 +03:00
|
|
|
|
|
|
|
for provider in six.itervalues(cls.providers):
|
|
|
|
provider.reset_override()
|
|
|
|
|
2016-05-27 14:37:15 +03:00
|
|
|
|
2016-05-27 19:12:49 +03:00
|
|
|
def override(container):
|
2016-05-27 14:37:15 +03:00
|
|
|
""":py:class:`DeclarativeContainer` overriding decorator.
|
|
|
|
|
2016-05-29 17:17:27 +03:00
|
|
|
:param container: Container that should be overridden by decorated
|
|
|
|
container.
|
|
|
|
:type container: :py:class:`DeclarativeContainer`
|
2016-05-27 14:37:15 +03:00
|
|
|
|
|
|
|
:return: Declarative container's overriding decorator.
|
|
|
|
:rtype: callable(:py:class:`DeclarativeContainer`)
|
|
|
|
"""
|
2016-05-29 17:17:27 +03:00
|
|
|
def _decorator(overriding_container):
|
2016-05-27 14:37:15 +03:00
|
|
|
"""Overriding decorator."""
|
2016-05-27 19:12:49 +03:00
|
|
|
container.override(overriding_container)
|
2016-05-27 14:37:15 +03:00
|
|
|
return overriding_container
|
2016-05-29 17:17:27 +03:00
|
|
|
return _decorator
|
|
|
|
|
|
|
|
|
|
|
|
def copy(container):
|
|
|
|
""":py:class:`DeclarativeContainer` copying decorator.
|
|
|
|
|
|
|
|
This decorator copy all providers from provided container to decorated one.
|
|
|
|
If one of the decorated container providers matches to source container
|
|
|
|
providers by name, it would be replaced by reference.
|
|
|
|
|
|
|
|
:param container: Container that should be copied by decorated container.
|
2016-06-03 17:32:47 +03:00
|
|
|
:type container: :py:class:`DeclarativeContainer`
|
2016-05-29 17:17:27 +03:00
|
|
|
|
|
|
|
:return: Declarative container's copying decorator.
|
|
|
|
:rtype: callable(:py:class:`DeclarativeContainer`)
|
|
|
|
"""
|
|
|
|
def _decorator(copied_container):
|
|
|
|
memo = dict()
|
|
|
|
for name, provider in six.iteritems(copied_container.cls_providers):
|
|
|
|
try:
|
|
|
|
source_provider = getattr(container, name)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
memo[id(source_provider)] = provider
|
|
|
|
|
2016-06-01 18:53:35 +03:00
|
|
|
providers_copy = utils.deepcopy(container.providers, memo)
|
2016-05-29 17:17:27 +03:00
|
|
|
for name, provider in six.iteritems(providers_copy):
|
|
|
|
setattr(copied_container, name, provider)
|
|
|
|
|
|
|
|
return copied_container
|
|
|
|
return _decorator
|
2016-06-01 16:00:11 +03:00
|
|
|
|
|
|
|
|
|
|
|
def _check_provider_type(cls, provider):
|
|
|
|
if not isinstance(provider, cls.provider_type):
|
|
|
|
raise errors.Error('{0} can contain only {1} '
|
|
|
|
'instances'.format(cls, cls.provider_type))
|