40 lines
1.8 KiB
Python
40 lines
1.8 KiB
Python
import re
|
|
|
|
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]+\|")
|
|
|
|
|
|
def 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):
|
|
continue
|
|
matched = ROW.fullmatch(row)
|
|
if not matched or not 1 <= int(matched[2]) <= LEVELS.get(matched[1], 0):
|
|
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 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."""
|
|
by_id = {outcome.item.id: outcome for outcome in outcomes}
|
|
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 merge commit on the default branch shipped it." for id_ in ids]
|
|
return lines
|