File size: 1,832 Bytes
fbd9d3d cc826a1 fbd9d3d e5e756a 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 54 55 56 57 58 59 60 | import logging
from typing import Optional
from .core import ContentExtractorCore
logger = logging.getLogger(__name__)
class ContentExtractorPlugin:
"""内容提取插件"""
def __init__(self) -> None:
self.name = "content"
self.version = "1.0.0"
self.enabled = False
self.core: Optional[ContentExtractorCore] = None
def on_enable(self) -> None:
"""启用插件并初始化内容提取核心逻辑"""
self.enabled = True
self.core = ContentExtractorCore()
self._sync_api_core(self.core)
logger.info(f"内容提取插件 {self.version} 已启用")
def on_disable(self) -> None:
"""禁用插件并释放运行时实例"""
self.enabled = False
self._sync_api_core(None)
self.core = None
logger.info(f"内容提取插件 {self.version} 已禁用")
def get_status(self) -> dict:
"""获取插件运行状态"""
return {
"name": self.name,
"version": self.version,
"enabled": self.enabled,
"ocr_loaded": self._is_ocr_loaded(),
"message": "运行正常" if self.enabled else "已禁用",
}
def _is_ocr_loaded(self) -> bool:
"""检查OCR引擎是否已加载,不触发懒加载初始化"""
if self.core is None:
return False
return self.core.ocr_service.is_loaded()
def _sync_api_core(self, core: Optional[ContentExtractorCore]) -> None:
"""同步API模块的插件实例和核心实例"""
try:
from . import api
api.set_plugin_instance(self)
api.set_core_instance(core)
except ImportError as e:
logger.warning(f"同步内容提取插件API实例失败: {e}")
plugin = ContentExtractorPlugin()
|