61 lines
2.7 KiB
Python
61 lines
2.7 KiB
Python
"""Plan stores for tests, held to plan_store.contract like every backend."""
|
|
import dataclasses
|
|
|
|
from plan_store.markdown_edits import bold_cell, plain
|
|
from plan_store.plans import cells, no_item, Plan, Written
|
|
from failure import Failure
|
|
from plan.todo_table import Item, parse_row, SHAPE, Table
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class MemoryPlan:
|
|
"""A plan held in memory, as a tracker holds one: in the order filed, unranked, with no bar. Where fails is set, every read and write fails with it as the reason."""
|
|
rows: dict[int, dict[str, str]] = dataclasses.field(default_factory=dict)
|
|
details: dict[int, str] = dataclasses.field(default_factory=dict)
|
|
# Each comment an edit gave, under its item's ID, as a tracker posts them on the item's issue.
|
|
comments: dict[int, list[str]] = dataclasses.field(default_factory=dict)
|
|
next_id: int = 1
|
|
label: str = "the plan in memory"
|
|
tracked: str | None = None
|
|
fails: str | None = None
|
|
|
|
def read(self) -> Plan | Failure:
|
|
if self.fails:
|
|
return Failure(self.fails)
|
|
found = [self.item(id_, row) for id_, row in self.rows.items()]
|
|
return next((item for item in found if isinstance(item, Failure)), None) or Plan([item for item in found if isinstance(item, Item)])
|
|
|
|
def add(self, fields: dict[str, str], detail: str) -> Written | Failure:
|
|
return self.put(self.next_id, cells(fields), detail)
|
|
|
|
def edit(self, id_: int, fields: dict[str, str], detail: str | None, comment: str | None = None) -> Written | Failure:
|
|
if id_ not in self.rows:
|
|
return Failure(no_item(self.label, id_))
|
|
written = self.put(id_, self.rows[id_] | cells(fields), self.details.get(id_, "") if detail is None else detail)
|
|
if comment is not None and isinstance(written, Written):
|
|
self.comments.setdefault(id_, []).append(comment)
|
|
return written
|
|
|
|
def finish(self, id_: int) -> Written | Failure:
|
|
if self.fails or id_ not in self.rows:
|
|
return Failure(self.fails or no_item(self.label, id_))
|
|
del self.rows[id_]
|
|
self.details.pop(id_, None)
|
|
return Written(id_)
|
|
|
|
def put(self, id_: int, row: dict[str, str], detail: str) -> Written | Failure:
|
|
"""Holds row as item id_, or fails where row's cells don't make an item."""
|
|
if self.fails:
|
|
return Failure(self.fails)
|
|
row = row | {"Item": bold_cell(plain(row.get("Item", "")))}
|
|
read = self.item(id_, row)
|
|
if isinstance(read, Failure):
|
|
return read
|
|
self.rows[id_], self.details[id_], self.next_id = row, detail.strip(), max(self.next_id, id_ + 1)
|
|
return Written(id_)
|
|
|
|
def item(self, id_: int, row: dict[str, str]) -> Item | Failure:
|
|
line = "| " + " | ".join({**row, "ID": str(id_)}.get(column, "") for column in SHAPE.columns) + " |"
|
|
read = parse_row(line, Table(0, SHAPE.columns, [line]))
|
|
return read if isinstance(read, Failure) else dataclasses.replace(read, detail=self.details.get(id_, ""))
|