Refactor YAML environment variables interpolation

This commit is contained in:
Roman Mogylatov 2021-01-23 10:07:27 -05:00
parent 922b7b8ec6
commit 6386a0b94d
2 changed files with 5981 additions and 6621 deletions

File diff suppressed because it is too large Load Diff

View File

@ -54,14 +54,6 @@ else: # pragma: no cover
copy.deepcopy(obj.im_self, memo), copy.deepcopy(obj.im_self, memo),
obj.im_class) 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: if sys.version_info[0] == 3:
class EnvInterpolation(iniconfigparser.BasicInterpolation): class EnvInterpolation(iniconfigparser.BasicInterpolation):
@ -91,31 +83,23 @@ else:
if yaml: if yaml:
# TODO: use SafeLoader without env interpolation by default in version 5.*
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)
class YamlLoader(yaml.SafeLoader): class YamlLoader(yaml.SafeLoader):
"""Custom YAML loader. """Custom YAML loader.
Inherits ``yaml.SafeLoader`` and add environment variables interpolation. Inherits ``yaml.SafeLoader`` and add environment variables interpolation.
""" """
tag = '!!str' YamlLoader.add_implicit_resolver('!path', yaml_env_marker_pattern, None)
pattern = re.compile('.*?\${(\w+)}.*?') YamlLoader.add_constructor('!path', yaml_env_marker_constructor)
@classmethod
def constructor_env_variables(cls, loader, node):
value = loader.construct_scalar(node)
match = cls.pattern.findall(value)
if match:
full_value = value
for group in match:
full_value = full_value.replace(
f'${{{group}}}', os.environ.get(group, group)
)
return full_value
return value
# TODO: use SafeLoader without env interpolation by default in version 5.*
YamlLoader.add_implicit_resolver(YamlLoader.tag, YamlLoader.pattern, None)
YamlLoader.add_constructor(YamlLoader.tag, YamlLoader.constructor_env_variables)
else: else:
class YamlLoader: class YamlLoader:
"""Custom YAML loader. """Custom YAML loader.
@ -124,7 +108,6 @@ else:
""" """
cdef int ASYNC_MODE_UNDEFINED = 0 cdef int ASYNC_MODE_UNDEFINED = 0
cdef int ASYNC_MODE_ENABLED = 1 cdef int ASYNC_MODE_ENABLED = 1
cdef int ASYNC_MODE_DISABLED = 2 cdef int ASYNC_MODE_DISABLED = 2