comparison src/tools/common/template.py @ 2402:f905dfe69fcc

template: new item_value filter this filter is used with XMLUI to specify filters before using a generate method.
author Goffi <goffi@goffi.org>
date Fri, 27 Oct 2017 18:17:35 +0200
parents 221478058d8a
children dec31114c402
comparison
equal deleted inserted replaced
2401:221478058d8a 2402:f905dfe69fcc
201 self.env.filters['next_gidx'] = self._next_gidx 201 self.env.filters['next_gidx'] = self._next_gidx
202 self.env.filters['cur_gidx'] = self._cur_gidx 202 self.env.filters['cur_gidx'] = self._cur_gidx
203 self.env.filters['date_days'] = self._date_days 203 self.env.filters['date_days'] = self._date_days
204 self.env.filters['xmlui_class'] = self._xmlui_class 204 self.env.filters['xmlui_class'] = self._xmlui_class
205 self.env.filters['attr_escape'] = self.attr_escape 205 self.env.filters['attr_escape'] = self.attr_escape
206 self.env.filters['item_filter'] = self._item_filter
206 self.env.filters['adv_format'] = self._adv_format 207 self.env.filters['adv_format'] = self._adv_format
207 208
208 def installTranslations(self): 209 def installTranslations(self):
209 i18n_dir = os.path.join(self.base_dir, 'i18n') 210 i18n_dir = os.path.join(self.base_dir, 'i18n')
210 self.translations = {} 211 self.translations = {}
363 except KeyError: 364 except KeyError:
364 log.debug(_(u"ignoring field \"{name}\": it doesn't exists").format(name=name)) 365 log.debug(_(u"ignoring field \"{name}\": it doesn't exists").format(name=name))
365 continue 366 continue
366 return u' '.join(classes) or None 367 return u' '.join(classes) or None
367 368
369 def _item_filter(self, item, filters):
370 """return item's value, filtered if suitable
371
372 @param item(object): item to filter
373 value must have name and value attributes,
374 mostly used for XMLUI items
375 @param filters(dict[unicode, (callable, dict, None)]): map of name => filter
376 if filter is None, return the value unchanged
377 if filter is a callable, apply it
378 if filter is a dict, it can have following keys:
379 - filters: iterable of filters to apply
380 - filters_args: kwargs of filters in the same order as filters (use empty dict if needed)
381 - template: template to format where {value} is the filtered value
382 """
383 value = item.value
384 filter_ = filters.get(item.name, None)
385 if filter_ is None:
386 return value
387 elif isinstance(filter_, dict):
388 filter_args = filter_.get(u'filter_args')
389 for idx, f_name in enumerate(filter_.get(u'filters', [])):
390 kwargs = filter_args[idx] if filter_args is not None else {}
391 value = self.env.filters[f_name](value, **kwargs)
392 template = filter_.get(u'template', u'{value}')
393 return template.format(value=value)
394 else:
395 return filter_(item.value)
396
368 def _adv_format(self, value, template, **kwargs): 397 def _adv_format(self, value, template, **kwargs):
369 """Advancer formatter 398 """Advancer formatter
370 399
371 like format() method, but take care or special values like None 400 like format() method, but take care or special values like None
372 @param value(unicode): value to format 401 @param value(unicode): value to format