From: Fredrik Tolf Date: Wed, 29 Feb 2012 11:51:44 +0000 (+0100) Subject: Used the pagetree stacks to extend the pageiter to a general page cursor. X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=055ad3fd9eed3b89009f0b8f31a476369423bb79;p=automanga.git Used the pagetree stacks to extend the pageiter to a general page cursor. --- diff --git a/manga/lib.py b/manga/lib.py index 0890eaf..d976993 100644 --- a/manga/lib.py +++ b/manga/lib.py @@ -92,30 +92,30 @@ class imgstream(object): stream of SZ is not given.""" raise NotImplementedError() -class pageiter(object): - def __init__(self, root): - self.nstack = [0] - self.lstack = [root] +class cursor(object): + def __init__(self, ob): + self.cur = self.descend(ob) + + def descend(self, ob): + while isinstance(ob, pagelist): + ob = ob[0] + if not isinstance(ob, page): + raise TypeError("object in page tree was unexpectedly not a pagetree") + return ob def next(self): - while True: - if len(self.nstack) == 0: - raise StopIteration - try: - node = self.lstack[-1][self.nstack[-1]] - except IndexError: - self.lstack.pop() - self.nstack.pop() - if len(self.nstack) > 0: - self.nstack[-1] += 1 - continue - if isinstance(node, page): - nl = tuple(self.nstack) - self.nstack[-1] += 1 - return nl, node - elif isinstance(node, pagelist): - self.lstack.append(node) - self.nstack.append(0) + for n, i in reversed(self.cur.stack): + if i < len(n) - 1: + self.cur = self.descend(n[i + 1]) + return self.cur + raise StopIteration() + + def prev(self): + for n, i in reversed(self.cur,stack): + if i > 0: + self.cur = self.descend(n[i - 1]) + return self.cur + raise StopIteration() def __iter__(self): return self