update empty cards hook

Closes #515
This commit is contained in:
Damien Elmes 2020-05-20 13:39:21 +10:00
parent 9876c30d89
commit 4fd8a4cf2d
3 changed files with 17 additions and 52 deletions

View File

@ -7,6 +7,7 @@ import re
import aqt
from anki.backend_pb2 import EmptyCardsReport, NoteWithEmptyCards
from aqt import gui_hooks
from aqt.qt import QDialog, QDialogButtonBox, qconnect
from aqt.utils import TR, restoreGeom, saveGeom, tooltip, tr
@ -41,6 +42,8 @@ class EmptyCardsDialog(QDialog):
self.form.webview.title = "empty cards"
self.form.webview.set_bridge_command(self._on_note_link_clicked, self)
gui_hooks.empty_cards_will_show(self)
# make the note ids clickable
html = re.sub(
r"\[anki:nid:(\d+)\]",

View File

@ -1343,49 +1343,30 @@ class _EditorWillUseFontForFieldFilter:
editor_will_use_font_for_field = _EditorWillUseFontForFieldFilter()
class _EmptyCardsWillBeDeletedFilter:
"""Allow to change the list of cards to delete.
class _EmptyCardsWillShowHook:
"""Allows changing the list of cards to delete."""
For example, an add-on creating a method to delete only empty
new cards would be done as follow:
```
from anki.consts import CARD_TYPE_NEW
from anki.utils import ids2str
from aqt import mw
from aqt import gui_hooks
_hooks: List[Callable[["aqt.emptycards.EmptyCardsDialog"], None]] = []
def filter(cids, col):
return col.db.list(
f"select id from cards where (type={CARD_TYPE_NEW} and (id in {ids2str(cids)))")
def emptyNewCard():
gui_hooks.append(filter)
mw.onEmptyCards()
gui_hooks.remove(filter)
```"""
_hooks: List[Callable[[List[int]], List[int]]] = []
def append(self, cb: Callable[[List[int]], List[int]]) -> None:
"""(cids: List[int])"""
def append(self, cb: Callable[["aqt.emptycards.EmptyCardsDialog"], None]) -> None:
"""(diag: aqt.emptycards.EmptyCardsDialog)"""
self._hooks.append(cb)
def remove(self, cb: Callable[[List[int]], List[int]]) -> None:
def remove(self, cb: Callable[["aqt.emptycards.EmptyCardsDialog"], None]) -> None:
if cb in self._hooks:
self._hooks.remove(cb)
def __call__(self, cids: List[int]) -> List[int]:
for filter in self._hooks:
def __call__(self, diag: aqt.emptycards.EmptyCardsDialog) -> None:
for hook in self._hooks:
try:
cids = filter(cids)
hook(diag)
except:
# if the hook fails, remove it
self._hooks.remove(filter)
self._hooks.remove(hook)
raise
return cids
empty_cards_will_be_deleted = _EmptyCardsWillBeDeletedFilter()
empty_cards_will_show = _EmptyCardsWillShowHook()
class _MediaSyncDidProgressHook:

View File

@ -402,28 +402,9 @@ hooks = [
),
Hook(name="media_sync_did_start_or_stop", args=["running: bool"]),
Hook(
name="empty_cards_will_be_deleted",
args=["cids: List[int]"],
return_type="List[int]",
doc="""Allow to change the list of cards to delete.
For example, an add-on creating a method to delete only empty
new cards would be done as follow:
```
from anki.consts import CARD_TYPE_NEW
from anki.utils import ids2str
from aqt import mw
from aqt import gui_hooks
def filter(cids, col):
return col.db.list(
f"select id from cards where (type={CARD_TYPE_NEW} and (id in {ids2str(cids)))")
def emptyNewCard():
gui_hooks.append(filter)
mw.onEmptyCards()
gui_hooks.remove(filter)
```""",
name="empty_cards_will_show",
args=["diag: aqt.emptycards.EmptyCardsDialog"],
doc="""Allows changing the list of cards to delete.""",
),
# Adding cards
###################