Python mako.template() Examples

The following are 27 code examples of mako.template(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module mako , or try the search function .
Example #1
Source File: exceptions.py    From android_universal with MIT License 6 votes vote down vote up
def text_error_template(lookup=None):
    """Provides a template that renders a stack trace in a similar format to
    the Python interpreter, substituting source template filenames, line
    numbers and code for that of the originating source template, as
    applicable.

    """
    import mako.template
    return mako.template.Template(r"""
<%page args="error=None, traceback=None"/>
<%!
    from mako.exceptions import RichTraceback
%>\
<%
    tback = RichTraceback(error=error, traceback=traceback)
%>\
Traceback (most recent call last):
% for (filename, lineno, function, line) in tback.traceback:
  File "${filename}", line ${lineno}, in ${function or '?'}
    ${line | trim}
% endfor
${tback.errorname}: ${tback.message}
""") 
Example #2
Source File: exceptions.py    From ansible-cmdb with GNU General Public License v3.0 6 votes vote down vote up
def text_error_template(lookup=None):
    """Provides a template that renders a stack trace in a similar format to
    the Python interpreter, substituting source template filenames, line
    numbers and code for that of the originating source template, as
    applicable.

    """
    import mako.template
    return mako.template.Template(r"""
<%page args="error=None, traceback=None"/>
<%!
    from mako.exceptions import RichTraceback
%>\
<%
    tback = RichTraceback(error=error, traceback=traceback)
%>\
Traceback (most recent call last):
% for (filename, lineno, function, line) in tback.traceback:
  File "${filename}", line ${lineno}, in ${function or '?'}
    ${line | trim}
% endfor
${tback.errorname}: ${tback.message}
""") 
Example #3
Source File: exceptions.py    From jbox with MIT License 6 votes vote down vote up
def text_error_template(lookup=None):
    """Provides a template that renders a stack trace in a similar format to
    the Python interpreter, substituting source template filenames, line
    numbers and code for that of the originating source template, as
    applicable.

    """
    import mako.template
    return mako.template.Template(r"""
<%page args="error=None, traceback=None"/>
<%!
    from mako.exceptions import RichTraceback
%>\
<%
    tback = RichTraceback(error=error, traceback=traceback)
%>\
Traceback (most recent call last):
% for (filename, lineno, function, line) in tback.traceback:
  File "${filename}", line ${lineno}, in ${function or '?'}
    ${line | trim}
% endfor
${tback.errorname}: ${tback.message}
""") 
Example #4
Source File: exceptions.py    From teleport with Apache License 2.0 6 votes vote down vote up
def text_error_template(lookup=None):
    """Provides a template that renders a stack trace in a similar format to
    the Python interpreter, substituting source template filenames, line
    numbers and code for that of the originating source template, as
    applicable.

    """
    import mako.template
    return mako.template.Template(r"""
<%page args="error=None, traceback=None"/>
<%!
    from mako.exceptions import RichTraceback
%>\
<%
    tback = RichTraceback(error=error, traceback=traceback)
%>\
Traceback (most recent call last):
% for (filename, lineno, function, line) in tback.traceback:
  File "${filename}", line ${lineno}, in ${function or '?'}
    ${line | trim}
% endfor
${tback.errorname}: ${tback.message}
""") 
Example #5
Source File: exceptions.py    From SA-ctf_scoreboard with Creative Commons Zero v1.0 Universal 6 votes vote down vote up
def text_error_template(lookup=None):
    """Provides a template that renders a stack trace in a similar format to
    the Python interpreter, substituting source template filenames, line
    numbers and code for that of the originating source template, as
    applicable.

    """
    import mako.template
    return mako.template.Template(r"""
<%page args="error=None, traceback=None"/>
<%!
    from mako.exceptions import RichTraceback
%>\
<%
    tback = RichTraceback(error=error, traceback=traceback)
%>\
Traceback (most recent call last):
% for (filename, lineno, function, line) in tback.traceback:
  File "${filename}", line ${lineno}, in ${function or '?'}
    ${line | trim}
% endfor
${tback.errorname}: ${tback.message}
""") 
Example #6
Source File: exceptions.py    From misp42splunk with GNU Lesser General Public License v3.0 5 votes vote down vote up
def text_error_template(lookup=None):
    """Provides a template that renders a stack trace in a similar format to
    the Python interpreter, substituting source template filenames, line
    numbers and code for that of the originating source template, as
    applicable.

    """
    import mako.template

    return mako.template.Template(
        r"""
<%page args="error=None, traceback=None"/>
<%!
    from mako.exceptions import RichTraceback
%>\
<%
    tback = RichTraceback(error=error, traceback=traceback)
%>\
Traceback (most recent call last):
% for (filename, lineno, function, line) in tback.traceback:
  File "${filename}", line ${lineno}, in ${function or '?'}
    ${line | trim}
% endfor
${tback.errorname}: ${tback.message}
"""
    ) 
Example #7
Source File: exceptions.py    From android_universal with MIT License 5 votes vote down vote up
def traceback(self):
        """Return a list of 4-tuple traceback records (i.e. normal python
        format) with template-corresponding lines remapped to the originating
        template.

        """
        return list(self._get_reformatted_records(self.records)) 
Example #8
Source File: exceptions.py    From ansible-cmdb with GNU General Public License v3.0 5 votes vote down vote up
def traceback(self):
        """Return a list of 4-tuple traceback records (i.e. normal python
        format) with template-corresponding lines remapped to the originating
        template.

        """
        return list(self._get_reformatted_records(self.records)) 
Example #9
Source File: exceptions.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def text_error_template(lookup=None):
    """Provides a template that renders a stack trace in a similar format to
    the Python interpreter, substituting source template filenames, line
    numbers and code for that of the originating source template, as
    applicable.

    """
    import mako.template

    return mako.template.Template(
        r"""
<%page args="error=None, traceback=None"/>
<%!
    from mako.exceptions import RichTraceback
%>\
<%
    tback = RichTraceback(error=error, traceback=traceback)
%>\
Traceback (most recent call last):
% for (filename, lineno, function, line) in tback.traceback:
  File "${filename}", line ${lineno}, in ${function or '?'}
    ${line | trim}
% endfor
${tback.errorname}: ${tback.message}
"""
    ) 
Example #10
Source File: exceptions.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def traceback(self):
        """Return a list of 4-tuple traceback records (i.e. normal python
        format) with template-corresponding lines remapped to the originating
        template.

        """
        return list(self._get_reformatted_records(self.records)) 
Example #11
Source File: exceptions.py    From SA-ctf_scoreboard with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def traceback(self):
        """Return a list of 4-tuple traceback records (i.e. normal python
        format) with template-corresponding lines remapped to the originating
        template.

        """
        return list(self._get_reformatted_records(self.records)) 
Example #12
Source File: render_benchmark.py    From spitfire with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def profile_tests(which=None):
    print 'Profiling...'
    print
    import hotshot, hotshot.stats
    profile_data = 'template.prof'
    profile = hotshot.Profile(profile_data)
    profile.runcall(run_tests, which=which, number=1, compare=False)
    stats = hotshot.stats.load(profile_data)
    stats.strip_dirs()
    stats.sort_stats('time', 'calls')
    print
    stats.print_stats()
    print 'Profile data written to %s' % profile_data 
Example #13
Source File: render_benchmark.py    From spitfire with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_mako_tests():
    if not mako:
        return []

    tmpl_src = """
        <table>
            % for row in table:
                <tr>
                    % for column in row.values():
                        <td>${column}</td>
                    % endfor
                </tr>
            % endfor
        </table>
    """

    tmpl = mako.template.Template(tmpl_src)
    tmpl_autoescaped = mako.template.Template(tmpl_src, default_filters=['h'])

    def test_mako():
        """Mako template"""
        tmpl.render(table=TABLE_DATA)

    def test_mako_autoescaped():
        """Mako template autoescaped"""
        tmpl_autoescaped.render(table=TABLE_DATA)

    return [
        test_mako,
        test_mako_autoescaped,
    ] 
Example #14
Source File: render_benchmark.py    From spitfire with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_jinja2_tests():
    if not jinja2:
        return []

    tmpl_src = """
        <table>
            {% for row in table %}
                <tr>
                    {% for column in row.values() %}
                        <td>{{ column }}</td>
                    {% endfor %}
                </tr>
            {% endfor %}
        </table>
    """

    tmpl = jinja2.Template(tmpl_src)
    tmpl_autoescaped = jinja2.Template(tmpl_src, autoescape=True)

    def test_jinja2():
        """Jinja2 template"""
        tmpl.render(table=TABLE_DATA)

    def test_jinja2_autoescaped():
        """Jinja2 template autoescaped"""
        tmpl_autoescaped.render(table=TABLE_DATA)

    return [
        test_jinja2,
        test_jinja2_autoescaped,
    ] 
Example #15
Source File: render_benchmark.py    From spitfire with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_django_tests():
    if not django:
        return []

    django.conf.settings.configure()
    django.setup()

    tmpl_src = """
        <table>
            {% for row in table %}
                <tr>
                    {% for column in row.values %}
                        <td>{{ column }}</td>
                    {% endfor %}
                </tr>
            {% endfor %}
        </table>
    """
    tmpl_autoescaped_src = ('{% autoescape on %}' +
                            tmpl_src +
                            '{% endautoescape %}')

    tmpl = django.template.Template(tmpl_src)
    tmpl_autoescaped = django.template.Template(tmpl_autoescaped_src)

    tmpl_context = django.template.Context({'table': TABLE_DATA})

    def test_django():
        """Django template"""
        tmpl.render(tmpl_context)

    def test_django_autoescaped():
        """Django template autoescaped"""
        tmpl_autoescaped.render(tmpl_context)

    return [
        test_django,
        test_django_autoescaped,
    ] 
Example #16
Source File: render_benchmark.py    From spitfire with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_cheetah_tests():
    if not Cheetah:
        return []

    tmpl_src = """
        <table>
            #for $row in $table
                <tr>
                    #for $column in $row.values()
                        <td>$column</td>
                    #end for
                </tr>
            #end for
        </table>
    """

    tmpl_search_list = [{'table': TABLE_DATA}]

    tmpl = Cheetah.Template.Template(tmpl_src, searchList=tmpl_search_list)

    def test_cheetah():
        """Cheetah template"""
        tmpl.respond()

    return [
        test_cheetah,
    ] 
Example #17
Source File: exceptions.py    From mako with MIT License 5 votes vote down vote up
def text_error_template(lookup=None):
    """Provides a template that renders a stack trace in a similar format to
    the Python interpreter, substituting source template filenames, line
    numbers and code for that of the originating source template, as
    applicable.

    """
    import mako.template

    return mako.template.Template(
        r"""
<%page args="error=None, traceback=None"/>
<%!
    from mako.exceptions import RichTraceback
%>\
<%
    tback = RichTraceback(error=error, traceback=traceback)
%>\
Traceback (most recent call last):
% for (filename, lineno, function, line) in tback.traceback:
  File "${filename}", line ${lineno}, in ${function or '?'}
    ${line | trim}
% endfor
${tback.errorname}: ${tback.message}
"""
    ) 
Example #18
Source File: exceptions.py    From mako with MIT License 5 votes vote down vote up
def traceback(self):
        """Return a list of 4-tuple traceback records (i.e. normal python
        format) with template-corresponding lines remapped to the originating
        template.

        """
        return list(self._get_reformatted_records(self.records)) 
Example #19
Source File: exceptions.py    From teleport with Apache License 2.0 5 votes vote down vote up
def traceback(self):
        """Return a list of 4-tuple traceback records (i.e. normal python
        format) with template-corresponding lines remapped to the originating
        template.

        """
        return list(self._get_reformatted_records(self.records)) 
Example #20
Source File: exceptions.py    From teleport with Apache License 2.0 5 votes vote down vote up
def text_error_template(lookup=None):
    """Provides a template that renders a stack trace in a similar format to
    the Python interpreter, substituting source template filenames, line
    numbers and code for that of the originating source template, as
    applicable.

    """
    import mako.template

    return mako.template.Template(
        r"""
<%page args="error=None, traceback=None"/>
<%!
    from mako.exceptions import RichTraceback
%>\
<%
    tback = RichTraceback(error=error, traceback=traceback)
%>\
Traceback (most recent call last):
% for (filename, lineno, function, line) in tback.traceback:
  File "${filename}", line ${lineno}, in ${function or '?'}
    ${line | trim}
% endfor
${tback.errorname}: ${tback.message}
"""
    ) 
Example #21
Source File: exceptions.py    From teleport with Apache License 2.0 5 votes vote down vote up
def traceback(self):
        """Return a list of 4-tuple traceback records (i.e. normal python
        format) with template-corresponding lines remapped to the originating
        template.

        """
        return list(self._get_reformatted_records(self.records)) 
Example #22
Source File: exceptions.py    From teleport with Apache License 2.0 5 votes vote down vote up
def traceback(self):
        """Return a list of 4-tuple traceback records (i.e. normal python
        format) with template-corresponding lines remapped to the originating
        template.

        """
        return list(self._get_reformatted_records(self.records)) 
Example #23
Source File: exceptions.py    From jbox with MIT License 5 votes vote down vote up
def traceback(self):
        """Return a list of 4-tuple traceback records (i.e. normal python
        format) with template-corresponding lines remapped to the originating
        template.

        """
        return list(self._get_reformatted_records(self.records)) 
Example #24
Source File: exceptions.py    From misp42splunk with GNU Lesser General Public License v3.0 5 votes vote down vote up
def text_error_template(lookup=None):
    """Provides a template that renders a stack trace in a similar format to
    the Python interpreter, substituting source template filenames, line
    numbers and code for that of the originating source template, as
    applicable.

    """
    import mako.template

    return mako.template.Template(
        r"""
<%page args="error=None, traceback=None"/>
<%!
    from mako.exceptions import RichTraceback
%>\
<%
    tback = RichTraceback(error=error, traceback=traceback)
%>\
Traceback (most recent call last):
% for (filename, lineno, function, line) in tback.traceback:
  File "${filename}", line ${lineno}, in ${function or '?'}
    ${line | trim}
% endfor
${tback.errorname}: ${tback.message}
"""
    ) 
Example #25
Source File: exceptions.py    From misp42splunk with GNU Lesser General Public License v3.0 5 votes vote down vote up
def traceback(self):
        """Return a list of 4-tuple traceback records (i.e. normal python
        format) with template-corresponding lines remapped to the originating
        template.

        """
        return list(self._get_reformatted_records(self.records)) 
Example #26
Source File: exceptions.py    From misp42splunk with GNU Lesser General Public License v3.0 5 votes vote down vote up
def traceback(self):
        """Return a list of 4-tuple traceback records (i.e. normal python
        format) with template-corresponding lines remapped to the originating
        template.

        """
        return list(self._get_reformatted_records(self.records)) 
Example #27
Source File: render_benchmark.py    From spitfire with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def get_python_tests():
    tmpl_table = string.Template('<table>\n$table\n</table>\n')
    tmpl_row = string.Template('<tr>\n$row\n</tr>\n')
    tmpl_column = string.Template('<td>$column</td>\n')

    def _buffer_fn(write, table):
        write('<table>\n')
        for row in table:
            write('<tr>\n')
            for column in row.itervalues():
                write('<td>')
                write('%s' % column)
                write('</td>\n')
            write('</tr>\n')
        write('</table>\n')

    def test_python_template():
        """Python string template"""
        rows = ''
        for row in TABLE_DATA:
            columns = ''
            for column in row.itervalues():
                columns = columns + tmpl_column.substitute(column=column)
            rows = rows + tmpl_row.substitute(row=columns)
        return tmpl_table.substitute(table=rows)

    def test_python_stringio():
        """Python StringIO buffer"""
        buffer = StringIO.StringIO()
        _buffer_fn(buffer.write, TABLE_DATA)
        return buffer.getvalue()

    def test_python_cstringio():
        """Python cStringIO buffer"""
        buffer = cStringIO.StringIO()
        _buffer_fn(buffer.write, TABLE_DATA)
        return buffer.getvalue()

    def test_python_list():
        """Python list concatenation"""
        buffer = []
        _buffer_fn(buffer.append, TABLE_DATA)
        return ''.join(buffer)

    return [
        test_python_template,
        test_python_stringio,
        test_python_cstringio,
        test_python_list,
    ]