♻ 移动插件路径 区分系统插件和查询插件

This commit is contained in:
洛水居室 2022-08-09 20:25:25 +08:00
parent 602ce67e41
commit 7adf404600
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC
26 changed files with 26 additions and 24 deletions

View File

@ -9,7 +9,7 @@ from core.template import TemplateService
from logger import Log
from models.apihelper.gacha import GachaInfo
from plugins.base import BasePlugins
from plugins.gacha.wish import WishCountInfo, get_one
from plugins.genshin.gacha.wish import WishCountInfo, get_one
from utils.bot import get_all_args
from utils.decorators.error import error_callable
from utils.decorators.restricts import restricts

View File

@ -29,7 +29,6 @@ class PluginsManager:
self.exclude_list: List[str] = []
def refresh_list(self, plugin_paths):
self.plugin_list.clear()
plugin_paths = glob(plugin_paths)
for plugin_path in plugin_paths:
if plugin_path.startswith('__'):

View File

@ -1,12 +1,13 @@
from importlib import import_module
from typing import Optional
from telegram.ext import CommandHandler, MessageHandler, filters, CallbackQueryHandler, Application, InlineQueryHandler
from logger import Log
from plugins.base import NewChatMembersHandler
from plugins.errorhandler import error_handler
from plugins.inline import Inline
from plugins.start import start, ping, reply_keyboard_remove, unknown_command
from plugins.system.errorhandler import error_handler
from plugins.system.inline import Inline
from plugins.system.start import start, ping, reply_keyboard_remove, unknown_command
from utils.plugins.manager import PluginsManager
@ -32,10 +33,10 @@ def register_plugin_handlers(application: Application):
Log.info("正在加载插件管理器")
plugins_manager = PluginsManager()
plugins_manager.refresh_list("./plugins/*")
plugins_manager.refresh_list("./plugins/genshin/*")
# 忽略内置模块
plugins_manager.add_exclude(["start", "base", "auth", "inline", "errorhandler"])
# plugins_manager.add_exclude(["start", "auth", "inline", "errorhandler"])
Log.info("加载插件管理器正在加载插件")
plugins_manager.import_module()
@ -57,4 +58,6 @@ def register_plugin_handlers(application: Application):
application.add_handler(MessageHandler(filters.COMMAND & filters.ChatType.PRIVATE, unknown_command))
application.add_error_handler(error_handler, block=False)
import_module(f"plugins.system.admin")
Log.info("插件加载成功")

View File

@ -3,7 +3,7 @@ from functools import wraps
from logger import Log
from models.types import Func
from utils.service.manager import ServiceDict
from utils.service.manager import ServicesDict
def get_injections(func: Func):
@ -21,7 +21,7 @@ def get_injections(func: Func):
for parameter_name, parameter in signature.parameters.items():
annotation = parameter.annotation
class_name = annotation.__name__
param = ServiceDict.get(class_name)
param = ServicesDict.get(class_name)
if param is not None:
injections.setdefault(parameter_name, param)
return injections

View File

@ -10,15 +10,15 @@ from utils.aiobrowser import AioBrowser
from utils.mysql import MySQL
from utils.redisdb import RedisDB
ServiceFunctions: List[Func] = []
ServiceDict: Dict[str, Func] = {}
ServicesFunctions: List[Func] = []
ServicesDict: Dict[str, Func] = {}
def listener_service():
"""监听服务"""
def decorator(func: Func):
ServiceFunctions.append(
ServicesFunctions.append(
func
)
return func
@ -31,16 +31,16 @@ class ServicesManager:
self.browser = browser
self.redis = redis
self.mysql = mysql
self.app_list: List[str] = []
self.services_list: List[str] = []
self.exclude_list: List[str] = []
def refresh_list(self, app_paths):
self.app_list.clear()
self.services_list.clear()
app_paths = glob(app_paths)
for app_path in app_paths:
if os.path.isdir(app_path):
app_path = os.path.basename(app_path)
self.app_list.append(app_path)
self.services_list.append(app_path)
def add_exclude(self, exclude: Union[str, List[str]]):
if isinstance(exclude, str):
@ -51,21 +51,21 @@ class ServicesManager:
raise TypeError
def import_module(self):
for app_name in self.app_list:
if app_name not in self.exclude_list:
for services_name in self.services_list:
if services_name not in self.exclude_list:
try:
import_module(f"apps.{app_name}")
import_module(f"apps.{services_name}")
except ImportError as exc:
Log.warning(f"Service模块 {app_name} 导入失败", exc)
Log.warning(f"Service模块 {services_name} 导入失败", exc)
except ImportWarning as exc:
Log.warning(f"Service模块 {app_name} 加载成功但有警告", exc)
Log.warning(f"Service模块 {services_name} 加载成功但有警告", exc)
except Exception as exc:
Log.warning(f"Service模块 {app_name} 加载失败", exc)
Log.warning(f"Service模块 {services_name} 加载失败", exc)
else:
Log.debug(f"Service模块 {app_name} 加载成功")
Log.debug(f"Service模块 {services_name} 加载成功")
def add_service(self):
for func in ServiceFunctions:
for func in ServicesFunctions:
if callable(func):
kwargs = {}
try:
@ -91,7 +91,7 @@ class ServicesManager:
try:
handlers_list = func(**kwargs)
class_name = handlers_list.__class__.__name__
ServiceDict.setdefault(class_name, handlers_list)
ServicesDict.setdefault(class_name, handlers_list)
except BaseException as exc:
Log.error("初始化Service失败", exc)
finally: