1
1
from typing import Dict , Iterable , Tuple
2
2
from enum import Enum , auto
3
- from collections import defaultdict
4
3
import threading
5
4
from selfie_lib .TypedPath import TypedPath
6
5
from selfie_lib .Slice import Slice
@@ -17,12 +16,6 @@ def sourcePathForCall(self, location) -> "TypedPath":
17
16
raise NotImplementedError ("sourcePathForCall is not implemented" )
18
17
19
18
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
-
26
19
class WritableComment (Enum ):
27
20
NO_COMMENT = auto ()
28
21
ONCE = auto ()
@@ -35,9 +28,7 @@ def writable(self) -> bool:
35
28
36
29
class CommentTracker :
37
30
def __init__ (self ):
38
- self .cache : Dict [TypedPath , WritableComment ] = defaultdict (
39
- lambda : WritableComment .NO_COMMENT
40
- )
31
+ self .cache : Dict [TypedPath , WritableComment ] = {}
41
32
self .lock = threading .Lock ()
42
33
43
34
def pathsWithOnce (self ) -> Iterable [TypedPath ]:
@@ -48,23 +39,23 @@ def pathsWithOnce(self) -> Iterable[TypedPath]:
48
39
if comment == WritableComment .ONCE
49
40
]
50
41
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 :
55
43
path = layout .sourcePathForCall (call )
56
44
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
60
51
else :
61
- new_comment , _ = self .commentAndLine (path , fs )
52
+ new_comment , _ = self .__commentAndLine (path )
62
53
self .cache [path ] = new_comment
63
54
return new_comment .writable
64
55
65
56
@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 )
68
59
if comment == WritableComment .NO_COMMENT :
69
60
raise ValueError ("No writable comment found" )
70
61
elif comment == WritableComment .ONCE :
@@ -75,8 +66,9 @@ def commentString(typedPath: TypedPath, fs: FS) -> Tuple[str, int]:
75
66
raise ValueError ("Invalid comment type" )
76
67
77
68
@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 ())
80
72
for comment_str in [
81
73
"//selfieonce" ,
82
74
"// selfieonce" ,
0 commit comments