2from typing
import Any, Callable, Generic, Optional, TypeVar, Union, overload
4from .Literals
import LiteralString, LiteralValue, TodoStub
5from .Roundtrip
import Roundtrip
6from .Snapshot
import Snapshot
7from .SnapshotSystem
import DiskStorage, _selfieSystem
8from .WriteTracker
import recordCall
10T = TypeVar(
"T", covariant=
True)
14def cache_selfie(to_cache: Callable[..., str]) ->
"CacheSelfie[str]": ...
19 to_cache: Callable[..., T], roundtrip: Roundtrip[T, str]
20) ->
"CacheSelfie[T]": ...
24 to_cache: Union[Callable[..., str], Callable[..., T]],
25 roundtrip: Optional[Roundtrip[T, str]] =
None,
26) -> Union[
"CacheSelfie[str]",
"CacheSelfie[T]"]:
29 return cache_selfie(to_cache, Roundtrip.identity())
30 elif isinstance(roundtrip, Roundtrip)
and to_cache
is not None:
31 deferred_disk_storage = _selfieSystem().disk_thread_local()
32 return CacheSelfie(deferred_disk_storage, roundtrip, to_cache)
34 raise TypeError(
"Invalid arguments provided to cache_selfie")
37def cache_selfie_json(to_cache: Callable[..., T]) ->
"CacheSelfie[T]":
38 return cache_selfie(to_cache, Roundtrip.json())
42def cache_selfie_binary(
43 to_cache: Callable[..., bytes],
44) ->
"CacheSelfieBinary[bytes]": ...
48def cache_selfie_binary(
49 to_cache: Callable[..., T], roundtrip: Roundtrip[T, bytes]
50) ->
"CacheSelfieBinary[T]": ...
53def cache_selfie_binary(
54 to_cache: Union[Callable[..., bytes], Callable[..., T]],
55 roundtrip: Optional[Roundtrip[T, bytes]] =
None,
56) -> Union[
"CacheSelfieBinary[bytes]",
"CacheSelfieBinary[T]"]:
59 return cache_selfie_binary(to_cache, Roundtrip.identity())
60 elif isinstance(roundtrip, Roundtrip)
and to_cache
is not None:
61 deferred_disk_storage = _selfieSystem().disk_thread_local()
64 raise TypeError(
"Invalid arguments provided to cache_selfie")
71 roundtrip: Roundtrip[T, str],
72 generator: Callable[..., T],
85 call = recordCall(
False)
86 system = _selfieSystem()
87 if system.mode.can_write(is_todo, call, system):
90 Snapshot.of(self.
roundtrip.serialize(actual)), sub, call
93 system.write_inline(TodoStub.to_match_disk.create_literal(), call)
97 raise Exception(
"Can't call `to_match_disk_todo` in readonly mode!")
99 snapshot = self.
disk.read_disk(sub, call)
101 raise Exception(system.mode.msg_snapshot_not_found())
102 if snapshot.subject.is_binary
or len(snapshot.facets) > 0:
104 f
"Expected a string subject with no facets, got {snapshot}"
106 return self.
roundtrip.parse(snapshot.subject.value_string())
111 def to_be(self, expected: str) -> T:
115 call = recordCall(
False)
116 system = _selfieSystem()
117 writable = system.mode.can_write(snapshot
is None, call, system)
123 snapshot, self.
roundtrip.serialize(actual), literal_string_formatter
130 raise Exception(
"Can't call `to_be_todo` in readonly mode!")
139 roundtrip: Roundtrip[T, bytes],
140 generator: Callable[..., T],
153 system = _selfieSystem()
154 call = recordCall(
False)
156 if system.mode.can_write(is_todo, call, system):
158 serialized_data = self.
roundtrip.serialize(actual)
159 self.
disk.write_disk(Snapshot.of(serialized_data), sub, call)
162 system.write_inline(TodoStub.to_match_disk.create_literal(), call)
167 raise Exception(
"Can't call `to_match_disk_TODO` in read-only mode!")
169 snapshot = self.
disk.read_disk(sub, call)
172 raise Exception(system.mode.msg_snapshot_not_found())
174 if snapshot.subject.is_binary
or len(snapshot.facets) > 0:
176 f
"Expected a binary subject with no facets, got {snapshot}"
179 return self.
roundtrip.parse(snapshot.subject.value_binary())
188 system = _selfieSystem()
189 call = recordCall(
False)
190 writable = system.mode.can_write(is_todo, call, system)
196 system.write_inline(TodoStub.to_be_file.create_literal(), call)
198 with open(subpath,
"wb")
as file:
199 file.write(self.
roundtrip.serialize(actual))
204 raise Exception(
"Can't call `to_be_file_TODO` in read-only mode!")
206 with open(subpath,
"rb")
as file:
207 serialized_data = file.read()
208 return self.
roundtrip.parse(serialized_data)
217 system = _selfieSystem()
218 call = recordCall(
False)
219 writable = system.mode.can_write(snapshot
is None, call, system)
223 base64_data = base64.b64encode(self.
roundtrip.serialize(actual)).decode(
228 LiteralValue(snapshot, base64_data, literal_string_formatter),
234 raise Exception(
"Can't call `to_be_TODO` in read-only mode!")
236 decoded_data = base64.b64decode(snapshot.encode(
"utf-8"))
237 return self.
roundtrip.parse(decoded_data)
T _to_be_file_impl(self, str subpath, bool is_todo)
T _to_match_disk_impl(self, str sub, bool is_todo)
T _to_be_base64_impl(self, Optional[str] snapshot)
T to_match_disk_TODO(self, str sub="")
__init__(self, DiskStorage disk, Roundtrip[T, bytes] roundtrip, Callable[..., T] generator)
T to_be_base64(self, str snapshot)
T to_be_file(self, str subpath)
T to_be_base64_TODO(self, Optional[Any] _=None)
T to_match_disk(self, str sub="")
T to_be_file_TODO(self, str subpath)
T to_be(self, str expected)
T _to_be_impl(self, Optional[str] snapshot)
T to_match_disk(self, str sub="")
__init__(self, DiskStorage disk, Roundtrip[T, str] roundtrip, Callable[..., T] generator)
T _to_match_disk_impl(self, str sub, bool is_todo)
T to_be_TODO(self, Optional[Any] _=None)
T to_match_disk_TODO(self, str sub="")