Hooks and plugins¶
Lifecycle hooks, writer protocols, and operation context objects.
Hooks are structural protocols: a plugin object participates in a lifecycle
phase by implementing the corresponding method, such as
before_validate_metadata(context) or extract_metadata(context). Hook
methods receive an OperationContext, a mutable object that carries the
catalog root, operation id, source description, planned locators, user metadata,
derived metadata, rollback registrar, and accumulated hook warnings.
The design intentionally keeps hook classes lightweight. Plugin authors do not
subclass a base class; they implement one or more protocol methods, usually with
context: OperationContext and -> None or -> MetadataDict | None type
hints. HookManager dispatches protocols in deterministic registration order,
merges returned derived metadata, records non-fatal after-commit failures as
warnings, and preserves the original exception when error or rollback hooks
fail.
Artifact writers use the same context model. Any object satisfying
ArtifactWriter can materialise an ogcat.models.ArtifactLocator
from an OperationSource before the catalog record is committed.
Operation context¶
Hook methods receive an OperationContext unless their signature documents
additional arguments such as a validation report or exception. The context is
the main coordination object for metadata mutation, locator planning, rollback
registration, and source information.
- class ogcat.OperationContext(catalog_root, operation_id, operation_type, record_type, user_metadata, derived_metadata=<factory>, planned_locators=<factory>, register_rollback=None, source=<factory>, storage_mode=None, storage_plan=None, original_path=None, original_filename=None, suffixes=<factory>, warnings=<factory>)[source]¶
Bases:
objectMutable context passed to catalog lifecycle hooks.
Hooks exert their effect by mutating documented fields on this object, raising an exception, or registering rollback work. user_metadata may be mutated before validation. planned_locators may be changed during resolve_artifact_locator; the first locator is treated as canonical. derived_metadata may be updated during metadata extraction.
- Parameters:
catalog_root (Path) – Root path of the catalog.
operation_id (str) – Identifier shared with the transaction.
operation_type (str) – Catalog operation name, such as
"add_file".record_type (str) – Record type being created.
user_metadata (MetadataDict) – User-supplied metadata, mutable by hooks before validation.
derived_metadata (MetadataDict) – Derived metadata collected during the operation.
planned_locators (list[ArtifactLocator]) – Locators planned or supplied for the operation.
register_rollback (RollbackRegistrar | None) – Low-level rollback registrar. Hook authors should normally call
context.rollback(...)instead.source (OperationSource) – Artifact source description for this operation.
storage_mode (str | None) – Optional storage mode, such as
"copy"or"move".storage_plan (StoragePlan | None) – Optional planned artifact storage decision.
original_path (str | Path | None) – Optional original path or URI.
original_filename (str | None) – Optional original filename.
suffixes (list[str]) – Source suffixes associated with the artifact.
- storage_plan: StoragePlan | None¶
- property source_path: Path | None¶
Optional local source path, kept for compatibility with existing hooks.
- property source_descriptor: str | None¶
Optional source description, kept for compatibility with existing hooks.
- class ogcat.OperationSource(kind, path=None, descriptor=None, metadata=<factory>, payload=None)[source]¶
Bases:
objectDescription of the artifact source for a catalog operation.
- Parameters:
kind (str) – Short source kind, such as
"local_file"or"external".path (Path | None) – Optional local source path.
descriptor (str | None) – Optional non-path source description or URI.
metadata (MetadataDict) – Source-specific JSON-compatible metadata.
payload (object | None) – Optional in-memory Python object for writer helpers.
Plugin registry¶
- class ogcat.PluginRegistry(hooks=())[source]¶
Bases:
objectRegistry for direct Python hook registration.
- Parameters:
hooks (
Iterable[object]) – Hook objects to register in dispatch order.
- property hooks: tuple[object, ...]¶
Registered hooks in insertion order.
- register(hook)[source]¶
Register a hook object and return it for decorator-style usage.
- Return type:
object
Hook protocols¶
- class ogcat.hooks.BeforeValidateMetadataHook(*args, **kwargs)[source]¶
Bases:
ProtocolHook called before schema metadata validation.
- class ogcat.hooks.AfterValidateMetadataHook(*args, **kwargs)[source]¶
Bases:
ProtocolHook called after schema metadata validation.
- class ogcat.hooks.ResolveArtifactLocatorHook(*args, **kwargs)[source]¶
Bases:
ProtocolHook called after an artifact locator has been proposed.
- class ogcat.hooks.BeforeRecordWriteHook(*args, **kwargs)[source]¶
Bases:
ProtocolHook called before writing the catalog record.
- class ogcat.hooks.AfterRecordWriteHook(*args, **kwargs)[source]¶
Bases:
ProtocolHook called after writing the catalog record.
- class ogcat.hooks.ExtractMetadataHook(*args, **kwargs)[source]¶
Bases:
ProtocolHook called during derived metadata extraction.
- class ogcat.hooks.BeforeCommitHook(*args, **kwargs)[source]¶
Bases:
ProtocolHook called before committing the catalog transaction.
- class ogcat.hooks.AfterCommitHook(*args, **kwargs)[source]¶
Bases:
ProtocolHook called after committing the catalog transaction.
Dispatch¶
- class ogcat.HookManager(hooks=())[source]¶
Bases:
objectDeterministic dispatcher for registered catalog hooks.
- property hooks: tuple[object, ...]¶
Registered hooks in dispatch order.
- register(hook)[source]¶
Register a hook object and return it for decorator-style usage.
- Return type:
object
- before_validate_metadata(context)[source]¶
Dispatch
before_validate_metadatahooks.- Return type:
None
- after_validate_metadata(context, report)[source]¶
Dispatch
after_validate_metadatahooks.- Return type:
None
- resolve_artifact_locator(context)[source]¶
Dispatch
resolve_artifact_locatorhooks.- Return type:
None
- extract_metadata(context)[source]¶
Dispatch metadata extraction hooks and merge returned metadata.
- Return type:
None
- after_commit(context)[source]¶
Dispatch
after_commithooks without failing committed work.- Return type:
None