已经可以顺利运行
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
from pickle import NONE
|
||||
|
||||
from ..Convention.Runtime.Config import *
|
||||
from ..Convention.Runtime.GlobalConfig import ProjectConfig
|
||||
from ..Convention.Runtime.Architecture import Architecture
|
||||
from ..Convention.Runtime.File import ToolFile
|
||||
@@ -19,7 +22,7 @@ class DatabaseModel(BaseModel):
|
||||
class PluginInterface(ABC):
|
||||
plugin_instances: Dict[str, "PluginInterface"] = {}
|
||||
|
||||
def callback(self, message: str, chat_id: int, user_id: int) -> str:
|
||||
async def callback(self, message: str, chat_id: int, user_id: int) -> str|None:
|
||||
'''
|
||||
继承后重写该方法接受消息并返回消息
|
||||
返回空字符串代表不进行反馈
|
||||
@@ -27,9 +30,8 @@ class PluginInterface(ABC):
|
||||
message: 消息内容
|
||||
chat_id: 会话ID
|
||||
user_id: 用户ID
|
||||
Returns:
|
||||
str: 消息内容
|
||||
'''
|
||||
config.Log("Warning", f"插件{self.__class__.__name__}未实现callback方法")
|
||||
return ""
|
||||
|
||||
def execute(self, path:str) -> Optional[APIRouter]:
|
||||
@@ -39,7 +41,7 @@ class PluginInterface(ABC):
|
||||
'''
|
||||
Architecture.Register(self.__class__, self, self.wake_up, *self.dependencies())
|
||||
router = APIRouter()
|
||||
router.get(path)(self.generate_router_callback())
|
||||
router.post(path)(self.generate_router_callback())
|
||||
# 在数据库保证必要的表和列存在
|
||||
db = get_db()
|
||||
db_model = self.register_db_model()
|
||||
@@ -54,8 +56,8 @@ class PluginInterface(ABC):
|
||||
'''
|
||||
继承后重写该方法生成路由回调函数
|
||||
'''
|
||||
async def callback(*args: Any, **kwargs: Any) -> Any:
|
||||
pass
|
||||
async def callback(message: str, chat_id: int, user_id: int) -> Any:
|
||||
return await self.callback(message, chat_id, user_id)
|
||||
return callback
|
||||
|
||||
def dependencies(self) -> List[Type]:
|
||||
@@ -83,7 +85,16 @@ class PluginInterface(ABC):
|
||||
'''
|
||||
return DatabaseModel()
|
||||
|
||||
def ImportPlugins(app: FastAPI, plugin_dir:str = "Plugins/") -> None:
|
||||
def is_enable_plugin(self) -> bool:
|
||||
'''
|
||||
继承后重写该方法判断是否启用该插件
|
||||
'''
|
||||
return False
|
||||
|
||||
def get_plugin_tag(self) -> str:
|
||||
return "plugin"
|
||||
|
||||
def ImportPlugins(app: FastAPI, plugin_dir:str = "Plugins") -> None:
|
||||
'''
|
||||
导入插件
|
||||
|
||||
@@ -91,26 +102,31 @@ def ImportPlugins(app: FastAPI, plugin_dir:str = "Plugins/") -> None:
|
||||
app: FastAPI应用
|
||||
plugin_dir: 插件目录
|
||||
'''
|
||||
plugin_tool_dir = ToolFile(plugin_dir)
|
||||
plugin_tool_dir = ToolFile(plugin_dir)|None
|
||||
if plugin_tool_dir.Exists() == False:
|
||||
plugin_tool_dir.MustExistsPath()
|
||||
return
|
||||
if plugin_tool_dir.IsDir() == False:
|
||||
config.Log("Error", f"插件目录不是目录: {plugin_tool_dir.GetFullPath()}")
|
||||
return
|
||||
for file in plugin_tool_dir.DirIter():
|
||||
if file.endswith(".py") and not file.startswith("__"):
|
||||
module_name = file[:-3]
|
||||
try:
|
||||
module = importlib.import_module(module_name)
|
||||
for class_name in dir(module):
|
||||
plugin_class = getattr(module, class_name)
|
||||
if issubclass(plugin_class, PluginInterface):
|
||||
plugin = plugin_class()
|
||||
router = plugin.execute(f"/{module_name}")
|
||||
if router:
|
||||
app.include_router(router, prefix=f"/api", tags=[module_name])
|
||||
except Exception as e:
|
||||
config.Log("Error", f"加载插件{module_name}失败: {e}")
|
||||
for dir_name, sub_dirs, files in plugin_tool_dir.DirWalk():
|
||||
for file_name in files:
|
||||
module_file = ToolFile(dir_name)|file_name
|
||||
if file_name.endswith(".py") and not file_name.startswith("__"):
|
||||
try:
|
||||
module = importlib.import_module(f"{module_file.GetFullPath().replace(".py", '').replace('/', '.').replace('\\', '.')}")
|
||||
for class_name in dir(module):
|
||||
plugin_class = getattr(module, class_name)
|
||||
if not isinstance(plugin_class, type):
|
||||
continue
|
||||
if issubclass(plugin_class, PluginInterface):
|
||||
plugin = plugin_class()
|
||||
if plugin.is_enable_plugin() == False:
|
||||
continue
|
||||
router = plugin.execute(f"/{module_file}")
|
||||
if router:
|
||||
app.include_router(router, prefix=f"/api", tags=[plugin.get_plugin_tag()])
|
||||
except Exception as e:
|
||||
config.Log("Error", f"{ConsoleFrontColor.RED}加载插件 {module_file} 失败: {e}{ConsoleFrontColor.RESET}")
|
||||
|
||||
__all__ = ["ImportPlugins", "PluginInterface", "DatabaseModel"]
|
||||
Reference in New Issue
Block a user