Answer architecture round 1: 1 🔴, 1 🟡; build the report text in a pure function, and parse anchor rows and IDs with todo_table's parsers
This commit is contained in:
@@ -1,36 +1,38 @@
|
||||
import re
|
||||
|
||||
from todo_table import cells, LEVELS, parse_ids
|
||||
from failure import Failure
|
||||
from todo_table import LEVELS
|
||||
from score_outcomes.outcomes import Outcome, render
|
||||
|
||||
# Where a project names the shipped items that show each level of L, E and I.
|
||||
ANCHORS = "docs/score-anchors.md"
|
||||
# One row of its table: a level, such as `L 1`, and the IDs of the items that show it.
|
||||
ROW = re.compile(r"\|\s*([A-Z]) ([0-9]+)\s*\|\s*([0-9]+(?:\s*,\s*[0-9]+)*)\s*\|")
|
||||
# The table's header and separator rows.
|
||||
FRAME = re.compile(r"\|\s*Level\s*\|\s*Items\s*\||\|[-|\s]+\|")
|
||||
ANCHORS_PATH = "docs/score-anchors.md"
|
||||
# A level, such as `L 1`.
|
||||
LEVEL = re.compile(r"([A-Z]) ([0-9]+)")
|
||||
|
||||
|
||||
def anchors(text: str) -> list[tuple[str, tuple[int, ...]]] | Failure:
|
||||
def parse_anchors(text: str) -> list[tuple[str, tuple[int, ...]]] | Failure:
|
||||
"""Each level the anchors table names, such as `L 1`, with its items' IDs, in the file's order."""
|
||||
found: list[tuple[str, tuple[int, ...]]] = []
|
||||
for row in (line.strip() for line in text.splitlines() if line.strip().startswith("|")):
|
||||
if FRAME.fullmatch(row):
|
||||
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
|
||||
matched = ROW.fullmatch(row)
|
||||
if not matched or not 1 <= int(matched[2]) <= LEVELS.get(matched[1], 0):
|
||||
level, ids = LEVEL.fullmatch(pair[0]), parse_ids(pair[1])
|
||||
if not level or not 1 <= 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"{ANCHORS} cannot read the row {row}; each row names a level, {scales}, and the IDs of shipped items, such as | L 1 | 12, 14 |")
|
||||
level = f"{matched[1]} {matched[2]}"
|
||||
if level in dict(found):
|
||||
return Failure(f"{ANCHORS} names {level} twice")
|
||||
found.append((level, tuple(int(id_) for id_ in matched[3].split(","))))
|
||||
return Failure(f"{ANCHORS_PATH} cannot read the row {row}; 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: list[tuple[str, tuple[int, ...]]], outcomes: list[Outcome]) -> list[str]:
|
||||
"""Each level, then each of its anchors with what its chunk measured."""
|
||||
"""Each level, then each of its anchors with what its chunk measured; where the project names no level, where to name them."""
|
||||
if not found:
|
||||
return [f"- None: the project's {ANCHORS_PATH} names no level; ~/.claude/rules/todo-items.md says how to choose them."]
|
||||
by_id = {outcome.item.id: outcome for outcome in outcomes}
|
||||
lines = []
|
||||
for level, ids in found:
|
||||
|
||||
Reference in New Issue
Block a user