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
+8 -5
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 False
return True if not hasattr(obj, 'execute') or not hasattr(obj, "name"):
return False
return obj.__module__.startswith("app.actions")
# 加载所有动作 # 加载所有动作
self._actions = {} self._actions = {}
actions = ModuleHelper.load( actions = ModuleHelper.load(
"app.actions", "app.actions",
filter_func=lambda _, obj: check_module(obj) filter_func=lambda _, obj: filter_func(obj)
) )
for action in actions: for action in actions:
logger.debug(f"加载动作: {action.__name__}") logger.debug(f"加载动作: {action.__name__}")
+4
View File
@@ -23,6 +23,7 @@ class ModuleHelper:
""" """
submodules: list = [] submodules: list = []
loaded_modules = set()
packages = importlib.import_module(package_path) packages = importlib.import_module(package_path)
for importer, package_name, _ in pkgutil.iter_modules(packages.__path__): for importer, package_name, _ in pkgutil.iter_modules(packages.__path__):
try: try:
@@ -35,6 +36,9 @@ class ModuleHelper:
if name.startswith('_'): if name.startswith('_'):
continue continue
if isinstance(obj, type) and filter_func(name, obj): if isinstance(obj, type) and filter_func(name, obj):
if name in loaded_modules:
continue
loaded_modules.add(name)
submodules.append(obj) submodules.append(obj)
except Exception as err: except Exception as err:
logger.debug(f'加载模块 {package_name} 失败:{str(err)} - {traceback.format_exc()}') logger.debug(f'加载模块 {package_name} 失败:{str(err)} - {traceback.format_exc()}')