Skip to content

Commit e6f6fad

Browse files
committed
Move CacheSelfieBinary into CacheSelfie to fix a circular import.
1 parent 1ddb9d0 commit e6f6fad

File tree

2 files changed

+102
-113
lines changed

2 files changed

+102
-113
lines changed

python/selfie-lib/selfie_lib/CacheSelfie.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,105 @@ def _to_be_impl(self, snapshot: Optional[str]) -> T:
7575
raise Exception("Can't call `to_be_todo` in readonly mode!")
7676
else:
7777
return self.roundtrip.parse(snapshot)
78+
79+
80+
class CacheSelfieBinary(Generic[T]):
81+
def __init__(self, disk: DiskStorage, roundtrip: Roundtrip[T, bytes], generator):
82+
self.disk: DiskStorage = disk
83+
self.roundtrip: Roundtrip[T, bytes] = roundtrip
84+
self.generator = generator
85+
86+
def to_match_disk(self, sub: str = "") -> T:
87+
return self._to_match_disk_impl(sub, False)
88+
89+
def to_match_disk_TODO(self, sub: str = "") -> T:
90+
return self._to_match_disk_impl(sub, True)
91+
92+
def _to_match_disk_impl(self, sub: str, is_todo: bool) -> T:
93+
system = get_system()
94+
call = recordCall(False)
95+
96+
if system.mode.can_write(is_todo, call, system):
97+
actual = self.generator()
98+
serialized_data = self.roundtrip.serialize(actual)
99+
self.disk.write_disk(Snapshot.of(serialized_data), sub, call)
100+
101+
if is_todo:
102+
system.write_inline(TodoStub.to_match_disk.create_literal(), call)
103+
104+
return actual
105+
else:
106+
if is_todo:
107+
raise Exception("Can't call `to_match_disk_TODO` in read-only mode!")
108+
else:
109+
snapshot = self.disk.read_disk(sub, call)
110+
111+
if snapshot is None:
112+
raise Exception(system.mode.msg_snapshot_not_found())
113+
114+
if snapshot.subject.is_binary or len(snapshot.facets) > 0:
115+
raise Exception(
116+
"Expected a binary subject with no facets, got {}".format(
117+
snapshot
118+
)
119+
)
120+
121+
return self.roundtrip.parse(snapshot.subject.value_binary())
122+
123+
def to_be_file_TODO(self, subpath: str) -> T:
124+
return self._to_be_file_impl(subpath, True)
125+
126+
def to_be_file(self, subpath: str) -> T:
127+
return self._to_be_file_impl(subpath, False)
128+
129+
def _to_be_file_impl(self, subpath: str, is_todo: bool) -> T:
130+
system = get_system()
131+
call = recordCall(False)
132+
writable = system.mode.can_write(is_todo, call, system)
133+
134+
if writable:
135+
actual = self.generator()
136+
137+
if is_todo:
138+
system.write_inline(TodoStub.to_be_file.create_literal(), call)
139+
140+
with open(subpath, "wb") as file:
141+
file.write(self.roundtrip.serialize(actual))
142+
143+
return actual
144+
else:
145+
if is_todo:
146+
raise Exception("Can't call `toBeFile_TODO` in read-only mode!")
147+
else:
148+
with open(subpath, "rb") as file:
149+
serialized_data = file.read()
150+
return self.roundtrip.parse(serialized_data)
151+
152+
def to_be_base64_TODO(self, unused_arg: Optional[Any] = None) -> T:
153+
return self._to_be_base64_impl(None)
154+
155+
def to_be_base64(self, snapshot: str) -> T:
156+
return self._to_be_base64_impl(snapshot)
157+
158+
def _to_be_base64_impl(self, snapshot: Optional[str]) -> T:
159+
system = get_system()
160+
call = recordCall(False)
161+
writable = system.mode.can_write(snapshot is None, call, system)
162+
163+
if writable:
164+
actual = self.generator()
165+
base64_data = base64.b64encode(self.roundtrip.serialize(actual)).decode(
166+
"utf-8"
167+
)
168+
literal_string_formatter = LiteralString()
169+
system.write_inline(
170+
LiteralValue(snapshot, base64_data, literal_string_formatter),
171+
call,
172+
)
173+
return actual
174+
else:
175+
if snapshot is None:
176+
raise Exception("Can't call `toBe_TODO` in read-only mode!")
177+
else:
178+
decoded_data = base64.b64decode(snapshot.encode("utf-8"))
179+
return self.roundtrip.parse(decoded_data)

python/selfie-lib/selfie_lib/CacheSelfieBinary.py

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)