Renaming of Provider.__provide__() -> Provider._provide() for improving providers extending

This commit is contained in:
Roman Mogilatov 2015-07-22 10:26:34 +03:00
parent fd8be77635
commit 1b4c6dbf8b

View File

@ -25,9 +25,9 @@ class Provider(object):
"""Return provided instance.""" """Return provided instance."""
if self.overridden: if self.overridden:
return self.last_overriding(*args, **kwargs) return self.last_overriding(*args, **kwargs)
return self.__provide__(*args, **kwargs) return self._provide(*args, **kwargs)
def __provide__(self, *args, **kwargs): def _provide(self, *args, **kwargs):
"""Providing strategy implementation. """Providing strategy implementation.
Abstract protected method that implements providing strategy of Abstract protected method that implements providing strategy of
@ -80,7 +80,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 __provide__(self, *args, **kwargs): def _provide(self, *args, **kwargs):
"""Return provided instance.""" """Return provided instance."""
return self.delegated return self.delegated
@ -111,7 +111,7 @@ class Factory(Provider):
if is_method_injection(injection))) if is_method_injection(injection)))
super(Factory, self).__init__() super(Factory, self).__init__()
def __provide__(self, *args, **kwargs): def _provide(self, *args, **kwargs):
"""Return provided instance.""" """Return provided instance."""
init_kwargs = dict(((injection.name, injection.value) init_kwargs = dict(((injection.name, injection.value)
for injection in self.kwargs)) for injection in self.kwargs))
@ -152,7 +152,7 @@ class Singleton(Provider):
self.factory = Factory(*args, **kwargs) self.factory = Factory(*args, **kwargs)
super(Singleton, self).__init__() super(Singleton, self).__init__()
def __provide__(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)
@ -214,7 +214,7 @@ class _StaticProvider(Provider):
self.provides = provides self.provides = provides
super(_StaticProvider, self).__init__() super(_StaticProvider, self).__init__()
def __provide__(self, *args, **kwargs): def _provide(self, *args, **kwargs):
"""Return provided instance.""" """Return provided instance."""
return self.provides return self.provides
@ -259,7 +259,7 @@ class Callable(Provider):
if is_kwarg_injection(injection))) if is_kwarg_injection(injection)))
super(Callable, self).__init__() super(Callable, self).__init__()
def __provide__(self, *args, **kwargs): def _provide(self, *args, **kwargs):
"""Return provided instance.""" """Return provided instance."""
injections = dict(((injection.name, injection.value) injections = dict(((injection.name, injection.value)
for injection in self.injections)) for injection in self.injections))
@ -286,7 +286,11 @@ class Config(Provider):
self.value = value self.value = value
super(Config, self).__init__() super(Config, self).__init__()
def __provide__(self, paths=None): def __getattr__(self, item):
"""Return instance of deferred config."""
return _ChildConfig(parents=(item,), root_config=self)
def _provide(self, paths=None):
"""Return provided instance.""" """Return provided instance."""
value = self.value value = self.value
if paths: if paths:
@ -298,10 +302,6 @@ class Config(Provider):
'"{0}" is undefined'.format('.'.join(paths))) '"{0}" is undefined'.format('.'.join(paths)))
return value return value
def __getattr__(self, item):
"""Return instance of deferred config."""
return _ChildConfig(parents=(item,), root_config=self)
def update_from(self, value): def update_from(self, value):
"""Update current value from another one.""" """Update current value from another one."""
self.value.update(value) self.value.update(value)
@ -323,11 +323,11 @@ class _ChildConfig(Provider):
self.root_config = root_config self.root_config = root_config
super(_ChildConfig, self).__init__() super(_ChildConfig, self).__init__()
def __provide__(self, *args, **kwargs):
"""Return provided instance."""
return self.root_config(self.parents)
def __getattr__(self, item): def __getattr__(self, item):
"""Return instance of deferred config.""" """Return instance of deferred config."""
return _ChildConfig(parents=self.parents + (item,), return _ChildConfig(parents=self.parents + (item,),
root_config=self.root_config) root_config=self.root_config)
def _provide(self, *args, **kwargs):
"""Return provided instance."""
return self.root_config(self.parents)