Source code for cadrumo.entrypoints.tui.ledger.workspace_presentation
"""Reusable Ledger workspace layout, focus, and confirmation mechanics."""from__future__importannotationsfromcollections.abcimportCallable,GeneratorfromcontextlibimportcontextmanagerfromtypingimportClassVar,overridefrompydanticimportValidationErrorfromtextual.widgetsimportDataTable,Staticfrom....core.errors.error_codesimportresolve_error_messagefrom....core.errors.hierarchyimportCadrumoError,InternalInvariantErrorfrom....core.identity.transaction_idsimportTransactionIdfrom..components.widgetsimportContentDataTable,ContentScrollfrom.controllerimportLedgerWorkspaceController,LedgerWorkspaceScreen,ledger_copyfrom.modelsimportLedgerFlowState#: The legal moves of a confirmed Ledger flow, as a state a caller can read.#:#: A state absent from this mapping is terminal and absorbing: ``SUCCEEDED``,#: ``FAILED`` and ``CANCELLED`` admit no move at all, so a screen cannot be#: driven back into an editable state after its command has settled.#:#: Two absences are the safety of the machine rather than gaps in it.#: ``EDITING`` cannot reach ``SUBMITTING``, so nothing is written without#: passing the confirmation step; and ``SUBMITTING`` cannot reach#: ``CANCELLED``, so a write already in flight cannot be abandoned — which is#: what :meth:`LedgerConfirmationFlowScreen.action_back` refuses out loud.#:#: Held here rather than rebuilt inside the transition method so the machine#: can be read and tested as the specification it is.ALLOWED_FLOW_TRANSITIONS:dict[LedgerFlowState,frozenset[LedgerFlowState]]={LedgerFlowState.EDITING:frozenset({LedgerFlowState.CONFIRMING,LedgerFlowState.CANCELLED}),LedgerFlowState.CONFIRMING:frozenset({LedgerFlowState.SUBMITTING,LedgerFlowState.CANCELLED}),LedgerFlowState.SUBMITTING:frozenset({LedgerFlowState.SUCCEEDED,LedgerFlowState.FAILED}),}
[docs]defdoor_refusal_text(error:CadrumoError|ValidationError)->str:"""Render an application door's typed refusal in the operator's language."""ifisinstance(error,CadrumoError):returnresolve_error_message(error)return"; ".join(str(item.get("msg",""))foriteminerror.errors())
[docs]@contextmanagerdefledger_workspace_page()->Generator[ContentDataTable[str]]:"""Compose the one scroll owner and canonical workspace navigation table."""withContentScroll(id="ledger-page",classes="cadrumo-scroll ledger-page"):yieldContentDataTable[str](id="ledger-navigation",cursor_type="row",zebra_stripes=True)
[docs]defrestore_transaction_focus(*,navigation:DataTable[str],table:DataTable[str],transaction_id:TransactionId|None,)->None:"""Focus a transaction by semantic identity or return focus to navigation."""iftransaction_idisNone:navigation.focus()returnrow_index=next((indexforindex,rowinenumerate(table.ordered_rows)ifrow.key.value==transaction_id),None)ifrow_indexisnotNone:table.move_cursor(row=row_index)table.focus()
[docs]classLedgerConfirmationFlowScreen(LedgerWorkspaceScreen):"""Shared presentation state machine for confirmed, one-shot Ledger flows."""FLOW_NAME:ClassVar[str]def__init__(self,controller:LedgerWorkspaceController,*,id:str)->None:"""Initialise the shared flow state around an injected workspace controller."""super().__init__(controller,id=id)self._flow_state=LedgerFlowState.EDITING@propertydefflow_state(self)->LedgerFlowState:"""Expose the guarded interaction state without a public setter."""returnself._flow_statedef_transition(self,target:LedgerFlowState)->None:iftargetnotinALLOWED_FLOW_TRANSITIONS.get(self._flow_state,frozenset()):raiseInternalInvariantError(f"invalid {self.FLOW_NAME} flow transition")self._flow_state=targetdef_cancel_flow(self)->None:raiseNotImplementedError
[docs]defrefresh_after_write(self,then:Callable[[],object])->None:"""Continue with ``then`` over a re-read workspace once this flow has written. Only a settled write changes anything, so any other state continues at once over the snapshot already shown. """ifself.flow_stateisLedgerFlowState.SUCCEEDED:self.refresh_then(then)else:then()
[docs]@overridedefaction_back(self)->None:"""Refuse abandonment after submission; otherwise unwind confirmation."""ifself.flow_stateisLedgerFlowState.SUBMITTING:self.query_one("#ledger-flow-status",Static).update(ledger_copy("tui.ledger.flow.in_flight_refusal"))returnifself.flow_stateisLedgerFlowState.CONFIRMING:self._cancel_flow()returnself.refresh_after_write(super().action_back)