Mercurial > piecrust2
annotate piecrust/dataproviders/pageiterator.py @ 982:492b66482f12
pagination: Fix bad bug where all pages triggered pagination.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Mon, 30 Oct 2017 00:08:12 -0700 |
parents | 45ad976712ec |
children | d4e0c53aa6e8 |
rev | line source |
---|---|
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import logging |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 from piecrust.data.filters import PaginationFilter |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 from piecrust.data.paginationdata import PaginationData |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 from piecrust.events import Event |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 from piecrust.dataproviders.base import DataProvider |
877
d6d35b2efd04
bake: Rename "pass" to "step" and make the page pipeline use different steps.
Ludovic Chabant <ludovic@chabant.com>
parents:
867
diff
changeset
|
6 from piecrust.sources.base import ContentSource |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 logger = logging.getLogger(__name__) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
12 class _CombinedSource: |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
13 def __init__(self, sources): |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
14 self.sources = sources |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
15 self.app = sources[0].app |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
16 self.name = None |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
17 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
18 # This is for recursive traversal of the iterator chain. |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
19 # See later in `PageIterator`. |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 self.it = None |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
21 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
22 def __iter__(self): |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
23 sources = self.sources |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
24 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
25 if len(sources) == 1: |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
26 source = sources[0] |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
27 self.name = source.name |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
28 yield from source.getAllPages() |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
29 self.name = None |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
30 return |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
31 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
32 # Return the pages from all the combined sources, but skip |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
33 # those that are "overridden" -- e.g. a theme page that gets |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
34 # replaced by a user page of the same name. |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
35 used_uris = set() |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
36 for source in sources: |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
37 self.name = source.name |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
38 for page in source.getAllPages(): |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
39 page_uri = page.getUri() |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
40 if page_uri not in used_uris: |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
41 used_uris.add(page_uri) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
42 yield page |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
43 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
44 self.name = None |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 class PageIteratorDataProvider(DataProvider): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 """ A data provider that reads a content source as a list of pages. |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 This class supports wrapping another `PageIteratorDataProvider` |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
51 instance because several sources may want to be merged under the |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
52 same data endpoint (e.g. `site.pages` which lists both the user |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
53 pages and the theme pages). |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 """ |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
55 PROVIDER_NAME = 'page_iterator' |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
56 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
57 debug_render_doc_dynamic = ['_debugRenderDoc'] |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
58 debug_render_not_empty = True |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
59 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
60 def __init__(self, source, page): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
61 super().__init__(source, page) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
62 self._app = source.app |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
63 self._it = None |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
64 self._iterated = False |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
65 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
66 def __len__(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
67 self._load() |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
68 return len(self._it) |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
69 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
70 def __iter__(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
71 self._load() |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
72 yield from self._it |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
73 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
74 def _load(self): |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
75 if self._it is not None: |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
76 return |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
77 |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
78 combined_source = _CombinedSource(list(reversed(self._sources))) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
79 self._it = PageIterator(combined_source, current_page=self._page) |
982
492b66482f12
pagination: Fix bad bug where all pages triggered pagination.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
80 self._it._load_event += self._onIteration |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
81 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
82 def _onIteration(self, it): |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
83 if not self._iterated: |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
84 rcs = self._app.env.render_ctx_stack |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
85 rcs.current_ctx.addUsedSource(it._source) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
86 self._iterated = True |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
87 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
88 def _addSource(self, source): |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
89 if self._it is not None: |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
90 raise Exception("Can't add sources after the data provider " |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
91 "has been loaded.") |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
92 super()._addSource(source) |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
93 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
94 def _debugRenderDoc(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
95 return 'Provides a list of %d items' % len(self) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
96 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
97 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
98 class PageIterator: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
99 def __init__(self, source, *, current_page=None): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
100 self._source = source |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
101 self._is_content_source = isinstance( |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
102 source, (ContentSource, _CombinedSource)) |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
103 self._cache = None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
104 self._pagination_slicer = None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
105 self._has_sorter = False |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
106 self._next_page = None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
107 self._prev_page = None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
108 self._locked = False |
982
492b66482f12
pagination: Fix bad bug where all pages triggered pagination.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
109 self._load_event = Event() |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
110 self._iter_event = Event() |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
111 self._current_page = current_page |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
112 self._initIterator() |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
113 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
114 @property |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
115 def total_count(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
116 self._load() |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
117 if self._pagination_slicer is not None: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
118 return self._pagination_slicer.inner_count |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
119 return len(self._cache) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
120 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
121 @property |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
122 def next_page(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
123 self._load() |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
124 return self._next_page |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
125 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
126 @property |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
127 def prev_page(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
128 self._load() |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
129 return self._prev_page |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
130 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
131 def __len__(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
132 self._load() |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
133 return len(self._cache) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
134 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
135 def __getitem__(self, key): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
136 self._load() |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
137 return self._cache[key] |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
138 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
139 def __iter__(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
140 self._load() |
982
492b66482f12
pagination: Fix bad bug where all pages triggered pagination.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
141 self._iter_event.fire(self) |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
142 return iter(self._cache) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
143 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
144 def __getattr__(self, name): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
145 if name[:3] == 'is_' or name[:3] == 'in_': |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
146 def is_filter(value): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
147 conf = {'is_%s' % name[3:]: value} |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
148 return self._simpleNonSortedWrap(SettingFilterIterator, conf) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
149 return is_filter |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
150 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
151 if name[:4] == 'has_': |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
152 def has_filter(value): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
153 conf = {name: value} |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
154 return self._simpleNonSortedWrap(SettingFilterIterator, conf) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
155 return has_filter |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
156 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
157 if name[:5] == 'with_': |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
158 def has_filter(value): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
159 conf = {'has_%s' % name[5:]: value} |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
160 return self._simpleNonSortedWrap(SettingFilterIterator, conf) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
161 return has_filter |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
162 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
163 return self.__getattribute__(name) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
164 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
165 def skip(self, count): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
166 return self._simpleWrap(SliceIterator, count) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
167 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
168 def limit(self, count): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
169 return self._simpleWrap(SliceIterator, 0, count) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
170 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
171 def slice(self, skip, limit): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
172 return self._simpleWrap(SliceIterator, skip, limit) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
173 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
174 def filter(self, filter_name): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
175 if self._current_page is None: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
176 raise Exception("Can't use `filter()` because no parent page was " |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
177 "set for this page iterator.") |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
178 filter_conf = self._current_page.config.get(filter_name) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
179 if filter_conf is None: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
180 raise Exception("Couldn't find filter '%s' in the configuration " |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
181 "header for page: %s" % |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
182 (filter_name, self._current_page.path)) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
183 return self._simpleNonSortedWrap(SettingFilterIterator, filter_conf) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
184 |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
185 def sort(self, setting_name=None, reverse=False): |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
186 if setting_name: |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
187 self._wrapAsSort(SettingSortIterator, setting_name, reverse) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
188 else: |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
189 self._wrapAsSort(NaturalSortIterator, reverse) |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
190 return self |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
191 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
192 def reset(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
193 self._ensureUnlocked() |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
194 self._unload() |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
195 return self |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
196 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
197 @property |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
198 def _is_loaded(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
199 return self._cache is not None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
200 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
201 @property |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
202 def _has_more(self): |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
203 self._load() |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
204 if self._pagination_slicer: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
205 return self._pagination_slicer.has_more |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
206 return False |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
207 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
208 def _simpleWrap(self, it_class, *args, **kwargs): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
209 self._ensureUnlocked() |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
210 self._ensureUnloaded() |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
211 self._ensureSorter() |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
212 self._it = it_class(self._it, *args, **kwargs) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
213 if self._pagination_slicer is None and it_class is SliceIterator: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
214 self._pagination_slicer = self._it |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
215 self._pagination_slicer.current_page = self._current_page |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
216 return self |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
217 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
218 def _simpleNonSortedWrap(self, it_class, *args, **kwargs): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
219 self._ensureUnlocked() |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
220 self._ensureUnloaded() |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
221 self._it = it_class(self._it, *args, **kwargs) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
222 return self |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
223 |
856
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
224 def _wrapAsSort(self, sort_it_class, *args, **kwargs): |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
225 self._ensureUnlocked() |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
226 self._ensureUnloaded() |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
227 self._it = sort_it_class(self._it, *args, **kwargs) |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
228 self._has_sorter = True |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
229 return self |
9bb22bbe093c
refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
230 |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
231 def _lockIterator(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
232 self._ensureUnlocked() |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
233 self._locked = True |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
234 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
235 def _ensureUnlocked(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
236 if self._locked: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
237 raise Exception( |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
238 "This page iterator has been locked and can't be modified.") |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
239 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
240 def _ensureUnloaded(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
241 if self._cache: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
242 raise Exception( |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
243 "This page iterator has already been iterated upon and " |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
244 "can't be modified anymore.") |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
245 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
246 def _ensureSorter(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
247 if self._has_sorter: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
248 return |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
249 if self._is_content_source: |
939
abc52a6262a1
bake: Support the `draft` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
905
diff
changeset
|
250 # For content sources, the default sorting is reverse |
abc52a6262a1
bake: Support the `draft` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
905
diff
changeset
|
251 # date/time sorting. |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
252 self._it = DateSortIterator(self._it, reverse=True) |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
253 self._has_sorter = True |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
254 |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
255 def _initIterator(self): |
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
256 if self._is_content_source: |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
257 if isinstance(self._source, _CombinedSource): |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
258 self._it = self._source |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
259 else: |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
260 self._it = PageContentSourceIterator(self._source) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
261 |
939
abc52a6262a1
bake: Support the `draft` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
905
diff
changeset
|
262 app = self._source.app |
abc52a6262a1
bake: Support the `draft` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
905
diff
changeset
|
263 if app.config.get('baker/is_baking'): |
abc52a6262a1
bake: Support the `draft` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
905
diff
changeset
|
264 # While baking, automatically exclude any page with |
abc52a6262a1
bake: Support the `draft` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
905
diff
changeset
|
265 # the `draft` setting. |
abc52a6262a1
bake: Support the `draft` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
905
diff
changeset
|
266 draft_setting = app.config['baker/no_bake_setting'] |
abc52a6262a1
bake: Support the `draft` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
905
diff
changeset
|
267 self._it = NoDraftsIterator(self._it, draft_setting) |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
268 else: |
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
269 self._it = GenericSourceIterator(self._source) |
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
270 |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
271 def _unload(self): |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
272 self._initIterator() |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
273 self._cache = None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
274 self._paginationSlicer = None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
275 self._has_sorter = False |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
276 self._next_page = None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
277 self._prev_page = None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
278 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
279 def _load(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
280 if self._cache is not None: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
281 return |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
282 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
283 self._ensureSorter() |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
284 |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
285 if self._is_content_source: |
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
286 self._it = PaginationDataBuilderIterator(self._it) |
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
287 |
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
288 self._cache = list(self._it) |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
289 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
290 if (self._current_page is not None and |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
291 self._pagination_slicer is not None): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
292 pn = [self._pagination_slicer.prev_page, |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
293 self._pagination_slicer.next_page] |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
294 pn_it = PaginationDataBuilderIterator(iter(pn)) |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
295 self._prev_page, self._next_page = (list(pn_it)) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
296 |
982
492b66482f12
pagination: Fix bad bug where all pages triggered pagination.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
297 self._load_event.fire(self) |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
298 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
299 def _debugRenderDoc(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
300 return "Contains %d items" % len(self) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
301 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
302 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
303 class SettingFilterIterator: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
304 def __init__(self, it, fil_conf): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
305 self.it = it |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
306 self.fil_conf = fil_conf |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
307 self._fil = None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
308 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
309 def __iter__(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
310 if self._fil is None: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
311 self._fil = PaginationFilter() |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
312 self._fil.addClausesFromConfig(self.fil_conf) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
313 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
314 for i in self.it: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
315 if self._fil.pageMatches(i): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
316 yield i |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
317 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
318 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
319 class HardCodedFilterIterator: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
320 def __init__(self, it, fil): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
321 self.it = it |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
322 self._fil = fil |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
323 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
324 def __iter__(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
325 for i in self.it: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
326 if self._fil.pageMatches(i): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
327 yield i |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
328 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
329 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
330 class SliceIterator: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
331 def __init__(self, it, offset=0, limit=-1): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
332 self.it = it |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
333 self.offset = offset |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
334 self.limit = limit |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
335 self.current_page = None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
336 self.has_more = False |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
337 self.inner_count = -1 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
338 self.next_page = None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
339 self.prev_page = None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
340 self._cache = None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
341 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
342 def __iter__(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
343 if self._cache is None: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
344 inner_list = list(self.it) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
345 self.inner_count = len(inner_list) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
346 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
347 if self.limit > 0: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
348 self.has_more = self.inner_count > (self.offset + self.limit) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
349 self._cache = inner_list[self.offset:self.offset + self.limit] |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
350 else: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
351 self.has_more = False |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
352 self._cache = inner_list[self.offset:] |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
353 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
354 if self.current_page: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
355 try: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
356 idx = inner_list.index(self.current_page) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
357 except ValueError: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
358 idx = -1 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
359 if idx >= 0: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
360 if idx < self.inner_count - 1: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
361 self.next_page = inner_list[idx + 1] |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
362 if idx > 0: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
363 self.prev_page = inner_list[idx - 1] |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
364 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
365 return iter(self._cache) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
366 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
367 |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
368 class NaturalSortIterator: |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
369 def __init__(self, it, reverse=False): |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
370 self.it = it |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
371 self.reverse = reverse |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
372 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
373 def __iter__(self): |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
374 return iter(sorted(self.it, reverse=self.reverse)) |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
375 |
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
376 |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
377 class SettingSortIterator: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
378 def __init__(self, it, name, reverse=False): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
379 self.it = it |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
380 self.name = name |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
381 self.reverse = reverse |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
382 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
383 def __iter__(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
384 return iter(sorted(self.it, key=self._key_getter, |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
385 reverse=self.reverse)) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
386 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
387 def _key_getter(self, item): |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
939
diff
changeset
|
388 key = item.config.get(self.name) |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
389 if key is None: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
390 return 0 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
391 return key |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
392 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
393 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
394 class DateSortIterator: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
395 def __init__(self, it, reverse=True): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
396 self.it = it |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
397 self.reverse = reverse |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
398 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
399 def __iter__(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
400 return iter(sorted(self.it, |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
401 key=lambda x: x.datetime, reverse=self.reverse)) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
402 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
403 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
404 class PageContentSourceIterator: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
405 def __init__(self, source): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
406 self.source = source |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
407 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
408 # This is to permit recursive traversal of the |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
409 # iterator chain. It acts as the end. |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
410 self.it = None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
411 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
412 def __iter__(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
413 source = self.source |
905
1d0364614665
internal: Sources can cache their pages in addition to their items.
Ludovic Chabant <ludovic@chabant.com>
parents:
877
diff
changeset
|
414 yield from source.getAllPages() |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
415 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
416 |
939
abc52a6262a1
bake: Support the `draft` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
905
diff
changeset
|
417 class NoDraftsIterator: |
abc52a6262a1
bake: Support the `draft` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
905
diff
changeset
|
418 def __init__(self, source, no_draft_setting): |
abc52a6262a1
bake: Support the `draft` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
905
diff
changeset
|
419 self.it = source |
abc52a6262a1
bake: Support the `draft` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
905
diff
changeset
|
420 self.no_draft_setting = no_draft_setting |
abc52a6262a1
bake: Support the `draft` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
905
diff
changeset
|
421 |
abc52a6262a1
bake: Support the `draft` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
905
diff
changeset
|
422 def __iter__(self): |
abc52a6262a1
bake: Support the `draft` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
905
diff
changeset
|
423 nds = self.no_draft_setting |
abc52a6262a1
bake: Support the `draft` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
905
diff
changeset
|
424 yield from filter(lambda i: not i.config.get(nds), self.it) |
abc52a6262a1
bake: Support the `draft` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
905
diff
changeset
|
425 |
abc52a6262a1
bake: Support the `draft` setting.
Ludovic Chabant <ludovic@chabant.com>
parents:
905
diff
changeset
|
426 |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
427 class PaginationDataBuilderIterator: |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
428 def __init__(self, it): |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
429 self.it = it |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
430 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
431 def __iter__(self): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
432 for page in self.it: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
433 if page is not None: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
434 yield PaginationData(page) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
435 else: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
436 yield None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
437 |
867
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
438 |
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
439 class GenericSourceIterator: |
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
440 def __init__(self, source): |
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
441 self.source = source |
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
442 self.it = None |
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
443 |
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
444 def __iter__(self): |
757fba54bfd3
refactor: Improve pagination and iterators to work with other sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
856
diff
changeset
|
445 yield from self.source |