substitute_vars

View as MarkdownOpen in Claude

Recursively substitute environment variables in a configuration value. Supports ${VAR} and ${VAR|default} syntax.

Parameters

value
AnyRequired

The value to process. Can be a string, dict, list, or any other type. Strings are scanned for ${VAR|default} patterns. Dicts and lists are processed recursively.

max_depth
intDefaults to 10

Maximum recursion depth to prevent infinite substitution loops.

Returns

Any — The value with all ${VAR|default} patterns replaced by the corresponding environment variable values. Strings that resolve to "true"/"false" are converted to booleans. Numeric strings are converted to int or float.

Example

1import os
2from signalwire.core.config_loader import ConfigLoader
3
4os.environ["API_KEY"] = "secret123"
5loader = ConfigLoader()
6
7# Simple substitution
8result = loader.substitute_vars("${API_KEY}")
9print(result) # "secret123"
10
11# With default value
12result = loader.substitute_vars("${MISSING_VAR|fallback}")
13print(result) # "fallback"
14
15# Nested structure
16config = {
17 "auth": {"token": "${API_KEY}"},
18 "timeout": "${TIMEOUT|30}",
19}
20result = loader.substitute_vars(config)
21print(result) # {"auth": {"token": "secret123"}, "timeout": 30}