import pathlib import time from larv_review.agreement import agreement_report from comprehension_history import appended, DIMENSIONS, FLOOR, heading, Heading, HISTORY, latest_places, recorded, ruling_entry, scoring_entry, ScoringRun, vote_text from git_repo import commit_exists, top from larv_review.ruling import counted, overall, ruling, split, surplus, votes from failure import Failure from larv_review.seats import FIRST_SEATS, scores, seats_file, SeatsFile from larv_review.scoring import means, under_floor from larv_review.plan import plan_reasons import rank_todo from file_writes import replace_file def record_ruling(votes_file: pathlib.Path) -> Failure | None: read = read_seats(votes_file, paired=True) if isinstance(read, Failure): return read heading_read, seats_read = read written = votes(seats_read.seats) if isinstance(written, Failure): return written counts = counted(seats_read.seats, written) overall_votes = [overall(each) for each in counts.values()] if surplus(overall_votes): return Failure(f"{' and '.join(FIRST_SEATS)} both count as {overall_votes[0]} overall, which settles the ruling; drop the other seats' lines") tally = ", ".join(f"{seat.name} {vote_text(DIMENSIONS[-1], overall(written[seat.name]), overall(counts[seat.name]))}" for seat in seats_read.seats) if split(overall_votes): print(f"Split: {tally}. Launch the maintainability senior and Junior A together, add their lines to the votes file, and run this again.") return None ruled = ruling(overall_votes) print(f"Ruling: {ruled}. Overall votes as counted: {tally}.") return append(seats_read.read, ruling_entry(heading_read.title, seats_read.seats, written, counts, ruled)) def record_scoring(default: str, scores_file: pathlib.Path, ids: list[int]) -> Failure | None: read = read_seats(scores_file, paired=False) if isinstance(read, Failure): return read named, given = read scoring = scored_seats(given) if isinstance(scoring, Failure): return scoring reasons = plan_reasons(default, ids) if isinstance(reasons, Failure): return reasons if not reasons: return Failure("no plan item or release calls this scoring run; name the item IDs the chunk ships, as needs-scoring-run took them") print(f"Means: {', '.join(f'{name} {value}' for name, value in zip(DIMENSIONS, scoring.means))}.") if under := under_floor(scoring.means): print(f"Floor: {', '.join(under)} under the floor of {FLOOR}.") if unrecorded := append(given.read, scoring_entry(named.title, scoring)): return unrecorded return rerank() def rerank() -> Failure | None: """Rank the checkout's todo.md again as rank_todo.py does, since the overall sets code health's weight, and fail where rank_todo.py would. The run stays recorded whatever happens, so those failures say to settle todo.md and run rank_todo.py on it, not to record the run again.""" checkout = top() if isinstance(checkout, Failure): return checkout todo = pathlib.Path(checkout, "todo.md") ranked = rank_todo.rank_file(todo) if isinstance(ranked, Failure): return Failure(f"recorded the run, but rank_todo.py cannot rank {todo}: {ranked.reason}. Fix what that names, then run rank_todo.py on {todo}, not record-scoring again") if ranked.under_bar: return Failure(f"recorded the run and ranked {todo}, but the lines above name items now under the bar. Settle each as its line says, then run rank_todo.py on {todo}, not record-scoring again") return None def scored_seats(given: SeatsFile) -> ScoringRun | Failure: found = scores(given.seats) if isinstance(found, Failure): return found return ScoringRun(given.seats, found, means(list(found.values()))) def read_seats(path: pathlib.Path, paired: bool) -> tuple[Heading, SeatsFile] | Failure: """The seats file and the heading its record goes under. Fails when the file names a commit the checkout lacks, or when the history already holds an entry on what it reads; it runs before anything is printed.""" try: given = seats_file(path.read_text(encoding="utf-8")) except (OSError, UnicodeDecodeError) as failure: return Failure(f"could not read {path}: {failure}") if isinstance(given, Failure): return given named = heading(time.time(), given.read, paired) if isinstance(named, Failure): return named if unknown := [commit for commit in (named.commit, named.against) if commit and not commit_exists(commit)]: return Failure(f"the seats file names {', '.join(unknown)}, which is no commit in this checkout") history = history_file() if isinstance(history, Failure): return history if recorded(history[1], given.read): return Failure(f"{history[0]} already holds an entry on {given.read}; record each ruling or run once") return named, given def show_places() -> Failure | None: history = history_file() if isinstance(history, Failure): return history print((latest_places(history[1]) or "Seats found no places hard.").rstrip("\n")) return None def show_agreement() -> Failure | None: history = history_file() if isinstance(history, Failure): return history print(agreement_report(history[1])) return None def history_file() -> tuple[pathlib.Path, str] | Failure: """The checkout's history and its text, empty when the file is missing.""" checkout = top() if isinstance(checkout, Failure): return checkout history = pathlib.Path(checkout, HISTORY) try: return history, history.read_text(encoding="utf-8") if history.exists() else "" except (OSError, UnicodeDecodeError) as failure: return Failure(f"could not read {history}: {failure}") def append(read: str, entry: str) -> Failure | None: """Append entry unless the history already holds one on what the seats read.""" read_history = history_file() if isinstance(read_history, Failure): return read_history history, text = read_history if recorded(text, read): return Failure(f"{history} already holds an entry on {read}; record each ruling or run once") try: history.parent.mkdir(parents=True, exist_ok=True) except OSError as failure: return Failure(f"could not create {history.parent}: {failure}") if unwritten := replace_file(history, appended(text, entry).encode()): return Failure(f"could not write {history}: {unwritten.reason}") print(f"Appended to {history}.") return None