import json import os import logging import sqlite3 import os import time import toml from pathlib import Path from http import HTTPStatus from datetime import datetime class ConfigManager: """配置管理类,处理应用配置""" def __init__(self, config_path="src"): self.config = {} self.config_path = config_path self.build_config_dict() def build_config_dict(self) -> dict[str, str]: config_dict = {} for config_file in Path(self.config_path).rglob("*.toml"): if not config_file.is_file(): continue # 获取相对路径的父目录名 rel_path = config_file.relative_to(self.config_path) parent_name = rel_path.parent.name if rel_path.parent.name else None if parent_name: key = parent_name else: key = config_file.stem # 去掉扩展名 config_dict[key] = str(config_file.absolute()) self.config = config_dict def load_config(self,name): """加载配置文件""" if not os.path.exists(self.config[name]): return {} with open(self.config[name], 'r', encoding='utf-8') as f: try: return toml.load(f) except toml.TomlDecodeError: return {} def save_config(self, key=None, value=None): """保存配置项""" if key is not None and value is not None: # 如果提供了 key 和 value,则更新单个值 self.config[key] = value with open(self.config_path, 'w', encoding='utf-8') as f: toml.dump(self.config, f) def update_config(self, config_dict): """更新配置字典""" self.config.update(config_dict) self.save_config() class ChatManager: def __init__(self): return None