Adding Template renderer for Cheetah

This commit is contained in:
Joseph Hall 2014-08-02 00:31:49 -06:00
parent 6be96f82a6
commit faeba57e01
2 changed files with 46 additions and 0 deletions

36
salt/renderers/cheetah.py Normal file
View File

@ -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]))

View File

@ -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,
}