Skip to content

Commit

Permalink
Add Task.setup_callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
kostrykin committed Sep 13, 2024
1 parent 99b793a commit 985d9e8
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions repype/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from repype.typing import (
Any,
Dict,
get_args,
FrozenSet,
InputID,
Iterator,
Expand Down Expand Up @@ -634,6 +635,7 @@ def run(
assert self.runnable
if pipeline is None:
pipeline = self.create_pipeline()
self.setup_callbacks(pipeline)

# Find a task and stage to pick up from
if pickup:
Expand Down Expand Up @@ -714,3 +716,18 @@ def run(
def __repr__(self):
config = self.create_config()
return f'<Task "{self.path}" {config.sha.hexdigest()[:7]}>'

def setup_callbacks(self, pipeline: repype.pipeline.Pipeline) -> None:
"""
Add callbacks to the pipeline stages.
Callbacks are added to the pipeline stages for those stages and events, for which there is a corresponding
method defined in the task object. The method name is constructed from the stage identifier and the event name,
separated by underscores. For example, the method ``on_stage1_start`` is called when the stage with the
identifier ``stage1`` starts.
"""
for stage in pipeline.stages:
for event in get_args(pipeline.stage.StageEvent):
callback_name = f'on_{stage.id.replace("-", "_")}_{event}'
if hasattr(self, callback_name):
stage.add_callback(event, getattr(self, callback_name))

0 comments on commit 985d9e8

Please sign in to comment.