-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from DustinMoriarty/feature/env-var
Feature/env var
- Loading branch information
Showing
8 changed files
with
172 additions
and
8 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
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 |
---|---|---|
@@ -1,5 +1,46 @@ | ||
import os | ||
import re | ||
|
||
from typing import Callable | ||
from typing import Dict | ||
from typing import List | ||
from typing import SupportsFloat | ||
from typing import SupportsInt | ||
from typing import Text | ||
from typing import Union | ||
|
||
from config_injector.exc import EnvironmentVariableNotFound | ||
|
||
|
||
def get_type(f: Callable): | ||
return f.__name__ | ||
|
||
|
||
class EnvFiller: | ||
def __init__(self): | ||
""" | ||
Object for filling environment variables in json like python objects | ||
(e.g. dict, list, str, float, int). | ||
""" | ||
self.env_var_regex = re.compile("\\${[a-zA-Z_][a-zA-Z0-9_]*}") | ||
|
||
@staticmethod | ||
def _get_env(m: re.Match) -> Text: | ||
k = m.group(0).replace("${", "").replace("}", "") | ||
val = os.getenv(str(k)) | ||
if val is None: | ||
raise EnvironmentVariableNotFound( | ||
f"Environment variable {k} not found.", variable_name=k | ||
) | ||
return str(val) | ||
|
||
def __call__( | ||
self, o: Union[Dict, List, Text, SupportsInt, SupportsFloat] | ||
) -> Union[Dict, List, Text, SupportsInt, SupportsFloat]: | ||
if isinstance(o, str): | ||
return self.env_var_regex.sub(self._get_env, o) | ||
if hasattr(o, "items"): | ||
return {k: self(v) for k, v in o.items()} | ||
if hasattr(o, "__iter__"): | ||
return [self(v) for v in o] | ||
return o |
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
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,43 @@ | ||
import os | ||
|
||
import pytest | ||
|
||
from config_injector.exc import EnvironmentVariableNotFound | ||
from config_injector.utils import EnvFiller | ||
|
||
|
||
@pytest.fixture() | ||
def test_env_context(): | ||
os.environ["FOO"] = "foo" | ||
os.environ["TOFU"] = "tofu" | ||
os.environ["KUNG_FU"] = "kung fu" | ||
os.environ["mu"] = "μ" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"object,expected", | ||
[ | ||
("${FOO} bar", "foo bar"), | ||
("abc def", "abc def"), | ||
("$abc {def}", "$abc {def}"), | ||
("${abc def}", "${abc def}"), | ||
("${mu}", "μ"), | ||
( | ||
[["abc${FOO}def", "I like ${TOFU}"], {"${KUNG_FU}": "${KUNG_FU}"}], | ||
[["abcfoodef", "I like tofu"], {"${KUNG_FU}": "kung fu"}], | ||
), | ||
( | ||
{"a": 1, "b": 1.1, "c": ["${FOO}"], 10: "ten"}, | ||
{"a": 1, "b": 1.1, "c": ["foo"], 10: "ten"}, | ||
), | ||
], | ||
) | ||
def test_env_filler(object, expected, test_env_context): | ||
env_filler = EnvFiller() | ||
assert env_filler(object) == expected | ||
|
||
|
||
def test_env_filler_raises_env_variable_not_found(): | ||
env_filler = EnvFiller() | ||
with pytest.raises(EnvironmentVariableNotFound): | ||
env_filler("${NOT_A_VAR}") |