mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 01:26:51 +03:00
Add Configuration.from_dict() method
This commit is contained in:
parent
a985952d0c
commit
f95dadfc23
|
@ -10,6 +10,7 @@ follows `Semantic versioning`_
|
|||
Development version
|
||||
-------------------
|
||||
- Add ``Configuration.from_ini()`` method to load configuration from ini file.
|
||||
- Add ``Configuration.from_dict()`` method to load configuration from dictionary.
|
||||
|
||||
3.17.1
|
||||
------
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1159,9 +1159,26 @@ cdef class Configuration(Object):
|
|||
"""
|
||||
self.override(value)
|
||||
|
||||
def from_dict(self, options):
|
||||
"""Load configuration from dictionary.
|
||||
|
||||
Loaded configuration is merged recursively over current configuration.
|
||||
|
||||
:param options: Configuration options.
|
||||
:type options: dict
|
||||
|
||||
:rtype: None
|
||||
"""
|
||||
current_config = self.__call__()
|
||||
if not current_config:
|
||||
current_config = {}
|
||||
self.override(merge_dicts(current_config, options))
|
||||
|
||||
def from_ini(self, filepath):
|
||||
"""Load configuration from ini file.
|
||||
|
||||
Loaded configuration is merged recursively over current configuration.
|
||||
|
||||
:param filepath: Path to the configuration file.
|
||||
:type filepath: str
|
||||
|
||||
|
@ -1174,7 +1191,10 @@ cdef class Configuration(Object):
|
|||
for section in parser.sections():
|
||||
config[section] = dict(parser.items(section))
|
||||
|
||||
self.override(config)
|
||||
current_config = self.__call__()
|
||||
if not current_config:
|
||||
current_config = {}
|
||||
self.override(merge_dicts(current_config, config))
|
||||
|
||||
def _create_children(self, value):
|
||||
children = dict()
|
||||
|
@ -2342,3 +2362,24 @@ def __add_sys_streams(memo):
|
|||
memo[id(sys.stdin)] = sys.stdin
|
||||
memo[id(sys.stdout)] = sys.stdout
|
||||
memo[id(sys.stderr)] = sys.stderr
|
||||
|
||||
|
||||
def merge_dicts(dict1, dict2):
|
||||
"""Merge dictionaries recursively.
|
||||
|
||||
:param dict1: Dictionary 1
|
||||
:type dict1: dict
|
||||
|
||||
:param dict2: Dictionary 2
|
||||
:type dict2: dict
|
||||
|
||||
:return: New resulting dictionary
|
||||
:rtype: dict
|
||||
"""
|
||||
for key, value in dict1.items():
|
||||
if key in dict2:
|
||||
if isinstance(value, dict) and isinstance(dict2[key], dict):
|
||||
dict2[key] = merge_dicts(value, dict2[key])
|
||||
result = dict1.copy()
|
||||
result.update(dict2)
|
||||
return result
|
||||
|
|
|
@ -251,13 +251,73 @@ class ConfigLinkingTests(unittest.TestCase):
|
|||
self.assertEqual(services.value_getter(), 'services2')
|
||||
|
||||
|
||||
class ConfigFromDict(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.config = providers.Configuration(name='config')
|
||||
|
||||
self.config_options_1 = {
|
||||
'section1': {
|
||||
'value1': '1',
|
||||
},
|
||||
'section2': {
|
||||
'value2': '2',
|
||||
},
|
||||
}
|
||||
self.config_options_2 = {
|
||||
'section1': {
|
||||
'value1': '11',
|
||||
'value11': '11',
|
||||
},
|
||||
'section3': {
|
||||
'value3': '3',
|
||||
},
|
||||
}
|
||||
|
||||
def test(self):
|
||||
self.config.from_dict(self.config_options_1)
|
||||
|
||||
self.assertEqual(self.config(), {'section1': {'value1': '1'}, 'section2': {'value2': '2'}})
|
||||
self.assertEqual(self.config.section1(), {'value1': '1'})
|
||||
self.assertEqual(self.config.section1.value1(), '1')
|
||||
self.assertEqual(self.config.section2(), {'value2': '2'})
|
||||
self.assertEqual(self.config.section2.value2(), '2')
|
||||
|
||||
def test_merge(self):
|
||||
self.config.from_dict(self.config_options_1)
|
||||
self.config.from_dict(self.config_options_2)
|
||||
|
||||
self.assertEqual(
|
||||
self.config(),
|
||||
{
|
||||
'section1': {
|
||||
'value1': '11',
|
||||
'value11': '11',
|
||||
},
|
||||
'section2': {
|
||||
'value2': '2',
|
||||
},
|
||||
'section3': {
|
||||
'value3': '3',
|
||||
},
|
||||
},
|
||||
)
|
||||
self.assertEqual(self.config.section1(), {'value1': '11', 'value11': '11'})
|
||||
self.assertEqual(self.config.section1.value1(), '11')
|
||||
self.assertEqual(self.config.section1.value11(), '11')
|
||||
self.assertEqual(self.config.section2(), {'value2': '2'})
|
||||
self.assertEqual(self.config.section2.value2(), '2')
|
||||
self.assertEqual(self.config.section3(), {'value3': '3'})
|
||||
self.assertEqual(self.config.section3.value3(), '3')
|
||||
|
||||
|
||||
class ConfigFromIniTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.config = providers.Configuration(name='config')
|
||||
_, self.config_file = tempfile.mkstemp()
|
||||
|
||||
with open(self.config_file, 'w') as config_file:
|
||||
_, self.config_file_1 = tempfile.mkstemp()
|
||||
with open(self.config_file_1, 'w') as config_file:
|
||||
config_file.write(
|
||||
'[section1]\n'
|
||||
'value1=1\n'
|
||||
|
@ -266,15 +326,53 @@ class ConfigFromIniTests(unittest.TestCase):
|
|||
'value2=2\n'
|
||||
)
|
||||
|
||||
_, self.config_file_2 = tempfile.mkstemp()
|
||||
with open(self.config_file_2, 'w') as config_file:
|
||||
config_file.write(
|
||||
'[section1]\n'
|
||||
'value1=11\n'
|
||||
'value11=11\n'
|
||||
'[section3]\n'
|
||||
'value3=3\n'
|
||||
)
|
||||
|
||||
def tearDown(self):
|
||||
del self.config
|
||||
os.unlink(self.config_file)
|
||||
os.unlink(self.config_file_1)
|
||||
os.unlink(self.config_file_2)
|
||||
|
||||
def test(self):
|
||||
self.config.from_ini(self.config_file)
|
||||
self.config.from_ini(self.config_file_1)
|
||||
|
||||
self.assertEqual(self.config(), {'section1': {'value1': '1'}, 'section2': {'value2': '2'}})
|
||||
self.assertEqual(self.config.section1(), {'value1': '1'})
|
||||
self.assertEqual(self.config.section1.value1(), '1')
|
||||
self.assertEqual(self.config.section2(), {'value2': '2'})
|
||||
self.assertEqual(self.config.section2.value2(), '2')
|
||||
|
||||
def test_merge(self):
|
||||
self.config.from_ini(self.config_file_1)
|
||||
self.config.from_ini(self.config_file_2)
|
||||
|
||||
self.assertEqual(
|
||||
self.config(),
|
||||
{
|
||||
'section1': {
|
||||
'value1': '11',
|
||||
'value11': '11',
|
||||
},
|
||||
'section2': {
|
||||
'value2': '2',
|
||||
},
|
||||
'section3': {
|
||||
'value3': '3',
|
||||
},
|
||||
},
|
||||
)
|
||||
self.assertEqual(self.config.section1(), {'value1': '11', 'value11': '11'})
|
||||
self.assertEqual(self.config.section1.value1(), '11')
|
||||
self.assertEqual(self.config.section1.value11(), '11')
|
||||
self.assertEqual(self.config.section2(), {'value2': '2'})
|
||||
self.assertEqual(self.config.section2.value2(), '2')
|
||||
self.assertEqual(self.config.section3(), {'value3': '3'})
|
||||
self.assertEqual(self.config.section3.value3(), '3')
|
||||
|
|
Loading…
Reference in New Issue
Block a user