This repository was archived by the owner on Apr 4, 2024. It is now read-only.
forked from diffplug/selfie
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWriteTracker.py
74 lines (55 loc) · 2.24 KB
/
WriteTracker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from typing import List, Optional
from selfie_lib.CommentTracker import SnapshotFileLayout
import inspect
class CallLocation:
def __init__(self, file_name: Optional[str], line: int):
self._file_name = file_name
self._line = line
@property
def file_name(self) -> Optional[str]:
return self._file_name
@property
def line(self) -> int:
return self._line
def with_line(self, line: int) -> "CallLocation":
return CallLocation(self._file_name, line)
def ide_link(self, layout: "SnapshotFileLayout") -> str:
return f"File: {self._file_name}, Line: {self._line}"
def same_path_as(self, other: "CallLocation") -> bool:
if not isinstance(other, CallLocation):
return False
return self._file_name == other.file_name
def source_filename_without_extension(self) -> str:
if self._file_name is not None:
return self._file_name.rsplit(".", 1)[0]
return ""
def __lt__(self, other) -> bool:
if not isinstance(other, CallLocation):
return NotImplemented
return (self._file_name, self._line) < (other.file_name, other.line)
def __eq__(self, other) -> bool:
if not isinstance(other, CallLocation):
return NotImplemented
return (self._file_name, self._line) == (other.file_name, other.line)
class CallStack:
def __init__(self, location: CallLocation, rest_of_stack: List[CallLocation]):
self.location = location
self.rest_of_stack = rest_of_stack
def ide_link(self, layout: "SnapshotFileLayout") -> str:
links = [self.location.ide_link(layout)] + [
loc.ide_link(layout) for loc in self.rest_of_stack
]
return "\n".join(links)
def recordCall(callerFileOnly: bool = False) -> CallStack:
stack_frames = inspect.stack()[1:]
if callerFileOnly:
caller_file = stack_frames[0].filename
stack_frames = [
frame for frame in stack_frames if frame.filename == caller_file
]
call_locations = [
CallLocation(frame.filename, frame.lineno) for frame in stack_frames
]
location = call_locations[0]
rest_of_stack = call_locations[1:]
return CallStack(location, rest_of_stack)