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