-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'bugfix/fix_tree_processing_env_vars' into 'main'
Fix expansion of env vars Closes PACMAN-357 See merge request espressif/idf-component-manager!128
- Loading branch information
Showing
6 changed files
with
103 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import os | ||
from string import Template | ||
|
||
from ..errors import ManifestError | ||
|
||
try: | ||
from typing import Any | ||
except ImportError: | ||
pass | ||
|
||
|
||
def _expand_env_vars_in_str(s, env): # type: (str, dict[str, Any]) -> str | ||
try: | ||
return Template(s).substitute(env) | ||
except KeyError as e: | ||
raise ManifestError( | ||
'Using environment variable "{}" in the manifest file but not specifying it'.format(e.args[0])) | ||
|
||
|
||
def expand_env_vars( | ||
obj, # type: dict[str, Any] | list | str | Any | ||
env=None # type: dict | None | ||
): | ||
# type: (...) -> dict[str, Any] | list | str | Any | ||
''' | ||
Expand variables in the results of YAML/JSON file parsing | ||
''' | ||
if env is None: | ||
env = dict(os.environ) | ||
|
||
if isinstance(obj, dict): | ||
return {k: expand_env_vars(v, env) for k, v in obj.items()} | ||
elif isinstance(obj, str): | ||
return _expand_env_vars_in_str(obj, env) | ||
elif isinstance(obj, list): | ||
# yaml dict won't have other iterable data types like set or tuple | ||
return [expand_env_vars(i, env) for i in obj] | ||
|
||
# we don't process other data types, like numbers | ||
return obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import pytest | ||
|
||
from idf_component_tools.manifest.env_expander import expand_env_vars | ||
|
||
TEST_ENVIRON = { | ||
'A': '', | ||
'B': 'b', | ||
} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'inp,env,exp', [ | ||
( | ||
{ | ||
'a': 1, | ||
'b': None, | ||
'c': '$A${B}C' | ||
}, | ||
TEST_ENVIRON, | ||
{ | ||
'a': 1, | ||
'b': None, | ||
'c': 'bC' | ||
}, | ||
), ( | ||
{ | ||
'a': ['1', '2', '3'], | ||
}, | ||
{}, | ||
{ | ||
'a': ['1', '2', '3'], | ||
}, | ||
), ( | ||
{ | ||
'a': [{ | ||
'b': '$B' | ||
}], | ||
}, | ||
TEST_ENVIRON, | ||
{ | ||
'a': [{ | ||
'b': 'b' | ||
}], | ||
}, | ||
) | ||
]) | ||
def test_env_expander(inp, env, exp): | ||
assert expand_env_vars(inp, env) == exp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters