chore: bootstrap rdb-discovery
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
import re
|
||||
|
||||
TASK_HEADING_RE = re.compile(r"^##\s+(TASK-\d+)\s+-\s+(.+)$", re.MULTILINE)
|
||||
STATUS_RE = re.compile(r"^Status:\s*(.+)$", re.MULTILINE)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Task:
|
||||
task_id: str
|
||||
title: str
|
||||
status: str
|
||||
body: str
|
||||
|
||||
|
||||
def read_tasks(root: Path) -> list[Task]:
|
||||
tasks_path = root / "TASKS.md"
|
||||
if not tasks_path.exists():
|
||||
return []
|
||||
|
||||
text = tasks_path.read_text(encoding="utf-8")
|
||||
matches = list(TASK_HEADING_RE.finditer(text))
|
||||
tasks: list[Task] = []
|
||||
|
||||
for index, match in enumerate(matches):
|
||||
start = match.start()
|
||||
end = matches[index + 1].start() if index + 1 < len(matches) else len(text)
|
||||
block = text[start:end]
|
||||
status_match = STATUS_RE.search(block)
|
||||
status = status_match.group(1).strip() if status_match else "Unknown"
|
||||
tasks.append(Task(match.group(1), match.group(2).strip(), status, block))
|
||||
|
||||
return tasks
|
||||
|
||||
|
||||
def get_next_task(root: Path) -> Task | None:
|
||||
for task in read_tasks(root):
|
||||
if task.status.lower() == "todo":
|
||||
return task
|
||||
return None
|
||||
|
||||
|
||||
def update_task_status(root: Path, task_id: str, new_status: str) -> bool:
|
||||
tasks_path = root / "TASKS.md"
|
||||
if not tasks_path.exists():
|
||||
return False
|
||||
|
||||
text = tasks_path.read_text(encoding="utf-8")
|
||||
matches = list(TASK_HEADING_RE.finditer(text))
|
||||
|
||||
for index, match in enumerate(matches):
|
||||
if match.group(1) != task_id:
|
||||
continue
|
||||
|
||||
start = match.start()
|
||||
end = matches[index + 1].start() if index + 1 < len(matches) else len(text)
|
||||
block = text[start:end]
|
||||
if STATUS_RE.search(block):
|
||||
new_block = STATUS_RE.sub(f"Status: {new_status}", block, count=1)
|
||||
else:
|
||||
lines = block.splitlines()
|
||||
lines.insert(1, f"Status: {new_status}")
|
||||
new_block = "\n".join(lines) + "\n"
|
||||
|
||||
tasks_path.write_text(text[:start] + new_block + text[end:], encoding="utf-8")
|
||||
return True
|
||||
|
||||
return False
|
||||
Reference in New Issue
Block a user