feat: 支持插件热重载

This commit is contained in:
Rock Chin
2023-01-15 22:23:18 +08:00
parent f06b16437c
commit 37153e7360
2 changed files with 11 additions and 1 deletions

View File

@@ -36,8 +36,10 @@ def walk_plugin_path(module, prefix=''):
"""遍历插件路径"""
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 + '.')
else:
logging.debug("扫描插件模块: {}".format(item.name))
logging.info('加载模块: {}'.format(prefix + item.name))
importlib.import_module(module.__name__ + '.' + item.name)
@@ -142,6 +144,9 @@ class EventContext:
def __init__(self, name: str):
self.name = name
self.eid = EventContext.eid
self.__prevent_default__ = False
self.__prevent_postorder__ = False
self.__return_value__ = {}
EventContext.eid += 1
@@ -182,6 +187,7 @@ class PluginHost:
def emit(self, event_name: str, **kwargs) -> EventContext:
""" 触发事件 """
event_context = EventContext(event_name)
logging.debug("触发事件: {} ({})".format(event_name, event_context.eid))
for plugin in __plugins__.values():
for hook in plugin['hooks'].get(event_name, []):
try:

View File

@@ -4,6 +4,7 @@ import threading
import importlib
import pkgutil
import pkg.utils.context
import pkg.plugin.host
def walk(module, prefix=''):
@@ -32,9 +33,12 @@ def reload_all(notify=True):
importlib.reload(__import__('config'))
importlib.reload(__import__('main'))
importlib.reload(__import__('banlist'))
importlib.reload(__import__('plugins'))
pkg.utils.context.context = context
# 重载插件
import plugins
walk(plugins)
# 执行启动流程
logging.info("执行程序启动流程")
threading.Thread(target=main.main, args=(False,), daemon=False).start()