11from typing import Dict , Iterable , Tuple
22from enum import Enum , auto
3- from collections import defaultdict
43import threading
54from selfie_lib .TypedPath import TypedPath
65from selfie_lib .Slice import Slice
@@ -17,12 +16,6 @@ def sourcePathForCall(self, location) -> "TypedPath":
1716 raise NotImplementedError ("sourcePathForCall is not implemented" )
1817
1918
20- class FS :
21- def fileRead (self , typedPath : "TypedPath" ) -> str :
22- # Placeholder return or raise NotImplementedError
23- raise NotImplementedError ("fileRead is not implemented" )
24-
25-
2619class WritableComment (Enum ):
2720 NO_COMMENT = auto ()
2821 ONCE = auto ()
@@ -35,9 +28,7 @@ def writable(self) -> bool:
3528
3629class CommentTracker :
3730 def __init__ (self ):
38- self .cache : Dict [TypedPath , WritableComment ] = defaultdict (
39- lambda : WritableComment .NO_COMMENT
40- )
31+ self .cache : Dict [TypedPath , WritableComment ] = {}
4132 self .lock = threading .Lock ()
4233
4334 def pathsWithOnce (self ) -> Iterable [TypedPath ]:
@@ -48,23 +39,23 @@ def pathsWithOnce(self) -> Iterable[TypedPath]:
4839 if comment == WritableComment .ONCE
4940 ]
5041
51- # def hasWritableComment(self, call: CallStack, layout: SnapshotFileLayout) -> bool:
52- def hasWritableComment (
53- self , call : CallStack , layout : SnapshotFileLayout , fs : FS
54- ) -> bool :
42+ def hasWritableComment (self , call : CallStack , layout : SnapshotFileLayout ) -> bool :
5543 path = layout .sourcePathForCall (call )
5644 with self .lock :
57- comment = self .cache .get (path )
58- if comment and comment .writable :
59- return True
45+ if path in self .cache :
46+ comment = self .cache [path ]
47+ if comment .writable :
48+ return True
49+ else :
50+ return False
6051 else :
61- new_comment , _ = self .commentAndLine (path , fs )
52+ new_comment , _ = self .__commentAndLine (path )
6253 self .cache [path ] = new_comment
6354 return new_comment .writable
6455
6556 @staticmethod
66- def commentString (typedPath : TypedPath , fs : FS ) -> Tuple [str , int ]:
67- comment , line = CommentTracker .commentAndLine (typedPath , fs )
57+ def commentString (typedPath : TypedPath ) -> Tuple [str , int ]:
58+ comment , line = CommentTracker .__commentAndLine (typedPath )
6859 if comment == WritableComment .NO_COMMENT :
6960 raise ValueError ("No writable comment found" )
7061 elif comment == WritableComment .ONCE :
@@ -75,8 +66,9 @@ def commentString(typedPath: TypedPath, fs: FS) -> Tuple[str, int]:
7566 raise ValueError ("Invalid comment type" )
7667
7768 @staticmethod
78- def commentAndLine (typedPath : TypedPath , fs : FS ) -> Tuple [WritableComment , int ]:
79- content = Slice (fs .fileRead (typedPath ))
69+ def __commentAndLine (typedPath : TypedPath ) -> Tuple [WritableComment , int ]:
70+ with open (typedPath .absolute_path , "r" ) as file :
71+ content = Slice (file .read ())
8072 for comment_str in [
8173 "//selfieonce" ,
8274 "// selfieonce" ,
0 commit comments