tweak add-on wording

This commit is contained in:
Damien Elmes 2020-03-20 20:59:59 +10:00
parent b7c64aaf39
commit 0d43e9dca3
4 changed files with 48 additions and 50 deletions

View file

@ -167,12 +167,12 @@ class AddCards(QDialog):
def addNote(self, note) -> Optional[Note]: def addNote(self, note) -> Optional[Note]:
note.model()["did"] = self.deckChooser.selectedId() note.model()["did"] = self.deckChooser.selectedId()
ret = note.dupeOrEmpty() ret = note.dupeOrEmpty()
rejection = None problem = None
if ret == 1: if ret == 1:
rejection = _("The first field is empty.") problem = _("The first field is empty.")
rejection = gui_hooks.add_card_accepts(rejection, note) problem = gui_hooks.add_cards_will_add_note(problem, note)
if rejection is not None: if problem is not None:
showWarning(rejection, help="AddItems#AddError") showWarning(problem, help="AddItems#AddError")
return None return None
if "{{cloze:" in note.model()["tmpls"][0]["qfmt"]: if "{{cloze:" in note.model()["tmpls"][0]["qfmt"]:
if not self.mw.col.models._availClozeOrds( if not self.mw.col.models._availClozeOrds(

View file

@ -23,45 +23,6 @@ from aqt.qt import QDialog, QMenu
# @@AUTOGEN@@ # @@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: class _AddCardsDidAddNoteHook:
_hooks: List[Callable[["anki.notes.Note"], None]] = [] _hooks: List[Callable[["anki.notes.Note"], None]] = []
@ -88,6 +49,43 @@ class _AddCardsDidAddNoteHook:
add_cards_did_add_note = _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: class _AddCardsWillShowHistoryMenuHook:
_hooks: List[Callable[["aqt.addcards.AddCards", QMenu], None]] = [] _hooks: List[Callable[["aqt.addcards.AddCards", QMenu], None]] = []
@ -311,7 +309,7 @@ class _AvPlayerWillPlayHook:
av_player_will_play = _AvPlayerWillPlayHook() av_player_will_play = _AvPlayerWillPlayHook()
class _BackupIsDoneHook: class _BackupDidCompleteHook:
_hooks: List[Callable[[], None]] = [] _hooks: List[Callable[[], None]] = []
def append(self, cb: Callable[[], None]) -> None: def append(self, cb: Callable[[], None]) -> None:
@ -332,7 +330,7 @@ class _BackupIsDoneHook:
raise raise
backup_is_done = _BackupIsDoneHook() backup_did_complete = _BackupDidCompleteHook()
class _BrowserDidChangeRowHook: class _BrowserDidChangeRowHook:

View file

@ -561,7 +561,7 @@ from the profile screen."
fname = backups.pop(0) fname = backups.pop(0)
path = os.path.join(dir, fname) path = os.path.join(dir, fname)
os.unlink(path) os.unlink(path)
gui_hooks.backup_is_done() gui_hooks.backup_did_complete()
def maybeOptimize(self) -> None: def maybeOptimize(self) -> None:
# have two weeks passed? # have two weeks passed?

View file

@ -341,7 +341,7 @@ hooks = [
), ),
# Main # Main
################### ###################
Hook(name="backup_is_done"), Hook(name="backup_did_complete"),
Hook(name="profile_did_open", legacy_hook="profileLoaded"), Hook(name="profile_did_open", legacy_hook="profileLoaded"),
Hook(name="profile_will_close", legacy_hook="unloadProfile"), Hook(name="profile_will_close", legacy_hook="unloadProfile"),
Hook( Hook(
@ -414,8 +414,8 @@ def emptyNewCard():
legacy_hook="AddCards.noteAdded", legacy_hook="AddCards.noteAdded",
), ),
Hook( Hook(
name="add_card_accepts", name="add_cards_will_add_note",
args=["reason_to_already_reject: Optional[str]", "note: anki.notes.Note"], args=["problem: Optional[str]", "note: anki.notes.Note"],
return_type="Optional[str]", return_type="Optional[str]",
doc="""Decides whether the note should be added to the collection or doc="""Decides whether the note should be added to the collection or
not. It is assumed to come from the addCards window. not. It is assumed to come from the addCards window.