import dataclasses import decimal import re from failure import Failure ARCHITECT = "Inherited architect" DIMENSIONS = ("Navigation", "Locality", "Shape", "Self-sufficiency", "Overall") JUNIOR = "Junior A" # A scoring run moved from a `comprehension floor:` decision entry kept only the seats' means. MEAN = "Mean" MID = "Mid A" SCORES = re.compile(", ".join(rf"{name} ([0-9]+(?:\.[0-9]+)?)" for name in DIMENSIONS)) SENIOR = "Maintainability senior" FIRST_SEATS = (MID, ARCHITECT) SCORING_SEATS = (JUNIOR, MID, SENIOR, ARCHITECT) SEAT_LINE = re.compile(rf"^({'|'.join(SCORING_SEATS)}): (.+)$") SPLIT_SEATS = (SENIOR, JUNIOR) @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, ` at `, with `, against ` for a paired ruling, then each seat's line and its places, or, for a run moved from a decision entry, its Mean line alone.""" read: str seats: list[Seat] moved_mean: str | None 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: at ") seats: list[Seat] = [] moved_mean = None for line in lines[1:]: if line.startswith(f"{MEAN}: ") and not seats and moved_mean is None: moved_mean = line[len(MEAN) + 2:].strip() elif moved_mean is not None: return Failure(f"a {MEAN} line stands alone in its seats file: {line}") elif 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, opening with one of {', '.join(SCORING_SEATS)} and a colon, nor one of its places: {line}") return Report(lines[0].strip(), seats, moved_mean) 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, or a {MEAN} line alone; the file has {', '.join(seat.name for seat in seats) or 'none'}") found = {} for seat in seats: values = score_line(seat.name, seat.said) if isinstance(values, Failure): return values found[seat.name] = values return found def score_line(name: str, said: str) -> list[str] | Failure: """A line's five scores as written, each from 1 to 10 and the overall at most 1 above the lowest dimension.""" if not (written := SCORES.fullmatch(said)): return Failure(f"{name}'s line is not {', '.join(score + ' ' for score in DIMENSIONS)}: {said}") values = [decimal.Decimal(value) for value in written.groups()] if not all(1 <= value <= 10 for value in values): return Failure(f"{name}'s scores are not each from 1 to 10: {said}") if values[-1] > min(values[:-1]) + 1: return Failure(f"{name}'s overall exceeds the lowest dimension by more than 1; ask the seat to rescore: {said}") return list(written.groups())