From 0d43e9dca36147bbfe4c9691eceeeacfc6a76e95 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 20 Mar 2020 20:59:59 +1000 Subject: [PATCH] tweak add-on wording --- qt/aqt/addcards.py | 10 ++--- qt/aqt/gui_hooks.py | 80 ++++++++++++++++++++-------------------- qt/aqt/main.py | 2 +- qt/tools/genhooks_gui.py | 6 +-- 4 files changed, 48 insertions(+), 50 deletions(-) diff --git a/qt/aqt/addcards.py b/qt/aqt/addcards.py index d2adc2966..efe20e9b2 100644 --- a/qt/aqt/addcards.py +++ b/qt/aqt/addcards.py @@ -167,12 +167,12 @@ class AddCards(QDialog): def addNote(self, note) -> Optional[Note]: note.model()["did"] = self.deckChooser.selectedId() ret = note.dupeOrEmpty() - rejection = None + problem = None if ret == 1: - rejection = _("The first field is empty.") - rejection = gui_hooks.add_card_accepts(rejection, note) - if rejection is not None: - showWarning(rejection, help="AddItems#AddError") + problem = _("The first field is empty.") + problem = gui_hooks.add_cards_will_add_note(problem, note) + if problem is not None: + showWarning(problem, help="AddItems#AddError") return None if "{{cloze:" in note.model()["tmpls"][0]["qfmt"]: if not self.mw.col.models._availClozeOrds( diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index cc7fce494..30a9ec0a7 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -23,45 +23,6 @@ from aqt.qt import QDialog, QMenu # @@AUTOGEN@@ -class _AddCardAcceptsFilter: - """Decides whether the note should be added to the collection or - not. It is assumed to come from the addCards window. - - reason_to_already_reject is the first reason to reject that - was found, or None. If your filter wants to reject, it should - replace return the reason to reject. Otherwise return the - input.""" - - _hooks: List[Callable[[Optional[str], "anki.notes.Note"], Optional[str]]] = [] - - def append( - self, cb: Callable[[Optional[str], "anki.notes.Note"], Optional[str]] - ) -> None: - """(reason_to_already_reject: Optional[str], note: anki.notes.Note)""" - self._hooks.append(cb) - - def remove( - self, cb: Callable[[Optional[str], "anki.notes.Note"], Optional[str]] - ) -> None: - if cb in self._hooks: - self._hooks.remove(cb) - - def __call__( - self, reason_to_already_reject: Optional[str], note: anki.notes.Note - ) -> Optional[str]: - for filter in self._hooks: - try: - reason_to_already_reject = filter(reason_to_already_reject, note) - except: - # if the hook fails, remove it - self._hooks.remove(filter) - raise - return reason_to_already_reject - - -add_card_accepts = _AddCardAcceptsFilter() - - class _AddCardsDidAddNoteHook: _hooks: List[Callable[["anki.notes.Note"], None]] = [] @@ -88,6 +49,43 @@ class _AddCardsDidAddNoteHook: add_cards_did_add_note = _AddCardsDidAddNoteHook() +class _AddCardsWillAddNoteFilter: + """Decides whether the note should be added to the collection or + not. It is assumed to come from the addCards window. + + reason_to_already_reject is the first reason to reject that + was found, or None. If your filter wants to reject, it should + replace return the reason to reject. Otherwise return the + input.""" + + _hooks: List[Callable[[Optional[str], "anki.notes.Note"], Optional[str]]] = [] + + def append( + self, cb: Callable[[Optional[str], "anki.notes.Note"], Optional[str]] + ) -> None: + """(problem: Optional[str], note: anki.notes.Note)""" + self._hooks.append(cb) + + def remove( + self, cb: Callable[[Optional[str], "anki.notes.Note"], Optional[str]] + ) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def __call__(self, problem: Optional[str], note: anki.notes.Note) -> Optional[str]: + for filter in self._hooks: + try: + problem = filter(problem, note) + except: + # if the hook fails, remove it + self._hooks.remove(filter) + raise + return problem + + +add_cards_will_add_note = _AddCardsWillAddNoteFilter() + + class _AddCardsWillShowHistoryMenuHook: _hooks: List[Callable[["aqt.addcards.AddCards", QMenu], None]] = [] @@ -311,7 +309,7 @@ class _AvPlayerWillPlayHook: av_player_will_play = _AvPlayerWillPlayHook() -class _BackupIsDoneHook: +class _BackupDidCompleteHook: _hooks: List[Callable[[], None]] = [] def append(self, cb: Callable[[], None]) -> None: @@ -332,7 +330,7 @@ class _BackupIsDoneHook: raise -backup_is_done = _BackupIsDoneHook() +backup_did_complete = _BackupDidCompleteHook() class _BrowserDidChangeRowHook: diff --git a/qt/aqt/main.py b/qt/aqt/main.py index 013bdf5e4..49e418f06 100644 --- a/qt/aqt/main.py +++ b/qt/aqt/main.py @@ -561,7 +561,7 @@ from the profile screen." fname = backups.pop(0) path = os.path.join(dir, fname) os.unlink(path) - gui_hooks.backup_is_done() + gui_hooks.backup_did_complete() def maybeOptimize(self) -> None: # have two weeks passed? diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index 5a91089e8..21f8de560 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -341,7 +341,7 @@ hooks = [ ), # Main ################### - Hook(name="backup_is_done"), + Hook(name="backup_did_complete"), Hook(name="profile_did_open", legacy_hook="profileLoaded"), Hook(name="profile_will_close", legacy_hook="unloadProfile"), Hook( @@ -414,8 +414,8 @@ def emptyNewCard(): legacy_hook="AddCards.noteAdded", ), Hook( - name="add_card_accepts", - args=["reason_to_already_reject: Optional[str]", "note: anki.notes.Note"], + name="add_cards_will_add_note", + args=["problem: Optional[str]", "note: anki.notes.Note"], return_type="Optional[str]", doc="""Decides whether the note should be added to the collection or not. It is assumed to come from the addCards window.