forked from frostschutz/Anki-Sibling-Spacing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sibling_spacing.py
118 lines (86 loc) · 3.13 KB
/
sibling_spacing.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/python
#-*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# Sibling Spacing is an addon for Anki 2 - http://ankisrs.net
# ---------------------------------------------------------------------------
# Author: Andreas Klauer (Andreas Klauer@metamorpher.de)
# Version: 0.01 (2013-02-22)
# License: GPL
# ---------------------------------------------------------------------------
# --- Imports: ---
from anki.hooks import addHook, wrap
from aqt import *
from aqt.utils import showInfo
# --- Constants ---
lookBehindPercent = 0.25
boostPercent = 0.25
# --- Globals: ---
enabled = True
debug = True
# --- Functions: ---
def siblingIvl(self, card, idealIvl, _old):
origIvl = _old(self, card, idealIvl)
if not enabled:
return origIvl
ivl = origIvl
# Penalty
minIvl = self.col.db.scalar('''SELECT MIN(ivl) FROM cards WHERE ivl > 0 AND nid = ? AND id != ? AND queue = 2''',
card.nid, card.id)
#while minIvl and minIvl > 0 and ivl > minIvl*4:
# ivl = max(1, int(ivl/2.0))
# Boost
delta = max(1, int(ivl * lookBehindPercent))
boost = max(1, int(ivl * boostPercent))
siblings = True
while siblings:
siblings = self.col.db.scalar('''SELECT count() FROM cards WHERE due >= ? AND due <= ? AND nid = ? AND id != ? AND queue = 2''',
self.today + ivl - delta, self.today + ivl, card.nid, card.id)
if siblings:
ivl += boost
if debug:
if minIvl and minIvl > 0:
print "Sibling Spacing %d%+d = %d days for card %d (sibling has %d days)" % (origIvl,ivl-origIvl,ivl,card.id,minIvl,)
else:
print "Sibling Spacing == %d days for card %d without visible siblings" % (ivl,card.id,)
return ivl
def toggle():
global enabled
enabled = not enabled
if enabled:
showInfo("Sibling Spacing is now ON")
else:
showInfo("Sibling Spacing is now OFF")
def toggle_debug():
global debug
debug = not debug
if debug:
showInfo("Sibling Spacing Debug is now ON")
else:
showInfo("Sibling Spacing Debug is now OFF")
def siblingMenu():
'''Extend the addon menu with toggle.'''
m = None
for action in mw.form.menuPlugins.actions():
menu = action.menu()
if menu and menu.title() == "sibling_spacing":
m = menu
break
if not m:
return
a = QAction(_("Toggle ON/OFF..."), mw)
mw.connect(a, SIGNAL("triggered()"), toggle)
m.addAction(a)
a = QAction(_("Debug ON/OFF..."), mw)
mw.connect(a, SIGNAL("triggered()"), toggle_debug)
m.addAction(a)
def profileLoaded():
# add menu entry
mw.addonManager.rebuildAddonsMenu = wrap(mw.addonManager.rebuildAddonsMenu,
siblingMenu)
mw.addonManager.rebuildAddonsMenu()
# add scheduler
anki.sched.Scheduler._adjRevIvl = wrap(anki.sched.Scheduler._adjRevIvl,
siblingIvl, "around")
# --- Hooks: ---
addHook("profileLoaded", profileLoaded)
# --- End of file. ---