feat(context): collect persons and active projects from vault
This commit is contained in:
parent
3a9c298a2c
commit
bc91e61167
|
|
@ -0,0 +1,50 @@
|
||||||
|
from datetime import date
|
||||||
|
from pathlib import Path
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
WEEKDAYS_DE = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"]
|
||||||
|
|
||||||
|
|
||||||
|
def _read_frontmatter(text: str) -> dict[str, str]:
|
||||||
|
if not text.startswith("---"):
|
||||||
|
return {}
|
||||||
|
end = text.find("\n---", 3)
|
||||||
|
if end == -1:
|
||||||
|
return {}
|
||||||
|
block = text[3:end]
|
||||||
|
out: dict[str, str] = {}
|
||||||
|
for line in block.splitlines():
|
||||||
|
if ":" in line:
|
||||||
|
k, v = line.split(":", 1)
|
||||||
|
out[k.strip()] = v.strip()
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def collect_vault_context(vault_path: Path, today: date) -> dict:
|
||||||
|
persons: list[dict] = []
|
||||||
|
persons_dir = vault_path / "00 Kontext" / "Personen"
|
||||||
|
if persons_dir.exists():
|
||||||
|
for md in sorted(persons_dir.glob("*.md")):
|
||||||
|
fm = _read_frontmatter(md.read_text(encoding="utf-8"))
|
||||||
|
display = md.stem
|
||||||
|
persons.append({
|
||||||
|
"display": display,
|
||||||
|
"vault_path": f"00 Kontext/Personen/{display}",
|
||||||
|
"vorname": fm.get("vorname", ""),
|
||||||
|
"nachname": fm.get("nachname", ""),
|
||||||
|
"spitzname": fm.get("spitzname", ""),
|
||||||
|
})
|
||||||
|
|
||||||
|
projects: list[str] = []
|
||||||
|
projects_dir = vault_path / "02 Projekte"
|
||||||
|
if projects_dir.exists():
|
||||||
|
for md in sorted(projects_dir.glob("*.md")):
|
||||||
|
projects.append(md.stem)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"today": today.isoformat(),
|
||||||
|
"weekday": WEEKDAYS_DE[today.weekday()],
|
||||||
|
"persons": persons,
|
||||||
|
"projects": projects,
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
from pathlib import Path
|
||||||
|
from datetime import date
|
||||||
|
from journal_bot.context import collect_vault_context
|
||||||
|
|
||||||
|
|
||||||
|
def test_collect_persons_from_personen_folder(tmp_path):
|
||||||
|
persons = tmp_path / "00 Kontext" / "Personen"
|
||||||
|
persons.mkdir(parents=True)
|
||||||
|
(persons / "Vera Kauer.md").write_text(
|
||||||
|
"---\nvorname: Vera\nnachname: Kauer\nspitzname: Vera\n---\n", encoding="utf-8"
|
||||||
|
)
|
||||||
|
(persons / "Philip Losch.md").write_text(
|
||||||
|
"---\nvorname: Philip\nnachname: Losch\n---\n", encoding="utf-8"
|
||||||
|
)
|
||||||
|
ctx = collect_vault_context(tmp_path, today=date(2026, 6, 14))
|
||||||
|
names = {p["display"] for p in ctx["persons"]}
|
||||||
|
assert "Vera Kauer" in names
|
||||||
|
assert "Philip Losch" in names
|
||||||
|
|
||||||
|
|
||||||
|
def test_collect_active_projects(tmp_path):
|
||||||
|
projects = tmp_path / "02 Projekte"
|
||||||
|
projects.mkdir()
|
||||||
|
(projects / "Telegram Bot.md").write_text("aktiv", encoding="utf-8")
|
||||||
|
(projects / "Andere Idee.md").write_text("aktiv", encoding="utf-8")
|
||||||
|
ctx = collect_vault_context(tmp_path, today=date(2026, 6, 14))
|
||||||
|
assert "Telegram Bot" in ctx["projects"]
|
||||||
|
assert "Andere Idee" in ctx["projects"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_today_and_weekday(tmp_path):
|
||||||
|
ctx = collect_vault_context(tmp_path, today=date(2026, 6, 14))
|
||||||
|
assert ctx["today"] == "2026-06-14"
|
||||||
|
assert ctx["weekday"] == "Sonntag"
|
||||||
Loading…
Reference in New Issue