Coverage for netbox_data_import/target_runtime.py: 100%
25 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-09 20:50 +0000
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-09 20:50 +0000
1# SPDX-License-Identifier: Apache-2.0
2# SPDX-FileCopyrightText: 2026 Marcin Zieba <marcinpsk@gmail.com>
3"""The seam every Target Module implements and the coordinator calls.
5It lives apart from the implementations so one Target Module can depend on it without depending
6on another.
7"""
9from __future__ import annotations
11from collections.abc import Mapping
12from dataclasses import dataclass, field
13from typing import Any, Protocol
15from .plan import PlannedChange, SynchronizationUnit
18class PreconditionFailed(Exception):
19 """Target state moved between planning and the write, so the change no longer applies."""
22@dataclass(frozen=True)
23class ExecutionContext:
24 """What a Target Module needs while the coordinator's transaction is open."""
26 actor: Any
27 reader: Any
28 profile: Any
31@dataclass(frozen=True)
32class DeletedObject:
33 """One object a Planned Change removed, for the execution's deleted-object snapshot."""
35 object_type: str
36 object_id: int
37 display: str
38 detail: Mapping[str, Any] = field(default_factory=dict)
40 def to_dict(self) -> dict:
41 """Return the serialized form the audit row stores."""
42 return {
43 "object_type": self.object_type,
44 "object_id": self.object_id,
45 "display": self.display,
46 "detail": dict(self.detail),
47 }
50class TargetModuleRuntime(Protocol):
51 """What the coordinator may call on a Target Module."""
53 key: str
54 consumes: frozenset[str]
56 def plan(
57 self,
58 source_batch,
59 profile,
60 catalog,
61 netbox_reader,
62 *,
63 lock_plan_references: bool = False,
64 ) -> list[SynchronizationUnit]:
65 """Return this module's units and optionally lock target rows the plan only reads.
67 The caller must open a transaction before it requests locks.
68 """
69 ...
71 def apply(self, planned_change: PlannedChange, execution_context) -> Any:
72 """Apply one Planned Change inside the coordinator's transaction."""
73 ...
76__all__ = (
77 "DeletedObject",
78 "ExecutionContext",
79 "PreconditionFailed",
80 "TargetModuleRuntime",
81)