Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions bc2/core/pipeline.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from typing import Any

from pydantic import BaseModel

Expand Down Expand Up @@ -33,19 +34,26 @@ def validate(self, runtime_config: RuntimeConfig):
"""Validate the pipeline configuration."""
self.pipeline.validate(runtime_config)

def run(self, runtime_config: RuntimeConfig | None = None) -> Context:
def run(
self,
runtime_config: RuntimeConfig | None = None,
context: Context | dict[str, Any] | None = None,
) -> Context:
"""Run the pipeline.

Args:
runtime_config: The runtime configuration.
Most values in the runtime config are dependent on the pipeline.
The `debug` flag is interpretted globally.
context: Initial context object to use for the pipeline run.

Returns:
The context object created during the pipeline run.
Note if an initial context is provided, it will be copied but not modified;
a new reference is returned at the end of the run.
"""
runtime_config = runtime_config or {}
ctx = Context()
ctx = Context(context or {})
ctx.debug = runtime_config.get("debug", False)
ctx.errors = list[Exception]()
if ctx.debug:
Expand Down
31 changes: 31 additions & 0 deletions bc2/core/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,34 @@ def test_pipeline_invalid_optional_step():
"<class 'bc2.core.common.file.MemoryFile'> is not compatible with "
"the output type <class 'bc2.core.common.text.Text'>."
)


def test_pipeline_initial_context():
cfg = PipelineConfig.model_validate(
{
"pipe": [
{"engine": "in:memory"},
{"engine": "extract:raw"},
{"engine": "redact:noop", "delimiters": ["[", "]"]},
{"engine": "render:text"},
{"engine": "out:memory"},
],
}
)
# Run the pipeline.
pipe = Pipeline(cfg)

in_buf = io.BytesIO(b"Hello, world!")
out_buf = io.BytesIO()
opts = {
"debug": True,
"in": {
"buffer": in_buf,
},
"out": {
"buffer": out_buf,
},
}
ctx = pipe.run(runtime_config=opts, context={"foo": "bar"})
assert ctx.foo == "bar"
assert ctx.debug is True
Loading