feat: hot reload for config/templates and GitPython-based hot update with pre-update notification

This commit is contained in:
snowy-yang committed 2026-09-12 19:59:39 +08:00
1 parent 8775c022bd
commit 67776b0092
7 files changed
+164 -5

No files matched your search

+8 -4
View File
@@ -1,11 +1,12 @@
"""模板渲染:从 templates/ 目录加载 .txt 模板并用 str.format 填充。
查找顺序:本模块所在目录、当前工作目录。
文件按修改时间自动热重载,改动后无需重启。
"""
from pathlib import Path
_CANDIDATE_DIRS = [Path(__file__).parent / "templates", Path("templates")]
_cache: dict[str, str] = {}
_cache: dict[str, tuple[float, str]] = {} # name -> (mtime, content)
def _dir() -> Path:
@@ -16,6 +17,9 @@ def _dir() -> Path:
def render(name: str, **kwargs) -> str:
if name not in _cache:
_cache[name] = (_dir() / f"{name}.txt").read_text(encoding="utf-8")
return _cache[name].format(**kwargs)
path = _dir() / f"{name}.txt"
mtime = path.stat().st_mtime
cached = _cache.get(name)
if cached is None or cached[0] != mtime:
_cache[name] = (mtime, path.read_text(encoding="utf-8"))
return _cache[name][1].format(**kwargs)