Skip to content

Commit

Permalink
fix: settings env var regex and split
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloogc committed Oct 27, 2023
1 parent 9448bcb commit f398fdc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
6 changes: 3 additions & 3 deletions private_gpt/settings/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

from yaml import SafeLoader

_env_replace_matcher = re.compile(r"\$\{(\w|_)+(:(\w|_)*)?}")
_env_replace_matcher = re.compile(r"\$\{(\w|_)+:?.*}")


@typing.no_type_check # pyaml does not have good hints, everything is Any
def load_yaml_with_envvars(
stream: TextIO, environ: dict[str, Any] = os.environ
stream: TextIO, environ: dict[str, Any] = os.environ
) -> dict[str, Any]:
"""Load yaml file with environment variable expansion.
Expand All @@ -22,7 +22,7 @@ def load_yaml_with_envvars(
def load_env_var(_, node) -> str:
"""Extract the matched value, expand env variable, and replace the match."""
value = str(node.value).removeprefix("${").removesuffix("}")
split = value.split(":")
split = value.split(":", 1)
env_var = split[0]
value = environ.get(env_var)
default = None if len(split) == 1 else split[1]
Expand Down
13 changes: 11 additions & 2 deletions tests/settings/test_settings_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ def test_environment_variables_are_loaded() -> None:

def test_environment_defaults_variables_are_loaded() -> None:
sample_yaml = """
replaced: ${TEST_REPLACE_ME:default}
replaced: ${PGPT_EMBEDDING_HF_MODEL_NAME:BAAI/bge-small-en-v1.5}
"""
loaded = load_yaml_with_envvars(io.StringIO(sample_yaml), {})
assert loaded["replaced"] == "default"
assert loaded["replaced"] == "BAAI/bge-small-en-v1.5"


def test_environment_defaults_variables_are_loaded_with_duplicated_delimiters() -> None:
sample_yaml = """
replaced: ${PGPT_EMBEDDING_HF_MODEL_NAME::duped::}
"""
loaded = load_yaml_with_envvars(io.StringIO(sample_yaml), {})
assert loaded["replaced"] == ":duped::"



def test_environment_without_defaults_fails() -> None:
Expand Down

0 comments on commit f398fdc

Please sign in to comment.