comparison src/tools/common/template.py @ 2425:d294527bd46f

template: added dict_ext filter to extend a dictionary
author Goffi <goffi@goffi.org>
date Thu, 09 Nov 2017 07:54:28 +0100
parents 5425cf18929b
children 7aa863cbc47f
comparison
equal deleted inserted replaced
2424:467ddb437a71 2425:d294527bd46f
205 self.env.filters['date_fmt'] = self._date_fmt 205 self.env.filters['date_fmt'] = self._date_fmt
206 self.env.filters['xmlui_class'] = self._xmlui_class 206 self.env.filters['xmlui_class'] = self._xmlui_class
207 self.env.filters['attr_escape'] = self.attr_escape 207 self.env.filters['attr_escape'] = self.attr_escape
208 self.env.filters['item_filter'] = self._item_filter 208 self.env.filters['item_filter'] = self._item_filter
209 self.env.filters['adv_format'] = self._adv_format 209 self.env.filters['adv_format'] = self._adv_format
210 self.env.filters['dict_ext'] = self._dict_ext
210 # custom tests 211 # custom tests
211 self.env.tests['in_the_past'] = self._in_the_past 212 self.env.tests['in_the_past'] = self._in_the_past
212 213
213 def installTranslations(self): 214 def installTranslations(self):
214 i18n_dir = os.path.join(self.base_dir, 'i18n') 215 i18n_dir = os.path.join(self.base_dir, 'i18n')
483 if template is None: 484 if template is None:
484 return value 485 return value
485 # jinja use string when no special char is used, so we have to convert to unicode 486 # jinja use string when no special char is used, so we have to convert to unicode
486 return unicode(template).format(value=value, **kwargs) 487 return unicode(template).format(value=value, **kwargs)
487 488
489 def _dict_ext(self, source_dict, extra_dict, key=None):
490 """extend source_dict with extra dict and return the result
491
492 @param source_dict(dict): dictionary to extend
493 @param extra_dict(dict, None): dictionary to use to extend first one
494 None to return source_dict unmodified
495 @param key(unicode, None): if specified extra_dict[key] will be used
496 if it doesn't exists, a copy of unmodified source_dict is returned
497 @return (dict): resulting dictionary
498 """
499 if extra_dict is None:
500 return source_dict
501 if key is not None:
502 extra_dict = extra_dict.get(key, {})
503 ret = source_dict.copy()
504 ret.update(extra_dict)
505 return ret
506
488 ## custom tests ## 507 ## custom tests ##
489 508
490 def _in_the_past(self, timestamp): 509 def _in_the_past(self, timestamp):
491 """check if a date is in the past 510 """check if a date is in the past
492 511