98 lines
3.7 KiB
Python
98 lines
3.7 KiB
Python
import dataclasses
|
|
import re
|
|
|
|
from failure import Failure
|
|
|
|
# Each of R, S, C, A and G runs from 1 to TOP_SCORE.
|
|
TOP_SCORE = 10
|
|
EXEMPTIONS = ("", "decision", "defect", "principle", "question", "security")
|
|
COLUMNS = ("ID", "Release", "Exempt", "Item", "R", "S", "C", "A", "G", "Goals", "Score")
|
|
ITEMS_HEADER = "| " + " | ".join(COLUMNS) + " |"
|
|
# Tables in git history from before C joined lack it; items() still parses them, though rank_todo refuses one in a checkout.
|
|
WITHOUT_C = "| " + " | ".join(column for column in COLUMNS if column != "C") + " |"
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Item:
|
|
id: int
|
|
release: tuple[int, int, int]
|
|
exempt: str
|
|
tagline: str
|
|
scores: dict[str, int]
|
|
goals: tuple[str, ...]
|
|
|
|
@property
|
|
def risk_and_complexity(self) -> int:
|
|
"""R + C, which routes the item's chunk to a rung; 0 without a C."""
|
|
return self.scores["R"] + self.scores["C"] if "C" in self.scores else 0
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Table:
|
|
"""Body rows of a markdown table, and the index of the first row in the file's lines."""
|
|
start: int
|
|
rows: list[str]
|
|
|
|
|
|
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()
|
|
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
|
|
found = []
|
|
for row in table.rows:
|
|
item = parse_row(row, headers[0])
|
|
if isinstance(item, Failure):
|
|
return item
|
|
found.append(item)
|
|
return found
|
|
|
|
|
|
def table_rows(lines: list[str], header: str) -> Table | Failure:
|
|
if lines.count(header) != 1:
|
|
return Failure(f"todo.md needs the table header {header} once")
|
|
start = lines.index(header) + 2
|
|
if start > len(lines) or not re.fullmatch(r"\|(-+\|)+", lines[start - 1].replace(" ", "")):
|
|
return Failure(f"the table under {header} has no |---| divider line")
|
|
end = start
|
|
while end < len(lines) and lines[end].startswith("|"):
|
|
end += 1
|
|
return Table(start, lines[start:end])
|
|
|
|
|
|
def cells(row: str, count: int) -> list[str] | Failure:
|
|
found = [cell.strip() for cell in re.split(r"(?<!\\)\|", row.strip())[1:-1]]
|
|
return found if len(found) == count else Failure(f"this row needs {count} cells: {row}")
|
|
|
|
|
|
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
|
|
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):
|
|
return Failure(f"item {id_}'s release {release!r} is not a semver version")
|
|
if exempt not in EXEMPTIONS:
|
|
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: dict[str, int] = {}
|
|
for name in ("R", "S", "C", "A", "G"):
|
|
if name not in named:
|
|
continue
|
|
if not re.fullmatch(r"[0-9]+", named[name]) or not 1 <= int(named[name]) <= TOP_SCORE:
|
|
return Failure(f"item {id_}'s {name} {named[name]!r} is not a whole number from 1 to {TOP_SCORE}")
|
|
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)
|