utils.read_file

This commit is contained in:
John Lancaster
2026-08-29 19:13:20 -05:00
parent c866d1bdb0
commit 73eb490537
+20
View File
@@ -0,0 +1,20 @@
import logging
from pathlib import Path
logger = logging.getLogger(__name__)
_FILE_CACHE: dict[Path, tuple[int, str]] = {}
def read_file(path: Path) -> str:
new_ts = path.stat().st_mtime_ns
match _FILE_CACHE.get(path):
case (int(timestamp), str(content)) if timestamp == new_ts:
return content
case _:
with path.open("r", encoding="utf-8") as f:
logger.info("Loading file %s", path)
content = f.read()
_FILE_CACHE[path] = (new_ts, content)
return content