Add interpolation of environment variables for Configuration provider

This commit is contained in:
Roman Mogylatov 2020-06-25 23:16:16 -04:00
parent 5718140a82
commit 1cd25e701f
5 changed files with 4994 additions and 3747 deletions

View File

@ -7,6 +7,11 @@ that were made in every particular version.
From version 0.7.6 *Dependency Injector* framework strictly
follows `Semantic versioning`_
3.18.1
------
- Add interpolation of environment variables to ``Configuration.from_yaml()`` and
``Configuration.from_ini()``.
3.18.0
------
- Add ``Configuration.from_yaml()`` method to load configuration from the yaml file.

View File

@ -1,6 +1,6 @@
"""Dependency injector top-level package."""
__version__ = '3.18.0'
__version__ = '3.18.1'
"""Version number that follows semantic versioning.
:type: str

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,7 @@ Powered by Cython.
import copy
import os
import re
import sys
import types
import threading
@ -49,6 +50,29 @@ else: # pragma: no cover
copy.deepcopy(obj.im_self, memo),
obj.im_class)
if yaml:
yaml_env_marker_pattern = re.compile(r'\$\{([^}^{]+)\}')
def yaml_env_marker_constructor(_, node):
""""Replace environment variable marker with its value."""
return os.path.expandvars(node.value)
yaml.add_implicit_resolver('!path', yaml_env_marker_pattern)
yaml.add_constructor('!path', yaml_env_marker_constructor)
if sys.version_info[0] == 3:
class EnvInterpolation(iniconfigparser.BasicInterpolation):
"""Interpolation which expands environment variables in values."""
def before_get(self, parser, section, option, value, defaults):
value = super().before_get(parser, section, option, value, defaults)
return os.path.expandvars(value)
def get_ini_parser():
return iniconfigparser.ConfigParser(interpolation=EnvInterpolation())
else:
def get_ini_parser():
return iniconfigparser.SafeConfigParser(os.environ)
cdef class Provider(object):
"""Base provider class.
@ -1178,7 +1202,7 @@ cdef class Configuration(Object):
:rtype: None
"""
parser = iniconfigparser.ConfigParser()
parser = get_ini_parser()
parser.read([filepath])
config = {}

View File

@ -324,6 +324,41 @@ class ConfigFromIniTests(unittest.TestCase):
self.assertEqual(self.config.section3.value3(), '3')
class ConfigFromIniWithEnvInterpolationTests(unittest.TestCase):
def setUp(self):
self.config = providers.Configuration(name='config')
os.environ['CONFIG_TEST_ENV'] = 'test-value'
_, self.config_file = tempfile.mkstemp()
with open(self.config_file, 'w') as config_file:
config_file.write(
'[section1]\n'
'value1=${CONFIG_TEST_ENV}\n'
)
def tearDown(self):
del self.config
del os.environ['CONFIG_TEST_ENV']
os.unlink(self.config_file)
def test_env_variable_interpolation(self):
self.config.from_ini(self.config_file)
self.assertEqual(
self.config(),
{
'section1': {
'value1': 'test-value',
},
},
)
self.assertEqual(self.config.section1(), {'value1': 'test-value'})
self.assertEqual(self.config.section1.value1(), 'test-value')
class ConfigFromYamlTests(unittest.TestCase):
def setUp(self):
@ -414,6 +449,41 @@ class ConfigFromYamlTests(unittest.TestCase):
)
class ConfigFromYamlWithEnvInterpolationTests(unittest.TestCase):
def setUp(self):
self.config = providers.Configuration(name='config')
os.environ['CONFIG_TEST_ENV'] = 'test-value'
_, self.config_file = tempfile.mkstemp()
with open(self.config_file, 'w') as config_file:
config_file.write(
'section1:\n'
' value1: ${CONFIG_TEST_ENV}\n'
)
def tearDown(self):
del self.config
del os.environ['CONFIG_TEST_ENV']
os.unlink(self.config_file)
@unittest.skipIf(sys.version_info[:2] == (3, 4), 'PyYAML does not support Python 3.4')
def test_env_variable_interpolation(self):
self.config.from_yaml(self.config_file)
self.assertEqual(
self.config(),
{
'section1': {
'value1': 'test-value',
},
},
)
self.assertEqual(self.config.section1(), {'value1': 'test-value'})
self.assertEqual(self.config.section1.value1(), 'test-value')
class ConfigFromDict(unittest.TestCase):
def setUp(self):