|
6 | 6 | from pipeline_lib.core.steps import PipelineStep
|
7 | 7 |
|
8 | 8 |
|
| 9 | +class StepClassNotFoundError(Exception): |
| 10 | + pass |
| 11 | + |
| 12 | + |
9 | 13 | class StepRegistry:
|
10 | 14 | """A helper class for managing the registry of pipeline steps."""
|
11 | 15 |
|
12 | 16 | def __init__(self):
|
13 | 17 | self._step_registry = {}
|
14 |
| - self.logger = logging.getLogger(StepRegistry.__name__) |
| 18 | + self.logger = logging.getLogger(__name__) |
15 | 19 |
|
16 |
| - def register_step(self, step_class): |
| 20 | + def register_step(self, step_class: type): |
17 | 21 | """Register a step class using its class name."""
|
18 | 22 | step_name = step_class.__name__
|
19 | 23 | if not issubclass(step_class, PipelineStep):
|
20 | 24 | raise ValueError(f"{step_class} must be a subclass of PipelineStep")
|
21 | 25 | self._step_registry[step_name] = step_class
|
22 | 26 |
|
23 |
| - def get_step_class(self, step_name): |
| 27 | + def get_step_class(self, step_name: str) -> type: |
24 | 28 | """Retrieve a step class by name."""
|
25 | 29 | if step_name in self._step_registry:
|
26 | 30 | return self._step_registry[step_name]
|
27 | 31 | else:
|
28 |
| - raise ValueError(f"Step class '{step_name}' not found in registry.") |
| 32 | + raise StepClassNotFoundError(f"Step class '{step_name}' not found in registry.") |
| 33 | + |
| 34 | + def get_all_step_classes(self) -> dict: |
| 35 | + """Retrieve all registered step classes.""" |
| 36 | + return self._step_registry |
29 | 37 |
|
30 |
| - def auto_register_steps_from_package(self, package_name): |
| 38 | + def auto_register_steps_from_package(self, package_name: str): |
31 | 39 | """
|
32 | 40 | Automatically registers all step classes found within a specified package.
|
33 | 41 | """
|
34 |
| - package = importlib.import_module(package_name) |
35 |
| - prefix = package.__name__ + "." |
36 |
| - for importer, modname, ispkg in pkgutil.walk_packages(package.__path__, prefix): |
37 |
| - module = importlib.import_module(modname) |
38 |
| - for name in dir(module): |
39 |
| - attribute = getattr(module, name) |
40 |
| - if ( |
41 |
| - isinstance(attribute, type) |
42 |
| - and issubclass(attribute, PipelineStep) |
43 |
| - and attribute is not PipelineStep |
44 |
| - ): |
45 |
| - self.register_step(attribute) |
| 42 | + try: |
| 43 | + package = importlib.import_module(package_name) |
| 44 | + prefix = package.__name__ + "." |
| 45 | + for importer, modname, ispkg in pkgutil.walk_packages(package.__path__, prefix): |
| 46 | + module = importlib.import_module(modname) |
| 47 | + for name in dir(module): |
| 48 | + attribute = getattr(module, name) |
| 49 | + if ( |
| 50 | + isinstance(attribute, type) |
| 51 | + and issubclass(attribute, PipelineStep) |
| 52 | + and attribute is not PipelineStep |
| 53 | + ): |
| 54 | + self.register_step(attribute) |
| 55 | + except ImportError as e: |
| 56 | + self.logger.error(f"Failed to import package: {package_name}. Error: {e}") |
46 | 57 |
|
47 | 58 | def load_and_register_custom_steps(self, custom_steps_path: str) -> None:
|
48 | 59 | """
|
|
0 commit comments