Merge pull request #14680 from techhat/genshi

Add Genshi template rendering
This commit is contained in:
Pedro Algarvio 2014-08-02 00:18:14 +01:00
commit d971892556
2 changed files with 91 additions and 0 deletions

58
salt/renderers/genshi.py Normal file
View File

@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
'''
Genshi Renderer for Salt
'''
from __future__ import absolute_import
# Import 3rd party libs
try:
from genshi.template import MarkupTemplate
from genshi.template import NewTextTemplate
from genshi.template import OldTextTemplate
HAS_LIBS = True
except ImportError:
HAS_LIBS = False
# Import salt libs
from salt._compat import string_types
def render(genshi_data, saltenv='base', sls='', method='xml', **kws):
'''
Render a Genshi template. A method should be passed in as part of the
kwargs. If no method is passed in, xml is assumed. Valid methods are:
.. code-block:
- xml
- xhtml
- html
- text
- newtext
- oldtext
Note that the ``text`` method will call ``NewTextTemplate``. If ``oldtext``
is desired, it must be called explicitly
:rtype: A Python data structure
'''
if not HAS_LIBS:
return {}
if not isinstance(genshi_data, string_types):
genshi_data = genshi_data.read()
if genshi_data.startswith('#!'):
genshi_data = genshi_data[(genshi_data.find('\n') + 1):]
if not genshi_data.strip():
return {}
if method == 'text' or method == 'newtext':
tmpl = NewTextTemplate(genshi_data)
elif method == 'oldtext':
tmpl = OldTextTemplate(genshi_data)
else:
tmpl = MarkupTemplate(genshi_data)
return tmpl.generate(**kws).render(method)

View File

@ -352,6 +352,37 @@ def render_wempy_tmpl(tmplstr, context, tmplpath=None):
return Template(tmplstr).render(**context)
def render_genshi_tmpl(tmplstr, context, tmplpath=None):
'''
Render a Genshi template. A method should be passed in as part of the
context. If no method is passed in, xml is assumed. Valid methods are:
.. code-block:
- xml
- xhtml
- html
- text
- newtext
- oldtext
Note that the ``text`` method will call ``NewTextTemplate``. If ``oldtext``
is desired, it must be called explicitly
'''
method = context.get('method', 'xml')
if method == 'text' or method == 'newtext':
from genshi.template import NewTextTemplate
tmpl = NewTextTemplate(tmplstr)
elif method == 'oldtext':
from genshi.template import OldTextTemplate
tmpl = OldTextTemplate(tmplstr)
else:
from genshi.template import MarkupTemplate
tmpl = MarkupTemplate(tmplstr)
return tmpl.generate(**context).render(method)
def py(sfn, string=False, **kwargs): # pylint: disable=C0103
'''
Render a template from a python source file
@ -398,10 +429,12 @@ def py(sfn, string=False, **kwargs): # pylint: disable=C0103
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)
TEMPLATE_REGISTRY = {
'jinja': JINJA,
'mako': MAKO,
'py': py,
'wempy': WEMPY,
'genshi': GENSHI,
}