From 07be272b3a303f89e8d58492f9b140cd1ce300b2 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Wed, 29 Feb 2012 08:17:22 +0100 Subject: [PATCH] Added a flat page iterator for arbitrary page lists. --- manga/lib.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/manga/lib.py b/manga/lib.py index 712de71..7988438 100644 --- a/manga/lib.py +++ b/manga/lib.py @@ -9,3 +9,31 @@ class manga(pagelist): class page(object): pass + +class pageiter(object): + def __init__(self, root): + self.nstack = [0] + self.lstack = [root] + + 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) + + def __iter__(self): + return self -- 2.11.0