File size: 1,579 Bytes
fbd9d3d cc826a1 fbd9d3d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import logging
from typing import Optional
from .core import JsonContentExtractorCore
logger = logging.getLogger(__name__)
class JsonContentExtractorPlugin:
"""从JSON文件中提取content字段的插件"""
def __init__(self):
self.name = "json"
self.version = "1.0.0"
self.enabled = False
self.core: Optional[JsonContentExtractorCore] = None
def on_enable(self):
"""启用插件并初始化核心逻辑"""
self.enabled = True
self.core = JsonContentExtractorCore()
self._sync_api_core(self.core)
logger.info(f"JSON内容提取插件 {self.version} 已启用")
def on_disable(self):
"""禁用插件并释放资源"""
self.enabled = False
self._sync_api_core(None)
self.core = None
logger.info(f"JSON内容提取插件 {self.version} 已禁用")
def get_status(self) -> dict:
"""获取插件运行状态"""
return {
"name": self.name,
"version": self.version,
"enabled": self.enabled,
"message": "运行正常" if self.enabled else "已禁用",
}
def _sync_api_core(self, core: Optional[JsonContentExtractorCore]) -> None:
"""同步API模块的插件实例和核心实例"""
try:
from . import api
api.set_plugin_instance(self)
api.set_core_instance(core)
except ImportError as e:
logger.warning(f"同步JSON内容提取插件API实例失败: {e}")
plugin = JsonContentExtractorPlugin()
|