Minor refactoring

This commit is contained in:
Roman Mogilatov 2015-03-23 16:27:48 +02:00
parent bb31186e51
commit 65f7704463
2 changed files with 21 additions and 14 deletions

View File

@ -1,6 +1,6 @@
"""Providers module.""" """Providers module."""
from inspect import isclass from six import class_types
from .utils import ensure_is_provider from .utils import ensure_is_provider
from .utils import is_kwarg_injection from .utils import is_kwarg_injection
@ -80,7 +80,7 @@ class NewInstance(Provider):
def __init__(self, provides, *injections): def __init__(self, provides, *injections):
"""Initializer.""" """Initializer."""
if not isclass(provides): if not isinstance(provides, class_types):
raise Error('NewInstance provider expects to get class, ' + raise Error('NewInstance provider expects to get class, ' +
'got {0} instead'.format(str(provides))) 'got {0} instead'.format(str(provides)))
self.provides = provides self.provides = provides
@ -151,6 +151,9 @@ class ExternalDependency(Provider):
def __init__(self, instance_of): def __init__(self, instance_of):
"""Initializer.""" """Initializer."""
if not isinstance(instance_of, class_types):
raise Error('ExternalDependency provider expects to get class, ' +
'got {0} instead'.format(str(instance_of)))
self.instance_of = instance_of self.instance_of = instance_of
super(ExternalDependency, self).__init__() super(ExternalDependency, self).__init__()
@ -260,14 +263,6 @@ class Config(Provider):
self.value = value self.value = value
super(Config, self).__init__() super(Config, self).__init__()
def update_from(self, value):
"""Update current value from another one."""
self.value.update(value)
def __getattr__(self, item):
"""Return instance of deferred config."""
return _ChildConfig(parents=(item,), root_config=self)
def __call__(self, paths=None): def __call__(self, paths=None):
"""Return provided instance.""" """Return provided instance."""
value = self.value value = self.value
@ -280,6 +275,14 @@ 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):
"""Update current value from another one."""
self.value.update(value)
class _ChildConfig(Provider): class _ChildConfig(Provider):
@ -297,11 +300,11 @@ 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):
"""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 __call__(self, *args, **kwargs):
"""Return provided instance."""
return self.root_config(self.parents)

View File

@ -313,6 +313,10 @@ class ExternalDependencyTests(unittest.TestCase):
"""Set test cases environment up.""" """Set test cases environment up."""
self.provider = ExternalDependency(instance_of=list) self.provider = ExternalDependency(instance_of=list)
def test_init_with_not_class(self):
"""Test creation with not a class."""
self.assertRaises(Error, ExternalDependency, object())
def test_is_provider(self): def test_is_provider(self):
"""Test `is_provider` check.""" """Test `is_provider` check."""
self.assertTrue(is_provider(self.provider)) self.assertTrue(is_provider(self.provider))