mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 11:04:01 +03:00
Add Configuration.from_env() method + refactoring
This commit is contained in:
parent
3217feb73b
commit
507a832bb8
|
@ -9,9 +9,10 @@ follows `Semantic versioning`_
|
|||
|
||||
Development version
|
||||
-------------------
|
||||
- Add ``Configuration.from_yaml()`` method to load configuration from yaml file.
|
||||
- Add ``Configuration.from_ini()`` method to load configuration from ini file.
|
||||
- Add ``Configuration.from_dict()`` method to load configuration from dictionary.
|
||||
- Add ``Configuration.from_yaml()`` method to load configuration from the yaml file.
|
||||
- Add ``Configuration.from_ini()`` method to load configuration from the ini file.
|
||||
- Add ``Configuration.from_dict()`` method to load configuration from the dictionary.
|
||||
- Add ``Configuration.from_env()`` method to load configuration from the environment variable.
|
||||
- Add default value for ``name`` argument of ``Configuration`` provider.
|
||||
- Remove undocumented positional parameter of ``DependenciesContainer`` provider.
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -4,6 +4,7 @@ Powered by Cython.
|
|||
"""
|
||||
|
||||
import copy
|
||||
import os
|
||||
import sys
|
||||
import types
|
||||
import threading
|
||||
|
@ -1164,23 +1165,8 @@ 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.
|
||||
"""Load configuration from the ini file.
|
||||
|
||||
Loaded configuration is merged recursively over current configuration.
|
||||
|
||||
|
@ -1202,7 +1188,7 @@ cdef class Configuration(Object):
|
|||
self.override(merge_dicts(current_config, config))
|
||||
|
||||
def from_yaml(self, filepath):
|
||||
"""Load configuration from yaml file.
|
||||
"""Load configuration from the yaml file.
|
||||
|
||||
Loaded configuration is merged recursively over current configuration.
|
||||
|
||||
|
@ -1226,6 +1212,35 @@ cdef class Configuration(Object):
|
|||
current_config = {}
|
||||
self.override(merge_dicts(current_config, config))
|
||||
|
||||
def from_dict(self, options):
|
||||
"""Load configuration from the 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_env(self, name, default=None):
|
||||
"""Load configuration value from the environment variable.
|
||||
|
||||
:param name: Name of the environment variable.
|
||||
:type name: str
|
||||
|
||||
:param default: Default value that is used if environment variable does not exist.
|
||||
:type default: str
|
||||
|
||||
:rtype: None
|
||||
"""
|
||||
value = os.getenv(name, default)
|
||||
self.override(value)
|
||||
|
||||
def _create_children(self, value):
|
||||
children = dict()
|
||||
|
||||
|
|
|
@ -257,66 +257,6 @@ 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):
|
||||
|
@ -472,3 +412,89 @@ class ConfigFromYamlTests(unittest.TestCase):
|
|||
'Install PyYAML or install Dependency Injector with yaml extras: '
|
||||
'"pip install dependency-injector[yaml]"',
|
||||
)
|
||||
|
||||
|
||||
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 ConfigFromEnvTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.config = providers.Configuration(name='config')
|
||||
os.environ['CONFIG_TEST_ENV'] = 'test-value'
|
||||
|
||||
def tearDown(self):
|
||||
del self.config
|
||||
del os.environ['CONFIG_TEST_ENV']
|
||||
|
||||
def test(self):
|
||||
self.config.from_env('CONFIG_TEST_ENV')
|
||||
self.assertEqual(self.config(), 'test-value')
|
||||
|
||||
def test_default(self):
|
||||
self.config.from_env('UNDEFINED_ENV', 'default-value')
|
||||
self.assertEqual(self.config(), 'default-value')
|
||||
|
||||
def test_with_children(self):
|
||||
self.config.section1.value1.from_env('CONFIG_TEST_ENV')
|
||||
|
||||
self.assertIsNone(self.config())
|
||||
self.assertIsNone(self.config.section1())
|
||||
self.assertEqual(self.config.section1.value1(), 'test-value')
|
||||
|
|
Loading…
Reference in New Issue
Block a user