删除插件配置文件的双向迁移逻辑
This commit is contained in:
@@ -39,76 +39,6 @@ class PluginManager:
|
||||
self._ensure_plugin_directories()
|
||||
logger.info("插件管理器初始化完成")
|
||||
|
||||
def _synchronize_plugin_config(self, plugin_name: str, plugin_dir: str):
|
||||
"""
|
||||
同步单个插件的配置。
|
||||
|
||||
此过程确保中央配置与插件本地配置保持同步,包含两个主要步骤:
|
||||
1. 如果中央配置不存在,则从插件目录复制默认配置到中央配置目录。
|
||||
2. 使用中央配置覆盖插件的本地配置,以确保插件运行时使用的是最新的用户配置。
|
||||
"""
|
||||
try:
|
||||
plugin_path = Path(plugin_dir)
|
||||
# 修正:插件的配置文件路径应为 config.toml 文件,而不是目录
|
||||
plugin_config_file = plugin_path / "config.toml"
|
||||
central_config_dir = Path("config") / "plugins" / plugin_name
|
||||
|
||||
# 确保中央配置目录存在
|
||||
central_config_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# 步骤 1: 从插件目录复制默认配置到中央目录
|
||||
self._copy_default_config_to_central(plugin_name, plugin_config_file, central_config_dir)
|
||||
|
||||
# 步骤 2: 从中央目录同步配置到插件目录
|
||||
self._sync_central_config_to_plugin(plugin_name, plugin_config_file, central_config_dir)
|
||||
|
||||
except OSError as e:
|
||||
logger.error(f"处理插件 '{plugin_name}' 的配置时发生文件操作错误: {e}")
|
||||
except Exception as e:
|
||||
logger.error(f"同步插件 '{plugin_name}' 配置时发生未知错误: {e}")
|
||||
|
||||
@staticmethod
|
||||
def _copy_default_config_to_central(plugin_name: str, plugin_config_file: Path, central_config_dir: Path):
|
||||
"""
|
||||
如果中央配置不存在,则将插件的默认 config.toml 复制到中央目录。
|
||||
"""
|
||||
if not plugin_config_file.is_file():
|
||||
return # 插件没有提供默认配置文件,直接跳过
|
||||
|
||||
central_config_file = central_config_dir / plugin_config_file.name
|
||||
if not central_config_file.exists():
|
||||
shutil.copy2(plugin_config_file, central_config_file)
|
||||
logger.info(f"为插件 '{plugin_name}' 从模板复制了默认配置: {plugin_config_file.name}")
|
||||
|
||||
def _sync_central_config_to_plugin(self, plugin_name: str, plugin_config_file: Path, central_config_dir: Path):
|
||||
"""
|
||||
将中央配置同步(覆盖)到插件的本地配置。
|
||||
"""
|
||||
# 遍历中央配置目录中的所有文件
|
||||
for central_file in central_config_dir.iterdir():
|
||||
if not central_file.is_file():
|
||||
continue
|
||||
|
||||
# 目标文件应与中央配置文件同名,这里我们强制它为 config.toml
|
||||
target_plugin_file = plugin_config_file
|
||||
|
||||
# 仅在文件内容不同时才执行复制,以减少不必要的IO操作
|
||||
if not self._is_file_content_identical(central_file, target_plugin_file):
|
||||
shutil.copy2(central_file, target_plugin_file)
|
||||
logger.info(f"已将中央配置 '{central_file.name}' 同步到插件 '{plugin_name}'")
|
||||
|
||||
@staticmethod
|
||||
def _is_file_content_identical(file1: Path, file2: Path) -> bool:
|
||||
"""
|
||||
通过比较 MD5 哈希值检查两个文件的内容是否相同。
|
||||
"""
|
||||
if not file2.exists():
|
||||
return False # 目标文件不存在,视为不同
|
||||
|
||||
# 使用 'rb' 模式以二进制方式读取文件,确保哈希值计算的一致性
|
||||
with open(file1, "rb") as f1, open(file2, "rb") as f2:
|
||||
return hashlib.md5(f1.read()).hexdigest() == hashlib.md5(f2.read()).hexdigest()
|
||||
|
||||
# === 插件目录管理 ===
|
||||
|
||||
def add_plugin_directory(self, directory: str) -> bool:
|
||||
|
||||
Reference in New Issue
Block a user