mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-04 20:33:13 +03:00
Update from_env() method
This commit is contained in:
parent
f701ef8085
commit
a7a0294f5d
File diff suppressed because it is too large
Load Diff
|
@ -1415,7 +1415,7 @@ cdef class ConfigurationOption(Provider):
|
||||||
|
|
||||||
self.override(merge_dicts(current_config, options))
|
self.override(merge_dicts(current_config, options))
|
||||||
|
|
||||||
def from_env(self, name, default=UNDEFINED):
|
def from_env(self, name, default=UNDEFINED, required=UNDEFINED):
|
||||||
"""Load configuration value from the environment variable.
|
"""Load configuration value from the environment variable.
|
||||||
|
|
||||||
:param name: Name of the environment variable.
|
:param name: Name of the environment variable.
|
||||||
|
@ -1424,12 +1424,16 @@ cdef class ConfigurationOption(Provider):
|
||||||
:param default: Default value that is used if environment variable does not exist.
|
:param default: Default value that is used if environment variable does not exist.
|
||||||
:type default: object
|
:type default: object
|
||||||
|
|
||||||
|
:param required: When required is True, raise an exception if environment variable is undefined.
|
||||||
|
:type required: bool
|
||||||
|
|
||||||
:rtype: None
|
:rtype: None
|
||||||
"""
|
"""
|
||||||
value = os.environ.get(name, default)
|
value = os.environ.get(name, default)
|
||||||
|
|
||||||
if value is UNDEFINED:
|
if value is UNDEFINED:
|
||||||
if self._is_strict_mode_enabled():
|
if required is not False \
|
||||||
|
and (self._is_strict_mode_enabled() or required is True):
|
||||||
raise ValueError('Environment variable "{0}" is undefined'.format(name))
|
raise ValueError('Environment variable "{0}" is undefined'.format(name))
|
||||||
value = None
|
value = None
|
||||||
|
|
||||||
|
@ -1738,7 +1742,7 @@ cdef class Configuration(Object):
|
||||||
current_config = {}
|
current_config = {}
|
||||||
self.override(merge_dicts(current_config, options))
|
self.override(merge_dicts(current_config, options))
|
||||||
|
|
||||||
def from_env(self, name, default=UNDEFINED):
|
def from_env(self, name, default=UNDEFINED, required=UNDEFINED):
|
||||||
"""Load configuration value from the environment variable.
|
"""Load configuration value from the environment variable.
|
||||||
|
|
||||||
:param name: Name of the environment variable.
|
:param name: Name of the environment variable.
|
||||||
|
@ -1747,12 +1751,16 @@ cdef class Configuration(Object):
|
||||||
:param default: Default value that is used if environment variable does not exist.
|
:param default: Default value that is used if environment variable does not exist.
|
||||||
:type default: object
|
:type default: object
|
||||||
|
|
||||||
|
:param required: When required is True, raise an exception if environment variable is undefined.
|
||||||
|
:type required: bool
|
||||||
|
|
||||||
:rtype: None
|
:rtype: None
|
||||||
"""
|
"""
|
||||||
value = os.environ.get(name, default)
|
value = os.environ.get(name, default)
|
||||||
|
|
||||||
if value is UNDEFINED:
|
if value is UNDEFINED:
|
||||||
if self._is_strict_mode_enabled():
|
if required is not False \
|
||||||
|
and (self._is_strict_mode_enabled() or required is True):
|
||||||
raise ValueError('Environment variable "{0}" is undefined'.format(name))
|
raise ValueError('Environment variable "{0}" is undefined'.format(name))
|
||||||
value = None
|
value = None
|
||||||
|
|
||||||
|
|
|
@ -816,10 +816,25 @@ class ConfigFromEnvTests(unittest.TestCase):
|
||||||
self.config.from_env('CONFIG_TEST_ENV')
|
self.config.from_env('CONFIG_TEST_ENV')
|
||||||
self.assertEqual(self.config(), 'test-value')
|
self.assertEqual(self.config(), 'test-value')
|
||||||
|
|
||||||
|
def test_with_children(self):
|
||||||
|
self.config.section1.value1.from_env('CONFIG_TEST_ENV')
|
||||||
|
|
||||||
|
self.assertEqual(self.config(), {'section1': {'value1': 'test-value'}})
|
||||||
|
self.assertEqual(self.config.section1(), {'value1': 'test-value'})
|
||||||
|
self.assertEqual(self.config.section1.value1(), 'test-value')
|
||||||
|
|
||||||
def test_default(self):
|
def test_default(self):
|
||||||
self.config.from_env('UNDEFINED_ENV', 'default-value')
|
self.config.from_env('UNDEFINED_ENV', 'default-value')
|
||||||
self.assertEqual(self.config(), 'default-value')
|
self.assertEqual(self.config(), 'default-value')
|
||||||
|
|
||||||
|
def test_default_none(self):
|
||||||
|
self.config.from_env('UNDEFINED_ENV')
|
||||||
|
self.assertIsNone(self.config())
|
||||||
|
|
||||||
|
def test_option_default_none(self):
|
||||||
|
self.config.option.from_env('UNDEFINED_ENV')
|
||||||
|
self.assertIsNone(self.config.option())
|
||||||
|
|
||||||
def test_undefined_in_strict_mode(self):
|
def test_undefined_in_strict_mode(self):
|
||||||
self.config = providers.Configuration(strict=True)
|
self.config = providers.Configuration(strict=True)
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
|
@ -840,17 +855,38 @@ class ConfigFromEnvTests(unittest.TestCase):
|
||||||
self.config.option.from_env('UNDEFINED_ENV', 'default-value')
|
self.config.option.from_env('UNDEFINED_ENV', 'default-value')
|
||||||
self.assertEqual(self.config.option(), 'default-value')
|
self.assertEqual(self.config.option(), 'default-value')
|
||||||
|
|
||||||
def test_default_none(self):
|
def test_required_undefined(self):
|
||||||
self.config.from_env('UNDEFINED_ENV')
|
with self.assertRaises(ValueError):
|
||||||
|
self.config.from_env('UNDEFINED_ENV', required=True)
|
||||||
|
|
||||||
|
def test_required_undefined_with_default(self):
|
||||||
|
self.config.from_env('UNDEFINED_ENV', default='default-value', required=True)
|
||||||
|
self.assertEqual(self.config(), 'default-value')
|
||||||
|
|
||||||
|
def test_option_required_undefined(self):
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
self.config.option.from_env('UNDEFINED_ENV', required=True)
|
||||||
|
|
||||||
|
def test_option_required_undefined_with_default(self):
|
||||||
|
self.config.option.from_env('UNDEFINED_ENV', default='default-value', required=True)
|
||||||
|
self.assertEqual(self.config.option(), 'default-value')
|
||||||
|
|
||||||
|
def test_not_required_undefined_in_strict_mode(self):
|
||||||
|
self.config = providers.Configuration(strict=True)
|
||||||
|
self.config.from_env('UNDEFINED_ENV', required=False)
|
||||||
self.assertIsNone(self.config())
|
self.assertIsNone(self.config())
|
||||||
|
|
||||||
def test_option_default_none(self):
|
def test_option_not_required_undefined_in_strict_mode(self):
|
||||||
self.config.option.from_env('UNDEFINED_ENV')
|
self.config = providers.Configuration(strict=True)
|
||||||
|
self.config.option.from_env('UNDEFINED_ENV', required=False)
|
||||||
self.assertIsNone(self.config.option())
|
self.assertIsNone(self.config.option())
|
||||||
|
|
||||||
def test_with_children(self):
|
def test_not_required_undefined_with_default_in_strict_mode(self):
|
||||||
self.config.section1.value1.from_env('CONFIG_TEST_ENV')
|
self.config = providers.Configuration(strict=True)
|
||||||
|
self.config.from_env('UNDEFINED_ENV', default='default-value', required=False)
|
||||||
|
self.assertEqual(self.config(), 'default-value')
|
||||||
|
|
||||||
self.assertEqual(self.config(), {'section1': {'value1': 'test-value'}})
|
def test_option_not_required_undefined_with_default_in_strict_mode(self):
|
||||||
self.assertEqual(self.config.section1(), {'value1': 'test-value'})
|
self.config = providers.Configuration(strict=True)
|
||||||
self.assertEqual(self.config.section1.value1(), 'test-value')
|
self.config.option.from_env('UNDEFINED_ENV', default='default-value', required=False)
|
||||||
|
self.assertEqual(self.config.option(), 'default-value')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user