83 lines
3.8 KiB
Python
83 lines
3.8 KiB
Python
import dataclasses
|
|
import re
|
|
|
|
from failure import Failure
|
|
|
|
DIMENSIONS = ("Navigation", "Locality", "Shape", "Self-sufficiency", "Overall")
|
|
FIRST_SEATS = ("Mid A", "Inherited architect")
|
|
SPLIT_SEATS = ("Maintainability senior", "Junior A")
|
|
SCORING_SEATS = ("Junior A", "Mid A", "Maintainability senior", "Inherited architect")
|
|
SEAT_LINE = re.compile(r"^(Junior A|Mid A|Maintainability senior|Inherited architect): (.+)$")
|
|
SCORES = re.compile(", ".join(rf"{name} ([0-9]+(?:\.[0-9]+)?)" for name in DIMENSIONS))
|
|
VOTES = ("better", "same", "worse")
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Seat:
|
|
name: str
|
|
said: str
|
|
places: list[str]
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Report:
|
|
"""A seats file: what the seats read, `<PR or branch> at <commit>`, with `, against <base>` for a paired ruling, then each seat's line and its places."""
|
|
read: str
|
|
seats: list[Seat]
|
|
|
|
|
|
def report(text: str) -> Report | Failure:
|
|
lines = [line for line in text.splitlines() if line.strip()]
|
|
if not lines:
|
|
return Failure("the seats file is empty; its first line names what the seats read: <PR or branch> at <commit>")
|
|
seats: list[Seat] = []
|
|
for line in lines[1:]:
|
|
if seat := SEAT_LINE.match(line):
|
|
if seat.group(1) in [known.name for known in seats]:
|
|
return Failure(f"{seat.group(1)} appears twice in the seats file")
|
|
seats.append(Seat(seat.group(1), seat.group(2).strip(), []))
|
|
elif seats and line.startswith("- "):
|
|
seats[-1].places.append(line[2:].strip())
|
|
elif seats and seats[-1].places and line.startswith(" "):
|
|
seats[-1].places[-1] += " " + line.strip()
|
|
else:
|
|
return Failure(f"the seats file has a line that is neither a seat's line nor one of its places: {line}")
|
|
return Report(lines[0].strip(), seats)
|
|
|
|
|
|
def votes(seats: list[Seat]) -> dict[str, str] | Failure:
|
|
"""Each seat's vote as it counts: a better or worse vote naming no `path:line` place counts as same."""
|
|
names = [seat.name for seat in seats]
|
|
if names[:2] != list(FIRST_SEATS) or names[2:] not in ([], list(SPLIT_SEATS)):
|
|
return Failure(f"a paired ruling's seats are {' and '.join(FIRST_SEATS)}, then, when their votes differ, {' and '.join(SPLIT_SEATS)}, in that order; the file has {', '.join(names) or 'none'}")
|
|
if unknown := [seat for seat in seats if seat.said not in VOTES]:
|
|
return Failure(f"{unknown[0].name}'s vote {unknown[0].said!r} is not better, same or worse")
|
|
return {seat.name: seat.said if seat.said == "same" or any(re.search(r"`[^`\s]*:\d+`", place) for place in seat.places) else "same" for seat in seats}
|
|
|
|
|
|
def ruling(counted: list[str]) -> str | None:
|
|
"""The vote that leads each other vote by two or more, else same; None when only the first two seats voted and they differ, so two more must vote."""
|
|
if len(counted) == 2 and counted[0] != counted[1]:
|
|
return None
|
|
for vote in VOTES:
|
|
if all(counted.count(vote) >= counted.count(other) + 2 for other in VOTES if other != vote):
|
|
return vote
|
|
return "same"
|
|
|
|
|
|
def scores(seats: list[Seat]) -> dict[str, list[str]] | Failure:
|
|
"""Each seat's five scores as written, in DIMENSIONS' order."""
|
|
if [seat.name for seat in seats] != list(SCORING_SEATS):
|
|
return Failure(f"a scoring run's seats are {', '.join(SCORING_SEATS)}, in that order; the file has {', '.join(seat.name for seat in seats) or 'none'}")
|
|
found = {}
|
|
for seat in seats:
|
|
if not (written := SCORES.fullmatch(seat.said)):
|
|
return Failure(f"{seat.name}'s line is not {', '.join(name + ' <score>' for name in DIMENSIONS)}: {seat.said}")
|
|
values = [float(value) for value in written.groups()]
|
|
if not all(1 <= value <= 10 for value in values):
|
|
return Failure(f"{seat.name}'s scores are not each from 1 to 10: {seat.said}")
|
|
if values[-1] > min(values[:-1]) + 1:
|
|
return Failure(f"{seat.name}'s overall exceeds their lowest dimension by more than 1; ask the seat to rescore: {seat.said}")
|
|
found[seat.name] = list(written.groups())
|
|
return found
|