mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 19:14:00 +03:00
Config linking (#255)
* Add config linking test * Add provider linking * Update changelog
This commit is contained in:
parent
86021f2948
commit
e6f096270e
|
@ -7,6 +7,10 @@ that were made in every particular version.
|
||||||
From version 0.7.6 *Dependency Injector* framework strictly
|
From version 0.7.6 *Dependency Injector* framework strictly
|
||||||
follows `Semantic versioning`_
|
follows `Semantic versioning`_
|
||||||
|
|
||||||
|
Development version
|
||||||
|
-------------------
|
||||||
|
- Add ``Configuration`` providers linking.
|
||||||
|
|
||||||
3.16.1
|
3.16.1
|
||||||
------
|
------
|
||||||
- Update ``singleton_thread_locals.py`` to support Python 3 (thanks to
|
- Update ``singleton_thread_locals.py`` to support Python 3 (thanks to
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -92,6 +92,7 @@ cdef class CoroutineDelegate(Delegate):
|
||||||
cdef class Configuration(Object):
|
cdef class Configuration(Object):
|
||||||
cdef str __name
|
cdef str __name
|
||||||
cdef dict __children
|
cdef dict __children
|
||||||
|
cdef tuple __linked
|
||||||
|
|
||||||
|
|
||||||
# Factory providers
|
# Factory providers
|
||||||
|
|
|
@ -1029,6 +1029,7 @@ cdef class Configuration(Object):
|
||||||
|
|
||||||
self.__name = name
|
self.__name = name
|
||||||
self.__children = self._create_children(default)
|
self.__children = self._create_children(default)
|
||||||
|
self.__linked = tuple()
|
||||||
|
|
||||||
def __deepcopy__(self, memo):
|
def __deepcopy__(self, memo):
|
||||||
"""Create and return full copy of provider."""
|
"""Create and return full copy of provider."""
|
||||||
|
@ -1041,6 +1042,7 @@ cdef class Configuration(Object):
|
||||||
copied = self.__class__(self.__name)
|
copied = self.__class__(self.__name)
|
||||||
copied.__provides = deepcopy(self.__provides, memo)
|
copied.__provides = deepcopy(self.__provides, memo)
|
||||||
copied.__children = deepcopy(self.__children, memo)
|
copied.__children = deepcopy(self.__children, memo)
|
||||||
|
copied.__linked = deepcopy(self.__linked, memo)
|
||||||
|
|
||||||
self._copy_overridings(copied, memo)
|
self._copy_overridings(copied, memo)
|
||||||
|
|
||||||
|
@ -1093,11 +1095,17 @@ cdef class Configuration(Object):
|
||||||
"""
|
"""
|
||||||
overriding_context = super(Configuration, self).override(provider)
|
overriding_context = super(Configuration, self).override(provider)
|
||||||
|
|
||||||
|
for linked in self.__linked:
|
||||||
|
linked.override(provider)
|
||||||
|
|
||||||
|
if isinstance(provider, Configuration):
|
||||||
|
provider.link_provider(self)
|
||||||
|
|
||||||
value = self.__call__()
|
value = self.__call__()
|
||||||
if not isinstance(value, dict):
|
if not isinstance(value, dict):
|
||||||
return
|
return
|
||||||
|
|
||||||
for name in value:
|
for name in value.keys():
|
||||||
child_provider = self.__children.get(name)
|
child_provider = self.__children.get(name)
|
||||||
if child_provider is None:
|
if child_provider is None:
|
||||||
continue
|
continue
|
||||||
|
@ -1129,6 +1137,10 @@ cdef class Configuration(Object):
|
||||||
child.reset_override()
|
child.reset_override()
|
||||||
super(Configuration, self).reset_override()
|
super(Configuration, self).reset_override()
|
||||||
|
|
||||||
|
def link_provider(self, provider):
|
||||||
|
"""Configuration link two configuration providers."""
|
||||||
|
self.__linked += (<Configuration?>provider,)
|
||||||
|
|
||||||
def update(self, value):
|
def update(self, value):
|
||||||
"""Set configuration options.
|
"""Set configuration options.
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
import unittest2 as unittest
|
import unittest2 as unittest
|
||||||
|
|
||||||
from dependency_injector import providers
|
from dependency_injector import containers, providers
|
||||||
|
|
||||||
|
|
||||||
class ConfigTests(unittest.TestCase):
|
class ConfigTests(unittest.TestCase):
|
||||||
|
@ -177,3 +177,72 @@ class ConfigTests(unittest.TestCase):
|
||||||
'Configuration({0}) at {1}>'.format(
|
'Configuration({0}) at {1}>'.format(
|
||||||
repr('config.a.b.c'),
|
repr('config.a.b.c'),
|
||||||
hex(id(self.config.a.b.c))))
|
hex(id(self.config.a.b.c))))
|
||||||
|
|
||||||
|
|
||||||
|
class ConfigLinkingTests(unittest.TestCase):
|
||||||
|
|
||||||
|
class TestCore(containers.DeclarativeContainer):
|
||||||
|
config = providers.Configuration('core')
|
||||||
|
value_getter = providers.Callable(lambda _: _, config.value)
|
||||||
|
|
||||||
|
class TestServices(containers.DeclarativeContainer):
|
||||||
|
config = providers.Configuration('services')
|
||||||
|
value_getter = providers.Callable(lambda _: _, config.value)
|
||||||
|
|
||||||
|
def test(self):
|
||||||
|
root_config = providers.Configuration('main')
|
||||||
|
core = self.TestCore(config=root_config.core)
|
||||||
|
services = self.TestServices(config=root_config.services)
|
||||||
|
|
||||||
|
root_config.override(
|
||||||
|
{
|
||||||
|
'core': {
|
||||||
|
'value': 'core',
|
||||||
|
},
|
||||||
|
'services': {
|
||||||
|
'value': 'services',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(core.config(), {'value': 'core'})
|
||||||
|
self.assertEqual(core.config.value(), 'core')
|
||||||
|
self.assertEqual(core.value_getter(), 'core')
|
||||||
|
|
||||||
|
self.assertEqual(services.config(), {'value': 'services'})
|
||||||
|
self.assertEqual(services.config.value(), 'services')
|
||||||
|
self.assertEqual(services.value_getter(), 'services')
|
||||||
|
|
||||||
|
def test_double_override(self):
|
||||||
|
root_config = providers.Configuration('main')
|
||||||
|
core = self.TestCore(config=root_config.core)
|
||||||
|
services = self.TestServices(config=root_config.services)
|
||||||
|
|
||||||
|
root_config.override(
|
||||||
|
{
|
||||||
|
'core': {
|
||||||
|
'value': 'core1',
|
||||||
|
},
|
||||||
|
'services': {
|
||||||
|
'value': 'services1',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
root_config.override(
|
||||||
|
{
|
||||||
|
'core': {
|
||||||
|
'value': 'core2',
|
||||||
|
},
|
||||||
|
'services': {
|
||||||
|
'value': 'services2',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(core.config(), {'value': 'core2'})
|
||||||
|
self.assertEqual(core.config.value(), 'core2')
|
||||||
|
self.assertEqual(core.value_getter(), 'core2')
|
||||||
|
|
||||||
|
self.assertEqual(services.config(), {'value': 'services2'})
|
||||||
|
self.assertEqual(services.config.value(), 'services2')
|
||||||
|
self.assertEqual(services.value_getter(), 'services2')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user