You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
#gq:
import os
import logging
from django . conf import settings
# 获取当前模块的日志记录器,用于输出插件加载过程中的日志
logger = logging . getLogger ( __name__ )
def load_plugins ( ) :
"""
动态加载并初始化 plugins 目录下的插件。
应在 Django App Registry 完全就绪后调用(如 AppConfig.ready() 中)。
遍历 settings.ACTIVE_PLUGINS 列表,依次导入每个插件的 plugin.py 模块。
"""
# 遍历配置中启用的插件列表
for plugin_name in settings . ACTIVE_PLUGINS :
# 拼接插件目录绝对路径
plugin_path = os . path . join ( settings . PLUGINS_DIR , plugin_name )
# 仅当目录存在且目录下包含 plugin.py 文件时才尝试导入
if os . path . isdir ( plugin_path ) and os . path . exists ( os . path . join ( plugin_path , ' plugin.py ' ) ) :
try :
# 使用 __import__ 动态导入插件模块
# 导入路径示例: plugins.<plugin_name>.plugin
__import__ ( f ' plugins. { plugin_name } .plugin ' )
logger . info ( f " Successfully loaded plugin: { plugin_name } " )
except ImportError as e :
# 导入失败时记录错误日志,包含异常堆栈
logger . error ( f " Failed to import plugin: { plugin_name } " , exc_info = e )