python-dependency-injector/dependency_injector/providers.py

328 lines
9.7 KiB
Python
Raw Normal View History

"""Providers module."""
2015-01-04 17:26:33 +03:00
import six
from .injections import KwArg
2015-03-10 12:51:13 +03:00
from .utils import ensure_is_provider
from .utils import is_kwarg_injection
from .utils import is_attribute_injection
from .utils import is_method_injection
2015-07-27 01:44:20 +03:00
from .utils import get_injectable_kwargs
2015-09-04 02:33:15 +03:00
from .utils import GLOBAL_LOCK
2015-01-10 12:24:25 +03:00
2015-03-13 18:31:07 +03:00
from .errors import Error
2015-01-04 17:26:33 +03:00
class Provider(object):
2015-03-09 01:01:39 +03:00
"""Base provider class."""
2015-01-04 17:26:33 +03:00
2015-07-22 10:53:16 +03:00
__IS_PROVIDER__ = True
2015-10-14 17:51:05 +03:00
__slots__ = ('overridden_by', 'bind')
2015-01-11 19:10:11 +03:00
def __init__(self):
2015-03-09 01:01:39 +03:00
"""Initializer."""
2015-09-14 10:53:24 +03:00
self.overridden_by = None
2015-10-14 17:51:05 +03:00
self.bind = None
2015-01-11 16:03:45 +03:00
2015-01-04 17:26:33 +03:00
def __call__(self, *args, **kwargs):
2015-03-09 01:01:39 +03:00
"""Return provided instance."""
2015-09-14 10:53:24 +03:00
if self.overridden_by:
2015-05-25 10:45:52 +03:00
return self.last_overriding(*args, **kwargs)
return self._provide(*args, **kwargs)
2015-05-25 10:45:52 +03:00
def _provide(self, *args, **kwargs):
2015-05-25 10:45:52 +03:00
"""Providing strategy implementation.
Abstract protected method that implements providing strategy of
particular provider. Current method is called every time when not
overridden provider is called. Need to be overridden in subclasses.
"""
2015-01-04 17:26:33 +03:00
raise NotImplementedError()
2015-02-23 01:16:27 +03:00
def delegate(self):
"""Return provider's delegate."""
2015-03-14 01:02:01 +03:00
return Delegate(self)
2015-02-25 02:48:51 +03:00
def override(self, provider):
"""Override provider with another provider."""
2015-09-14 10:53:24 +03:00
if not self.is_overridden:
self.overridden_by = (ensure_is_provider(provider),)
else:
2015-09-14 10:53:24 +03:00
self.overridden_by += (ensure_is_provider(provider),)
@property
def is_overridden(self):
"""Check if provider is overridden by another provider."""
2015-09-14 10:53:24 +03:00
return bool(self.overridden_by)
@property
def last_overriding(self):
"""Return last overriding provider."""
try:
2015-09-14 10:53:24 +03:00
return self.overridden_by[-1]
except (TypeError, IndexError):
raise Error('Provider {0} is not overridden'.format(str(self)))
def reset_last_overriding(self):
"""Reset last overriding provider."""
2015-09-14 10:53:24 +03:00
if not self.is_overridden:
raise Error('Provider {0} is not overridden'.format(str(self)))
2015-09-14 10:53:24 +03:00
self.overridden_by = self.overridden_by[:-1]
def reset_override(self):
"""Reset all overriding providers."""
2015-09-14 10:53:24 +03:00
self.overridden_by = None
2015-10-16 23:54:51 +03:00
@property
def is_bound(self):
"""Check if provider is bound to any catalog."""
return bool(self.bind)
2015-02-25 02:48:51 +03:00
2015-03-14 01:02:01 +03:00
class Delegate(Provider):
2015-03-09 01:01:39 +03:00
"""Provider's delegate."""
2015-02-25 02:48:51 +03:00
2015-09-14 10:53:24 +03:00
__slots__ = ('delegated',)
2015-02-25 02:48:51 +03:00
def __init__(self, delegated):
2015-03-09 01:01:39 +03:00
"""Initializer.
2015-02-25 02:48:51 +03:00
:type delegated: Provider
"""
2015-09-14 10:53:24 +03:00
self.delegated = ensure_is_provider(delegated)
2015-03-14 01:02:01 +03:00
super(Delegate, self).__init__()
2015-02-25 02:48:51 +03:00
def _provide(self, *args, **kwargs):
2015-03-09 01:01:39 +03:00
"""Return provided instance."""
2015-09-14 10:53:24 +03:00
return self.delegated
2015-02-23 01:16:27 +03:00
2015-01-11 16:03:45 +03:00
class Factory(Provider):
"""Factory provider.
2015-01-04 17:26:33 +03:00
Factory provider creates new instance of specified class on every call.
2015-01-04 17:26:33 +03:00
"""
2015-09-14 10:53:24 +03:00
__slots__ = ('provides', 'kwargs', 'attributes', 'methods')
def __init__(self, provides, *injections, **kwargs):
2015-03-09 01:01:39 +03:00
"""Initializer."""
if not callable(provides):
raise Error('Factory provider expects to get callable, ' +
'got {0} instead'.format(str(provides)))
2015-09-14 10:53:24 +03:00
self.provides = provides
self.kwargs = tuple(injection
for injection in injections
if is_kwarg_injection(injection))
if kwargs:
2015-09-14 10:53:24 +03:00
self.kwargs += tuple(KwArg(name, value)
for name, value in six.iteritems(kwargs))
self.attributes = tuple(injection
for injection in injections
if is_attribute_injection(injection))
self.methods = tuple(injection
for injection in injections
if is_method_injection(injection))
super(Factory, self).__init__()
2015-01-04 17:26:33 +03:00
def _provide(self, *args, **kwargs):
2015-03-09 01:01:39 +03:00
"""Return provided instance."""
2015-09-14 10:53:24 +03:00
instance = self.provides(*args,
**get_injectable_kwargs(kwargs,
self.kwargs))
for attribute in self.attributes:
setattr(instance, attribute.name, attribute.value)
2015-09-14 10:53:24 +03:00
for method in self.methods:
getattr(instance, method.name)(method.value)
2015-01-04 17:26:33 +03:00
2015-03-09 01:01:39 +03:00
return instance
2015-01-04 17:26:33 +03:00
2015-09-14 10:53:24 +03:00
@property
def injections(self):
"""Return tuple of all injections."""
return self.kwargs + self.attributes + self.methods
class Singleton(Provider):
2015-03-09 01:01:39 +03:00
"""Singleton provider.
2015-01-04 17:26:33 +03:00
Singleton provider will create instance once and return it on every call.
"""
2015-09-14 10:53:24 +03:00
__slots__ = ('instance', 'factory')
def __init__(self, provides, *injections, **kwargs):
2015-03-09 01:01:39 +03:00
"""Initializer."""
2015-09-14 10:53:24 +03:00
self.instance = None
self.factory = Factory(provides, *injections, **kwargs)
super(Singleton, self).__init__()
2015-01-04 17:26:33 +03:00
def _provide(self, *args, **kwargs):
2015-03-09 01:01:39 +03:00
"""Return provided instance."""
2015-09-04 02:33:15 +03:00
with GLOBAL_LOCK:
2015-09-14 10:53:24 +03:00
if not self.instance:
self.instance = self.factory(*args, **kwargs)
return self.instance
2015-01-04 17:26:33 +03:00
2015-03-15 01:50:24 +03:00
def reset(self):
2015-03-09 01:01:39 +03:00
"""Reset instance."""
2015-09-14 10:53:24 +03:00
self.instance = None
2015-01-28 01:21:31 +03:00
class ExternalDependency(Provider):
"""External dependency provider.
Those provider is used when dependency obviously have to be overridden by
the client's code, but it's interface is known.
"""
2015-09-14 10:53:24 +03:00
__slots__ = ('instance_of',)
2015-03-09 01:01:39 +03:00
def __init__(self, instance_of):
"""Initializer."""
if not isinstance(instance_of, six.class_types):
2015-03-23 17:27:48 +03:00
raise Error('ExternalDependency provider expects to get class, ' +
'got {0} instead'.format(str(instance_of)))
2015-09-14 10:53:24 +03:00
self.instance_of = instance_of
super(ExternalDependency, self).__init__()
def __call__(self, *args, **kwargs):
2015-03-09 01:01:39 +03:00
"""Return provided instance."""
2015-09-14 10:53:24 +03:00
if not self.is_overridden:
raise Error('Dependency is not defined')
instance = self.last_overriding(*args, **kwargs)
2015-09-14 10:53:24 +03:00
if not isinstance(instance, self.instance_of):
raise Error('{0} is not an '.format(instance) +
2015-09-14 10:53:24 +03:00
'instance of {0}'.format(self.instance_of))
return instance
def provided_by(self, provider):
"""Set external dependency provider."""
return self.override(provider)
2015-09-14 10:53:24 +03:00
class StaticProvider(Provider):
2015-03-09 01:01:39 +03:00
"""Static provider.
2015-01-04 17:26:33 +03:00
Static provider is base implementation that provides exactly the same as
it got on input.
"""
2015-09-14 10:53:24 +03:00
__slots__ = ('provides',)
2015-01-04 17:26:33 +03:00
def __init__(self, provides):
2015-03-09 01:01:39 +03:00
"""Initializer."""
2015-09-14 10:53:24 +03:00
self.provides = provides
super(StaticProvider, self).__init__()
2015-01-04 17:26:33 +03:00
def _provide(self, *args, **kwargs):
2015-03-09 01:01:39 +03:00
"""Return provided instance."""
2015-09-14 10:53:24 +03:00
return self.provides
2015-01-04 17:26:33 +03:00
2015-09-14 10:53:24 +03:00
class Class(StaticProvider):
2015-03-09 01:01:39 +03:00
"""Class provider provides class."""
2015-01-04 17:26:33 +03:00
2015-09-14 10:53:24 +03:00
class Object(StaticProvider):
2015-03-09 01:01:39 +03:00
"""Object provider provides object."""
2015-01-04 17:26:33 +03:00
2015-09-14 10:53:24 +03:00
class Function(StaticProvider):
2015-03-09 01:01:39 +03:00
"""Function provider provides function."""
2015-01-04 17:26:33 +03:00
2015-09-14 10:53:24 +03:00
class Value(StaticProvider):
2015-03-09 01:01:39 +03:00
"""Value provider provides value."""
2015-01-28 01:48:33 +03:00
class Callable(Provider):
2015-03-09 01:01:39 +03:00
"""Callable provider.
Callable provider provides callable that is called on every provider call
with some predefined dependency injections.
2015-01-28 01:48:33 +03:00
"""
2015-09-14 10:53:24 +03:00
__slots__ = ('callback', 'kwargs')
def __init__(self, callback, **kwargs):
2015-03-09 01:01:39 +03:00
"""Initializer."""
if not callable(callback):
raise Error('Callable expected, got {0}'.format(str(callback)))
2015-09-14 10:53:24 +03:00
self.callback = callback
self.kwargs = tuple(KwArg(name, value)
for name, value in six.iteritems(kwargs))
2015-01-28 01:48:33 +03:00
super(Callable, self).__init__()
def _provide(self, *args, **kwargs):
2015-03-09 01:01:39 +03:00
"""Return provided instance."""
2015-09-14 10:53:24 +03:00
return self.callback(*args, **get_injectable_kwargs(kwargs,
self.kwargs))
2015-01-28 14:08:54 +03:00
class Config(Provider):
2015-03-09 01:01:39 +03:00
"""Config provider.
2015-01-28 14:08:54 +03:00
Config provider provides dict values. Also config provider creates
child config objects for all undefined attribute calls. It makes possible
to create deferred config value provider.
2015-01-28 14:08:54 +03:00
"""
2015-09-14 10:53:24 +03:00
__slots__ = ('value',)
2015-01-28 14:08:54 +03:00
def __init__(self, value=None):
2015-03-09 01:01:39 +03:00
"""Initializer."""
2015-01-28 14:08:54 +03:00
if not value:
value = dict()
2015-09-14 10:53:24 +03:00
self.value = value
2015-01-28 14:08:54 +03:00
super(Config, self).__init__()
def __getattr__(self, item):
"""Return instance of deferred config."""
2015-09-14 10:53:24 +03:00
return ChildConfig(parents=(item,), root_config=self)
def _provide(self, paths=None):
2015-03-09 01:01:39 +03:00
"""Return provided instance."""
2015-09-14 10:53:24 +03:00
value = self.value
2015-01-28 14:08:54 +03:00
if paths:
for path in paths:
try:
value = value[path]
except KeyError:
raise Error('Config key '
'"{0}" is undefined'.format('.'.join(paths)))
return value
2015-03-10 12:51:13 +03:00
2015-03-23 17:27:48 +03:00
def update_from(self, value):
"""Update current value from another one."""
2015-09-14 10:53:24 +03:00
self.value.update(value)
2015-03-23 17:27:48 +03:00
2015-03-10 12:51:13 +03:00
2015-09-14 10:53:24 +03:00
class ChildConfig(Provider):
"""Child config provider.
2015-03-10 12:51:13 +03:00
Child config provide an value from the root config object according to
the current path in the config tree.
2015-03-10 12:51:13 +03:00
"""
2015-09-14 10:53:24 +03:00
__slots__ = ('parents', 'root_config')
2015-03-16 12:41:42 +03:00
def __init__(self, parents, root_config):
2015-03-10 12:51:13 +03:00
"""Initializer."""
2015-09-14 10:53:24 +03:00
self.parents = parents
self.root_config = root_config
super(ChildConfig, self).__init__()
2015-03-10 12:51:13 +03:00
def __getattr__(self, item):
"""Return instance of deferred config."""
2015-09-14 10:53:24 +03:00
return ChildConfig(parents=self.parents + (item,),
root_config=self.root_config)
def _provide(self, *args, **kwargs):
"""Return provided instance."""
2015-09-14 10:53:24 +03:00
return self.root_config(self.parents)