mirror of
https://github.com/ankitects/anki.git
synced 2025-12-01 08:57:12 -05:00
Accept all args from update() for backend progress
This commit is contained in:
parent
fda691fc94
commit
1ffcc5321c
5 changed files with 41 additions and 23 deletions
|
|
@ -18,6 +18,7 @@ from anki.notes import NoteId
|
||||||
from aqt import gui_hooks
|
from aqt import gui_hooks
|
||||||
from aqt.errors import show_exception
|
from aqt.errors import show_exception
|
||||||
from aqt.operations import QueryOp
|
from aqt.operations import QueryOp
|
||||||
|
from aqt.progress import ProgressUpdate
|
||||||
from aqt.qt import *
|
from aqt.qt import *
|
||||||
from aqt.utils import (
|
from aqt.utils import (
|
||||||
checkInvalidFilename,
|
checkInvalidFilename,
|
||||||
|
|
@ -195,7 +196,7 @@ class ColpkgExporter(Exporter):
|
||||||
options.out_path, include_media=options.include_media, legacy=False
|
options.out_path, include_media=options.include_media, legacy=False
|
||||||
),
|
),
|
||||||
success=on_success,
|
success=on_success,
|
||||||
).with_backend_progress(export_progress_label).failure(
|
).with_backend_progress(export_progress_update).failure(
|
||||||
on_failure
|
on_failure
|
||||||
).run_in_background()
|
).run_in_background()
|
||||||
|
|
||||||
|
|
@ -223,10 +224,12 @@ class ApkgExporter(Exporter):
|
||||||
success=lambda count: tooltip(
|
success=lambda count: tooltip(
|
||||||
tr.exporting_note_exported(count=count), parent=mw
|
tr.exporting_note_exported(count=count), parent=mw
|
||||||
),
|
),
|
||||||
).with_backend_progress(export_progress_label).run_in_background()
|
).with_backend_progress(export_progress_update).run_in_background()
|
||||||
|
|
||||||
|
|
||||||
def export_progress_label(progress: Progress) -> str | None:
|
def export_progress_update(progress: Progress) -> ProgressUpdate | None:
|
||||||
if not progress.HasField("exporting"):
|
if not progress.HasField("exporting"):
|
||||||
return None
|
return None
|
||||||
return tr.exporting_exported_media_file(count=progress.exporting)
|
return ProgressUpdate(
|
||||||
|
label=tr.exporting_exported_media_file(count=progress.exporting)
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import aqt.main
|
||||||
from anki.collection import Collection, ImportLogWithChanges, Progress
|
from anki.collection import Collection, ImportLogWithChanges, Progress
|
||||||
from anki.errors import Interrupted
|
from anki.errors import Interrupted
|
||||||
from aqt.operations import CollectionOp, QueryOp
|
from aqt.operations import CollectionOp, QueryOp
|
||||||
|
from aqt.progress import ProgressUpdate
|
||||||
from aqt.qt import *
|
from aqt.qt import *
|
||||||
from aqt.utils import askUser, getFile, showInfo, showText, showWarning, tooltip, tr
|
from aqt.utils import askUser, getFile, showInfo, showText, showWarning, tooltip, tr
|
||||||
|
|
||||||
|
|
@ -96,7 +97,7 @@ def import_collection_package_op(
|
||||||
)
|
)
|
||||||
|
|
||||||
return QueryOp(parent=mw, op=op, success=lambda _: success()).with_backend_progress(
|
return QueryOp(parent=mw, op=op, success=lambda _: success()).with_backend_progress(
|
||||||
import_progress_label
|
import_progress_update
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -104,7 +105,7 @@ def import_anki_package(mw: aqt.main.AnkiQt, path: str) -> None:
|
||||||
CollectionOp(
|
CollectionOp(
|
||||||
parent=mw,
|
parent=mw,
|
||||||
op=lambda col: col.import_anki_package(path),
|
op=lambda col: col.import_anki_package(path),
|
||||||
).with_backend_progress(import_progress_label).success(
|
).with_backend_progress(import_progress_update).success(
|
||||||
show_import_log
|
show_import_log
|
||||||
).run_in_background()
|
).run_in_background()
|
||||||
|
|
||||||
|
|
@ -142,5 +143,7 @@ def stringify_log(log: Any) -> str:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def import_progress_label(progress: Progress) -> str | None:
|
def import_progress_update(progress: Progress) -> ProgressUpdate | None:
|
||||||
return progress.importing if progress.HasField("importing") else None
|
if progress.HasField("importing"):
|
||||||
|
return ProgressUpdate(label=progress.importing)
|
||||||
|
return None
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ from anki.collection import (
|
||||||
Progress,
|
Progress,
|
||||||
)
|
)
|
||||||
from aqt.errors import show_exception
|
from aqt.errors import show_exception
|
||||||
|
from aqt.progress import ProgressUpdate
|
||||||
from aqt.qt import QWidget
|
from aqt.qt import QWidget
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -68,7 +69,7 @@ class CollectionOp(Generic[ResultWithChanges]):
|
||||||
|
|
||||||
_success: Callable[[ResultWithChanges], Any] | None = None
|
_success: Callable[[ResultWithChanges], Any] | None = None
|
||||||
_failure: Callable[[Exception], Any] | None = None
|
_failure: Callable[[Exception], Any] | None = None
|
||||||
_label_from_progress: Callable[[Progress], str | None] | None = None
|
_progress_update: Callable[[Progress], ProgressUpdate | None] | None = None
|
||||||
|
|
||||||
def __init__(self, parent: QWidget, op: Callable[[Collection], ResultWithChanges]):
|
def __init__(self, parent: QWidget, op: Callable[[Collection], ResultWithChanges]):
|
||||||
self._parent = parent
|
self._parent = parent
|
||||||
|
|
@ -87,9 +88,9 @@ class CollectionOp(Generic[ResultWithChanges]):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def with_backend_progress(
|
def with_backend_progress(
|
||||||
self, label_from_progress: Callable[[Progress], str | None] | None
|
self, progress_update: Callable[[Progress], ProgressUpdate | None] | None
|
||||||
) -> CollectionOp[ResultWithChanges]:
|
) -> CollectionOp[ResultWithChanges]:
|
||||||
self._label_from_progress = label_from_progress
|
self._progress_update = progress_update
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def run_in_background(self, *, initiator: object | None = None) -> None:
|
def run_in_background(self, *, initiator: object | None = None) -> None:
|
||||||
|
|
@ -133,9 +134,9 @@ class CollectionOp(Generic[ResultWithChanges]):
|
||||||
op: Callable[[], ResultWithChanges],
|
op: Callable[[], ResultWithChanges],
|
||||||
on_done: Callable[[Future], None],
|
on_done: Callable[[Future], None],
|
||||||
) -> None:
|
) -> None:
|
||||||
if self._label_from_progress:
|
if self._progress_update:
|
||||||
mw.taskman.with_backend_progress(
|
mw.taskman.with_backend_progress(
|
||||||
op, self._label_from_progress, on_done=on_done, parent=self._parent
|
op, self._progress_update, on_done=on_done, parent=self._parent
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
mw.taskman.with_progress(op, on_done, parent=self._parent)
|
mw.taskman.with_progress(op, on_done, parent=self._parent)
|
||||||
|
|
@ -195,7 +196,7 @@ class QueryOp(Generic[T]):
|
||||||
|
|
||||||
_failure: Callable[[Exception], Any] | None = None
|
_failure: Callable[[Exception], Any] | None = None
|
||||||
_progress: bool | str = False
|
_progress: bool | str = False
|
||||||
_label_from_progress: Callable[[Progress], str | None] | None = None
|
_progress_update: Callable[[Progress], ProgressUpdate | None] | None = None
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
|
@ -221,9 +222,9 @@ class QueryOp(Generic[T]):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def with_backend_progress(
|
def with_backend_progress(
|
||||||
self, label_from_progress: Callable[[Progress], str | None] | None
|
self, progress_update: Callable[[Progress], ProgressUpdate | None] | None
|
||||||
) -> QueryOp[T]:
|
) -> QueryOp[T]:
|
||||||
self._label_from_progress = label_from_progress
|
self._progress_update = progress_update
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def run_in_background(self) -> None:
|
def run_in_background(self) -> None:
|
||||||
|
|
@ -264,10 +265,10 @@ class QueryOp(Generic[T]):
|
||||||
on_done: Callable[[Future], None],
|
on_done: Callable[[Future], None],
|
||||||
) -> None:
|
) -> None:
|
||||||
label = self._progress if isinstance(self._progress, str) else None
|
label = self._progress if isinstance(self._progress, str) else None
|
||||||
if self._label_from_progress:
|
if self._progress_update:
|
||||||
mw.taskman.with_backend_progress(
|
mw.taskman.with_backend_progress(
|
||||||
op,
|
op,
|
||||||
self._label_from_progress,
|
self._progress_update,
|
||||||
on_done=on_done,
|
on_done=on_done,
|
||||||
start_label=label,
|
start_label=label,
|
||||||
parent=self._parent,
|
parent=self._parent,
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
from dataclasses import asdict, dataclass
|
||||||
|
|
||||||
import aqt.forms
|
import aqt.forms
|
||||||
from anki._legacy import print_deprecation_warning
|
from anki._legacy import print_deprecation_warning
|
||||||
|
|
@ -170,7 +171,7 @@ class ProgressManager:
|
||||||
|
|
||||||
def start_with_backend_updates(
|
def start_with_backend_updates(
|
||||||
self,
|
self,
|
||||||
label_from_progress: Callable[[Progress], str | None],
|
progress_update: Callable[[Progress], ProgressUpdate | None],
|
||||||
start_label: str | None = None,
|
start_label: str | None = None,
|
||||||
parent: QWidget | None = None,
|
parent: QWidget | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
@ -185,11 +186,11 @@ class ProgressManager:
|
||||||
assert self.mw
|
assert self.mw
|
||||||
|
|
||||||
progress = self.mw.backend.latest_progress()
|
progress = self.mw.backend.latest_progress()
|
||||||
if not (label := label_from_progress(progress)):
|
if not (update := progress_update(progress)):
|
||||||
return
|
return
|
||||||
if dialog and dialog.wantCancel:
|
if dialog and dialog.wantCancel:
|
||||||
self.mw.backend.set_wants_abort()
|
self.mw.backend.set_wants_abort()
|
||||||
self.update(label=label)
|
self.update(**asdict(update))
|
||||||
|
|
||||||
qconnect(self._backend_timer.timeout, on_progress)
|
qconnect(self._backend_timer.timeout, on_progress)
|
||||||
self._backend_timer.start()
|
self._backend_timer.start()
|
||||||
|
|
@ -326,3 +327,12 @@ class ProgressDialog(QDialog):
|
||||||
if evt.key() == Qt.Key.Key_Escape:
|
if evt.key() == Qt.Key.Key_Escape:
|
||||||
evt.ignore()
|
evt.ignore()
|
||||||
self.wantCancel = True
|
self.wantCancel = True
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ProgressUpdate:
|
||||||
|
label: str | None = None
|
||||||
|
value: int | None = None
|
||||||
|
process: bool = True
|
||||||
|
maybeShow: bool = True
|
||||||
|
max: int | None = None
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ from typing import Any, Callable
|
||||||
|
|
||||||
import aqt
|
import aqt
|
||||||
from anki.collection import Progress
|
from anki.collection import Progress
|
||||||
|
from aqt.progress import ProgressUpdate
|
||||||
from aqt.qt import *
|
from aqt.qt import *
|
||||||
|
|
||||||
Closure = Callable[[], None]
|
Closure = Callable[[], None]
|
||||||
|
|
@ -93,13 +94,13 @@ class TaskManager(QObject):
|
||||||
def with_backend_progress(
|
def with_backend_progress(
|
||||||
self,
|
self,
|
||||||
task: Callable,
|
task: Callable,
|
||||||
label_from_progress: Callable[[Progress], str | None],
|
progress_update: Callable[[Progress], ProgressUpdate | None],
|
||||||
on_done: Callable[[Future], None] | None = None,
|
on_done: Callable[[Future], None] | None = None,
|
||||||
parent: QWidget | None = None,
|
parent: QWidget | None = None,
|
||||||
start_label: str | None = None,
|
start_label: str | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.mw.progress.start_with_backend_updates(
|
self.mw.progress.start_with_backend_updates(
|
||||||
label_from_progress,
|
progress_update,
|
||||||
parent=parent,
|
parent=parent,
|
||||||
start_label=start_label,
|
start_label=start_label,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue