mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-05 21:03:14 +03:00
Update from_dict() method
This commit is contained in:
parent
ff4cb0e9a8
commit
f701ef8085
File diff suppressed because it is too large
Load Diff
|
@ -1387,7 +1387,7 @@ cdef class ConfigurationOption(Provider):
|
|||
current_config = {}
|
||||
self.override(merge_dicts(current_config, config))
|
||||
|
||||
def from_dict(self, options):
|
||||
def from_dict(self, options, required=UNDEFINED):
|
||||
"""Load configuration from the dictionary.
|
||||
|
||||
Loaded configuration is merged recursively over existing configuration.
|
||||
|
@ -1395,14 +1395,24 @@ cdef class ConfigurationOption(Provider):
|
|||
:param options: Configuration options.
|
||||
:type options: dict
|
||||
|
||||
:param required: When required is True, raise an exception if dictionary is empty.
|
||||
:type required: bool
|
||||
|
||||
:rtype: None
|
||||
"""
|
||||
if self._is_strict_mode_enabled() and not options:
|
||||
if required is not False \
|
||||
and (self._is_strict_mode_enabled() or required is True) \
|
||||
and not options:
|
||||
raise ValueError('Can not use empty dictionary')
|
||||
|
||||
current_config = self.__call__()
|
||||
if not current_config:
|
||||
try:
|
||||
current_config = self.__call__()
|
||||
except Error:
|
||||
current_config = {}
|
||||
else:
|
||||
if not current_config:
|
||||
current_config = {}
|
||||
|
||||
self.override(merge_dicts(current_config, options))
|
||||
|
||||
def from_env(self, name, default=UNDEFINED):
|
||||
|
@ -1705,7 +1715,7 @@ cdef class Configuration(Object):
|
|||
current_config = {}
|
||||
self.override(merge_dicts(current_config, config))
|
||||
|
||||
def from_dict(self, options):
|
||||
def from_dict(self, options, required=UNDEFINED):
|
||||
"""Load configuration from the dictionary.
|
||||
|
||||
Loaded configuration is merged recursively over existing configuration.
|
||||
|
@ -1713,9 +1723,14 @@ cdef class Configuration(Object):
|
|||
:param options: Configuration options.
|
||||
:type options: dict
|
||||
|
||||
:param required: When required is True, raise an exception if file does not exist.
|
||||
:type required: bool
|
||||
|
||||
:rtype: None
|
||||
"""
|
||||
if self._is_strict_mode_enabled() and not options:
|
||||
if required is not False \
|
||||
and (self._is_strict_mode_enabled() or required is True) \
|
||||
and not options:
|
||||
raise ValueError('Can not use empty dictionary')
|
||||
|
||||
current_config = self.__call__()
|
||||
|
|
|
@ -737,24 +737,6 @@ class ConfigFromDict(unittest.TestCase):
|
|||
self.assertEqual(self.config.section2(), {'value2': '2'})
|
||||
self.assertEqual(self.config.section2.value2(), '2')
|
||||
|
||||
def test_empty_dict(self):
|
||||
self.config.from_dict({})
|
||||
self.assertEqual(self.config(), {})
|
||||
|
||||
def test_option_empty_dict(self):
|
||||
self.config.option.from_dict({})
|
||||
self.assertEqual(self.config.option(), {})
|
||||
|
||||
def test_empty_dict_in_strict_mode(self):
|
||||
self.config = providers.Configuration(strict=True)
|
||||
with self.assertRaises(ValueError):
|
||||
self.config.from_dict({})
|
||||
|
||||
def test_option_empty_dict_in_strict_mode(self):
|
||||
self.config = providers.Configuration(strict=True)
|
||||
with self.assertRaises(ValueError):
|
||||
self.config.option.from_dict({})
|
||||
|
||||
def test_merge(self):
|
||||
self.config.from_dict(self.config_options_1)
|
||||
self.config.from_dict(self.config_options_2)
|
||||
|
@ -782,6 +764,43 @@ class ConfigFromDict(unittest.TestCase):
|
|||
self.assertEqual(self.config.section3(), {'value3': '3'})
|
||||
self.assertEqual(self.config.section3.value3(), '3')
|
||||
|
||||
def test_empty_dict(self):
|
||||
self.config.from_dict({})
|
||||
self.assertEqual(self.config(), {})
|
||||
|
||||
def test_option_empty_dict(self):
|
||||
self.config.option.from_dict({})
|
||||
self.assertEqual(self.config.option(), {})
|
||||
|
||||
def test_empty_dict_in_strict_mode(self):
|
||||
self.config = providers.Configuration(strict=True)
|
||||
with self.assertRaises(ValueError):
|
||||
self.config.from_dict({})
|
||||
|
||||
def test_option_empty_dict_in_strict_mode(self):
|
||||
self.config = providers.Configuration(strict=True)
|
||||
with self.assertRaises(ValueError):
|
||||
self.config.option.from_dict({})
|
||||
|
||||
def test_required_empty_dict(self):
|
||||
with self.assertRaises(ValueError):
|
||||
self.config.from_dict({}, required=True)
|
||||
|
||||
def test_required_option_empty_dict(self):
|
||||
with self.assertRaises(ValueError):
|
||||
self.config.option.from_dict({}, required=True)
|
||||
|
||||
def test_not_required_empty_dict_strict_mode(self):
|
||||
self.config = providers.Configuration(strict=True)
|
||||
self.config.from_dict({}, required=False)
|
||||
self.assertEqual(self.config(), {})
|
||||
|
||||
def test_not_required_option_empty_dict_strict_mode(self):
|
||||
self.config = providers.Configuration(strict=True)
|
||||
self.config.option.from_dict({}, required=False)
|
||||
self.assertEqual(self.config.option(), {})
|
||||
self.assertEqual(self.config(), {'option': {}})
|
||||
|
||||
|
||||
class ConfigFromEnvTests(unittest.TestCase):
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user