¡@

Home 

OpenStack Study: jinja_view.py

OpenStack Index

**** CubicPower OpenStack Study ****

# Copyright 2013 Red Hat, Inc.

#

# Licensed under the Apache License, Version 2.0 (the "License"); you may

# not use this file except in compliance with the License. You may obtain

# a copy of the License at

#

# http://www.apache.org/licenses/LICENSE-2.0

#

# Unless required by applicable law or agreed to in writing, software

# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT

# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the

# License for the specific language governing permissions and limitations

# under the License.

"""Provides Jinja Views

This module provides views that utilize the Jinja templating

system for serialization. For more information on Jinja, please

see http://jinja.pocoo.org/ .

"""

import jinja2

**** CubicPower OpenStack Study ****

class JinjaView(object):

"""A Jinja View

This view renders the given model using the provided Jinja

template. The template can be given in various ways.

If the `VIEw_TEXT` property is

**** CubicPower OpenStack Study ****

    def __init__(self, path=None, text=None):

        try:

            self._text = self.VIEW_TEXT

        except AttributeError:

            if path is not None:

                with open(path, 'r') as f:

                    self._text = f.read()

            elif text is not None:

                self._text = text

            else:

                self._text = ""

        if self._text[0] == "\n":

            self._text = self._text[1:]

        newtext = self._text.lstrip()

        amt = len(self._text) - len(newtext)

        if (amt > 0):

            base_indent = self._text[0:amt]

            lines = self._text.splitlines()

            newlines = []

            for line in lines:

                if line.startswith(base_indent):

                    newlines.append(line[amt:])

                else:

                    newlines.append(line)

            self._text = "\n".join(newlines)

        if self._text[-1] == "\n":

            self._text = self._text[:-1]

        self._regentemplate = True

        self._templatecache = None

**** CubicPower OpenStack Study ****

    def __call__(self, model):

        return self.template.render(**model)

    @property

**** CubicPower OpenStack Study ****

    def template(self):

        """Get the Compiled Template

        Gets the compiled template, using a cached copy if possible

        (stored in attr:`_templatecache`) or otherwise recompiling

        the template if the compiled template is not present or is

        invalid (due to attr:`_regentemplate` being set to True).

        :returns: the compiled Jinja template

        :rtype: :class:`jinja2.Template`

        """

        if self._templatecache is None or self._regentemplate:

            self._templatecache = jinja2.Template(self._text)

            self._regentemplate = False

        return self._templatecache

**** CubicPower OpenStack Study ****

    def _gettext(self):

        """Get the Template Text

        Gets the text of the current template

        :returns: the text of the Jinja template

        :rtype: str

        """

        return self._text

**** CubicPower OpenStack Study ****

    def _settext(self, textval):

        """Set the Template Text

        Sets the text of the current template, marking it

        for recompilation next time the compiled template

        is retrived via attr:`template` .

        :param str textval: the new text of the Jinja template

        """

        self._text = textval

        self.regentemplate = True

    text = property(_gettext, _settext)