Skip to content

Commit

Permalink
Merge 82130cd into af87256
Browse files Browse the repository at this point in the history
  • Loading branch information
rgonalo committed Apr 11, 2022
2 parents af87256 + 82130cd commit cb04df8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -6,6 +6,8 @@ v2.5.1

*Release date: In development*

- Update map_param method to allow recursive replacements

v2.5.0
------

Expand Down
24 changes: 24 additions & 0 deletions toolium/test/utils/test_dataset_map_param.py
Expand Up @@ -395,6 +395,30 @@ def test_a_combi_of_config_plustext_plusconfig():
assert expected == result


def test_a_combi_of_config_inside_config():
"""
Verification of a combination of a config param inside another config param
"""
dataset.project_config = {"var_name": "NAME2"}
os.environ['MY_VAR_NAME1'] = "name1 value"
os.environ['MY_VAR_NAME2'] = "name2 value"
result = map_param("[ENV:MY_VAR_[CONF:var_name]]")
expected = "name2 value"
assert expected == result


def test_a_combi_of_config_inside_config_recursively():
"""
Verification of a combination of a config param inside another config param recursively
"""
dataset.project_config = {"var_number": 2, "var_name_1": "A", "var_name_2": "B"}
os.environ['MY_VAR_A'] = "A value"
os.environ['MY_VAR_B'] = "B value"
result = map_param("[CONF:var_number] some text [ENV:MY_VAR_[CONF:var_name_[CONF:var_number]]]")
expected = "2 some text B value"
assert expected == result


def test_a_conf_param_with_special_characters():
"""
Verification of a combination of text plus a config param with special characters
Expand Down
25 changes: 16 additions & 9 deletions toolium/utils/dataset.py
Expand Up @@ -287,6 +287,8 @@ def _infer_param_type(param):
return new_param


# Ignore flake8 warning until deprecated context parameter is removed
# flake8: noqa: C901
def map_param(param, context=None):
"""
Transform the given string by replacing specific patterns containing keys with their values,
Expand Down Expand Up @@ -319,14 +321,20 @@ def map_param(param, context=None):
map_regex = r"[\[CONF:|\[LANG:|\[POE:|\[ENV:|\[BASE64:|\[TOOLIUM:|\[CONTEXT:|\[FILE:][a-zA-Z\.\:\/\_\-\ 0-9]*\]"
map_expressions = re.compile(map_regex)

# The parameter is just one config value
mapped_param = param
if map_expressions.split(param) == ['', '']:
return map_one_param(param)
# The parameter is just one config value
mapped_param = map_one_param(param)
else:
# The parameter is a combination of text and configuration parameters.
for match in map_expressions.findall(param):
mapped_param = mapped_param.replace(match, str(map_one_param(match)))

# The parameter is a combination of text and configuration parameters.
for match in map_expressions.findall(param):
param = param.replace(match, str(map_one_param(match)))
return param
if mapped_param != param:
# Calling to map_param recursively to replace parameters that include another parameters
mapped_param = map_param(mapped_param, context)

return mapped_param


def map_one_param(param):
Expand Down Expand Up @@ -398,9 +406,8 @@ def map_one_param(param):
}

if key and mapping_functions[type]["prerequisites"]:
return mapping_functions[type]["function"](*mapping_functions[type]["args"])
else:
return param
param = mapping_functions[type]["function"](*mapping_functions[type]["args"])
return param


def _get_mapping_type_and_key(param):
Expand Down

0 comments on commit cb04df8

Please sign in to comment.