mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 09:23:56 +00:00
Merge pull request #14680 from techhat/genshi
Add Genshi template rendering
This commit is contained in:
commit
d971892556
58
salt/renderers/genshi.py
Normal file
58
salt/renderers/genshi.py
Normal 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)
|
@ -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,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user