Selfie
Loading...
Searching...
No Matches
selfie_lib.CacheSelfie.CacheSelfieBinary Class Reference
Inheritance diagram for selfie_lib.CacheSelfie.CacheSelfieBinary:

Public Member Functions

 __init__ (self, DiskStorage disk, Roundtrip[T, bytes] roundtrip, Callable[..., T] generator)
 
T to_match_disk (self, str sub="")
 
T to_match_disk_TODO (self, str sub="")
 
T to_be_file_TODO (self, str subpath)
 
T to_be_file (self, str subpath)
 
T to_be_base64_TODO (self, Optional[Any] _=None)
 
T to_be_base64 (self, str snapshot)
 

Public Attributes

 disk
 
 roundtrip
 
 generator
 

Protected Member Functions

T _to_match_disk_impl (self, str sub, bool is_todo)
 
T _to_be_file_impl (self, str subpath, bool is_todo)
 
T _to_be_base64_impl (self, Optional[str] snapshot)
 

Detailed Description

Definition at line 135 of file CacheSelfie.py.

Constructor & Destructor Documentation

◆ __init__()

selfie_lib.CacheSelfie.CacheSelfieBinary.__init__ (   self,
DiskStorage  disk,
Roundtrip[T, bytes]  roundtrip,
Callable[..., T generator 
)

Definition at line 136 of file CacheSelfie.py.

141 ):
142 self.disk = disk
143 self.roundtrip = roundtrip
144 self.generator = generator
145

Member Function Documentation

◆ _to_be_base64_impl()

T selfie_lib.CacheSelfie.CacheSelfieBinary._to_be_base64_impl (   self,
Optional[str]  snapshot 
)
protected

Definition at line 216 of file CacheSelfie.py.

216 def _to_be_base64_impl(self, snapshot: Optional[str]) -> T:
217 system = _selfieSystem()
218 call = recordCall(False)
219 writable = system.mode.can_write(snapshot is None, call, system)
220
221 if writable:
222 actual = self.generator()
223 base64_data = base64.b64encode(self.roundtrip.serialize(actual)).decode(
224 "utf-8"
225 )
226 literal_string_formatter = LiteralString()
227 system.write_inline(
228 LiteralValue(snapshot, base64_data, literal_string_formatter),
229 call,
230 )
231 return actual
232 else:
233 if snapshot is None:
234 raise Exception("Can't call `to_be_TODO` in read-only mode!")
235 else:
236 decoded_data = base64.b64decode(snapshot.encode("utf-8"))
237 return self.roundtrip.parse(decoded_data)

◆ _to_be_file_impl()

T selfie_lib.CacheSelfie.CacheSelfieBinary._to_be_file_impl (   self,
str  subpath,
bool  is_todo 
)
protected

Definition at line 187 of file CacheSelfie.py.

187 def _to_be_file_impl(self, subpath: str, is_todo: bool) -> T:
188 system = _selfieSystem()
189 call = recordCall(False)
190 writable = system.mode.can_write(is_todo, call, system)
191
192 if writable:
193 actual = self.generator()
194
195 if is_todo:
196 system.write_inline(TodoStub.to_be_file.create_literal(), call)
197
198 with open(subpath, "wb") as file:
199 file.write(self.roundtrip.serialize(actual))
200
201 return actual
202 else:
203 if is_todo:
204 raise Exception("Can't call `to_be_file_TODO` in read-only mode!")
205 else:
206 with open(subpath, "rb") as file:
207 serialized_data = file.read()
208 return self.roundtrip.parse(serialized_data)
209

◆ _to_match_disk_impl()

T selfie_lib.CacheSelfie.CacheSelfieBinary._to_match_disk_impl (   self,
str  sub,
bool  is_todo 
)
protected

Definition at line 152 of file CacheSelfie.py.

152 def _to_match_disk_impl(self, sub: str, is_todo: bool) -> T:
153 system = _selfieSystem()
154 call = recordCall(False)
155
156 if system.mode.can_write(is_todo, call, system):
157 actual = self.generator()
158 serialized_data = self.roundtrip.serialize(actual)
159 self.disk.write_disk(Snapshot.of(serialized_data), sub, call)
160
161 if is_todo:
162 system.write_inline(TodoStub.to_match_disk.create_literal(), call)
163
164 return actual
165 else:
166 if is_todo:
167 raise Exception("Can't call `to_match_disk_TODO` in read-only mode!")
168 else:
169 snapshot = self.disk.read_disk(sub, call)
170
171 if snapshot is None:
172 raise Exception(system.mode.msg_snapshot_not_found())
173
174 if snapshot.subject.is_binary or len(snapshot.facets) > 0:
175 raise Exception(
176 f"Expected a binary subject with no facets, got {snapshot}"
177 )
178
179 return self.roundtrip.parse(snapshot.subject.value_binary())
180

◆ to_be_base64()

T selfie_lib.CacheSelfie.CacheSelfieBinary.to_be_base64 (   self,
str  snapshot 
)

Definition at line 213 of file CacheSelfie.py.

213 def to_be_base64(self, snapshot: str) -> T:
214 return self._to_be_base64_impl(snapshot)
215

◆ to_be_base64_TODO()

T selfie_lib.CacheSelfie.CacheSelfieBinary.to_be_base64_TODO (   self,
Optional[Any]   _ = None 
)

Definition at line 210 of file CacheSelfie.py.

210 def to_be_base64_TODO(self, _: Optional[Any] = None) -> T:
211 return self._to_be_base64_impl(None)
212

◆ to_be_file()

T selfie_lib.CacheSelfie.CacheSelfieBinary.to_be_file (   self,
str  subpath 
)

Definition at line 184 of file CacheSelfie.py.

184 def to_be_file(self, subpath: str) -> T:
185 return self._to_be_file_impl(subpath, False)
186

◆ to_be_file_TODO()

T selfie_lib.CacheSelfie.CacheSelfieBinary.to_be_file_TODO (   self,
str  subpath 
)

Definition at line 181 of file CacheSelfie.py.

181 def to_be_file_TODO(self, subpath: str) -> T:
182 return self._to_be_file_impl(subpath, True)
183

◆ to_match_disk()

T selfie_lib.CacheSelfie.CacheSelfieBinary.to_match_disk (   self,
str   sub = "" 
)

Definition at line 146 of file CacheSelfie.py.

146 def to_match_disk(self, sub: str = "") -> T:
147 return self._to_match_disk_impl(sub, False)
148

◆ to_match_disk_TODO()

T selfie_lib.CacheSelfie.CacheSelfieBinary.to_match_disk_TODO (   self,
str   sub = "" 
)

Definition at line 149 of file CacheSelfie.py.

149 def to_match_disk_TODO(self, sub: str = "") -> T:
150 return self._to_match_disk_impl(sub, True)
151

Member Data Documentation

◆ disk

selfie_lib.CacheSelfie.CacheSelfieBinary.disk

Definition at line 142 of file CacheSelfie.py.

◆ generator

selfie_lib.CacheSelfie.CacheSelfieBinary.generator

Definition at line 144 of file CacheSelfie.py.

◆ roundtrip

selfie_lib.CacheSelfie.CacheSelfieBinary.roundtrip

Definition at line 143 of file CacheSelfie.py.


The documentation for this class was generated from the following file: