From 08d86dbd3065df838a3b9a04c4cb79df55e08746 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Mon, 16 Jan 2023 23:47:57 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8F=92=E4=BB=B6=E5=BC=80=E5=85=B3?= =?UTF-8?q?=E7=9B=B8=E5=85=B3bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/plugin/host.py | 21 +++++++++++++++------ pkg/plugin/models.py | 1 + 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/pkg/plugin/host.py b/pkg/plugin/host.py index a8332ced..100d7ec4 100644 --- a/pkg/plugin/host.py +++ b/pkg/plugin/host.py @@ -35,15 +35,21 @@ __plugins__ = {} }""" -def walk_plugin_path(module, prefix=''): +__current_module_path__ = "" + + +def walk_plugin_path(module, prefix='', path_prefix=''): + global __current_module_path__ """遍历插件路径""" for item in pkgutil.iter_modules(module.__path__): if item.ispkg: - logging.debug("扫描插件包: {}".format(item.name)) - walk_plugin_path(__import__(module.__name__ + '.' + item.name, fromlist=['']), prefix + item.name + '.') + logging.debug("扫描插件包: plugins/{}".format(path_prefix + item.name)) + walk_plugin_path(__import__(module.__name__ + '.' + item.name, fromlist=['']), + prefix + item.name + '.', path_prefix + item.name + '/') else: - logging.debug("扫描插件模块: {}".format(item.name)) - logging.info('加载模块: {}'.format(prefix + item.name)) + logging.debug("扫描插件模块: plugins/{}".format(path_prefix + item.name + '.py')) + logging.info('加载模块: plugins/{}'.format(path_prefix + item.name + '.py')) + __current_module_path__ = "plugins/"+path_prefix + item.name + '.py' importlib.import_module(module.__name__ + '.' + item.name) @@ -56,6 +62,9 @@ def load_plugins(): logging.debug(__plugins__) + # 加载开关数据 + switch.load_switch() + def initialize_plugins(): """ 初始化插件 """ @@ -72,7 +81,7 @@ def initialize_plugins(): def unload_plugins(): """ 卸载插件 """ for plugin in __plugins__.values(): - if plugin['instance'] is not None: + if plugin['enabled'] and plugin['instance'] is not None: if not hasattr(plugin['instance'], '__del__'): logging.warning("插件{}没有定义析构函数".format(plugin['name'])) else: diff --git a/pkg/plugin/models.py b/pkg/plugin/models.py index 53b13581..2c0e35a6 100644 --- a/pkg/plugin/models.py +++ b/pkg/plugin/models.py @@ -197,6 +197,7 @@ def register(name: str, description: str, version: str, author: str): "hooks": {}, "path": host.__current_module_path__, "enabled": True, + "instance": None, } def wrapper(cls: Plugin):