mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-25 16:43:55 +08:00
update:优化配置及文档
This commit is contained in:
@@ -31,37 +31,19 @@ def load_config():
|
||||
|
||||
# 加载默认配置
|
||||
default_config = read_config(default_config_path)
|
||||
custom_config = read_config(custom_config_path)
|
||||
|
||||
# 加载用户自定义配置(如果存在)
|
||||
if os.path.exists(custom_config_path):
|
||||
custom_config = read_config(custom_config_path)
|
||||
if custom_config.get("manager-api", {}).get("url"):
|
||||
config = get_config_from_api(custom_config)
|
||||
else:
|
||||
# 合并配置
|
||||
config = merge_configs(default_config, custom_config)
|
||||
else:
|
||||
config = default_config
|
||||
|
||||
if config.get("manager-api", {}).get("url"):
|
||||
config = get_config_from_api(config)
|
||||
|
||||
# 初始化目录
|
||||
ensure_directories(config)
|
||||
_config_cache = config
|
||||
return config
|
||||
|
||||
|
||||
def get_config_file():
|
||||
"""获取配置文件路径,优先使用私有配置文件(若存在)。
|
||||
|
||||
Returns:
|
||||
str: 配置文件路径(相对路径或默认路径)
|
||||
"""
|
||||
default_config_file = "config.yaml"
|
||||
config_file = default_config_file
|
||||
if os.path.exists(get_project_dir() + "data/." + default_config_file):
|
||||
config_file = "data/." + default_config_file
|
||||
return config_file
|
||||
|
||||
|
||||
def get_config_from_api(config):
|
||||
"""从Java API获取配置"""
|
||||
# 初始化API客户端
|
||||
@@ -128,23 +110,29 @@ def ensure_directories(config):
|
||||
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):
|
||||
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):
|
||||
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
|
||||
|
||||
return merged
|
||||
|
||||
@@ -2,6 +2,7 @@ import os
|
||||
import sys
|
||||
from loguru import logger
|
||||
from config.config_loader import load_config
|
||||
from config.settings import check_config_file
|
||||
|
||||
SERVER_VERSION = "0.3.13"
|
||||
|
||||
@@ -32,6 +33,7 @@ def formatter(record):
|
||||
|
||||
|
||||
def setup_logging():
|
||||
check_config_file()
|
||||
"""从配置文件中读取日志配置,并设置日志输出格式和级别"""
|
||||
config = load_config()
|
||||
log_config = config["log"]
|
||||
|
||||
@@ -1,53 +1,33 @@
|
||||
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"
|
||||
|
||||
|
||||
def find_missing_keys(new_config, old_config, parent_key=""):
|
||||
"""
|
||||
递归查找缺失的配置项
|
||||
返回格式:[缺失配置路径]
|
||||
"""
|
||||
missing_keys = []
|
||||
|
||||
if not isinstance(new_config, Mapping):
|
||||
return missing_keys
|
||||
|
||||
for key, value in new_config.items():
|
||||
# 构建当前配置路径
|
||||
full_path = f"{parent_key}.{key}" if parent_key else key
|
||||
|
||||
# 检查键是否存在
|
||||
if key not in old_config:
|
||||
missing_keys.append(full_path)
|
||||
continue
|
||||
|
||||
# 递归检查嵌套字典
|
||||
if isinstance(value, Mapping):
|
||||
sub_missing = find_missing_keys(
|
||||
value, old_config[key], parent_key=full_path
|
||||
)
|
||||
missing_keys.extend(sub_missing)
|
||||
return missing_keys
|
||||
config_file_valid = False
|
||||
|
||||
|
||||
def check_config_file():
|
||||
global config_file_valid
|
||||
if config_file_valid:
|
||||
return
|
||||
"""
|
||||
简化的配置检查,仅提示用户配置文件的使用情况
|
||||
"""
|
||||
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,配置将覆盖默认值")
|
||||
|
||||
raise FileNotFoundError(
|
||||
"找不到data/.config.yaml文件,请按教程确认该配置文件是否存在"
|
||||
)
|
||||
|
||||
# 检查是否从API读取配置
|
||||
config = load_config()
|
||||
if config.get("read_config_from_api", False):
|
||||
logger.bind(tag=TAG).info("提示: 从API获取配置")
|
||||
print("从API读取配置")
|
||||
old_config_origin = read_config(custom_config_file)
|
||||
if old_config_origin.get("selected_module") is not None:
|
||||
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)
|
||||
config_file_valid = True
|
||||
|
||||
Reference in New Issue
Block a user