pyceo/ceo/tui/TransactionView.py

86 lines
2.9 KiB
Python
Raw Normal View History

2021-08-28 23:09:02 -04:00
from threading import Thread
2021-09-17 23:45:45 -04:00
from typing import List, Dict
2021-08-28 23:09:02 -04:00
from asciimatics.exceptions import NextScene
from asciimatics.widgets import Layout, Button, Divider, Label
2021-08-28 23:09:02 -04:00
from ..operation_strings import descriptions as op_desc
from ..utils import generic_handle_stream_response
from .CeoFrame import CeoFrame
2021-08-28 23:09:02 -04:00
from .TUIStreamResponseHandler import TUIStreamResponseHandler
class TransactionView(CeoFrame):
2021-08-28 23:09:02 -04:00
def __init__(self, screen, width, height, model):
super().__init__(
screen, height, width, model, 'Transaction',
on_load=self._txnview_on_load, title='Running Transaction',
2021-08-28 23:09:02 -04:00
)
# map operation names to label widgets
self._labels = {}
2021-08-28 23:09:02 -04:00
def _add_buttons(self):
layout = Layout([100])
self.add_layout(layout)
layout.add_widget(Divider())
layout = Layout([1, 1])
self.add_layout(layout)
self._next_btn = Button('Next', self._next)
layout.add_widget(self._next_btn, 1)
2021-09-05 18:48:20 -04:00
def _add_blank_line(self):
self._op_layout.add_widget(Label(''), 0)
self._op_layout.add_widget(Label(''), 2)
2021-08-28 23:09:02 -04:00
2021-09-06 12:40:05 -04:00
def _txnview_on_load(self):
self._op_layout = Layout([12, 1, 10])
self.add_layout(self._op_layout)
# store the layouts so that we can re-use them when the screen
# gets resized
for _ in range(2):
2021-09-05 18:48:20 -04:00
self._add_blank_line()
for operation in self._model.operations:
desc = op_desc[operation]
self._op_layout.add_widget(Label(desc + '...', align='>'), 0)
desc_label = Label('', align='<')
self._op_layout.add_widget(desc_label, 2)
self._labels[operation] = desc_label
self._add_blank_line()
# this is the where success/failure messages etc. get placed
self._msg_layout = Layout([100])
self.add_layout(self._msg_layout)
2021-09-05 18:48:20 -04:00
# fill up the rest of the space
2021-08-28 23:09:02 -04:00
self.add_layout(Layout([100], fill_frame=True))
self._add_buttons()
self.fix()
Thread(target=self._do_txn).start()
2021-08-28 23:09:02 -04:00
def _do_txn(self):
resp = self._model.deferred_req()
handler = TUIStreamResponseHandler(
model=self._model,
labels=self._labels,
msg_layout=self._msg_layout,
2021-09-05 18:48:20 -04:00
txn_view=self,
2021-08-28 23:09:02 -04:00
)
2021-09-17 23:45:45 -04:00
data = generic_handle_stream_response(resp, self._model.operations, handler)
self.write_extra_txn_info(data)
# to be overridden in child classes if desired
def write_extra_txn_info(self, data: List[Dict]):
pass
2021-08-28 23:09:02 -04:00
2021-09-05 18:48:20 -04:00
def enable_next_btn(self):
self._next_btn.disabled = False
# If we don't reset, the button isn't selectable, even though we
# enabled it.
# We don't want to reload, though (which reset() will trigger).
self.skip_reload = True
2021-09-05 18:48:20 -04:00
self.reset()
2021-08-28 23:09:02 -04:00
def _next(self):
2021-09-06 12:40:05 -04:00
self._model.reset()
2021-08-28 23:09:02 -04:00
raise NextScene('Welcome')