refactor: 收口 V3 分层架构与插件兼容边界

This commit is contained in:
jxxghp
2026-08-18 13:22:02 +08:00
parent cca99bd421
commit 8472bcff43
274 changed files with 10730 additions and 6130 deletions
+64
View File
@@ -11,6 +11,7 @@ ConfigWriter = Callable[[Any, Any], Any]
AsyncConfigWriter = Callable[[Any, Any], Awaitable[Any]]
ConfigDeleter = Callable[[Any], bool]
PluginDataDeleter = Callable[[str], Any]
PluginExists = Callable[[str], bool]
def _empty_read(_key: Any) -> Any:
@@ -75,6 +76,69 @@ class PluginStorage:
return self._delete_data(plugin_id)
class PluginConfigStore:
"""封装插件配置键、存在性和强制删除规则。"""
def __init__(
self,
*,
storage: Callable[[], "PluginStorage"],
plugin_exists: PluginExists,
key_prefix: str = "plugin.%s",
) -> None:
"""保存持久化端口和运行态插件查询端口。"""
self._storage = storage
self._plugin_exists = plugin_exists
self._key_prefix = key_prefix
def _key(self, plugin_id: str) -> str:
"""构造插件配置在统一配置存储中的键。"""
return self._key_prefix % plugin_id
def read(self, plugin_id: str) -> dict:
"""读取配置并过滤历史空键。"""
if not self._plugin_exists(plugin_id):
return {}
config = self._storage().read(self._key(plugin_id))
return {
key: value
for key, value in (config or {}).items()
if key
}
def write(self, plugin_id: str, config: dict, force: bool = False) -> bool:
"""保存配置,默认拒绝不存在插件的配置写入。"""
if not force and not self._plugin_exists(plugin_id):
return False
self._storage().write(self._key(plugin_id), config)
return True
async def async_write(
self,
plugin_id: str,
config: dict,
force: bool = False,
) -> bool:
"""异步保存配置并保持同步写入的存在性规则。"""
if not force and not self._plugin_exists(plugin_id):
return False
await self._storage().async_write(self._key(plugin_id), config)
return True
def delete(self, plugin_id: str, force: bool = False) -> bool:
"""删除配置并保持停止插件后的强制删除能力。"""
if not force and not self._plugin_exists(plugin_id):
return False
return self._storage().delete(self._key(plugin_id))
def delete_data(self, plugin_id: str, force: bool = False) -> bool:
"""删除插件业务数据并保持旧的布尔结果合同。"""
if not force and not self._plugin_exists(plugin_id):
return False
self._storage().delete_data(plugin_id)
return True
_plugin_storage = PluginStorage()