184 lines
6.9 KiB
Python
184 lines
6.9 KiB
Python
import dataclasses
|
|
import decimal
|
|
import re
|
|
|
|
from failure import Failure
|
|
from markdown_text import sections
|
|
from utc_stamp import utc
|
|
|
|
ARCHITECT = "Inherited architect"
|
|
DECIDED_BY = "decided by"
|
|
DIMENSIONS = ("Navigation", "Locality", "Shape", "Self-sufficiency", "Overall")
|
|
HARDEST_FIRST = "hardest first"
|
|
HEADING = re.compile(r"^([^,]+), (.+) at ([0-9a-f]{7,40})(?:, against ([0-9a-f]{7,40}))?$")
|
|
HISTORY = "docs/comprehension-history.md"
|
|
JUNIOR = "Junior A"
|
|
MEAN = "Mean"
|
|
MID = "Mid A"
|
|
# `path:line` or `:line`, then the unit's name where one follows.
|
|
PLACE = re.compile(r"`([^`\s]*):\d+`(?: `(\w+)`)?")
|
|
RESTRUCTURE_FIRST = "would restructure first"
|
|
RULING = re.compile(r"^Ruling: (better|same|worse)$", re.M)
|
|
RULED_PLACES = re.compile(rf"^.+, {DECIDED_BY}:$")
|
|
SCORED_PLACES = re.compile(rf"^.+, ({HARDEST_FIRST}|{RESTRUCTURE_FIRST}):$")
|
|
SENIOR = "Maintainability senior"
|
|
TITLE = "# Comprehension history\n"
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Seat:
|
|
name: str
|
|
said: str
|
|
places: list[str]
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Place:
|
|
path: str
|
|
name: str | None
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Entry:
|
|
"""One paired ruling, which names the commit it was against and its ruling, or one scoring run, which names neither. A scoring run's places, or those of the scores a fall called into a ruling, are scored_places."""
|
|
heading: str
|
|
time: str
|
|
commit: str
|
|
against: str | None
|
|
ruling: str | None
|
|
ruled_places: list[Place]
|
|
scored_places: list[Place]
|
|
|
|
@property
|
|
def paired(self) -> bool:
|
|
return self.against is not None
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class HistoryChange:
|
|
"""A merged change on the default branch that touched the history, and the history before and after it."""
|
|
commit: str
|
|
before: str
|
|
after: str
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Heading:
|
|
title: str
|
|
label: str
|
|
commit: str
|
|
against: str | None
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class ScoringRun:
|
|
"""The four scoring seats' lines, their scores as written and the means, in DIMENSIONS' order."""
|
|
seats: list[Seat]
|
|
scores: dict[str, list[str]]
|
|
means: list[decimal.Decimal]
|
|
|
|
|
|
# A paired ruling as entry_text writes it. A scoring run's heading has no `, against`; it has no Ruling line, ends its table with a Mean row, and labels each seat's places `<seat>, hardest first:` or, for the architect, `<seat>, would restructure first:`.
|
|
#
|
|
# ## 2026-10-09T09:47:25Z, PR #232 at 324d650, against 12d6d75
|
|
#
|
|
# Ruling: same
|
|
#
|
|
# | Seat | Navigation | Locality | Shape | Self-sufficiency | Overall |
|
|
# |---|---|---|---|---|---|
|
|
# | Mid A | better | worse | same | same | better |
|
|
#
|
|
# Mid A, decided by:
|
|
#
|
|
# - Locality: `bin/x.py:12` `run`: three branches to hold at once
|
|
def entries(history: str) -> list[Entry]:
|
|
"""Each paired ruling and scoring run in a comprehension history, in the order the file holds them; a section whose heading names no commit is left out."""
|
|
found = []
|
|
for heading, bodies in sections(history).items():
|
|
if named := HEADING.match(heading):
|
|
body = "\n".join(bodies)
|
|
ruling = RULING.search(body)
|
|
found.append(Entry(heading, named.group(1), named.group(3), named.group(4), ruling.group(1) if ruling else None, seat_places(body, RULED_PLACES), seat_places(body, SCORED_PLACES)))
|
|
return found
|
|
|
|
|
|
def recorded(history: str, read: str) -> bool:
|
|
"""Whether the history holds an entry on what a seats file's first line says the seats read."""
|
|
return any(entry.heading.partition(", ")[2] == read for entry in entries(history))
|
|
|
|
|
|
def seat_places(body: str, labels: re.Pattern[str]) -> list[Place]:
|
|
"""The places under each seat label labels matches."""
|
|
places = []
|
|
listing = False
|
|
for line in body.splitlines():
|
|
if labels.match(line):
|
|
listing = True
|
|
elif listing and line.startswith("- "):
|
|
places += bullet_places(line)
|
|
elif not line.startswith(" "):
|
|
listing = False
|
|
return places
|
|
|
|
|
|
def bullet_places(bullet: str) -> list[Place]:
|
|
"""A `:line` with no path is in the file named before it in the bullet."""
|
|
places = []
|
|
path = ""
|
|
for match in PLACE.finditer(bullet):
|
|
path = match.group(1) or path
|
|
if path:
|
|
places.append(Place(path, match.group(2)))
|
|
return places
|
|
|
|
|
|
def heading(at: float, read: str, paired: bool) -> Heading | Failure:
|
|
"""An entry's heading, matched by HEADING: when the seats finished, in UTC, and what they read."""
|
|
title = f"{utc(at)}, {read}"
|
|
if (named := HEADING.match(title)) and bool(named.group(4)) == paired:
|
|
return Heading(title, named.group(2), named.group(3), named.group(4))
|
|
return Failure(f"the seats file's first line is not `<PR or branch> at <commit>{', against <base>' if paired else ''}`: {read}")
|
|
|
|
|
|
def appended(history: str, entry: str) -> str:
|
|
"""The history with entry added last; an empty history gets the file's title first."""
|
|
text = history.rstrip("\n") + "\n" if history.strip() else TITLE
|
|
return f"{text}\n{entry}"
|
|
|
|
|
|
def ruling_entry(title: str, seats: list[Seat], said: dict[str, list[str]], counted: dict[str, list[str]], ruled: str, notes: list[str], scoring: ScoringRun | None) -> str:
|
|
"""A paired ruling's entry, with any notes, such as why a fall decided it, and the scores that decided a fall; a seat's vote that counted as same for want of a place says so."""
|
|
votes = [[seat.name, *map(vote_text, DIMENSIONS, said[seat.name], counted[seat.name])] for seat in seats]
|
|
tables = [[["Seat", *DIMENSIONS], *votes]] + ([scores_table(scoring)] if scoring else [])
|
|
labels = [(f"{seat.name}, {DECIDED_BY}:", seat.places) for seat in seats] + (scoring_labels(scoring.seats) if scoring else [])
|
|
return entry_text(title, [f"Ruling: {ruled}", *notes], tables, labels)
|
|
|
|
|
|
def vote_text(dimension: str, said: str, counted: str) -> str:
|
|
if counted == said:
|
|
return counted
|
|
return f"same (voted {said}, naming no place)" if dimension == DIMENSIONS[-1] else f"same (voted {said}, no place naming {dimension})"
|
|
|
|
|
|
def scoring_entry(title: str, scoring: ScoringRun) -> str:
|
|
return entry_text(title, [], [scores_table(scoring)], scoring_labels(scoring.seats))
|
|
|
|
|
|
def scores_table(scoring: ScoringRun) -> list[list[str]]:
|
|
return [["Seat", *DIMENSIONS], *([seat, *values] for seat, values in scoring.scores.items()), [MEAN, *map(str, scoring.means)]]
|
|
|
|
|
|
def scoring_labels(seats: list[Seat]) -> list[tuple[str, list[str]]]:
|
|
return [(f"{seat.name}, {RESTRUCTURE_FIRST if seat.name == ARCHITECT else HARDEST_FIRST}:", seat.places) for seat in seats]
|
|
|
|
|
|
def entry_text(title: str, lines: list[str], tables: list[list[list[str]]], seats: list[tuple[str, list[str]]]) -> str:
|
|
"""The heading, any lines such as `Ruling: same`, each table, whose first row is its header, and under each seat's label its places, one bullet each."""
|
|
parts = [f"## {title}", *lines, *("\n".join([row_text(header), "|" + "---|" * len(header), *map(row_text, rows)]) for header, *rows in tables)]
|
|
parts += [f"{label}\n\n" + "\n".join(f"- {place}" for place in places) for label, places in seats if places]
|
|
return "\n\n".join(parts) + "\n"
|
|
|
|
|
|
def row_text(cells: list[str]) -> str:
|
|
return "| " + " | ".join(cells) + " |"
|