mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-07-16 17:36:07 +00:00
feat: 支持插件热重载
This commit is contained in:
@@ -36,8 +36,10 @@ def walk_plugin_path(module, prefix=''):
|
|||||||
"""遍历插件路径"""
|
"""遍历插件路径"""
|
||||||
for item in pkgutil.iter_modules(module.__path__):
|
for item in pkgutil.iter_modules(module.__path__):
|
||||||
if item.ispkg:
|
if item.ispkg:
|
||||||
|
logging.debug("扫描插件包: {}".format(item.name))
|
||||||
walk_plugin_path(__import__(module.__name__ + '.' + item.name, fromlist=['']), prefix + item.name + '.')
|
walk_plugin_path(__import__(module.__name__ + '.' + item.name, fromlist=['']), prefix + item.name + '.')
|
||||||
else:
|
else:
|
||||||
|
logging.debug("扫描插件模块: {}".format(item.name))
|
||||||
logging.info('加载模块: {}'.format(prefix + item.name))
|
logging.info('加载模块: {}'.format(prefix + item.name))
|
||||||
|
|
||||||
importlib.import_module(module.__name__ + '.' + item.name)
|
importlib.import_module(module.__name__ + '.' + item.name)
|
||||||
@@ -142,6 +144,9 @@ class EventContext:
|
|||||||
def __init__(self, name: str):
|
def __init__(self, name: str):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.eid = EventContext.eid
|
self.eid = EventContext.eid
|
||||||
|
self.__prevent_default__ = False
|
||||||
|
self.__prevent_postorder__ = False
|
||||||
|
self.__return_value__ = {}
|
||||||
EventContext.eid += 1
|
EventContext.eid += 1
|
||||||
|
|
||||||
|
|
||||||
@@ -182,6 +187,7 @@ class PluginHost:
|
|||||||
def emit(self, event_name: str, **kwargs) -> EventContext:
|
def emit(self, event_name: str, **kwargs) -> EventContext:
|
||||||
""" 触发事件 """
|
""" 触发事件 """
|
||||||
event_context = EventContext(event_name)
|
event_context = EventContext(event_name)
|
||||||
|
logging.debug("触发事件: {} ({})".format(event_name, event_context.eid))
|
||||||
for plugin in __plugins__.values():
|
for plugin in __plugins__.values():
|
||||||
for hook in plugin['hooks'].get(event_name, []):
|
for hook in plugin['hooks'].get(event_name, []):
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import threading
|
|||||||
import importlib
|
import importlib
|
||||||
import pkgutil
|
import pkgutil
|
||||||
import pkg.utils.context
|
import pkg.utils.context
|
||||||
|
import pkg.plugin.host
|
||||||
|
|
||||||
|
|
||||||
def walk(module, prefix=''):
|
def walk(module, prefix=''):
|
||||||
@@ -32,9 +33,12 @@ def reload_all(notify=True):
|
|||||||
importlib.reload(__import__('config'))
|
importlib.reload(__import__('config'))
|
||||||
importlib.reload(__import__('main'))
|
importlib.reload(__import__('main'))
|
||||||
importlib.reload(__import__('banlist'))
|
importlib.reload(__import__('banlist'))
|
||||||
importlib.reload(__import__('plugins'))
|
|
||||||
pkg.utils.context.context = context
|
pkg.utils.context.context = context
|
||||||
|
|
||||||
|
# 重载插件
|
||||||
|
import plugins
|
||||||
|
walk(plugins)
|
||||||
|
|
||||||
# 执行启动流程
|
# 执行启动流程
|
||||||
logging.info("执行程序启动流程")
|
logging.info("执行程序启动流程")
|
||||||
threading.Thread(target=main.main, args=(False,), daemon=False).start()
|
threading.Thread(target=main.main, args=(False,), daemon=False).start()
|
||||||
|
|||||||
Reference in New Issue
Block a user