diff --git a/anki/deck.py b/anki/deck.py index 4e013e397..948d774a4 100644 --- a/anki/deck.py +++ b/anki/deck.py @@ -2,9 +2,7 @@ # Copyright: Damien Elmes # License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html -import time, os, random, re, stat, simplejson, datetime, copy, itertools -import operator - +import time, os, random, re, stat, simplejson, datetime, copy from anki.lang import _, ngettext from anki.utils import parseTags, tidyHTML, ids2str, hexifyID, \ checksum, fieldChecksum, addTags, delTags, stripHTML, intTime, \ @@ -557,24 +555,6 @@ update facts set tags = :t, mod = :n where id = :id""", [fix(row) for row in res "A list of all group names." return self.db.list("select name from groups order by name") - def groupsTree(self): - return self._groupChildren(self.groups()) - - def _groupChildren(self, grps): - tree = [] - for (head, tail) in itertools.groupby([x.split("::", 1) for x in grps], - key=operator.itemgetter(0)): - tail = list(tail) - l = [c[1] for c in tail if len(c) > 1] - children = self._groupChildren(l) - tree.append((head, children)) - return tuple(tree) - for g in grps: - names = g.split("::") - if g == last: - top[-1][1].append(g) - pass - def groupId(self, name): "Return the id for NAME, creating if necessary." id = self.db.scalar("select id from groups where name = ?", name) diff --git a/anki/sched.py b/anki/sched.py index 1010fd213..ddf9aad9b 100644 --- a/anki/sched.py +++ b/anki/sched.py @@ -2,7 +2,7 @@ # Copyright: Damien Elmes # License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html -import time, datetime, simplejson, random +import time, datetime, simplejson, random, itertools from operator import itemgetter from heapq import * #from anki.cards import Card @@ -133,6 +133,39 @@ group by gid""", self.today): self.deck.db.execute( "select id, name from groups order by name")] + def groupCountTree(self): + return self._groupChildren(self.groupCounts()) + + def _groupChildren(self, grps): + tree = [] + # split strings + for g in grps: + g[0] = g[0].split("::", 1) + # group and recurse + def key(grp): + return grp[0][0] + for (head, tail) in itertools.groupby(grps, key=key): + tail = list(tail) + rev = 0 + new = 0 + children = [] + for c in tail: + if len(c[0]) == 1: + # current node + rev += c[1] + new += c[2] + else: + # set new string to tail + c[0] = c[0][1] + children.append(c) + children = self._groupChildren(children) + # tally up children counts + for ch in children: + rev += ch[1] + new += ch[2] + tree.append((head, rev, new, children)) + return tuple(tree) + # Getting the next card ########################################################################## diff --git a/tests/test_deck.py b/tests/test_deck.py index 064727230..e4cc240a4 100644 --- a/tests/test_deck.py +++ b/tests/test_deck.py @@ -177,12 +177,3 @@ def test_selective(): assert deck.db.scalar("select count() from cards where gid = 3") == 3 deck.setGroupForTags(["one"], [], 2) assert deck.db.scalar("select count() from cards where gid = 2") == 2 - -def test_groups(): - d = getEmptyDeck() - tree = d._groupChildren( - ["a", "b", "c::1", "c::2", "d", "d::1", "d::1::2::3"]) - assert tree[0] == ("a", tuple()) - assert tree[1] == ("b", tuple()) - assert tree[2] == ("c", (("1", tuple()), ("2", tuple()))) - assert tree[3] == ('d', (('1', (('2', (('3', ()),)),)),)) diff --git a/tests/test_sched.py b/tests/test_sched.py index be4eec25b..36226a276 100644 --- a/tests/test_sched.py +++ b/tests/test_sched.py @@ -592,14 +592,16 @@ def test_collapse(): def test_groupCounts(): d = getEmptyDeck() - # add two facts + # add a fact with default group f = d.newFact() f['Front'] = u"one" d.addFact(f) + # and one that's a child f = d.newFact() f['Front'] = u"two" + f.gid = d.groupId("Default Group::1") d.addFact(f) - # make one a review card + # make it a review card c = f.cards()[0] c.queue = 2 c.due = 0 @@ -607,11 +609,32 @@ def test_groupCounts(): # add one more with a new group f = d.newFact() f['Front'] = u"two" - f.gid = d.groupId("new") + f.gid = d.groupId("foo::bar") + d.addFact(f) + # and one that's a sibling + f = d.newFact() + f['Front'] = u"three" + f.gid = d.groupId("foo::baz") d.addFact(f) d.reset() - assert d.sched.counts() == (2, 0, 1) - assert len(d.groups()) == 2 + assert d.sched.counts() == (3, 0, 1) + assert len(d.groups()) == 4 cnts = d.sched.groupCounts() - assert cnts[0] == ["Default Group", 1, 1] - assert cnts[1] == ["new", 0, 1] + assert cnts[0] == ["Default Group", 0, 1] + assert cnts[1] == ["Default Group::1", 1, 0] + assert cnts[2] == ["foo::bar", 0, 1] + assert cnts[3] == ["foo::baz", 0, 1] + tree = d.sched.groupCountTree() + assert tree[0][0] == "Default Group" + # sum of child and parent + assert tree[0][1] == 1 + assert tree[0][2] == 1 + # child count is just review + assert tree[0][3][0][0] == "1" + assert tree[0][3][0][1] == 1 + assert tree[0][3][0][2] == 0 + # event if parent group didn't exist, it should have been created with a + # counts summary. + assert tree[1][0] == "foo" + assert tree[1][1] == 0 + assert tree[1][2] == 2