diff --git a/salt/renderers/cheetah.py b/salt/renderers/cheetah.py new file mode 100644 index 0000000000..35f6c4754b --- /dev/null +++ b/salt/renderers/cheetah.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +''' +Cheetah Renderer for Salt +''' + +from __future__ import absolute_import + +# Import 3rd party libs +try: + from Cheetah.Template import Template + HAS_LIBS = True +except ImportError: + HAS_LIBS = False + +# Import salt libs +from salt._compat import string_types + + +def render(cheetah_data, saltenv='base', sls='', method='xml', **kws): + ''' + Render a Cheetah template. + + :rtype: A Python data structure + ''' + if not HAS_LIBS: + return {} + + if not isinstance(cheetah_data, string_types): + cheetah_data = cheetah_data.read() + + if cheetah_data.startswith('#!'): + cheetah_data = cheetah_data[(cheetah_data.find('\n') + 1):] + if not cheetah_data.strip(): + return {} + + return str(Template(cheetah_data, searchList=[kws])) diff --git a/salt/utils/templates.py b/salt/utils/templates.py index 70df1e1222..ae8031b848 100644 --- a/salt/utils/templates.py +++ b/salt/utils/templates.py @@ -383,6 +383,14 @@ def render_genshi_tmpl(tmplstr, context, tmplpath=None): return tmpl.generate(**context).render(method) +def render_cheetah_tmpl(tmplstr, context, tmplpath=None): + ''' + Render a Cheetah template. + ''' + from Cheetah.Template import Template + return str(Template(tmplstr, searchList=[context])) + + def py(sfn, string=False, **kwargs): # pylint: disable=C0103 ''' Render a template from a python source file @@ -430,6 +438,7 @@ JINJA = wrap_tmpl_func(render_jinja_tmpl) MAKO = wrap_tmpl_func(render_mako_tmpl) WEMPY = wrap_tmpl_func(render_wempy_tmpl) GENSHI = wrap_tmpl_func(render_genshi_tmpl) +CHEETAH = wrap_tmpl_func(render_cheetah_tmpl) TEMPLATE_REGISTRY = { 'jinja': JINJA, @@ -437,4 +446,5 @@ TEMPLATE_REGISTRY = { 'py': py, 'wempy': WEMPY, 'genshi': GENSHI, + 'cheetah': CHEETAH, }