22 self.cache: dict[TypedPath, WritableComment] = {}
23 self.
lock = threading.Lock()
29 for path, comment
in self.cache.items()
30 if comment == WritableComment.ONCE
34 path = layout.sourcefile_for_call(call.location)
36 if path
in self.cache:
37 return self.cache[path].writable
40 self.cache[path] = new_comment
41 return new_comment.writable
45 comment, line = CommentTracker.__commentAndLine(typedPath)
46 if comment == WritableComment.NO_COMMENT:
47 raise ValueError(
"No writable comment found")
48 elif comment == WritableComment.ONCE:
49 return (
"#selfieonce", line)
50 elif comment == WritableComment.FOREVER:
51 return (
"#SELFIEWRITE", line)
53 raise ValueError(
"Invalid comment type")
56 def __commentAndLine(typedPath: TypedPath) -> tuple[WritableComment, int]:
57 with open(typedPath.absolute_path, encoding=
"utf-8")
as file:
58 content =
Slice(file.read())
65 index = content.indexOf(comment_str)
67 lineNumber = content.baseLineAtOffset(index)
70 if "once" in comment_str
71 else WritableComment.FOREVER
73 return (comment, lineNumber)
74 return (WritableComment.NO_COMMENT, -1)