mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 06:22:22 -04:00
hooks for limit new/rev for a single deck
This commit is contained in:
parent
c4b2ab96a6
commit
0c9de3b19c
4 changed files with 75 additions and 3 deletions
|
@ -387,6 +387,62 @@ class _NotesWillBeDeletedHook:
|
||||||
notes_will_be_deleted = _NotesWillBeDeletedHook()
|
notes_will_be_deleted = _NotesWillBeDeletedHook()
|
||||||
|
|
||||||
|
|
||||||
|
class _SchedulerNewLimitForSingleDeckFilter:
|
||||||
|
"""Allows changing the number of new card for this deck (without
|
||||||
|
considering descendants)."""
|
||||||
|
|
||||||
|
_hooks: List[Callable[[int, Dict[str, Any]], int]] = []
|
||||||
|
|
||||||
|
def append(self, cb: Callable[[int, Dict[str, Any]], int]) -> None:
|
||||||
|
"""(count: int, deck: Dict[str, Any])"""
|
||||||
|
self._hooks.append(cb)
|
||||||
|
|
||||||
|
def remove(self, cb: Callable[[int, Dict[str, Any]], int]) -> None:
|
||||||
|
if cb in self._hooks:
|
||||||
|
self._hooks.remove(cb)
|
||||||
|
|
||||||
|
def __call__(self, count: int, deck: Dict[str, Any]) -> int:
|
||||||
|
for filter in self._hooks:
|
||||||
|
try:
|
||||||
|
count = filter(count, deck)
|
||||||
|
except:
|
||||||
|
# if the hook fails, remove it
|
||||||
|
self._hooks.remove(filter)
|
||||||
|
raise
|
||||||
|
return count
|
||||||
|
|
||||||
|
|
||||||
|
scheduler_new_limit_for_single_deck = _SchedulerNewLimitForSingleDeckFilter()
|
||||||
|
|
||||||
|
|
||||||
|
class _SchedulerReviewLimitForSingleDeckFilter:
|
||||||
|
"""Allows changing the number of rev card for this deck (without
|
||||||
|
considering descendants)."""
|
||||||
|
|
||||||
|
_hooks: List[Callable[[int, Dict[str, Any]], int]] = []
|
||||||
|
|
||||||
|
def append(self, cb: Callable[[int, Dict[str, Any]], int]) -> None:
|
||||||
|
"""(count: int, deck: Dict[str, Any])"""
|
||||||
|
self._hooks.append(cb)
|
||||||
|
|
||||||
|
def remove(self, cb: Callable[[int, Dict[str, Any]], int]) -> None:
|
||||||
|
if cb in self._hooks:
|
||||||
|
self._hooks.remove(cb)
|
||||||
|
|
||||||
|
def __call__(self, count: int, deck: Dict[str, Any]) -> int:
|
||||||
|
for filter in self._hooks:
|
||||||
|
try:
|
||||||
|
count = filter(count, deck)
|
||||||
|
except:
|
||||||
|
# if the hook fails, remove it
|
||||||
|
self._hooks.remove(filter)
|
||||||
|
raise
|
||||||
|
return count
|
||||||
|
|
||||||
|
|
||||||
|
scheduler_review_limit_for_single_deck = _SchedulerReviewLimitForSingleDeckFilter()
|
||||||
|
|
||||||
|
|
||||||
class _Schedv2DidAnswerReviewCardHook:
|
class _Schedv2DidAnswerReviewCardHook:
|
||||||
_hooks: List[Callable[["anki.cards.Card", int, bool], None]] = []
|
_hooks: List[Callable[["anki.cards.Card", int, bool], None]] = []
|
||||||
|
|
||||||
|
|
|
@ -525,7 +525,8 @@ and due <= ? limit ?)""",
|
||||||
if d["dyn"]:
|
if d["dyn"]:
|
||||||
return self.reportLimit
|
return self.reportLimit
|
||||||
c = self.col.decks.confForDid(d["id"])
|
c = self.col.decks.confForDid(d["id"])
|
||||||
return max(0, c["rev"]["perDay"] - d["revToday"][1])
|
limit = max(0, c["rev"]["perDay"] - d["revToday"][1])
|
||||||
|
return hooks.scheduler_review_limit_for_single_deck(limit, d)
|
||||||
|
|
||||||
def _revForDeck(self, did: int, lim: int) -> int: # type: ignore[override]
|
def _revForDeck(self, did: int, lim: int) -> int: # type: ignore[override]
|
||||||
lim = min(lim, self.reportLimit)
|
lim = min(lim, self.reportLimit)
|
||||||
|
|
|
@ -474,7 +474,8 @@ select count() from
|
||||||
if g["dyn"]:
|
if g["dyn"]:
|
||||||
return self.dynReportLimit
|
return self.dynReportLimit
|
||||||
c = self.col.decks.confForDid(g["id"])
|
c = self.col.decks.confForDid(g["id"])
|
||||||
return max(0, c["new"]["perDay"] - g["newToday"][1])
|
limit = max(0, c["new"]["perDay"] - g["newToday"][1])
|
||||||
|
return hooks.scheduler_new_limit_for_single_deck(limit, g)
|
||||||
|
|
||||||
def totalNewForCurrentDeck(self) -> int:
|
def totalNewForCurrentDeck(self) -> int:
|
||||||
return self.col.db.scalar(
|
return self.col.db.scalar(
|
||||||
|
@ -872,7 +873,7 @@ and due <= ? limit ?)""",
|
||||||
for parent in self.col.decks.parents(d["id"]):
|
for parent in self.col.decks.parents(d["id"]):
|
||||||
# pass in dummy parentLimit so we don't do parent lookup again
|
# pass in dummy parentLimit so we don't do parent lookup again
|
||||||
lim = min(lim, self._deckRevLimitSingle(parent, parentLimit=lim))
|
lim = min(lim, self._deckRevLimitSingle(parent, parentLimit=lim))
|
||||||
return lim
|
return hooks.scheduler_review_limit_for_single_deck(lim, d)
|
||||||
|
|
||||||
def _revForDeck(self, did: int, lim: int, childMap: Dict[int, Any]) -> Any:
|
def _revForDeck(self, did: int, lim: int, childMap: Dict[int, Any]) -> Any:
|
||||||
dids = [did] + self.col.decks.childDids(did, childMap)
|
dids = [did] + self.col.decks.childDids(did, childMap)
|
||||||
|
|
|
@ -95,6 +95,20 @@ hooks = [
|
||||||
name="schedv2_did_answer_review_card",
|
name="schedv2_did_answer_review_card",
|
||||||
args=["card: anki.cards.Card", "ease: int", "early: bool"],
|
args=["card: anki.cards.Card", "ease: int", "early: bool"],
|
||||||
),
|
),
|
||||||
|
Hook(
|
||||||
|
name="scheduler_new_limit_for_single_deck",
|
||||||
|
args=["count: int", "deck: Dict[str, Any]"],
|
||||||
|
return_type="int",
|
||||||
|
doc="""Allows changing the number of new card for this deck (without
|
||||||
|
considering descendants).""",
|
||||||
|
),
|
||||||
|
Hook(
|
||||||
|
name="scheduler_review_limit_for_single_deck",
|
||||||
|
args=["count: int", "deck: Dict[str, Any]"],
|
||||||
|
return_type="int",
|
||||||
|
doc="""Allows changing the number of rev card for this deck (without
|
||||||
|
considering descendants).""",
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in a new issue