Improving of Providers extending

This commit is contained in:
Roman Mogilatov 2015-05-25 10:45:52 +03:00
parent f67343c45c
commit b055a86c92

View File

@ -23,6 +23,17 @@ class Provider(object):
def __call__(self, *args, **kwargs):
"""Return provided instance."""
if self.overridden:
return self.last_overriding(*args, **kwargs)
return self.__provide__(*args, **kwargs)
def __provide__(self, *args, **kwargs):
"""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.
"""
raise NotImplementedError()
def delegate(self):
@ -64,7 +75,7 @@ class Delegate(Provider):
self.delegated = ensure_is_provider(delegated)
super(Delegate, self).__init__()
def __call__(self):
def __provide__(self, *args, **kwargs):
"""Return provided instance."""
return self.delegated
@ -95,11 +106,8 @@ class Factory(Provider):
if is_method_injection(injection)))
super(Factory, self).__init__()
def __call__(self, *args, **kwargs):
def __provide__(self, *args, **kwargs):
"""Return provided instance."""
if self.overridden:
return self.last_overriding(*args, **kwargs)
init_kwargs = dict(((injection.name, injection.value)
for injection in self.kwargs))
init_kwargs.update(kwargs)
@ -139,7 +147,7 @@ class Singleton(Provider):
self.factory = Factory(*args, **kwargs)
super(Singleton, self).__init__()
def __call__(self, *args, **kwargs):
def __provide__(self, *args, **kwargs):
"""Return provided instance."""
if not self.instance:
self.instance = self.factory(*args, **kwargs)
@ -197,10 +205,8 @@ class _StaticProvider(Provider):
self.provides = provides
super(_StaticProvider, self).__init__()
def __call__(self):
def __provide__(self, *args, **kwargs):
"""Return provided instance."""
if self.overridden:
return self.last_overriding()
return self.provides
@ -244,11 +250,8 @@ class Callable(Provider):
if is_kwarg_injection(injection)))
super(Callable, self).__init__()
def __call__(self, *args, **kwargs):
def __provide__(self, *args, **kwargs):
"""Return provided instance."""
if self.overridden:
return self.last_overriding()
injections = dict(((injection.name, injection.value)
for injection in self.injections))
injections.update(kwargs)
@ -274,7 +277,7 @@ class Config(Provider):
self.value = value
super(Config, self).__init__()
def __call__(self, paths=None):
def __provide__(self, paths=None):
"""Return provided instance."""
value = self.value
if paths:
@ -311,7 +314,7 @@ class _ChildConfig(Provider):
self.root_config = root_config
super(_ChildConfig, self).__init__()
def __call__(self, *args, **kwargs):
def __provide__(self, *args, **kwargs):
"""Return provided instance."""
return self.root_config(self.parents)