"""todo.md on disk, for the tools that work on the markdown file itself: rank or check it with the texts its FilePlan reads, write it back, and give the lines ranking reports.""" import pathlib from failure import Failure from plan_store.stores import file_plan from project_config.settings import ProjectConfig from release_plan.ranking import rank, Ranked from text_files.file_writes import replace_file def rank_file(todo: pathlib.Path, config: ProjectConfig) -> Ranked | Failure: """Rank todo in place: rank the texts its FilePlan reads, and write todo where that changed it. The caller prints `report_lines`.""" texts = file_plan(config, todo).texts() if isinstance(texts, Failure): return texts ranked = rank(*texts) if isinstance(ranked, Failure): return ranked if ranked.text != texts[0] and (unwritten := write(todo, ranked.text)): return unwritten return ranked def report_lines(todo: pathlib.Path, ranked: Ranked) -> list[str]: """Ranking's notes and under-bar lines, each prefixed with todo's path, as a tool prints them on stderr.""" return [f"{todo}: {line}" for line in ranked.notes + ranked.under_bar] def check_failures(todo: pathlib.Path, config: ProjectConfig) -> list[str]: """Why --check fails on todo, empty when it passes.""" texts = file_plan(config, todo).texts() if isinstance(texts, Failure): return [texts.reason] ranked = rank(*texts) if isinstance(ranked, Failure): return [ranked.reason] unranked = [] if ranked.text == texts[0] else [f"{todo}: not ranked; run rank_todo.py on it"] return unranked + [f"{todo}: {line}" for line in ranked.under_bar] def write(path: pathlib.Path, text: str) -> Failure | None: unwritten = replace_file(path.resolve(), text.encode("utf-8")) return Failure(f"{path}: {unwritten.reason}") if unwritten else None