| 1 |
from collections.abc import Iterator |
| 2 |
from dataclasses import ( |
| 3 |
dataclass, |
| 4 |
field, |
| 5 |
) |
| 6 |
from typing import ( |
| 7 |
TYPE_CHECKING, |
| 8 |
Optional, |
| 9 |
) |
| 10 |
|
| 11 |
from .constants import ( |
| 12 |
SNAPSHOT_EMPTY_COLLECTION_KEY, |
| 13 |
SNAPSHOT_UNKNOWN_COLLECTION_KEY, |
| 14 |
) |
| 15 |
|
| 16 |
if TYPE_CHECKING: |
| 17 |
from .types import SerializedData |
| 18 |
|
| 19 |
|
| 20 |
@dataclass(frozen=True) |
| 21 |
class Snapshot: |
| 22 |
name: str |
| 23 |
data: Optional["SerializedData"] = None |
| 24 |
# A tainted snapshot needs to be regenerated |
| 25 |
tainted: bool | None = field(default=None) |
| 26 |
|
| 27 |
|
| 28 |
@dataclass(frozen=True) |
| 29 |
class SnapshotEmpty(Snapshot): |
| 30 |
name: str = SNAPSHOT_EMPTY_COLLECTION_KEY |
| 31 |
|
| 32 |
|
| 33 |
@dataclass(frozen=True) |
| 34 |
class SnapshotUnknown(Snapshot): |
| 35 |
name: str = SNAPSHOT_UNKNOWN_COLLECTION_KEY |
| 36 |
|
| 37 |
|
| 38 |
@dataclass |
| 39 |
class SnapshotCollection: |
| 40 |
"""A collection of snapshots at a save location""" |
| 41 |
|
| 42 |
location: str |
| 43 |
_snapshots: dict[str, "Snapshot"] = field(default_factory=dict) |
| 44 |
|
| 45 |
# A tainted collection needs to be regenerated |
| 46 |
tainted: bool | None = field(default=None) |
| 47 |
|
| 48 |
@property |
| 49 |
def has_snapshots(self) -> bool: |
| 50 |
return bool(self._snapshots) |
| 51 |
|
| 52 |
def get(self, snapshot_name: str) -> Optional["Snapshot"]: |
| 53 |
return self._snapshots.get(snapshot_name) |
| 54 |
|
| 55 |
def add(self, snapshot: "Snapshot") -> None: |
| 56 |
self._snapshots[snapshot.name] = snapshot |
| 57 |
if snapshot.name != SNAPSHOT_EMPTY_COLLECTION_KEY: |
| 58 |
self.remove(SNAPSHOT_EMPTY_COLLECTION_KEY) |
| 59 |
|
| 60 |
def merge(self, snapshot_collection: "SnapshotCollection") -> None: |
| 61 |
for snapshot in snapshot_collection: |
| 62 |
self.add(snapshot) |
| 63 |
|
| 64 |
def remove(self, snapshot_name: str) -> None: |
| 65 |
self._snapshots.pop(snapshot_name, None) |
| 66 |
|
| 67 |
def __len__(self) -> int: |
| 68 |
return len(self._snapshots) |
| 69 |
|
| 70 |
def __iter__(self) -> Iterator["Snapshot"]: |
| 71 |
return iter(self._snapshots.values()) |
| 72 |
|
| 73 |
|
| 74 |
@dataclass |
| 75 |
class SnapshotEmptyCollection(SnapshotCollection): |
| 76 |
"""This is a saved collection that is known to be empty and thus can be removed""" |
| 77 |
|
| 78 |
_snapshots: dict[str, "Snapshot"] = field( |
| 79 |
default_factory=lambda: {SnapshotEmpty().name: SnapshotEmpty()} |
| 80 |
) |
| 81 |
|
| 82 |
@property |
| 83 |
def has_snapshots(self) -> bool: |
| 84 |
return False |
| 85 |
|
| 86 |
|
| 87 |
@dataclass |
| 88 |
class SnapshotUnknownCollection(SnapshotCollection): |
| 89 |
"""This is a saved collection that is unclaimed by any extension currently in use""" |
| 90 |
|
| 91 |
_snapshots: dict[str, "Snapshot"] = field( |
| 92 |
default_factory=lambda: {SnapshotUnknown().name: SnapshotUnknown()} |
| 93 |
) |
| 94 |
|
| 95 |
|
| 96 |
@dataclass |
| 97 |
class SnapshotCollections: |
| 98 |
_snapshot_collections: dict[str, "SnapshotCollection"] = field(default_factory=dict) |
| 99 |
|
| 100 |
def get(self, location: str) -> Optional["SnapshotCollection"]: |
| 101 |
return self._snapshot_collections.get(location) |
| 102 |
|
| 103 |
def add(self, snapshot_collection: "SnapshotCollection") -> None: |
| 104 |
self._snapshot_collections[snapshot_collection.location] = snapshot_collection |
| 105 |
|
| 106 |
def update(self, snapshot_collection: "SnapshotCollection") -> None: |
| 107 |
snapshot_collection_to_update = self.get(snapshot_collection.location) |
| 108 |
if snapshot_collection_to_update is None: |
| 109 |
snapshot_collection_to_update = SnapshotCollection( |
| 110 |
location=snapshot_collection.location |
| 111 |
) |
| 112 |
self.add(snapshot_collection_to_update) |
| 113 |
snapshot_collection_to_update.merge(snapshot_collection) |
| 114 |
|
| 115 |
def merge(self, snapshot_collections: "SnapshotCollections") -> None: |
| 116 |
for snapshot_collection in snapshot_collections: |
| 117 |
self.update(snapshot_collection) |
| 118 |
|
| 119 |
def __iter__(self) -> Iterator["SnapshotCollection"]: |
| 120 |
return iter(self._snapshot_collections.values()) |
| 121 |
|
| 122 |
def __contains__(self, key: str) -> bool: |
| 123 |
return key in self._snapshot_collections |
| 124 |
|
| 125 |
def serialize(self) -> dict[str, list[str]]: |
| 126 |
"""Reduce to a ``{location: [snapshot name, ...]}`` mapping for transport.""" |
| 127 |
return { |
| 128 |
location: [snapshot.name for snapshot in collection] |
| 129 |
for location, collection in self._snapshot_collections.items() |
| 130 |
} |
| 131 |
|
| 132 |
def merge_serialized(self, data: dict[str, list[str]]) -> None: |
| 133 |
"""Merge a mapping produced by :meth:`serialize` into this instance.""" |
| 134 |
for location, names in data.items(): |
| 135 |
collection = SnapshotCollection(location=location) |
| 136 |
for name in names: |
| 137 |
collection.add(Snapshot(name=name)) |
| 138 |
self.update(collection) |
| 139 |
|
| 140 |
|
| 141 |
@dataclass |
| 142 |
class DiffedLine: |
| 143 |
a: str | None = None |
| 144 |
b: str | None = None |
| 145 |
c: list[str] = field(default_factory=list) |
| 146 |
diff_a: str = "" |
| 147 |
diff_b: str = "" |
| 148 |
|
| 149 |
@property |
| 150 |
def has_snapshot(self) -> bool: |
| 151 |
return self.a is not None |
| 152 |
|
| 153 |
@property |
| 154 |
def has_received(self) -> bool: |
| 155 |
return self.b is not None |
| 156 |
|
| 157 |
@property |
| 158 |
def is_complete(self) -> bool: |
| 159 |
return self.has_snapshot and self.has_received |
| 160 |
|
| 161 |
@property |
| 162 |
def is_context(self) -> bool: |
| 163 |
return bool(self.c) |