Answer architecture round 1: 3 🔴, 13 🟡; one items-table reader, a pure measuring core, shared git and round-title modules, and a hook rule for round counts
This commit is contained in:
+27
-24
@@ -4,9 +4,10 @@ import re
|
||||
from failure import Failure
|
||||
|
||||
EXEMPTIONS = ("", "decision", "defect", "principle", "question", "security")
|
||||
ITEMS_HEADER = "| ID | Release | Exempt | Item | R | S | C | A | G | Goals | Score |"
|
||||
# The items header before C joined it, which rank_todo.py asks to have C added.
|
||||
LEGACY_HEADER = "| ID | Release | Exempt | Item | R | S | A | G | Goals | Score |"
|
||||
COLUMNS = ("ID", "Release", "Exempt", "Item", "R", "S", "C", "A", "G", "Goals", "Score")
|
||||
ITEMS_HEADER = "| " + " | ".join(COLUMNS) + " |"
|
||||
# A table may lack C alone: git history keeps tables written before C joined, which still read.
|
||||
WITHOUT_C = "| " + " | ".join(column for column in COLUMNS if column != "C") + " |"
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
@@ -26,20 +27,22 @@ class Table:
|
||||
rows: list[str]
|
||||
|
||||
|
||||
def items(todo: str) -> list[Item] | Failure:
|
||||
"""Each row of a todo.md's items table, or of one from before C joined it."""
|
||||
def items(todo: str) -> list[Item] | None | Failure:
|
||||
"""Each row of a todo.md's items table, with C or without it; None where todo.md holds no items table."""
|
||||
lines = todo.splitlines()
|
||||
legacy = LEGACY_HEADER in lines
|
||||
table = table_rows(lines, LEGACY_HEADER if legacy else ITEMS_HEADER)
|
||||
headers = [header for header in (ITEMS_HEADER, WITHOUT_C) if header in lines]
|
||||
if not headers:
|
||||
return None
|
||||
table = table_rows(lines, headers[0])
|
||||
if isinstance(table, Failure):
|
||||
return table
|
||||
items = []
|
||||
found = []
|
||||
for row in table.rows:
|
||||
item = parse_row(row, legacy)
|
||||
item = parse_row(row, headers[0])
|
||||
if isinstance(item, Failure):
|
||||
return item
|
||||
items.append(item)
|
||||
return items
|
||||
found.append(item)
|
||||
return found
|
||||
|
||||
|
||||
def table_rows(lines: list[str], header: str) -> Table | Failure:
|
||||
@@ -59,14 +62,14 @@ def cells(row: str, count: int) -> list[str] | Failure:
|
||||
return found if len(found) == count else Failure(f"this row needs {count} cells: {row}")
|
||||
|
||||
|
||||
def parse_row(row: str, legacy: bool = False) -> Item | Failure:
|
||||
"""An item row, every cell checked but the goals it cites; a legacy row has no C."""
|
||||
found = cells(row, 10 if legacy else 11)
|
||||
def parse_row(row: str, header: str = ITEMS_HEADER) -> Item | Failure:
|
||||
"""An item row under header, every cell checked but the goals it cites."""
|
||||
names = header.strip("| ").split(" | ")
|
||||
found = cells(row, len(names))
|
||||
if isinstance(found, Failure):
|
||||
return found
|
||||
if legacy:
|
||||
found.insert(6, "")
|
||||
id_, release, exempt, tagline, r, s, c, a, g, goals, _ = found
|
||||
named = dict(zip(names, found))
|
||||
id_, release, exempt, tagline = named["ID"], named["Release"], named["Exempt"], named["Item"]
|
||||
if not re.fullmatch(r"[0-9]+", id_) or int(id_) < 1:
|
||||
return Failure(f"the ID {id_!r} is not a positive whole number: {row}")
|
||||
if not re.fullmatch(r"[0-9]+\.[0-9]+\.[0-9]+", release):
|
||||
@@ -75,13 +78,13 @@ def parse_row(row: str, legacy: bool = False) -> Item | Failure:
|
||||
return Failure(f"item {id_}'s Exempt {exempt!r} is not empty or one of {', '.join(EXEMPTIONS[1:])}")
|
||||
if not re.fullmatch(r"\*\*[^*].*[^*]\*\*", tagline):
|
||||
return Failure(f"item {id_}'s Item cell is not one bold tagline: {tagline}")
|
||||
scores = {}
|
||||
for name, value in zip("RSCAG", (r, s, c, a, g)):
|
||||
if legacy and name == "C":
|
||||
scores: dict[str, int] = {}
|
||||
for name in ("R", "S", "C", "A", "G"):
|
||||
if name not in named:
|
||||
continue
|
||||
if not re.fullmatch(r"[0-9]+", value) or not 1 <= int(value) <= 10:
|
||||
return Failure(f"item {id_}'s {name} {value!r} is not a whole number from 1 to 10")
|
||||
scores[name] = int(value)
|
||||
cited = tuple(goal.strip() for goal in goals.split(","))
|
||||
if not re.fullmatch(r"[0-9]+", named[name]) or not 1 <= int(named[name]) <= 10:
|
||||
return Failure(f"item {id_}'s {name} {named[name]!r} is not a whole number from 1 to 10")
|
||||
scores[name] = int(named[name])
|
||||
cited = tuple(goal.strip() for goal in named["Goals"].split(","))
|
||||
major, minor, patch = (int(part) for part in release.split("."))
|
||||
return Item(int(id_), (major, minor, patch), exempt, tagline, scores, cited)
|
||||
|
||||
Reference in New Issue
Block a user