From 6622ea1c702f8a9b617b8b577dc9e6b0a298e7fd Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Mon, 10 May 2021 15:05:52 +1000 Subject: [PATCH] drop leech hook in test scheduler The explicit flush was clearing undo history, and the hook will need re-working to support propagating OpChanges correctly. It will likely come back as a GUI hook, instead of one in pylib. --- pylib/anki/scheduler/v3.py | 22 +++++----------------- pylib/tests/test_schedv2.py | 3 ++- pylib/tools/genhooks.py | 7 ++++++- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/pylib/anki/scheduler/v3.py b/pylib/anki/scheduler/v3.py index b57215b0d..6a3a79d0f 100644 --- a/pylib/anki/scheduler/v3.py +++ b/pylib/anki/scheduler/v3.py @@ -14,7 +14,6 @@ from __future__ import annotations from typing import Tuple, Union import anki._backend.backend_pb2 as _pb -from anki import hooks from anki.cards import Card from anki.consts import * from anki.scheduler.base import CongratsInfo @@ -23,6 +22,7 @@ from anki.types import assert_exhaustive from anki.utils import intTime QueuedCards = _pb.GetQueuedCardsOut.QueuedCards +SchedulingState = _pb.SchedulingState class Scheduler(SchedulerBaseWithLegacy): @@ -101,9 +101,7 @@ class Scheduler(SchedulerBaseWithLegacy): assert 1 <= ease <= 4 assert 0 <= card.queue <= 4 - new_state = self._answerCard(card, ease) - - self._handle_leech(card, new_state) + self._answerCard(card, ease) self.reps += 1 @@ -138,19 +136,9 @@ class Scheduler(SchedulerBaseWithLegacy): return new_state - def _handle_leech(self, card: Card, new_state: _pb.SchedulingState) -> bool: - "True if was leech." - if self.col._backend.state_is_leech(new_state): - if hooks.card_did_leech.count() > 0: - hooks.card_did_leech(card) - # leech hooks assumed that card mutations would be saved for them - card.mod = intTime() - card.usn = self.col.usn() - card.flush() - - return True - else: - return False + def state_is_leech(self, new_state: SchedulingState) -> bool: + "True if new state marks the card as a leech." + return self.col._backend.state_is_leech(new_state) # Next times ########################################################################## diff --git a/pylib/tests/test_schedv2.py b/pylib/tests/test_schedv2.py index b862afa3c..6f80be95f 100644 --- a/pylib/tests/test_schedv2.py +++ b/pylib/tests/test_schedv2.py @@ -429,7 +429,8 @@ def test_reviews(): hooks.card_did_leech.append(onLeech) col.sched.answerCard(c, 1) - assert hooked + if not is_2021(): + assert hooked assert c.queue == QUEUE_TYPE_SUSPENDED c.load() assert c.queue == QUEUE_TYPE_SUSPENDED diff --git a/pylib/tools/genhooks.py b/pylib/tools/genhooks.py index 6bfce8a31..b948047d8 100644 --- a/pylib/tools/genhooks.py +++ b/pylib/tools/genhooks.py @@ -18,7 +18,12 @@ from hookslib import Hook, write_file ###################################################################### hooks = [ - Hook(name="card_did_leech", args=["card: Card"], legacy_hook="leech"), + Hook( + name="card_did_leech", + args=["card: Card"], + legacy_hook="leech", + doc="Called by v1/v2 scheduler when a card is marked as a leech.", + ), Hook(name="card_odue_was_invalid"), Hook(name="schema_will_change", args=["proceed: bool"], return_type="bool"), Hook(