74 lines
2.6 KiB
Python
74 lines
2.6 KiB
Python
import dataclasses
|
|
import os
|
|
import re
|
|
|
|
from markdown_sections import sections
|
|
|
|
# The format: skills/larv-review/SKILL.md §3 → "History", which names this reader.
|
|
RUN = re.compile(r"^(.+), (.+) at ([0-9a-f]{7,40})$")
|
|
SEAT_PLACES = re.compile(r"^.+, (hardest first|would restructure first):$")
|
|
# `path:line` or `:line`, then the unit's name where one follows.
|
|
PLACE = re.compile(r"`([^`\s]*):\d+`(?: `(\w+)`)?")
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Place:
|
|
path: str
|
|
name: str | None
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class ScoringRun:
|
|
heading: str
|
|
commit: str
|
|
places: list[Place]
|
|
|
|
|
|
def scoring_runs(history: str) -> list[ScoringRun]:
|
|
"""Each scoring run in a comprehension history, with the places its seats named, as they wrote them; paired rulings name what changed, not what is hard, and are left out."""
|
|
runs = []
|
|
for heading, bodies in sections(history).items():
|
|
if (named := RUN.match(heading)) and ", against " not in heading:
|
|
runs.append(ScoringRun(heading, named.group(3), [place for body in bodies for place in seat_places(body)]))
|
|
return runs
|
|
|
|
|
|
def seat_places(body: str) -> list[Place]:
|
|
places = []
|
|
listing = False
|
|
for line in body.splitlines():
|
|
if SEAT_PLACES.match(line):
|
|
listing = True
|
|
elif listing and line.startswith("- "):
|
|
places += bullet_places(line)
|
|
elif not line.startswith(" "):
|
|
listing = False
|
|
return places
|
|
|
|
|
|
def bullet_places(bullet: str) -> list[Place]:
|
|
"""A `:line` with no path is in the file named before it in the bullet."""
|
|
places = []
|
|
path = ""
|
|
for match in PLACE.finditer(bullet):
|
|
path = match.group(1) or path
|
|
if path:
|
|
places.append(Place(path, match.group(2)))
|
|
return places
|
|
|
|
|
|
def resolved(places: list[Place], tracked: list[str]) -> tuple[list[Place], list[Place]]:
|
|
"""Entries written before paths were written from the repository's root shorten a path once they have named its folder: `cycle/run.py` after `bin/loop_claude/cycle/states.py`. Each place resolves to the one tracked file it names, or, where several match, the one in the folder of the place resolved before it; a place naming no file, or several, is dropped. Returns the resolved places and the dropped ones."""
|
|
found = []
|
|
dropped = []
|
|
before = ""
|
|
for place in places:
|
|
candidates = [place.path] if place.path in tracked else [path for path in tracked if path.endswith("/" + place.path)]
|
|
nearest = [path for path in candidates if path.startswith(os.path.dirname(before) + "/")] if len(candidates) > 1 else candidates
|
|
if len(nearest) == 1:
|
|
before = nearest[0]
|
|
found.append(Place(before, place.name))
|
|
else:
|
|
dropped.append(place)
|
|
return found, dropped
|