utils.read_file
This commit is contained in:
@@ -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
|
||||||
Reference in New Issue
Block a user