46 lines
2.0 KiB
Python
46 lines
2.0 KiB
Python
"""Which of a release's items may start chunks beside the next chunk, by what each is predicted to touch."""
|
|
import dataclasses
|
|
|
|
from failure import Failure
|
|
from release_plan.todo_table import Item, waiting
|
|
from touch_predictions.prediction import LABEL, overlaps, predicted, Touches
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Beside:
|
|
"""The IDs of the items that may start chunks beside first's, in the plan's order, and why each other item may not."""
|
|
picked: list[int]
|
|
refused: list[str]
|
|
|
|
|
|
def chunks_beside(found: list[Item], first: Item) -> Beside | str | None:
|
|
"""The items of first's release that don't wait and overlap neither first nor an item picked before them, taken in the plan's order.
|
|
|
|
The plan's order decides, not the largest set: a larger set could start a low-ranked item in place of one ranked above it. An item first's chunk takes starts no chunk of its own. Else why no item may, as a sentence's end; None where no such item's detail holds a prediction, as in a project that writes none."""
|
|
waits = waiting(found, set())
|
|
ready = [item for item in found if item.release == first.release and item.id not in waits]
|
|
if not any(LABEL in item.detail.splitlines() for item in ready):
|
|
return None
|
|
ours = predicted(first.detail)
|
|
if not isinstance(ours, dict):
|
|
return f"item {first.id}'s {unread(ours)}"
|
|
taken: dict[int, Touches] = {first.id: ours}
|
|
refused = []
|
|
for item in ready:
|
|
theirs = predicted(item.detail)
|
|
if item.id == first.id:
|
|
continue
|
|
if not isinstance(theirs, dict):
|
|
refused.append(f"item {item.id}, whose {unread(theirs)}")
|
|
continue
|
|
shared = next(((id_, places) for id_, touches in taken.items() if (places := overlaps(theirs, touches))), None)
|
|
if shared:
|
|
refused.append(f"item {item.id}, which overlaps item {shared[0]} at {', '.join(shared[1])}")
|
|
else:
|
|
taken[item.id] = theirs
|
|
return Beside([id_ for id_ in taken if id_ != first.id], refused)
|
|
|
|
|
|
def unread(prediction: Failure | None) -> str:
|
|
return f"detail has no `{LABEL}` list" if prediction is None else f"`{LABEL}` list cannot be read: {prediction.reason}"
|