Adds a decorator for registering jinja filters

The decorator adds the filter to a class attribute, `salt_jinja_filters`
that maps filter names to functions.
This commit is contained in:
Loren Gordon 2017-04-12 17:03:35 -04:00
parent d4461aa4ad
commit 42a608cc39
2 changed files with 28 additions and 0 deletions

View File

@ -633,3 +633,28 @@ def ignores_kwargs(*kwarg_names):
return fn(*args, **kwargs_filtered)
return __ignores_kwargs
return _ignores_kwargs
class JinjaFilter(object):
'''
This decorator is used to specify that a function is to be loaded as a
Jinja filter.
'''
salt_jinja_filters = {}
def __init__(self, name=None):
'''
'''
self.name = name
def __call__(self, function):
'''
'''
name = self.name or function.__name__
if name not in self.salt_jinja_filters:
log.debug('Marking "{0}" as a jinja filter'.format(name))
self.salt_jinja_filters[name] = function
return function
jinja_filter = JinjaFilter

View File

@ -32,6 +32,7 @@ from salt.exceptions import (
import salt.utils.jinja
import salt.utils.network
from salt.utils.odict import OrderedDict
from salt.utils.decorators import JinjaFilter
from salt import __path__ as saltpath
log = logging.getLogger(__name__)
@ -391,6 +392,8 @@ def render_jinja_tmpl(tmplstr, context, tmplpath=None):
jinja_env.filters['network_hosts'] = salt.utils.network.network_hosts # return the hosts within a network
jinja_env.filters['network_size'] = salt.utils.network.network_size # return the network size
jinja_env.filters.update(JinjaFilter.salt_jinja_filters)
# globals
jinja_env.globals['odict'] = OrderedDict
jinja_env.globals['show_full_context'] = salt.utils.jinja.show_full_context