-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpd_weakmap.py
More file actions
56 lines (46 loc) · 1.79 KB
/
Copy pathpd_weakmap.py
File metadata and controls
56 lines (46 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Weakmap helpers — locate the latest report, its verdict, and the top-miss
pattern. Mirrors upstream PAIDEIA's statusline/session-start logic.
"""
from __future__ import annotations
import re
from pathlib import Path
from . import pd_errlog
_VERDICT_RX = re.compile(r"##\s*One-line verdict\s*\n+\s*(.+?)(?:\n|$)")
def latest_weakmap(cwd: Path) -> Path | None:
"""Newest ``weakmap/weakmap_<YYYY-MM-DD_HHmm>.md`` by the timestamp in its name.
Name order, not mtime: the course folder is meant to be committed, and a
fresh ``git clone`` stamps every file with the checkout time, which would
make mtime ordering arbitrary exactly when the history matters most.
Path.glob also keeps a course folder whose name contains glob metacharacters
("Math [2026] Final") from matching nothing.
"""
d = Path(cwd) / "weakmap"
if not d.is_dir():
return None
matches = sorted(d.glob("weakmap_*.md"), key=lambda p: p.name, reverse=True)
return matches[0] if matches else None
def latest_verdict(cwd: Path) -> str | None:
wm = latest_weakmap(cwd)
if not wm:
return None
try:
text = wm.read_text(encoding="utf-8", errors="replace")
except OSError:
return None
m = _VERDICT_RX.search(text)
return m.group(1).strip() if m else None
def top_miss(cwd: Path) -> str | None:
"""Top-miss pattern: prefer the newest weakmap, else fall back to the error log."""
wm = latest_weakmap(cwd)
if wm:
try:
text = wm.read_text(encoding="utf-8", errors="replace")
except OSError:
text = ""
m = pd_errlog.PATTERN_RX.search(text)
if m:
return m.group(1)
m = re.search(r"\bP(\d+)\b", text)
if m:
return f"P{m.group(1)}"
return pd_errlog.top_pattern(cwd)