2015-03-12 13:45:51 +03:00
|
|
|
"""Providers module."""
|
2015-01-04 17:26:33 +03:00
|
|
|
|
2015-03-23 17:27:48 +03:00
|
|
|
from six import class_types
|
2015-03-10 12:51:13 +03:00
|
|
|
|
2015-03-13 18:46:03 +03:00
|
|
|
from .utils import ensure_is_provider
|
2015-03-23 02:04:18 +03:00
|
|
|
from .utils import is_kwarg_injection
|
2015-03-11 16:18:42 +03:00
|
|
|
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-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-07-22 16:42:36 +03:00
|
|
|
__slots__ = ('_overridden',)
|
2015-01-11 19:10:11 +03:00
|
|
|
|
|
|
|
def __init__(self):
|
2015-03-09 01:01:39 +03:00
|
|
|
"""Initializer."""
|
2015-07-22 16:42:36 +03:00
|
|
|
self._overridden = 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-07-22 16:42:36 +03:00
|
|
|
if self._overridden:
|
2015-05-25 10:45:52 +03:00
|
|
|
return self.last_overriding(*args, **kwargs)
|
2015-07-22 10:26:34 +03:00
|
|
|
return self._provide(*args, **kwargs)
|
2015-05-25 10:45:52 +03:00
|
|
|
|
2015-07-22 10:26:34 +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):
|
2015-03-12 13:45:51 +03:00
|
|
|
"""Return provider's delegate."""
|
2015-03-14 01:02:01 +03:00
|
|
|
return Delegate(self)
|
2015-02-25 02:48:51 +03:00
|
|
|
|
2015-03-12 13:45:51 +03:00
|
|
|
def override(self, provider):
|
|
|
|
"""Override provider with another provider."""
|
2015-07-22 16:42:36 +03:00
|
|
|
if not self._overridden:
|
|
|
|
self._overridden = (ensure_is_provider(provider),)
|
2015-03-20 16:16:11 +03:00
|
|
|
else:
|
2015-07-22 16:42:36 +03:00
|
|
|
self._overridden += (ensure_is_provider(provider),)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_overridden(self):
|
|
|
|
"""Check if provider is overridden by another provider."""
|
|
|
|
return bool(self._overridden)
|
2015-03-12 13:45:51 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def last_overriding(self):
|
|
|
|
"""Return last overriding provider."""
|
|
|
|
try:
|
2015-07-22 16:42:36 +03:00
|
|
|
return self._overridden[-1]
|
2015-03-20 16:16:11 +03:00
|
|
|
except (TypeError, IndexError):
|
2015-07-21 09:29:50 +03:00
|
|
|
raise Error('Provider {0} is not overridden'.format(str(self)))
|
|
|
|
|
|
|
|
def reset_last_overriding(self):
|
|
|
|
"""Reset last overriding provider."""
|
2015-07-22 16:42:36 +03:00
|
|
|
if not self._overridden:
|
2015-07-21 09:29:50 +03:00
|
|
|
raise Error('Provider {0} is not overridden'.format(str(self)))
|
2015-07-22 16:42:36 +03:00
|
|
|
self._overridden = self._overridden[:-1]
|
2015-07-21 09:29:50 +03:00
|
|
|
|
|
|
|
def reset_override(self):
|
|
|
|
"""Reset all overriding providers."""
|
2015-07-22 16:42:36 +03:00
|
|
|
self._overridden = None
|
2015-03-12 13:45:51 +03:00
|
|
|
|
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-07-22 16:42:36 +03:00
|
|
|
__slots__ = ('_delegated',)
|
2015-03-11 16:18:42 +03:00
|
|
|
|
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-07-22 16:42:36 +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
|
|
|
|
2015-07-22 10:26:34 +03:00
|
|
|
def _provide(self, *args, **kwargs):
|
2015-03-09 01:01:39 +03:00
|
|
|
"""Return provided instance."""
|
2015-07-22 16:42:36 +03:00
|
|
|
return self._delegated
|
2015-02-23 01:16:27 +03:00
|
|
|
|
2015-01-11 16:03:45 +03:00
|
|
|
|
2015-05-14 11:35:41 +03:00
|
|
|
class Factory(Provider):
|
2015-01-10 12:24:25 +03:00
|
|
|
|
2015-05-14 11:35:41 +03:00
|
|
|
"""Factory provider.
|
2015-01-04 17:26:33 +03:00
|
|
|
|
2015-05-14 11:59:47 +03:00
|
|
|
Factory provider creates new instance of specified class on every call.
|
2015-01-04 17:26:33 +03:00
|
|
|
"""
|
|
|
|
|
2015-07-22 16:42:36 +03:00
|
|
|
__slots__ = ('_provides', '_kwargs', '_attributes', '_methods')
|
2015-03-11 16:18:42 +03:00
|
|
|
|
2015-01-10 12:24:25 +03:00
|
|
|
def __init__(self, provides, *injections):
|
2015-03-09 01:01:39 +03:00
|
|
|
"""Initializer."""
|
2015-07-24 00:28:52 +03:00
|
|
|
if not callable(provides):
|
|
|
|
raise Error('Factory provider expects to get callable, ' +
|
2015-03-18 13:48:19 +03:00
|
|
|
'got {0} instead'.format(str(provides)))
|
2015-07-22 16:42:36 +03:00
|
|
|
self._provides = provides
|
|
|
|
self._kwargs = tuple((injection
|
2015-03-11 16:27:38 +03:00
|
|
|
for injection in injections
|
2015-07-22 16:42:36 +03:00
|
|
|
if is_kwarg_injection(injection)))
|
|
|
|
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)))
|
2015-05-14 11:35:41 +03:00
|
|
|
super(Factory, self).__init__()
|
2015-01-04 17:26:33 +03:00
|
|
|
|
2015-07-22 10:26:34 +03:00
|
|
|
def _provide(self, *args, **kwargs):
|
2015-03-09 01:01:39 +03:00
|
|
|
"""Return provided instance."""
|
2015-07-27 01:44:20 +03:00
|
|
|
instance = self._provides(*args,
|
|
|
|
**get_injectable_kwargs(kwargs,
|
|
|
|
self._kwargs))
|
2015-07-22 16:42:36 +03:00
|
|
|
for attribute in self._attributes:
|
2015-03-14 16:57:30 +03:00
|
|
|
setattr(instance, attribute.name, attribute.value)
|
2015-07-22 16:42:36 +03:00
|
|
|
for method in self._methods:
|
2015-03-14 16:57:30 +03:00
|
|
|
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-05-14 11:35:41 +03:00
|
|
|
class NewInstance(Factory):
|
|
|
|
|
|
|
|
"""NewInstance provider.
|
|
|
|
|
|
|
|
It is synonym of Factory provider. NewInstance provider is considered to
|
|
|
|
be deprecated, but will be able to use for further backward
|
|
|
|
compatibility.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
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-07-22 16:42:36 +03:00
|
|
|
__slots__ = ('_instance', '_factory')
|
2015-03-11 16:18:42 +03:00
|
|
|
|
2015-07-24 00:28:52 +03:00
|
|
|
def __init__(self, provides, *injections):
|
2015-03-09 01:01:39 +03:00
|
|
|
"""Initializer."""
|
2015-07-22 16:42:36 +03:00
|
|
|
self._instance = None
|
2015-07-24 00:28:52 +03:00
|
|
|
self._factory = Factory(provides, *injections)
|
2015-05-14 11:35:41 +03:00
|
|
|
super(Singleton, self).__init__()
|
2015-01-04 17:26:33 +03:00
|
|
|
|
2015-07-22 10:26:34 +03:00
|
|
|
def _provide(self, *args, **kwargs):
|
2015-03-09 01:01:39 +03:00
|
|
|
"""Return provided instance."""
|
2015-07-22 16:42:36 +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-07-22 16:42:36 +03:00
|
|
|
self._instance = None
|
2015-01-28 01:21:31 +03:00
|
|
|
|
|
|
|
|
2015-01-23 02:42:59 +03:00
|
|
|
class ExternalDependency(Provider):
|
|
|
|
|
2015-03-20 15:53:47 +03:00
|
|
|
"""External dependency provider.
|
2015-01-23 02:42:59 +03:00
|
|
|
|
2015-03-20 15:53:47 +03:00
|
|
|
Those provider is used when dependency obviously have to be overridden by
|
|
|
|
the client's code, but it's interface is known.
|
|
|
|
"""
|
|
|
|
|
2015-07-22 16:42:36 +03:00
|
|
|
__slots__ = ('_instance_of',)
|
2015-03-11 16:18:42 +03:00
|
|
|
|
2015-03-09 01:01:39 +03:00
|
|
|
def __init__(self, instance_of):
|
|
|
|
"""Initializer."""
|
2015-03-23 17:27:48 +03:00
|
|
|
if not isinstance(instance_of, class_types):
|
|
|
|
raise Error('ExternalDependency provider expects to get class, ' +
|
|
|
|
'got {0} instead'.format(str(instance_of)))
|
2015-07-22 16:42:36 +03:00
|
|
|
self._instance_of = instance_of
|
2015-01-23 02:42:59 +03:00
|
|
|
super(ExternalDependency, self).__init__()
|
|
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
2015-03-09 01:01:39 +03:00
|
|
|
"""Return provided instance."""
|
2015-07-22 16:42:36 +03:00
|
|
|
if not self._overridden:
|
2015-03-20 15:53:47 +03:00
|
|
|
raise Error('Dependency is not defined')
|
2015-01-23 02:42:59 +03:00
|
|
|
|
2015-03-20 15:53:47 +03:00
|
|
|
instance = self.last_overriding(*args, **kwargs)
|
2015-01-23 02:42:59 +03:00
|
|
|
|
2015-07-22 16:42:36 +03:00
|
|
|
if not isinstance(instance, self._instance_of):
|
2015-03-18 13:48:19 +03:00
|
|
|
raise Error('{0} is not an '.format(instance) +
|
2015-07-22 16:42:36 +03:00
|
|
|
'instance of {0}'.format(self._instance_of))
|
2015-01-24 01:31:30 +03:00
|
|
|
|
2015-03-16 02:10:43 +03:00
|
|
|
return instance
|
2015-01-23 02:42:59 +03:00
|
|
|
|
2015-06-18 16:34:26 +03:00
|
|
|
def provided_by(self, provider):
|
|
|
|
"""Set external dependency provider."""
|
|
|
|
return self.override(provider)
|
|
|
|
|
2015-01-23 02:42:59 +03:00
|
|
|
|
2015-01-04 17:26:33 +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-07-22 16:42:36 +03:00
|
|
|
__slots__ = ('_provides',)
|
2015-03-11 16:18:42 +03:00
|
|
|
|
2015-01-04 17:26:33 +03:00
|
|
|
def __init__(self, provides):
|
2015-03-09 01:01:39 +03:00
|
|
|
"""Initializer."""
|
2015-07-22 16:42:36 +03:00
|
|
|
self._provides = provides
|
2015-01-11 19:10:11 +03:00
|
|
|
super(_StaticProvider, self).__init__()
|
2015-01-04 17:26:33 +03:00
|
|
|
|
2015-07-22 10:26:34 +03:00
|
|
|
def _provide(self, *args, **kwargs):
|
2015-03-09 01:01:39 +03:00
|
|
|
"""Return provided instance."""
|
2015-07-22 16:42:36 +03:00
|
|
|
return self._provides
|
2015-01-04 17:26:33 +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
|
|
|
|
|
|
|
|
|
|
|
class Object(_StaticProvider):
|
2015-03-09 01:01:39 +03:00
|
|
|
|
|
|
|
"""Object provider provides object."""
|
2015-01-04 17:26:33 +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
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2015-03-16 02:56:23 +03:00
|
|
|
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-07-22 16:42:36 +03:00
|
|
|
__slots__ = ('_callback', '_injections')
|
2015-03-11 16:18:42 +03:00
|
|
|
|
2015-03-16 12:52:53 +03:00
|
|
|
def __init__(self, callback, *injections):
|
2015-03-09 01:01:39 +03:00
|
|
|
"""Initializer."""
|
2015-03-16 12:52:53 +03:00
|
|
|
if not callable(callback):
|
2015-03-18 13:48:19 +03:00
|
|
|
raise Error('Callable expected, got {0}'.format(str(callback)))
|
2015-07-22 16:42:36 +03:00
|
|
|
self._callback = callback
|
|
|
|
self._injections = tuple((injection
|
|
|
|
for injection in injections
|
|
|
|
if is_kwarg_injection(injection)))
|
2015-01-28 01:48:33 +03:00
|
|
|
super(Callable, self).__init__()
|
|
|
|
|
2015-07-22 10:26:34 +03:00
|
|
|
def _provide(self, *args, **kwargs):
|
2015-03-09 01:01:39 +03:00
|
|
|
"""Return provided instance."""
|
2015-07-27 18:50:27 +03:00
|
|
|
return self._callback(*args, **get_injectable_kwargs(kwargs,
|
|
|
|
self._injections))
|
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
|
2015-03-16 16:10:39 +03:00
|
|
|
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-07-22 16:42:36 +03:00
|
|
|
__slots__ = ('_value',)
|
2015-03-11 16:18:42 +03:00
|
|
|
|
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-07-22 16:42:36 +03:00
|
|
|
self._value = value
|
2015-01-28 14:08:54 +03:00
|
|
|
super(Config, self).__init__()
|
|
|
|
|
2015-07-22 10:26:34 +03:00
|
|
|
def __getattr__(self, item):
|
|
|
|
"""Return instance of deferred config."""
|
|
|
|
return _ChildConfig(parents=(item,), root_config=self)
|
|
|
|
|
|
|
|
def _provide(self, paths=None):
|
2015-03-09 01:01:39 +03:00
|
|
|
"""Return provided instance."""
|
2015-07-22 16:42:36 +03:00
|
|
|
value = self._value
|
2015-01-28 14:08:54 +03:00
|
|
|
if paths:
|
|
|
|
for path in paths:
|
2015-03-16 16:09:01 +03:00
|
|
|
try:
|
|
|
|
value = value[path]
|
|
|
|
except KeyError:
|
|
|
|
raise Error('Config key '
|
2015-03-18 13:48:19 +03:00
|
|
|
'"{0}" is undefined'.format('.'.join(paths)))
|
2015-03-16 16:09:01 +03:00
|
|
|
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-07-22 16:42:36 +03:00
|
|
|
self._value.update(value)
|
2015-03-23 17:27:48 +03:00
|
|
|
|
2015-03-10 12:51:13 +03:00
|
|
|
|
2015-03-16 16:09:01 +03:00
|
|
|
class _ChildConfig(Provider):
|
2015-03-10 12:51:13 +03:00
|
|
|
|
2015-03-16 16:09:01 +03:00
|
|
|
"""Child config provider.
|
2015-03-10 12:51:13 +03:00
|
|
|
|
2015-03-16 16:09:01 +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-07-22 16:42:36 +03:00
|
|
|
__slots__ = ('_parents', '_root_config')
|
2015-03-11 16:18:42 +03:00
|
|
|
|
2015-03-16 12:41:42 +03:00
|
|
|
def __init__(self, parents, root_config):
|
2015-03-10 12:51:13 +03:00
|
|
|
"""Initializer."""
|
2015-07-22 16:42:36 +03:00
|
|
|
self._parents = parents
|
|
|
|
self._root_config = root_config
|
2015-03-16 16:09:01 +03:00
|
|
|
super(_ChildConfig, self).__init__()
|
2015-03-10 12:51:13 +03:00
|
|
|
|
|
|
|
def __getattr__(self, item):
|
|
|
|
"""Return instance of deferred config."""
|
2015-07-22 16:42:36 +03:00
|
|
|
return _ChildConfig(parents=self._parents + (item,),
|
|
|
|
root_config=self._root_config)
|
2015-07-22 10:26:34 +03:00
|
|
|
|
|
|
|
def _provide(self, *args, **kwargs):
|
|
|
|
"""Return provided instance."""
|
2015-07-22 16:42:36 +03:00
|
|
|
return self._root_config(self._parents)
|