Selfie
Loading...
Searching...
No Matches
selfie_lib.WriteTracker.InlineWriteTracker Class Reference
Inheritance diagram for selfie_lib.WriteTracker.InlineWriteTracker:
selfie_lib.WriteTracker.WriteTracker selfie_lib.WriteTracker.CallLocation selfie_lib.Literals.LiteralValue

Public Member Functions

bool hasWrites (self)
 
 record (self, LiteralValue snapshot, CallStack call, SnapshotFileLayout layout)
 
 persist_writes (self, SnapshotFileLayout layout)
 
- Public Member Functions inherited from selfie_lib.WriteTracker.WriteTracker
 __init__ (self)
 
 recordInternal (self, T key, U snapshot, CallStack call, SnapshotFileLayout layout, bool allow_multiple_equivalent_writes=True)
 
- Public Member Functions inherited from selfie_lib.WriteTracker.CallLocation
 __init__ (self, Optional[str] file_name, int line)
 
Optional[str] file_name (self)
 
int line (self)
 
"CallLocation" with_line (self, int line)
 
str ide_link (self, "SnapshotFileLayout" _)
 
bool same_path_as (self, "CallLocation" other)
 
str source_filename_without_extension (self)
 
bool __lt__ (self, other)
 
bool __eq__ (self, other)
 
 __hash__ (self)
 
- Public Member Functions inherited from selfie_lib.Literals.LiteralValue
None __init__ (self, Optional[T] expected, T actual, "LiteralFormat" fmt)
 

Public Attributes

 writes
 
- Public Attributes inherited from selfie_lib.WriteTracker.WriteTracker
 lock
 
- Public Attributes inherited from selfie_lib.Literals.LiteralValue
 expected
 
 actual
 
 format
 

Additional Inherited Members

- Protected Attributes inherited from selfie_lib.WriteTracker.CallLocation
 _file_name
 
 _line
 

Detailed Description

Definition at line 168 of file WriteTracker.py.

Member Function Documentation

◆ hasWrites()

bool selfie_lib.WriteTracker.InlineWriteTracker.hasWrites (   self)

Definition at line 169 of file WriteTracker.py.

169 def hasWrites(self) -> bool:
170 return len(self.writes) > 0
171

◆ persist_writes()

selfie_lib.WriteTracker.InlineWriteTracker.persist_writes (   self,
SnapshotFileLayout  layout 
)

Definition at line 204 of file WriteTracker.py.

204 def persist_writes(self, layout: SnapshotFileLayout):
205 # Assuming there is at least one write to process
206 if not self.writes:
207 return
208
209 # Sorting writes based on file name and line number
210 sorted_writes = sorted(
211 self.writes.values(),
212 key=lambda x: (x.call_stack.location.file_name, x.call_stack.location.line),
213 )
214
215 # Initialize from the first write
216 first_write = sorted_writes[0]
217 current_file = layout.sourcefile_for_call(first_write.call_stack.location)
218 content = SourceFile(current_file.name, layout.fs.file_read(current_file))
219 delta_line_numbers = 0
220
221 for write in sorted_writes:
222 # Determine the file path for the current write
223 file_path = layout.sourcefile_for_call(write.call_stack.location)
224 # If we switch to a new file, write changes to the disk for the previous file
225 if file_path != current_file:
226 layout.fs.file_write(current_file, content.as_string)
227 current_file = file_path
228 content = SourceFile(
229 current_file.name, layout.fs.file_read(current_file)
230 )
231 delta_line_numbers = 0
232
233 # Calculate the line number taking into account changes that shifted line numbers
234 line = write.call_stack.location.line + delta_line_numbers
235 if isinstance(write.snapshot.format, LiteralTodoStub):
236 kind: TodoStub = write.snapshot.actual # type: ignore
237 content.replace_on_line(line, f".{kind.name}_TODO(", f".{kind.name}(")
238 else:
239 to_be_literal = content.parse_to_be_like(line)
240 # Attempt to set the literal value and adjust for line shifts due to content changes
241 literal_change = to_be_literal.set_literal_and_get_newline_delta(
242 write.snapshot
243 )
244 delta_line_numbers += literal_change
245
246 # Final write to disk for the last file processed
247 layout.fs.file_write(current_file, content.as_string)
248
249

◆ record()

selfie_lib.WriteTracker.InlineWriteTracker.record (   self,
LiteralValue  snapshot,
CallStack  call,
SnapshotFileLayout  layout 
)

Definition at line 172 of file WriteTracker.py.

177 ):
178 super().recordInternal(call.location, snapshot, call, layout)
179
180 file = layout.sourcefile_for_call(call.location)
181
182 if (
183 snapshot.expected is not None
184 and isinstance(snapshot.expected, str)
185 and isinstance(snapshot.format, LiteralString)
186 ):
187 content = SourceFile(file.name, layout.fs.file_read(file))
188 try:
189 snapshot = cast(LiteralValue, snapshot)
190 parsed_value = content.parse_to_be_like(
191 call.location.line
192 ).parse_literal(snapshot.format)
193 except Exception as e:
194 raise AssertionError(
195 f"Error while parsing the literal at {call.location.ide_link(layout)}. Please report this error at https://github.com/diffplug/selfie"
196 ) from e
197 if parsed_value != snapshot.expected:
198 raise layout.fs.assert_failed(
199 f"Selfie cannot modify the literal at {call.location.ide_link(layout)} because Selfie has a parsing bug. Please report this error at https://github.com/diffplug/selfie",
200 snapshot.expected,
201 parsed_value,
202 )
203

Member Data Documentation

◆ writes

selfie_lib.WriteTracker.InlineWriteTracker.writes

Definition at line 170 of file WriteTracker.py.


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