2025-04-17 11:34:42 +08:00
|
|
|
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
|
2025-04-07 16:25:55 +08:00
|
|
|
import os
|
2025-08-17 22:57:23 +08:00
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
2025-04-07 16:25:55 +08:00
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
|
2025-09-12 22:20:55 +08:00
|
|
|
def get_bool_env(name: str, default: bool = False) -> bool:
|
|
|
|
|
val = os.getenv(name)
|
|
|
|
|
if val is None:
|
|
|
|
|
return default
|
|
|
|
|
return str(val).strip().lower() in {"1", "true", "yes", "y", "on"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_str_env(name: str, default: str = "") -> str:
|
|
|
|
|
val = os.getenv(name)
|
|
|
|
|
return default if val is None else str(val).strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_int_env(name: str, default: int = 0) -> int:
|
|
|
|
|
val = os.getenv(name)
|
|
|
|
|
if val is None:
|
|
|
|
|
return default
|
|
|
|
|
try:
|
|
|
|
|
return int(val.strip())
|
|
|
|
|
except ValueError:
|
|
|
|
|
print(f"Invalid integer value for {name}: {val}. Using default {default}.")
|
|
|
|
|
return default
|
|
|
|
|
|
|
|
|
|
|
2025-04-07 16:25:55 +08:00
|
|
|
def replace_env_vars(value: str) -> str:
|
|
|
|
|
"""Replace environment variables in string values."""
|
|
|
|
|
if not isinstance(value, str):
|
|
|
|
|
return value
|
|
|
|
|
if value.startswith("$"):
|
|
|
|
|
env_var = value[1:]
|
2025-06-07 21:51:26 +08:00
|
|
|
return os.getenv(env_var, env_var)
|
2025-04-07 16:25:55 +08:00
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_dict(config: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
|
|
"""Recursively process dictionary to replace environment variables."""
|
2025-05-28 01:21:40 -07:00
|
|
|
if not config:
|
|
|
|
|
return {}
|
2025-04-07 16:25:55 +08:00
|
|
|
result = {}
|
|
|
|
|
for key, value in config.items():
|
|
|
|
|
if isinstance(value, dict):
|
|
|
|
|
result[key] = process_dict(value)
|
|
|
|
|
elif isinstance(value, str):
|
|
|
|
|
result[key] = replace_env_vars(value)
|
|
|
|
|
else:
|
|
|
|
|
result[key] = value
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_config_cache: Dict[str, Dict[str, Any]] = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_yaml_config(file_path: str) -> Dict[str, Any]:
|
|
|
|
|
"""Load and process YAML configuration file."""
|
|
|
|
|
# 如果文件不存在,返回{}
|
|
|
|
|
if not os.path.exists(file_path):
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
# 检查缓存中是否已存在配置
|
|
|
|
|
if file_path in _config_cache:
|
|
|
|
|
return _config_cache[file_path]
|
|
|
|
|
|
|
|
|
|
# 如果缓存中不存在,则加载并处理配置
|
|
|
|
|
with open(file_path, "r") as f:
|
|
|
|
|
config = yaml.safe_load(f)
|
|
|
|
|
processed_config = process_dict(config)
|
|
|
|
|
|
|
|
|
|
# 将处理后的配置存入缓存
|
|
|
|
|
_config_cache[file_path] = processed_config
|
|
|
|
|
return processed_config
|