Selfie
Loading...
Searching...
No Matches
SnapshotReader.py
Go to the documentation of this file.
1from typing import Optional
2
3from .Snapshot import Snapshot
4from .SnapshotValueReader import SnapshotValueReader
5
6
8 def __init__(self, value_reader: SnapshotValueReader) -> None:
9 self.value_reader: SnapshotValueReader = value_reader
10
11 def peek_key(self) -> Optional[str]:
12 next_key = self.value_reader.peek_key()
13 if next_key is None or next_key == "[end of file]":
14 return None
15 return next_key
16
17 def next_snapshot(self) -> Snapshot:
18 root_name: Optional[str] = self.peek_key()
19 snapshot: Snapshot = Snapshot.of(self.value_reader.next_value())
20 while True:
21 next_key: Optional[str] = self.value_reader.peek_key()
22 if next_key is None or next_key == "[end of file]":
23 break
24 facet_idx: int = next_key.find("[")
25 if facet_idx == -1:
26 break
27 else:
28 facet_root, facet_name = (
29 next_key[:facet_idx],
30 next_key[facet_idx + 1 : -1],
31 )
32 if facet_root == root_name:
33 facet_value = self.value_reader.next_value()
34 snapshot = snapshot.plus_facet(facet_name, facet_value)
35 else:
36 break
37 return snapshot
38
39 def skip_snapshot(self) -> None:
40 root_name: Optional[str] = self.peek_key()
41 if root_name is None:
42 raise ValueError("No snapshot to skip")
43 self.value_reader.skip_value()
44 while True:
45 next_key: Optional[str] = self.peek_key()
46 if next_key is None or not next_key.startswith(f"{root_name}["):
47 break
48 self.value_reader.skip_value()
None __init__(self, SnapshotValueReader value_reader)