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

import os
from signalwire.core.config_loader import ConfigLoader
os.environ["API_KEY"] = "secret123"
loader = ConfigLoader()
# Simple substitution
result = loader.substitute_vars("${API_KEY}")
print(result) # "secret123"
# With default value
result = loader.substitute_vars("${MISSING_VAR|fallback}")
print(result) # "fallback"
# Nested structure
config = {
"auth": {"token": "${API_KEY}"},
"timeout": "${TIMEOUT|30}",
}
result = loader.substitute_vars(config)
print(result) # {"auth": {"token": "secret123"}, "timeout": 30}