Files
sentinel-home-ai/fam-ui/src/config_loader.py

30 lines
844 B
Python

"""
配置加载器 (FAM-UI 复用)
"""
import os
import re
import yaml
def _resolve_env_vars(value):
if isinstance(value, str):
def replace_env(match):
return os.environ.get(match.group(1), match.group(0))
return re.sub(r'\$\{(\w+)\}', replace_env, value)
elif isinstance(value, dict):
return {k: _resolve_env_vars(v) for k, v in value.items()}
elif isinstance(value, list):
return [_resolve_env_vars(item) for item in value]
return value
def load_config(config_path=None):
if config_path is None:
config_path = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
'config', 'config.yaml'
)
with open(config_path, 'r', encoding='utf-8') as f:
raw = yaml.safe_load(f)
return _resolve_env_vars(raw)