fix modules load

This commit is contained in:
jxxghp
2025-02-16 17:24:17 +08:00
parent b86d06f632
commit 68cba44476
2 changed files with 12 additions and 5 deletions

View File

@@ -21,19 +21,22 @@ class WorkFlowManager(metaclass=Singleton):
"""
初始化
"""
def check_module(module: Any):
def filter_func(obj: Any):
"""
检查模块
过滤函数,确保只加载新定义的类
"""
if not hasattr(module, 'execute') or not hasattr(module, "name"):
if not isinstance(obj, type):
return False
return True
if not hasattr(obj, 'execute') or not hasattr(obj, "name"):
return False
return obj.__module__.startswith("app.actions")
# 加载所有动作
self._actions = {}
actions = ModuleHelper.load(
"app.actions",
filter_func=lambda _, obj: check_module(obj)
filter_func=lambda _, obj: filter_func(obj)
)
for action in actions:
logger.debug(f"加载动作: {action.__name__}")

View File

@@ -23,6 +23,7 @@ class ModuleHelper:
"""
submodules: list = []
loaded_modules = set()
packages = importlib.import_module(package_path)
for importer, package_name, _ in pkgutil.iter_modules(packages.__path__):
try:
@@ -35,6 +36,9 @@ class ModuleHelper:
if name.startswith('_'):
continue
if isinstance(obj, type) and filter_func(name, obj):
if name in loaded_modules:
continue
loaded_modules.add(name)
submodules.append(obj)
except Exception as err:
logger.debug(f'加载模块 {package_name} 失败:{str(err)} - {traceback.format_exc()}')