Skip to content
This repository has been archived by the owner on Aug 22, 2019. It is now read-only.

Commit

Permalink
Merge pull request #1705 from scottire/master
Browse files Browse the repository at this point in the history
Add multiple env vars per line
  • Loading branch information
wochinge committed Feb 26, 2019
2 parents 05c505e + 2f1b56e commit 8f8b0e4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Added
for tensorflow based pipelines
- open api spec for the Rasa Core SDK action server
- documentation about early deactivation of a form in validation
- added ability to use multiple env vars per line in yaml files

Changed
-------
Expand Down
10 changes: 8 additions & 2 deletions rasa_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,14 @@ def replace_environment_variables():
def env_var_constructor(loader, node):
"""Process environment variables found in the YAML."""
value = loader.construct_scalar(node)
prefix, env_var, remaining_path = env_var_pattern.match(value).groups()
return prefix + os.environ[env_var] + remaining_path
expanded_vars = os.path.expandvars(value)
if '$' in expanded_vars:
not_expanded = [w for w in expanded_vars.split() if '$' in w]
raise ValueError(
"Error when trying to expand the environment variables"
" in '{}'. Please make sure to also set these environment"
" variables: '{}'.".format(value, not_expanded))
return expanded_vars

yaml.SafeConstructor.add_constructor(u'!env_var', env_var_constructor)

Expand Down
11 changes: 10 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ def test_read_yaml_string_with_env_var():
assert r['user'] == 'user' and r['password'] == 'pass'


def test_read_yaml_string_with_multiple_env_vars_per_line():
config_with_env_var = """
user: ${USER_NAME} ${PASS}
password: ${PASS}
"""
r = utils.read_yaml_string(config_with_env_var)
assert r['user'] == 'user pass' and r['password'] == 'pass'


def test_read_yaml_string_with_env_var_prefix():
config_with_env_var_prefix = """
user: db_${USER_NAME}
Expand Down Expand Up @@ -174,5 +183,5 @@ def test_read_yaml_string_with_env_var_not_exist():
user: ${USER_NAME}
password: ${PASSWORD}
"""
with pytest.raises(KeyError):
with pytest.raises(ValueError):
r = utils.read_yaml_string(config_with_env_var_not_exist)

0 comments on commit 8f8b0e4

Please sign in to comment.