forked from Arthaey/anki-rebuild-all
/
RebuildAll.py
60 lines (45 loc) · 1.73 KB
/
RebuildAll.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
# See github page to report issues or to contribute:
# https://github.com/Arthaey/anki-rebuild-all
#
# Also available for Anki at https://ankiweb.net/shared/info/1639597619
#
# Contributors:
# - @Arthaey
# - @ankitest
import re
from anki.hooks import wrap
from aqt import mw
from aqt.deckbrowser import DeckBrowser
from aqt.utils import tooltip
def _updateFilteredDecks(actionFuncName):
dynDeckIds = [ d["id"] for d in sorted(mw.col.decks.all(),key=lambda d:d["name"]) if d["dyn"] ]
count = len(dynDeckIds)
if not count:
tooltip("No filtered decks found.")
return
# should be one of "rebuildDyn" or "emptyDyn"
actionFunc = getattr(mw.col.sched, actionFuncName)
mw.checkpoint("{0} {1} filtered decks".format(actionFuncName, count))
mw.progress.start()
[ actionFunc(did) for did in dynDeckIds ]
mw.progress.finish()
tooltip("Updated {0} filtered decks.".format(count))
mw.reset()
def _handleFilteredDeckButtons(self, url):
if url in ["rebuildDyn", "emptyDyn"]:
_updateFilteredDecks(url)
def _addButtons(self):
# There's no clean way to add a button, so hack it in. :(
newHtml = ""
newButtons = [
[_("Rebuild All"), "rebuildDyn"],
[_("Empty All"), "emptyDyn"],
]
for newButton in newButtons:
newHtml += "<button title='{0}' onclick='py.link(\"{1}\");'>{0}</button>".format(*newButton)
html = self.bottom.web.page().mainFrame().toHtml()
buttons = re.findall('<button.+</button>', html)
self.bottom.draw(''.join(buttons) + newHtml)
DeckBrowser._drawButtons = wrap(DeckBrowser._drawButtons, _addButtons, "after")
DeckBrowser._linkHandler = wrap(DeckBrowser._linkHandler, _handleFilteredDeckButtons, "after")