comparison piecrust/data/iterators.py @ 233:4379d8f8f831

internal: Removing some dependency of filters and iterators on pages.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 11 Feb 2015 20:36:18 -0800
parents 8015fb40c28b
children f130365568ff
comparison
equal deleted inserted replaced
232:e534d2bc657c 233:4379d8f8f831
44 44
45 return iter(self._cache) 45 return iter(self._cache)
46 46
47 47
48 class SettingFilterIterator(object): 48 class SettingFilterIterator(object):
49 def __init__(self, it, fil_conf, page_accessor=None): 49 def __init__(self, it, fil_conf, setting_accessor=None):
50 self.it = it 50 self.it = it
51 self.fil_conf = fil_conf 51 self.fil_conf = fil_conf
52 self._fil = None 52 self._fil = None
53 self.page_accessor = page_accessor 53 self.setting_accessor = setting_accessor
54 54
55 def __iter__(self): 55 def __iter__(self):
56 if self._fil is None: 56 if self._fil is None:
57 self._fil = PaginationFilter() 57 self._fil = PaginationFilter(self.setting_accessor)
58 self._fil.addClausesFromConfig(self.fil_conf) 58 self._fil.addClausesFromConfig(self.fil_conf)
59 59
60 for i in self.it: 60 for i in self.it:
61 if self.page_accessor: 61 if self._fil.pageMatches(i):
62 page = self.page_accessor(i)
63 else:
64 page = i
65 if self._fil.pageMatches(page):
66 yield i 62 yield i
67 63
68 64
69 class NaturalSortIterator(object): 65 class NaturalSortIterator(object):
70 def __init__(self, it, reverse=False): 66 def __init__(self, it, reverse=False):
172 168
173 def __getattr__(self, name): 169 def __getattr__(self, name):
174 if name[:3] == 'is_' or name[:3] == 'in_': 170 if name[:3] == 'is_' or name[:3] == 'in_':
175 def is_filter(value): 171 def is_filter(value):
176 conf = {'is_%s' % name[3:]: value} 172 conf = {'is_%s' % name[3:]: value}
177 return self._simpleNonSortedWrap(SettingFilterIterator, conf) 173 accessor = self._getSettingAccessor()
174 return self._simpleNonSortedWrap(SettingFilterIterator, conf,
175 accessor)
178 return is_filter 176 return is_filter
179 177
180 if name[:4] == 'has_': 178 if name[:4] == 'has_':
181 def has_filter(value): 179 def has_filter(value):
182 conf = {name: value} 180 conf = {name: value}
183 return self._simpleNonSortedWrap(SettingFilterIterator, conf) 181 accessor = self._getSettingAccessor()
182 return self._simpleNonSortedWrap(SettingFilterIterator, conf,
183 accessor)
184 return has_filter 184 return has_filter
185 185
186 if name[:5] == 'with_': 186 if name[:5] == 'with_':
187 def has_filter(value): 187 def has_filter(value):
188 conf = {'has_%s' % name[5:]: value} 188 conf = {'has_%s' % name[5:]: value}
189 return self._simpleNonSortedWrap(SettingFilterIterator, conf) 189 accessor = self._getSettingAccessor()
190 return self._simpleNonSortedWrap(SettingFilterIterator, conf,
191 accessor)
190 return has_filter 192 return has_filter
191 193
192 return self.__getattribute__(name) 194 return self.__getattribute__(name)
193 195
194 def skip(self, count): 196 def skip(self, count):
207 filter_conf = self._current_page.config.get(filter_name) 209 filter_conf = self._current_page.config.get(filter_name)
208 if filter_conf is None: 210 if filter_conf is None:
209 raise Exception("Couldn't find filter '%s' in the configuration " 211 raise Exception("Couldn't find filter '%s' in the configuration "
210 "header for page: %s" % 212 "header for page: %s" %
211 (filter_name, self._current_page.path)) 213 (filter_name, self._current_page.path))
212 return self._simpleNonSortedWrap(SettingFilterIterator, filter_conf) 214 accessor = self._getSettingAccessor()
215 return self._simpleNonSortedWrap(SettingFilterIterator, filter_conf,
216 accessor)
213 217
214 def sort(self, setting_name=None, reverse=False): 218 def sort(self, setting_name=None, reverse=False):
215 self._ensureUnlocked() 219 self._ensureUnlocked()
216 self._unload() 220 self._unload()
217 if setting_name is not None: 221 if setting_name is not None:
218 accessor = None 222 accessor = self._getSettingAccessor()
219 if isinstance(self._source, IPaginationSource):
220 accessor = self._source.getSettingAccessor()
221 self._pages = SettingSortIterator(self._pages, setting_name, 223 self._pages = SettingSortIterator(self._pages, setting_name,
222 reverse, accessor) 224 reverse, accessor)
223 else: 225 else:
224 self._pages = NaturalSortIterator(self._pages, reverse) 226 self._pages = NaturalSortIterator(self._pages, reverse)
225 self._has_sorter = True 227 self._has_sorter = True
251 self._ensureUnlocked() 253 self._ensureUnlocked()
252 self._unload() 254 self._unload()
253 self._pages = it_class(self._pages, *args, **kwargs) 255 self._pages = it_class(self._pages, *args, **kwargs)
254 return self 256 return self
255 257
258 def _getSettingAccessor(self):
259 accessor = None
260 if isinstance(self._source, IPaginationSource):
261 accessor = self._source.getSettingAccessor()
262 return accessor
263
256 def _ensureUnlocked(self): 264 def _ensureUnlocked(self):
257 if self._locked: 265 if self._locked:
258 raise Exception( 266 raise Exception(
259 "This page iterator has been locked, probably because " 267 "This page iterator has been locked, probably because "
260 "you're trying to tamper with pagination data.") 268 "you're trying to tamper with pagination data.")