diff --git a/pylib/anki/hooks.py b/pylib/anki/hooks.py index a9ee53464..5ab0f341a 100644 --- a/pylib/anki/hooks.py +++ b/pylib/anki/hooks.py @@ -307,6 +307,30 @@ class _NotesWillBeDeletedHook: notes_will_be_deleted = _NotesWillBeDeletedHook() +class _Schedv2DidAnswerReviewCardHook: + _hooks: List[Callable[["anki.cards.Card", int, bool], None]] = [] + + def append(self, cb: Callable[["anki.cards.Card", int, bool], None]) -> None: + """(card: anki.cards.Card, ease: int, early: bool)""" + self._hooks.append(cb) + + def remove(self, cb: Callable[["anki.cards.Card", int, bool], None]) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def __call__(self, card: anki.cards.Card, ease: int, early: bool) -> None: + for hook in self._hooks: + try: + hook(card, ease, early) + except: + # if the hook fails, remove it + self._hooks.remove(hook) + raise + + +schedv2_did_answer_review_card = _Schedv2DidAnswerReviewCardHook() + + class _SchemaWillChangeFilter: _hooks: List[Callable[[bool], bool]] = [] diff --git a/pylib/anki/schedv2.py b/pylib/anki/schedv2.py index ff6d438a7..22e448f29 100644 --- a/pylib/anki/schedv2.py +++ b/pylib/anki/schedv2.py @@ -959,7 +959,7 @@ select id from cards where did in %s and queue = 2 and due <= ? limit ?)""" def _answerRevCard(self, card: Card, ease: int) -> None: delay = 0 - early = card.odid and (card.odue > self.today) + early = bool(card.odid and (card.odue > self.today)) type = early and 3 or 1 if ease == 1: @@ -967,6 +967,7 @@ select id from cards where did in %s and queue = 2 and due <= ? limit ?)""" else: self._rescheduleRev(card, ease, early) + hooks.schedv2_did_answer_review_card(card, ease, early) self._logRev(card, ease, delay, type) def _rescheduleLapse(self, card: Card) -> Any: diff --git a/pylib/tools/genhooks.py b/pylib/tools/genhooks.py index 50e7f54fa..df83e73fa 100644 --- a/pylib/tools/genhooks.py +++ b/pylib/tools/genhooks.py @@ -75,6 +75,10 @@ hooks = [ ], doc="Can modify the resulting text after rendering completes.", ), + Hook( + name="schedv2_did_answer_review_card", + args=["card: anki.cards.Card", "ease: int", "early: bool"], + ), ] if __name__ == "__main__":