aqt.operations#

Submodules#

Attributes#

Classes#

HasChangesProperty

Base class for protocol classes.

CollectionOp

Helper to perform a mutating DB operation on a background thread, and update UI.

QueryOp

Helper to perform an operation on a background thread.

Functions#

on_op_finished(→ None)

Package Contents#

class aqt.operations.HasChangesProperty#

Bases: Protocol

Base class for protocol classes.

Protocol classes are defined as:

class Proto(Protocol):
    def meth(self) -> int:
        ...

Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing).

For example:

class C:
    def meth(self) -> int:
        return 0

def func(x: Proto) -> int:
    return x.meth()

func(C())  # Passes static type check

See PEP 544 for details. Protocol classes decorated with @typing.runtime_checkable act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures. Protocol classes can be generic, they are defined as:

class GenProto[T](Protocol):
    def meth(self) -> T:
        ...
changes: anki.collection.OpChanges#
aqt.operations.ResultWithChanges#
class aqt.operations.CollectionOp(parent: aqt.qt.QWidget, op: collections.abc.Callable[[anki.collection.Collection], ResultWithChanges])#

Bases: Generic[ResultWithChanges]

Helper to perform a mutating DB operation on a background thread, and update UI.

op should either return OpChanges, or an object with a ‘changes’ property. The changes will be passed to operation_did_execute so that the UI can decide whether it needs to update itself.

  • Shows progress popup for the duration of the op.

  • Ensures the browser doesn’t try to redraw during the operation, which can lead

to a frozen UI - Updates undo state at the end of the operation - Commits changes - Fires the operation_(will|did)_reset hooks - Fires the legacy state_did_reset hook

Be careful not to call any UI routines in op, as that may crash Qt. This includes things select .selectedCards() in the browse screen.

success will be called with the return value of op().

If op() throws an exception, it will be shown in a popup, or passed to failure if it is provided.

success(success: collections.abc.Callable[[ResultWithChanges], Any] | None) CollectionOp[ResultWithChanges]#
failure(failure: collections.abc.Callable[[Exception], Any] | None) CollectionOp[ResultWithChanges]#
with_backend_progress(progress_update: collections.abc.Callable[[anki.collection.Progress, aqt.progress.ProgressUpdate], None] | None) CollectionOp[ResultWithChanges]#
run_in_background(*, initiator: object | None = None) None#
aqt.operations.on_op_finished(mw: aqt.main.AnkiQt, result: ResultWithChanges, initiator: object | None) None#
aqt.operations.T#
class aqt.operations.QueryOp(*, parent: aqt.qt.QWidget, op: collections.abc.Callable[[anki.collection.Collection], T], success: collections.abc.Callable[[T], Any])#

Bases: Generic[T]

Helper to perform an operation on a background thread.

QueryOp is primarily used for read-only requests (reading information from the database, fetching data from the network, etc), but can also be used for mutable requests outside of the collection undo system (eg adding/deleting files, calling a collection method that doesn’t support undo, etc). For operations that support undo, use CollectionOp instead.

  • Optionally shows progress popup for the duration of the op.

  • Ensures the browser doesn’t try to redraw during the operation, which can lead

to a frozen UI

Be careful not to call any UI routines in op, as that may crash Qt. This includes things like .selectedCards() in the browse screen.

success will be called with the return value of op().

If op() throws an exception, it will be shown in a popup, or passed to failure if it is provided.

failure(failure: collections.abc.Callable[[Exception], Any] | None) QueryOp[T]#
without_collection() QueryOp[T]#

Flag this QueryOp as not needing the collection.

Operations that access the collection are serialized. If you’re doing something like a series of network queries, and your operation does not access the collection, then you can call this to allow the requests to run in parallel.

with_progress(label: str | None = None) QueryOp[T]#

If label not provided, will default to ‘Processing…’

with_backend_progress(progress_update: collections.abc.Callable[[anki.collection.Progress, aqt.progress.ProgressUpdate], None] | None) QueryOp[T]#
run_in_background() None#