Writers and transactions¶
Artifact writer helpers for lightweight catalog workflows.
Writers bridge Catalog.add_artifact and concrete storage code. A writer is
any object that satisfies ogcat.hooks.ArtifactWriter: it exposes a
write(context, source, target) method, receives an
ogcat.hooks.OperationContext, and materialises an
ogcat.models.ArtifactLocator from an ogcat.hooks.OperationSource.
This module provides small adapters for common examples. source_writer wraps
a function that accepts an OperationSource and target Path.
memory_writer and path_writer adapt narrower functions for in-memory
payloads and local files. UnzipArtifactWriter is a concrete directory writer
used by tutorials and tests. UnzipSingleFileArtifactWriter extracts one zip
member to a file target. These helpers register rollback actions through the
operation context before writing so partially-created targets can be cleaned up
if the catalog operation fails.
Writer protocol¶
Writer classes do not need to inherit from a concrete base class. They satisfy
the structural ogcat.hooks.ArtifactWriter protocol by providing a
write(context, source, target) method.
Writer helpers¶
- ogcat.writers.memory_source(data, *, kind='memory', descriptor=None, metadata=None)[source]¶
Build an operation source carrying an in-memory Python object.
- Parameters:
data (
object) – In-memory object to pass to a memory writer.kind (
str) – Source kind used for writer validation.descriptor (
str|None) – Optional human-readable source description.metadata (
MetadataDict|None) – Optional JSON-compatible source metadata.
- Return type:
- Returns:
An operation source with
payloadset todata.
- ogcat.writers.path_source(path, *, kind='path', descriptor=None, metadata=None)[source]¶
Build an operation source carrying a local path.
- Parameters:
path (
str|Path) – Local source path.kind (
str) – Source kind used for writer validation.descriptor (
str|None) – Optional human-readable source description. Defaults to the resolved source path string.metadata (
MetadataDict|None) – Optional JSON-compatible source metadata.
- Return type:
- Returns:
An operation source with
pathset to the resolved local path.
- ogcat.writers.source_writer(write_function, *, target_kind, source_kind=None)[source]¶
Wrap a function that writes from an
OperationSourceto a target path.- Return type:
- ogcat.writers.memory_writer(write_function, *, target_kind, source_kind='memory')[source]¶
Wrap a function that writes an in-memory payload to a target path.
- Return type:
FunctionArtifactWriter¶
- class ogcat.writers.FunctionArtifactWriter(write_function, target_kind, source_kind=None, write_mode='write')[source]¶
Artifact writer adapter around a small Python function.
The wrapped function receives an
OperationSourceand a local target path. It should write either a single file or a directory, matchingtarget_kind. If it returns metadata, the metadata is merged intocontext.derived_metadata.- Parameters:
write_function (
Callable[[OperationSource,Path],MetadataDict|None]) – Function that writes artifact data.target_kind (
Literal['file','directory']) – Whether the target locator should be treated as a file or directory.source_kind (
str|None) – Optional required source kind.
Attributes¶
- FunctionArtifactWriter.write_function¶
Callable that writes from an
OperationSourceto a target path and may returnMetadataDictderived metadata.
- FunctionArtifactWriter.target_kind¶
Target artifact kind, either
"file"or"directory".
- FunctionArtifactWriter.source_kind¶
Optional required source kind. When set,
writerejects sources whoseOperationSource.kinddoes not match.
Methods¶
CopyArtifactWriter¶
MoveArtifactWriter¶
UnzipArtifactWriter¶
- class ogcat.writers.UnzipArtifactWriter(source_kind='zip_file')[source]¶
Example writer that safely extracts a zip file into a directory target.
Attributes¶
- UnzipArtifactWriter.source_kind¶
Required source kind for zip extraction. Defaults to
"zip_file".
Methods¶
UnzipSingleFileArtifactWriter¶
- class ogcat.writers.UnzipSingleFileArtifactWriter(member_name=None, source_kind='zip_file')[source]¶
Extract a single zip member to a file target.
- Parameters:
member_name (
str|None) – Optional archive member to extract. When omitted, the archive must contain exactly one non-directory member.source_kind (
str) – Required operation source kind.
Attributes¶
- UnzipSingleFileArtifactWriter.member_name¶
Optional archive member to extract. When omitted, the archive must contain exactly one non-directory member.
- UnzipSingleFileArtifactWriter.source_kind¶
Required source kind for zip extraction. Defaults to
"zip_file".
Methods¶
Transactions¶
Best-effort catalog transaction helpers.
The TinyDB-backed implementation uses compensating rollback actions. This is a unit-of-work helper, not a true database transaction or ACID boundary.
UnitOfWork coordinates record writes with cleanup callbacks registered by
artifact writers and hooks. Staged work starts in PLANNED state, moves to
STAGED when a record or rollback action is registered, and becomes
COMMITTED only after all catalog and artifact work has succeeded. If the
operation exits early, rollback actions run in reverse order and failures are
captured as RollbackFailure entries while preserving the original exception.
- class ogcat.UnitOfWork(repository, *, operation_id=None)[source]¶
Best-effort unit of work for catalog record staging and cleanup.
- Parameters:
repository (
CatalogRepository) – Repository used for staged catalog record writes.operation_id (
str|None) – Optional externally supplied operation id. If omitted, a random id is generated for audit/debug correlation.
- register_rollback(action, *, description=None)[source]¶
Register an action to run if the unit of work rolls back.
- Parameters:
action (
RollbackAction|Callable[[],None]) – A rollback action object or a no-argument callable.description (
str|None) – Human-readable cleanup description for callable actions. Ignored for action objects that already carry one.
- Return type:
RollbackAction- Returns:
The registered rollback action.
- insert_staged_record(record)[source]¶
Insert a staged record and delete it if the unit of work rolls back.
- Return type:
- rollback(*, original_exception=None)[source]¶
Run registered rollback actions in reverse order.
- Parameters:
original_exception (
BaseException|None) – Optional exception that triggered rollback. When supplied, rollback failure summaries are added as exception notes so the original failure remains visible to callers.- Return type:
None