pygui revamped progress bar functionality into app task calls to simplify and commonize the functionality, handle and display task exceptions
This commit is contained in:
parent
835675480b
commit
0999fabb14
8 changed files with 82 additions and 87 deletions
|
@ -1,46 +1,39 @@
|
|||
import logging
|
||||
import threading
|
||||
from typing import Any, Callable
|
||||
from typing import Any, Callable, Tuple
|
||||
|
||||
from core.gui.errors import show_grpc_response_exceptions
|
||||
from core.gui.errors import show_exception
|
||||
|
||||
|
||||
class BackgroundTask:
|
||||
def __init__(self, master: Any, task: Callable, callback: Callable = None, args=()):
|
||||
self.master = master
|
||||
self.args = args
|
||||
class ProgressTask:
|
||||
def __init__(
|
||||
self, task: Callable, callback: Callable = None, args: Tuple[Any] = None
|
||||
):
|
||||
self.app = None
|
||||
self.task = task
|
||||
self.callback = callback
|
||||
self.thread = None
|
||||
self.args = args
|
||||
if self.args is None:
|
||||
self.args = ()
|
||||
|
||||
def start(self):
|
||||
logging.info("starting task")
|
||||
self.thread = threading.Thread(target=self.run, daemon=True)
|
||||
self.thread.start()
|
||||
def start(self) -> None:
|
||||
thread = threading.Thread(target=self.run, daemon=True)
|
||||
thread.start()
|
||||
|
||||
def run(self):
|
||||
result = self.task(*self.args)
|
||||
logging.info("task completed")
|
||||
# if start session fails, a response with Result: False and a list of
|
||||
# exceptions is returned
|
||||
if not getattr(result, "result", True):
|
||||
if len(getattr(result, "exceptions", [])) > 0:
|
||||
self.master.after(
|
||||
0,
|
||||
show_grpc_response_exceptions,
|
||||
*(
|
||||
result.__class__.__name__,
|
||||
result.exceptions,
|
||||
self.master,
|
||||
self.master,
|
||||
)
|
||||
)
|
||||
if self.callback:
|
||||
if result is None:
|
||||
args = ()
|
||||
elif isinstance(result, (list, tuple)):
|
||||
args = result
|
||||
else:
|
||||
args = (result,)
|
||||
logging.info("calling callback: %s", args)
|
||||
self.master.after(0, self.callback, *args)
|
||||
def run(self) -> None:
|
||||
logging.info("running task")
|
||||
try:
|
||||
values = self.task(*self.args)
|
||||
if values is None:
|
||||
values = ()
|
||||
elif values and not isinstance(values, tuple):
|
||||
values = (values,)
|
||||
if self.callback:
|
||||
logging.info("calling callback")
|
||||
self.app.after(0, self.callback, *values)
|
||||
except Exception as e:
|
||||
logging.exception("progress task exception")
|
||||
args = (self.app, "Task Error", e)
|
||||
self.app.after(0, show_exception, *args)
|
||||
finally:
|
||||
self.app.after(0, self.app.progress_task_complete)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue