Drop Config provider
This commit is contained in:
parent
1320c12780
commit
4a160ed999
|
@ -16,10 +16,6 @@ from dependency_injector.providers.creational import (
|
|||
Singleton,
|
||||
DelegatedSingleton,
|
||||
)
|
||||
from dependency_injector.providers.config import (
|
||||
Config,
|
||||
ChildConfig,
|
||||
)
|
||||
from dependency_injector.providers.utils import (
|
||||
OverridingContext,
|
||||
override,
|
||||
|
@ -40,9 +36,6 @@ __all__ = (
|
|||
'Singleton',
|
||||
'DelegatedSingleton',
|
||||
|
||||
'Config',
|
||||
'ChildConfig',
|
||||
|
||||
'OverridingContext',
|
||||
'override',
|
||||
)
|
||||
|
|
|
@ -1,138 +0,0 @@
|
|||
"""Dependency injector config providers."""
|
||||
|
||||
import six
|
||||
|
||||
from dependency_injector.providers.base import Provider
|
||||
from dependency_injector.utils import represent_provider
|
||||
from dependency_injector.errors import Error
|
||||
|
||||
|
||||
@six.python_2_unicode_compatible
|
||||
class Config(Provider):
|
||||
""":py:class:`Config` provider provide dict values.
|
||||
|
||||
:py:class:`Config` provider creates :py:class:`ChildConfig` objects for all
|
||||
undefined attribute calls. It makes possible to create deferred config
|
||||
value providers. It might be useful in cases where it is needed to
|
||||
define / pass some configuration in declarative manner, while
|
||||
configuration values will be loaded / updated in application's runtime.
|
||||
"""
|
||||
|
||||
__slots__ = ('value',)
|
||||
|
||||
def __init__(self, value=None):
|
||||
"""Initializer.
|
||||
|
||||
:param value: Configuration dictionary.
|
||||
:type value: dict[str, object]
|
||||
"""
|
||||
if not value:
|
||||
value = dict()
|
||||
self.value = value
|
||||
super(Config, self).__init__()
|
||||
|
||||
def __getattr__(self, item):
|
||||
"""Return instance of deferred config.
|
||||
|
||||
:param item: Name of configuration option or section.
|
||||
:type item: str
|
||||
|
||||
:rtype: :py:class:`ChildConfig`
|
||||
"""
|
||||
return ChildConfig(parents=(item,), root_config=self)
|
||||
|
||||
def _provide(self, paths=None):
|
||||
"""Return provided instance.
|
||||
|
||||
:param paths: Tuple of pieces of configuration option / section path.
|
||||
:type args: tuple[str]
|
||||
|
||||
:rtype: object
|
||||
"""
|
||||
value = self.value
|
||||
if paths:
|
||||
for path in paths:
|
||||
try:
|
||||
value = value[path]
|
||||
except KeyError:
|
||||
raise Error('Config key '
|
||||
'"{0}" is undefined'.format('.'.join(paths)))
|
||||
return value
|
||||
|
||||
def update_from(self, value):
|
||||
"""Update current value from another one.
|
||||
|
||||
:param value: Configuration dictionary.
|
||||
:type value: dict[str, object]
|
||||
|
||||
:rtype: None
|
||||
"""
|
||||
self.value.update(value)
|
||||
|
||||
def __str__(self):
|
||||
"""Return string representation of provider.
|
||||
|
||||
:rtype: str
|
||||
"""
|
||||
return represent_provider(provider=self, provides=self.value)
|
||||
|
||||
__repr__ = __str__
|
||||
|
||||
|
||||
@six.python_2_unicode_compatible
|
||||
class ChildConfig(Provider):
|
||||
""":py:class:`ChildConfig` provider provides value from :py:class:`Config`.
|
||||
|
||||
:py:class:`ChildConfig` provides value from the root config object
|
||||
according to the current path in the config tree.
|
||||
"""
|
||||
|
||||
__slots__ = ('parents', 'root_config')
|
||||
|
||||
def __init__(self, parents, root_config):
|
||||
"""Initializer.
|
||||
|
||||
:param parents: Tuple of pieces of configuration option / section
|
||||
parent path.
|
||||
:type parents: tuple[str]
|
||||
|
||||
:param root_config: Root configuration object.
|
||||
:type root_config: :py:class:`Config`
|
||||
"""
|
||||
self.parents = parents
|
||||
self.root_config = root_config
|
||||
super(ChildConfig, self).__init__()
|
||||
|
||||
def __getattr__(self, item):
|
||||
"""Return instance of deferred config.
|
||||
|
||||
:param item: Name of configuration option or section.
|
||||
:type item: str
|
||||
|
||||
:rtype: :py:class:`ChildConfig`
|
||||
"""
|
||||
return ChildConfig(parents=self.parents + (item,),
|
||||
root_config=self.root_config)
|
||||
|
||||
def _provide(self, *args, **kwargs):
|
||||
"""Return provided instance.
|
||||
|
||||
:param args: Tuple of context positional arguments.
|
||||
:type args: tuple[object]
|
||||
|
||||
:param kwargs: Dictionary of context keyword arguments.
|
||||
:type kwargs: dict[str, object]
|
||||
|
||||
:rtype: object
|
||||
"""
|
||||
return self.root_config(self.parents)
|
||||
|
||||
def __str__(self):
|
||||
"""Return string representation of provider.
|
||||
|
||||
:rtype: str
|
||||
"""
|
||||
return represent_provider(provider=self,
|
||||
provides='.'.join(self.parents))
|
||||
|
||||
__repr__ = __str__
|
|
@ -14,7 +14,12 @@ Development version
|
|||
2.0.0
|
||||
------
|
||||
- Drop backward compatibilities of 1.x.
|
||||
- Drop ``Static``, ``Value``, ``Function`` & ``Class`` providers.
|
||||
- Drop providers:
|
||||
- ``Static``
|
||||
- ``Value``
|
||||
- ``Function``
|
||||
- ``Class``
|
||||
- ``Config``
|
||||
|
||||
1.17.0
|
||||
------
|
||||
|
|
|
@ -1,87 +0,0 @@
|
|||
"""Dependency injector config providers unittests."""
|
||||
|
||||
import unittest2 as unittest
|
||||
|
||||
from dependency_injector import (
|
||||
providers,
|
||||
utils,
|
||||
errors,
|
||||
)
|
||||
|
||||
|
||||
class ConfigTests(unittest.TestCase):
|
||||
"""Config test cases."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set test cases environment up."""
|
||||
self.initial_data = dict(key='value',
|
||||
category=dict(setting='setting_value'))
|
||||
self.provider = providers.Config(self.initial_data)
|
||||
|
||||
def test_is_provider(self):
|
||||
"""Test `is_provider` check."""
|
||||
self.assertTrue(utils.is_provider(self.provider))
|
||||
|
||||
def test_init_without_initial_value(self):
|
||||
"""Test provider's creation with no initial value."""
|
||||
self.assertEqual(providers.Config()(), dict())
|
||||
|
||||
def test_call(self):
|
||||
"""Test returning of config value."""
|
||||
self.assertEqual(self.provider(), self.initial_data)
|
||||
|
||||
def test_update_from(self):
|
||||
"""Test update of config value."""
|
||||
self.assertEqual(self.provider(), self.initial_data)
|
||||
|
||||
self.initial_data['key'] = 'other_value'
|
||||
self.provider.update_from(self.initial_data)
|
||||
self.assertEqual(self.provider(), self.initial_data)
|
||||
|
||||
def test_call_child(self):
|
||||
"""Test returning of child config values."""
|
||||
category = self.provider.category
|
||||
category_setting = self.provider.category.setting
|
||||
|
||||
self.assertTrue(utils.is_provider(category))
|
||||
self.assertTrue(utils.is_provider(category_setting))
|
||||
|
||||
self.assertEqual(category(), self.initial_data['category'])
|
||||
self.assertEqual(category_setting(),
|
||||
self.initial_data['category']['setting'])
|
||||
|
||||
def test_call_deferred_child_and_update_from(self):
|
||||
"""Test returning of deferred child config values."""
|
||||
self.provider = providers.Config()
|
||||
category = self.provider.category
|
||||
category_setting = self.provider.category.setting
|
||||
|
||||
self.assertTrue(utils.is_provider(category))
|
||||
self.assertTrue(utils.is_provider(category_setting))
|
||||
|
||||
self.provider.update_from(self.initial_data)
|
||||
|
||||
self.assertEqual(category(), self.initial_data['category'])
|
||||
self.assertEqual(category_setting(),
|
||||
self.initial_data['category']['setting'])
|
||||
|
||||
def test_call_deferred_child_with_empty_value(self):
|
||||
"""Test returning of deferred child config values."""
|
||||
self.provider = providers.Config()
|
||||
category_setting = self.provider.category.setting
|
||||
self.assertRaises(errors.Error, category_setting)
|
||||
|
||||
def test_repr(self):
|
||||
"""Test representation of provider."""
|
||||
self.assertEqual(repr(self.provider),
|
||||
'<dependency_injector.providers.config.'
|
||||
'Config({0}) at {1}>'.format(
|
||||
repr(self.initial_data),
|
||||
hex(id(self.provider))))
|
||||
|
||||
category_setting = self.provider.category.setting
|
||||
self.assertEqual(repr(category_setting),
|
||||
'<dependency_injector.providers.config.'
|
||||
'ChildConfig({0}) at {1}>'.format(
|
||||
repr('.'.join(('category', 'setting'))),
|
||||
hex(id(category_setting))))
|
Loading…
Reference in New Issue