Anki/qt/aqt/operations/deck.py
David Culley 63afb0f8c6
Update type annotation syntax (#3283)
* chore: add myself to CONTRIBUTORS file

* refactor: use newer type hints for Union/Optional

* refactor: fix deprecated type annotations

use collections.abc rather than typing

* refactor: use lower letter type annotations

* style: reformat with black

* refactor: remove unused imports

* refactor: add missing imports for type hints

* fixup! refactor: use newer type hints for Union/Optional

* fix: add missing imports for type annotations

* fixup! refactor: use newer type hints for Union/Optional

* fixup! style: reformat with black

* refactor: fix remaining imports re: type hints
2024-07-21 14:00:52 +07:00

99 lines
2.8 KiB
Python

# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from __future__ import annotations
from collections.abc import Sequence
from anki.collection import OpChanges, OpChangesWithCount, OpChangesWithId
from anki.decks import DeckCollapseScope, DeckDict, DeckId, UpdateDeckConfigs
from aqt.operations import CollectionOp
from aqt.qt import QWidget
from aqt.utils import getOnlyText, tooltip, tr
def remove_decks(
*,
parent: QWidget,
deck_ids: Sequence[DeckId],
deck_name: str,
) -> CollectionOp[OpChangesWithCount]:
return CollectionOp(parent, lambda col: col.decks.remove(deck_ids)).success(
lambda out: tooltip(
tr.browsing_cards_deleted_with_deckname(
count=out.count,
deck_name=deck_name,
),
parent=parent,
)
)
def reparent_decks(
*, parent: QWidget, deck_ids: Sequence[DeckId], new_parent: DeckId
) -> CollectionOp[OpChangesWithCount]:
return CollectionOp(
parent, lambda col: col.decks.reparent(deck_ids=deck_ids, new_parent=new_parent)
).success(
lambda out: tooltip(
tr.browsing_reparented_decks(count=out.count), parent=parent
)
)
def rename_deck(
*,
parent: QWidget,
deck_id: DeckId,
new_name: str,
) -> CollectionOp[OpChanges]:
return CollectionOp(
parent,
lambda col: col.decks.rename(deck_id, new_name),
)
def add_deck_dialog(
*,
parent: QWidget,
default_text: str = "",
) -> CollectionOp[OpChangesWithId] | None:
if name := getOnlyText(
tr.decks_new_deck_name(), default=default_text, parent=parent
).strip():
return add_deck(parent=parent, name=name)
else:
return None
def add_deck(*, parent: QWidget, name: str) -> CollectionOp[OpChangesWithId]:
return CollectionOp(parent, lambda col: col.decks.add_normal_deck_with_name(name))
def set_deck_collapsed(
*,
parent: QWidget,
deck_id: DeckId,
collapsed: bool,
scope: DeckCollapseScope.V,
) -> CollectionOp[OpChanges]:
return CollectionOp(
parent,
lambda col: col.decks.set_collapsed(
deck_id=deck_id, collapsed=collapsed, scope=scope
),
)
def set_current_deck(*, parent: QWidget, deck_id: DeckId) -> CollectionOp[OpChanges]:
return CollectionOp(parent, lambda col: col.decks.set_current(deck_id))
def update_deck_configs(
*, parent: QWidget, input: UpdateDeckConfigs
) -> CollectionOp[OpChanges]:
return CollectionOp(parent, lambda col: col.decks.update_deck_configs(input))
def update_deck_dict(*, parent: QWidget, deck: DeckDict) -> CollectionOp[OpChanges]:
return CollectionOp(parent, lambda col: col.decks.update_dict(deck))