Skip to content

Commit b0586e9

Browse files
committed
split up the workers module into sub-modules for each worker type
1 parent 4f42f70 commit b0586e9

File tree

10 files changed

+1021
-921
lines changed

10 files changed

+1021
-921
lines changed

pydra/utils/general.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import re
88
import attrs
99
import ast
10+
import importlib
1011
import types
1112
import sysconfig
1213
import platformdirs
@@ -445,3 +446,32 @@ def is_workflow(obj):
445446
from pydra.engine.workflow import Workflow
446447

447448
return isinstance(obj, (Task, Workflow))
449+
450+
451+
def get_plugin_classes(namespace: ty.ModuleType, class_name: str) -> dict[str, type]:
452+
"""
453+
Get all classes within sub-packages of namespace package with a given name, e.g.
454+
"Worker" within "pydra.workers.*" sub-packages.
455+
456+
Parameters
457+
----------
458+
namespace : :obj:`str`
459+
The namespace to search for subclasses.
460+
base_class : :obj:`type`
461+
The base class to search for subclasses of.
462+
463+
Returns
464+
-------
465+
:obj:`dict[str, type]`
466+
A dictionary mapping the sub-package name to classes of 'class_name' within
467+
the namespace package
468+
"""
469+
sub_packages = [
470+
importlib.import_module(f"{namespace.__name__}.{m.name}")
471+
for m in pkgutil.iter_modules(namespace.__path__)
472+
]
473+
return {
474+
class_name: getattr(pkg, class_name)
475+
for pkg in sub_packages
476+
if hasattr(pkg, class_name)
477+
}

0 commit comments

Comments
 (0)