template: added dict_ext filter to extend a dictionary

This commit is contained in:
Goffi 2017-11-09 07:54:28 +01:00
commit 0b0bb0e2c9

View file

@ -207,6 +207,7 @@ class Renderer(object):
self.env.filters['attr_escape'] = self.attr_escape
self.env.filters['item_filter'] = self._item_filter
self.env.filters['adv_format'] = self._adv_format
self.env.filters['dict_ext'] = self._dict_ext
# custom tests
self.env.tests['in_the_past'] = self._in_the_past
@ -485,6 +486,24 @@ class Renderer(object):
# jinja use string when no special char is used, so we have to convert to unicode
return unicode(template).format(value=value, **kwargs)
def _dict_ext(self, source_dict, extra_dict, key=None):
"""extend source_dict with extra dict and return the result
@param source_dict(dict): dictionary to extend
@param extra_dict(dict, None): dictionary to use to extend first one
None to return source_dict unmodified
@param key(unicode, None): if specified extra_dict[key] will be used
if it doesn't exists, a copy of unmodified source_dict is returned
@return (dict): resulting dictionary
"""
if extra_dict is None:
return source_dict
if key is not None:
extra_dict = extra_dict.get(key, {})
ret = source_dict.copy()
ret.update(extra_dict)
return ret
## custom tests ##
def _in_the_past(self, timestamp):