"""The touch-predictions record: one entry per chunk, its prediction beside what its diff touched.""" import re from failure import Failure from record_store.stores import RecordStore from touch_predictions.prediction import rendered, Touches from text_files.utc_stamp import STAMPED TITLE = "# Touch predictions" def entry(items: list[int], at: str, head: str, predicted: Touches | None, touched: Touches) -> str: """A chunk's entry: the items it took, the UTC time at, the short commit its diff ends at, and both lists in a prediction's spelling. predicted is None where no item predicted anything.""" return f"## Chunk {', '.join(map(str, items))}, {at}, at {head}\n\n{listed('Predicted', predicted)}\n\n{listed('Touched', touched)}\n" def listed(label: str, touches: Touches | None) -> str: return f"{label}:\n\n{rendered(touches)}" if touches else f"{label}: none." def append(store: RecordStore, items: list[int], text: str) -> Failure | None: """Appends text, the entry of the chunk that took items, in place of each entry the record holds for the same items: a fix after the first entry changes what the chunk touched.""" held = store.held() if isinstance(held, Failure): return held kept = re.sub(rf"^## Chunk {', '.join(map(str, items))}, {STAMPED},.*?(?=^## |\Z)", "", held.text, flags=re.M | re.S) written = store.write(f"{kept.rstrip() or TITLE}\n\n{text}", held.version) return written if isinstance(written, Failure) else None