mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 07:03:53 +08:00
定义config.yaml为默认配置; data/.config.yaml 为自定义配置; 使用者通过设置自定义配置即可完成启动,无需关心默认配置项,减少用户冗余配置
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import os
|
||||
import argparse
|
||||
import yaml
|
||||
from collections.abc import Mapping
|
||||
from config.manage_api_client import init_service, get_server_config, get_agent_models
|
||||
|
||||
|
||||
@@ -25,12 +26,19 @@ def load_config():
|
||||
if _config_cache is not None:
|
||||
return _config_cache
|
||||
|
||||
parser = argparse.ArgumentParser(description="Server configuration")
|
||||
config_file = get_config_file()
|
||||
default_config_path = get_project_dir() + "config.yaml"
|
||||
custom_config_path = get_project_dir() + "data/.config.yaml"
|
||||
|
||||
parser.add_argument("--config_path", type=str, default=config_file)
|
||||
args = parser.parse_args()
|
||||
config = read_config(args.config_path)
|
||||
# 加载默认配置
|
||||
default_config = read_config(default_config_path)
|
||||
|
||||
# 加载用户自定义配置(如果存在)
|
||||
if os.path.exists(custom_config_path):
|
||||
custom_config = read_config(custom_config_path)
|
||||
# 合并配置
|
||||
config = merge_configs(default_config, custom_config)
|
||||
else:
|
||||
config = default_config
|
||||
|
||||
if config.get("manager-api", {}).get("url"):
|
||||
config = get_config_from_api(config)
|
||||
@@ -115,3 +123,28 @@ def ensure_directories(config):
|
||||
os.makedirs(dir_path, exist_ok=True)
|
||||
except PermissionError:
|
||||
print(f"警告:无法创建目录 {dir_path},请检查写入权限")
|
||||
|
||||
|
||||
def merge_configs(default_config, custom_config):
|
||||
"""
|
||||
递归合并配置,custom_config优先级更高
|
||||
|
||||
Args:
|
||||
default_config: 默认配置
|
||||
custom_config: 用户自定义配置
|
||||
|
||||
Returns:
|
||||
合并后的配置
|
||||
"""
|
||||
if not isinstance(default_config, Mapping) or not isinstance(custom_config, Mapping):
|
||||
return custom_config
|
||||
|
||||
merged = dict(default_config)
|
||||
|
||||
for key, value in custom_config.items():
|
||||
if key in merged and isinstance(merged[key], Mapping) and isinstance(value, Mapping):
|
||||
merged[key] = merge_configs(merged[key], value)
|
||||
else:
|
||||
merged[key] = value
|
||||
|
||||
return merged
|
||||
@@ -1,7 +1,11 @@
|
||||
import os
|
||||
from collections.abc import Mapping
|
||||
from config import logger
|
||||
from config.config_loader import read_config, get_project_dir, load_config
|
||||
|
||||
TAG = __name__
|
||||
logger = logger.setup_logging()
|
||||
|
||||
default_config_file = "config.yaml"
|
||||
|
||||
|
||||
@@ -34,31 +38,16 @@ def find_missing_keys(new_config, old_config, parent_key=""):
|
||||
|
||||
|
||||
def check_config_file():
|
||||
old_config_file = get_project_dir() + "data/." + default_config_file
|
||||
if not os.path.exists(old_config_file):
|
||||
return
|
||||
old_config = load_config()
|
||||
new_config = read_config(get_project_dir() + default_config_file)
|
||||
# 查找缺失的配置项
|
||||
missing_keys = find_missing_keys(new_config, old_config)
|
||||
read_config_from_api = old_config.get("read_config_from_api", False)
|
||||
if read_config_from_api:
|
||||
old_config_origin = read_config(old_config_file)
|
||||
if old_config_origin.get("selected_module") is not None:
|
||||
missing_keys_str = "\n".join(f"- {key}" for key in missing_keys)
|
||||
error_msg = "您的配置文件好像既包含智控台的配置又包含本地配置:\n"
|
||||
error_msg += "\n建议您:\n"
|
||||
error_msg += "1、将根目录的config_from_api.yaml文件复制到data下,重命名为.config.yaml\n"
|
||||
error_msg += "2、按教程配置好接口地址和密钥\n"
|
||||
raise ValueError(error_msg)
|
||||
return
|
||||
|
||||
if missing_keys:
|
||||
missing_keys_str = "\n".join(f"- {key}" for key in missing_keys)
|
||||
error_msg = "您的配置文件太旧了,缺少了:\n"
|
||||
error_msg += missing_keys_str
|
||||
error_msg += "\n建议您:\n"
|
||||
error_msg += "1、备份data/.config.yaml文件\n"
|
||||
error_msg += "2、将根目录的config.yaml文件复制到data下,重命名为.config.yaml\n"
|
||||
error_msg += "3、将密钥逐个复制到新的配置文件中\n"
|
||||
raise ValueError(error_msg)
|
||||
"""
|
||||
简化的配置检查,仅提示用户配置文件的使用情况
|
||||
"""
|
||||
custom_config_file = get_project_dir() + "data/." + default_config_file
|
||||
if not os.path.exists(custom_config_file):
|
||||
logger.bind(tag=TAG).info("提示: 使用默认配置文件。如需自定义配置,请创建 data/.config.yaml 文件")
|
||||
else:
|
||||
logger.bind(tag=TAG).info(f"提示: 使用自定义配置文件 data/.config.yaml,配置将覆盖默认值")
|
||||
|
||||
# 检查是否从API读取配置
|
||||
config = load_config()
|
||||
if config.get("read_config_from_api", False):
|
||||
logger.bind(tag=TAG).info("提示: 从API获取配置")
|
||||
|
||||
Reference in New Issue
Block a user