X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;f=manga%2Flib.py;h=d9769933ab5fce72a502575beb227902184d310e;hb=055ad3fd9eed3b89009f0b8f31a476369423bb79;hp=b5d13c1f69941987141ab4a07ae112a6e838bc9c;hpb=6a1e046b70ed20e46107010641f8fc00c6c4f33f;p=automanga.git diff --git a/manga/lib.py b/manga/lib.py index b5d13c1..d976993 100644 --- a/manga/lib.py +++ b/manga/lib.py @@ -15,7 +15,18 @@ class library(object): Not all libraries need implement this.""" raise NotImplementedError("manga.lib.library iterator") -class pagelist(object): +class pagetree(object): + """Base class for objects in the tree of pages and pagelists. + + All pagetree objects should contain an attribute `stack', contains + a list of pairs. The last pair in the list should be the pagetree + object which yielded this pagetree object, along with the index + which yielded it. Every non-last pair should be the same + information for the pair following it. The only objects with empty + `stack' lists should be `manga' objects.""" + pass + +class pagelist(pagetree): """Class representing a list of either pages, or nested pagelists. Might be, for instance, a volume or a chapter. @@ -43,7 +54,7 @@ class manga(pagelist): and all constraints valid for it.""" pass -class page(object): +class page(pagetree): """Class representing a single page of a manga. Pages make up the leaf nodes of a pagelist tree. @@ -81,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