Skip to content
This repository was archived by the owner on Apr 4, 2024. It is now read-only.

Commit 6c28563

Browse files
committed
Update CommentTracker and TypedPath
1 parent 8b4d9aa commit 6c28563

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

python/selfie-lib/selfie_lib/CommentTracker.py

+14-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from typing import Dict, Iterable, Tuple
22
from enum import Enum, auto
3-
from collections import defaultdict
43
import threading
54
from selfie_lib.TypedPath import TypedPath
65
from 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-
2619
class WritableComment(Enum):
2720
NO_COMMENT = auto()
2821
ONCE = auto()
@@ -35,9 +28,7 @@ def writable(self) -> bool:
3528

3629
class 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",

python/selfie-lib/selfie_lib/TypedPath.py

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ class TypedPath:
66
def __init__(self, absolute_path: str):
77
self.absolute_path = absolute_path
88

9+
def __hash__(self):
10+
return hash(self.absolute_path)
11+
912
@property
1013
def name(self) -> str:
1114
if self.absolute_path.endswith("/"):

0 commit comments

Comments
 (0)