Source code for utah.template

# Ubuntu Testing Automation Harness
# Copyright 2013 Canonical Ltd.

# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.

# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Provide easy accessors to the Jinja2 template library"""

import os

from jinja2 import Environment, FileSystemLoader

from utah.config import config


def _add_backslash_filter(value):
    r"""Append backslash character to each line.

    :returns: an version of the string where \\'s have been to each \\n
    :rtype: string

    """
    return '\n'.join(['{}\\'.format(line) for line in value.splitlines()])


[docs]def as_buff(template, **kw): """Return the rendered template as a string.""" if not getattr(as_buff, '_env', None): paths = [ os.path.join(os.path.dirname(__file__), '../templates'), config.template_dir, ] as_buff._env = \ Environment(loader=FileSystemLoader(paths)) as_buff._env.filters['add_backslash'] = _add_backslash_filter template = as_buff._env.get_template(template) return template.render(kw)
[docs]def write(template, path, **kw): """Render the given template as a file.""" content = as_buff(template, **kw) with open(path, 'w') as f: f.write(content)
Read the Docs v: latest
Versions
latest
Downloads
PDF
HTML
Epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.