Coverage for pydaconf/utils/interpolation.py: 100.00%
13 statements
« prev ^ index » next coverage.py v7.6.11, created at 2025-02-16 17:46 +0000
« prev ^ index » next coverage.py v7.6.11, created at 2025-02-16 17:46 +0000
1import re
3from pydaconf.utils.dict import get_dict_value
4from pydaconf.utils.exceptions import InterpolationException
7def has_interpolation_template(value: str) -> bool:
8 """Check if a string contains placeholders for interpolation."""
10 return bool(re.search(r"{{\s*(.*?)\s*}}", value))
12def interpolate_template(value: str, data: dict) -> str:
13 """Replace {{ ... }} placeholders with values from the data dictionary."""
15 def replace_match(match: re.Match) -> str:
16 key_path = match.group(1).strip() # Extract the key path
17 match_value = get_dict_value(data, str(key_path))
18 if match_value is None:
19 raise InterpolationException(f"Interpolation failed for key '{key_path}'")
21 return str(match_value)
23 return re.sub(r"{{\s*(.*?)\s*}}", replace_match, value)