Selfie
Loading...
Searching...
No Matches
SnapshotValue.py
Go to the documentation of this file.
1from abc import ABC, abstractmethod
2from typing import Union
3
4from .LineReader import _to_unix
5
6
7class SnapshotValue(ABC):
8 @property
9 def is_binary(self) -> bool:
10 return isinstance(self, SnapshotValueBinary)
11
12 @abstractmethod
13 def value_binary(self) -> bytes:
14 pass
15
16 @abstractmethod
17 def value_string(self) -> str:
18 pass
19
20 @staticmethod
21 def of(data: Union[bytes, str, "SnapshotValue"]) -> "SnapshotValue":
22 if isinstance(data, bytes):
23 return SnapshotValueBinary(data)
24 elif isinstance(data, str):
25 return SnapshotValueString(_to_unix(data))
26 elif isinstance(data, SnapshotValue):
27 return data
28 else:
29 raise TypeError("Unsupported type for Snapshot creation")
30
31
33 def __init__(self, value: bytes):
34 self._value = value
35
36 def value_binary(self) -> bytes:
37 return self._value
38
39 def value_string(self) -> str:
40 raise NotImplementedError("This is a binary value.")
41
42 def __eq__(self, other: object) -> bool:
43 if isinstance(other, SnapshotValueBinary):
44 return self.value_binaryvalue_binary() == other.value_binary()
45 return False
46
47 def __hash__(self) -> int:
48 return hash(self._value)
49
50
52 def __init__(self, value: str):
53 self._value = value
54
55 def value_binary(self) -> bytes:
56 raise NotImplementedError("This is a string value.")
57
58 def value_string(self) -> str:
59 return self._value
60
61 def __eq__(self, other: object) -> bool:
62 if isinstance(other, SnapshotValueString):
63 return self.value_stringvalue_string() == other.value_string()
64 return False
65
66 def __hash__(self) -> int:
67 return hash(self._value)
"SnapshotValue" of(Union[bytes, str, "SnapshotValue"] data)