44 lines
2.0 KiB
Python
44 lines
2.0 KiB
Python
import re
|
|
|
|
from plan.todo_table import cells, LEVELS, parse_ids
|
|
from failure import Failure
|
|
from score_outcomes.results import render, Result
|
|
|
|
ANCHORS_PATH = "docs/score-anchors.md"
|
|
# A level, such as `L 1`; every scale tops out below 10.
|
|
LEVEL = re.compile(r"([A-Z]) ([1-9])")
|
|
# Each level the anchors table names, such as `L 1`, with its items' IDs, in the file's order.
|
|
Anchors = list[tuple[str, tuple[int, ...]]]
|
|
|
|
|
|
def parse_anchors(text: str) -> Anchors | Failure:
|
|
found: Anchors = []
|
|
for row in (line.strip() for line in text.splitlines() if line.strip().startswith("|")):
|
|
pair = cells(row, 2)
|
|
if isinstance(pair, Failure):
|
|
return Failure(f"{ANCHORS_PATH}: {pair.reason}")
|
|
if pair == ["Level", "Items"] or all(re.fullmatch(r":?-+:?", cell) for cell in pair):
|
|
continue
|
|
level, ids = LEVEL.fullmatch(pair[0]), parse_ids(pair[1])
|
|
if not level or int(level[2]) > LEVELS.get(level[1], 0) or not ids:
|
|
scales = ", ".join(f"{name} 1 to {top}" for name, top in LEVELS.items())
|
|
return Failure(f"cannot read the row {row} in {ANCHORS_PATH}; each row names a level, {scales}, and the IDs of shipped items, such as | L 1 | 12, 14 |")
|
|
if pair[0] in dict(found):
|
|
return Failure(f"{ANCHORS_PATH} names {pair[0]} twice")
|
|
found.append((pair[0], ids))
|
|
return found
|
|
|
|
|
|
def anchor_lines(found: Anchors | Failure, results: list[Result]) -> list[str]:
|
|
"""Each level, then each of its anchors with what its chunk measured. Where the file cannot be read, one line saying why; where it names no level, one line saying how to choose anchors."""
|
|
if isinstance(found, Failure):
|
|
return [f"- None: {found.reason}"]
|
|
if not found:
|
|
return [f"- None: the project has no {ANCHORS_PATH}, or it names no level; ~/.claude/rules/todo-items.md says how to choose anchors."]
|
|
by_id = {result.item.id: result for result in results}
|
|
lines = []
|
|
for level, ids in found:
|
|
lines.append(f"{level}:")
|
|
lines += [render(by_id[id_]) if id_ in by_id else f"- Item {id_}: no chunk shipped it." for id_ in ids]
|
|
return lines
|