NF: childMapNode

This commit is contained in:
Arthur Milchior 2020-08-11 23:01:30 +02:00
parent d079307536
commit c3b2b8625e
4 changed files with 34 additions and 30 deletions

View file

@ -460,8 +460,11 @@ class DeckManager:
out.extend(self.child_ids(parent_name)) out.extend(self.child_ids(parent_name))
return out return out
def childDids(self, did: int, childMap: Dict[int, Any]) -> List: childMapNode = Dict[int, Any]
def gather(node, arr): # Change to Dict[int, "DeckManager.childMapNode"] when MyPy allow recursive type
def childDids(self, did: int, childMap: DeckManager.childMapNode) -> List:
def gather(node: DeckManager.childMapNode, arr):
for did, child in node.items(): for did, child in node.items():
arr.append(did) arr.append(did)
gather(child, arr) gather(child, arr)
@ -470,9 +473,9 @@ class DeckManager:
gather(childMap[did], arr) gather(childMap[did], arr)
return arr return arr
def childMap(self) -> Dict[Any, Dict[Any, dict]]: def childMap(self) -> DeckManager.childMapNode:
nameMap = self.nameMap() nameMap = self.nameMap()
childMap = {} childMap: DeckManager.childMapNode = {}
# go through all decks, sorted by name # go through all decks, sorted by name
for deck in sorted(self.all(), key=self.key): for deck in sorted(self.all(), key=self.key):

View file

@ -175,20 +175,20 @@ card_will_flush = _CardWillFlushHook()
class _DeckAddedHook: class _DeckAddedHook:
"""Obsolete, do not use.""" """Obsolete, do not use."""
_hooks: List[Callable[[Dict[str, Any]], None]] = [] _hooks: List[Callable[[Deck], None]] = []
def append(self, cb: Callable[[Dict[str, Any]], None]) -> None: def append(self, cb: Callable[[Deck], None]) -> None:
"""(deck: Dict[str, Any])""" """(deck: Deck)"""
self._hooks.append(cb) self._hooks.append(cb)
def remove(self, cb: Callable[[Dict[str, Any]], None]) -> None: def remove(self, cb: Callable[[Deck], None]) -> None:
if cb in self._hooks: if cb in self._hooks:
self._hooks.remove(cb) self._hooks.remove(cb)
def count(self) -> int: def count(self) -> int:
return len(self._hooks) return len(self._hooks)
def __call__(self, deck: Dict[str, Any]) -> None: def __call__(self, deck: Deck) -> None:
for hook in self._hooks: for hook in self._hooks:
try: try:
hook(deck) hook(deck)
@ -305,20 +305,20 @@ media_files_did_export = _MediaFilesDidExportHook()
class _NoteTypeAddedHook: class _NoteTypeAddedHook:
"""Obsolete, do not use.""" """Obsolete, do not use."""
_hooks: List[Callable[[Dict[str, Any]], None]] = [] _hooks: List[Callable[[NoteType], None]] = []
def append(self, cb: Callable[[Dict[str, Any]], None]) -> None: def append(self, cb: Callable[[NoteType], None]) -> None:
"""(notetype: Dict[str, Any])""" """(notetype: NoteType)"""
self._hooks.append(cb) self._hooks.append(cb)
def remove(self, cb: Callable[[Dict[str, Any]], None]) -> None: def remove(self, cb: Callable[[NoteType], None]) -> None:
if cb in self._hooks: if cb in self._hooks:
self._hooks.remove(cb) self._hooks.remove(cb)
def count(self) -> int: def count(self) -> int:
return len(self._hooks) return len(self._hooks)
def __call__(self, notetype: Dict[str, Any]) -> None: def __call__(self, notetype: NoteType) -> None:
for hook in self._hooks: for hook in self._hooks:
try: try:
hook(notetype) hook(notetype)
@ -397,20 +397,20 @@ class _SchedulerNewLimitForSingleDeckFilter:
"""Allows changing the number of new card for this deck (without """Allows changing the number of new card for this deck (without
considering descendants).""" considering descendants)."""
_hooks: List[Callable[[int, Dict[str, Any]], int]] = [] _hooks: List[Callable[[int, Deck], int]] = []
def append(self, cb: Callable[[int, Dict[str, Any]], int]) -> None: def append(self, cb: Callable[[int, Deck], int]) -> None:
"""(count: int, deck: Dict[str, Any])""" """(count: int, deck: Deck)"""
self._hooks.append(cb) self._hooks.append(cb)
def remove(self, cb: Callable[[int, Dict[str, Any]], int]) -> None: def remove(self, cb: Callable[[int, Deck], int]) -> None:
if cb in self._hooks: if cb in self._hooks:
self._hooks.remove(cb) self._hooks.remove(cb)
def count(self) -> int: def count(self) -> int:
return len(self._hooks) return len(self._hooks)
def __call__(self, count: int, deck: Dict[str, Any]) -> int: def __call__(self, count: int, deck: Deck) -> int:
for filter in self._hooks: for filter in self._hooks:
try: try:
count = filter(count, deck) count = filter(count, deck)
@ -428,20 +428,20 @@ class _SchedulerReviewLimitForSingleDeckFilter:
"""Allows changing the number of rev card for this deck (without """Allows changing the number of rev card for this deck (without
considering descendants).""" considering descendants)."""
_hooks: List[Callable[[int, Dict[str, Any]], int]] = [] _hooks: List[Callable[[int, Deck], int]] = []
def append(self, cb: Callable[[int, Dict[str, Any]], int]) -> None: def append(self, cb: Callable[[int, Deck], int]) -> None:
"""(count: int, deck: Dict[str, Any])""" """(count: int, deck: Deck)"""
self._hooks.append(cb) self._hooks.append(cb)
def remove(self, cb: Callable[[int, Dict[str, Any]], int]) -> None: def remove(self, cb: Callable[[int, Deck], int]) -> None:
if cb in self._hooks: if cb in self._hooks:
self._hooks.remove(cb) self._hooks.remove(cb)
def count(self) -> int: def count(self) -> int:
return len(self._hooks) return len(self._hooks)
def __call__(self, count: int, deck: Dict[str, Any]) -> int: def __call__(self, count: int, deck: Deck) -> int:
for filter in self._hooks: for filter in self._hooks:
try: try:
count = filter(count, deck) count = filter(count, deck)

View file

@ -13,6 +13,7 @@ import anki # pylint: disable=unused-import
from anki import hooks from anki import hooks
from anki.cards import Card from anki.cards import Card
from anki.consts import * from anki.consts import *
from anki.decks import DeckManager
from anki.lang import _ from anki.lang import _
from anki.rsbackend import ( from anki.rsbackend import (
CountsForDeckToday, CountsForDeckToday,
@ -791,7 +792,7 @@ and due <= ? limit ?)""",
lim = min(lim, self._deckRevLimitSingle(parent, parentLimit=lim)) lim = min(lim, self._deckRevLimitSingle(parent, parentLimit=lim))
return hooks.scheduler_review_limit_for_single_deck(lim, d) 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: DeckManager.childMapNode) -> Any:
dids = [did] + self.col.decks.childDids(did, childMap) dids = [did] + self.col.decks.childDids(did, childMap)
lim = min(lim, self.reportLimit) lim = min(lim, self.reportLimit)
return self.col.db.scalar( return self.col.db.scalar(

View file

@ -827,20 +827,20 @@ collection_did_load = _CollectionDidLoadHook()
class _CurrentNoteTypeDidChangeHook: class _CurrentNoteTypeDidChangeHook:
_hooks: List[Callable[[Dict[str, Any]], None]] = [] _hooks: List[Callable[[NoteType], None]] = []
def append(self, cb: Callable[[Dict[str, Any]], None]) -> None: def append(self, cb: Callable[[NoteType], None]) -> None:
"""(notetype: Dict[str, Any])""" """(notetype: NoteType)"""
self._hooks.append(cb) self._hooks.append(cb)
def remove(self, cb: Callable[[Dict[str, Any]], None]) -> None: def remove(self, cb: Callable[[NoteType], None]) -> None:
if cb in self._hooks: if cb in self._hooks:
self._hooks.remove(cb) self._hooks.remove(cb)
def count(self) -> int: def count(self) -> int:
return len(self._hooks) return len(self._hooks)
def __call__(self, notetype: Dict[str, Any]) -> None: def __call__(self, notetype: NoteType) -> None:
for hook in self._hooks: for hook in self._hooks:
try: try:
hook(notetype) hook(notetype)