Files
xiaozhi-esp32-server/main/xiaozhi-server/config/settings.py
T

57 lines
1.9 KiB
Python
Raw Normal View History

2025-02-14 00:54:59 +08:00
import os
2025-02-28 19:17:36 +08:00
from collections.abc import Mapping
2025-04-12 17:36:04 +08:00
from config.config_loader import read_config, get_project_dir, load_config
2025-02-02 23:01:14 +08:00
2025-02-28 19:17:36 +08:00
default_config_file = "config.yaml"
2025-02-02 23:01:14 +08:00
2025-04-12 17:36:04 +08:00
def find_missing_keys(new_config, old_config, parent_key=""):
2025-02-28 19:17:36 +08:00
"""
递归查找缺失的配置项
返回格式:[缺失配置路径]
"""
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(
2025-04-12 17:36:04 +08:00
value, old_config[key], parent_key=full_path
2025-02-28 19:17:36 +08:00
)
missing_keys.extend(sub_missing)
return missing_keys
def check_config_file():
2025-04-12 17:36:04 +08:00
old_config_file = get_project_dir() + "data/." + default_config_file
if not os.path.exists(old_config_file):
2025-02-28 19:17:36 +08:00
return
2025-04-12 17:36:04 +08:00
old_config = load_config()
2025-02-28 19:17:36 +08:00
new_config = read_config(get_project_dir() + default_config_file)
# 查找缺失的配置项
missing_keys = find_missing_keys(new_config, old_config)
2025-04-12 17:36:04 +08:00
read_config_from_api = old_config.get("read_config_from_api", False)
if read_config_from_api:
return
2025-02-28 19:17:36 +08:00
if missing_keys:
2025-04-12 17:36:04 +08:00
missing_keys_str = "\n".join(f"- {key}" for key in missing_keys)
2025-02-28 19:17:36 +08:00
error_msg = "您的配置文件太旧了,缺少了:\n"
2025-04-12 17:36:04 +08:00
error_msg += missing_keys_str
2025-02-28 19:17:36 +08:00
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)