新增了插件相关的完整领域模型、应用服务、基础设施实现,包括: 1. 插件状态、注册模式、来源等基础枚举和数据结构 2. 插件清单解析、发现、加载工具类 3. 插件注册表领域服务和内存存储实现 4. 插件相关的命令、查询、事件定义 5. 插件REST API接口和DTO映射 6. 集成了原有通道适配器到插件系统 7. 新增内置插件注册和自动发现能力
49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
from __future__ import annotations
|
|
|
|
import importlib
|
|
import importlib.util
|
|
import logging
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class PluginLoader:
|
|
async def load(self, entry_file: str) -> object:
|
|
try:
|
|
path = Path(entry_file)
|
|
if path.exists() and path.is_file():
|
|
module_name = entry_file.replace("/", ".").replace("\\", ".").removesuffix(".py")
|
|
if module_name in sys.modules:
|
|
return sys.modules[module_name]
|
|
|
|
spec = importlib.util.spec_from_file_location(module_name, str(path))
|
|
if spec and spec.loader:
|
|
module = importlib.util.module_from_spec(spec)
|
|
sys.modules[module_name] = module
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
module_name = entry_file.replace("/", ".").replace("\\", ".").removesuffix(".py")
|
|
return importlib.import_module(module_name)
|
|
except Exception as exc:
|
|
logger.error("failed to load plugin module %s: %s", entry_file, exc)
|
|
raise
|
|
|
|
def extract_adapter_class(self, module: object) -> type | None:
|
|
for attr_name in dir(module):
|
|
if attr_name.startswith("_"):
|
|
continue
|
|
attr = getattr(module, attr_name, None)
|
|
if attr is None or not isinstance(attr, type):
|
|
continue
|
|
if not hasattr(attr, "get_default_config"):
|
|
continue
|
|
if not hasattr(attr, "capabilities"):
|
|
continue
|
|
if not hasattr(attr, "open") or not hasattr(attr, "close"):
|
|
continue
|
|
return attr
|
|
return None
|