Skip to content
Snippets Groups Projects
template.py 19.3 KiB
Newer Older
Mike Bayer's avatar
Mike Bayer committed
# mako/template.py
# Copyright (C) 2006-2011 the Mako authors and contributors <see AUTHORS file>
Mike Bayer's avatar
Mike Bayer committed
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

Mike Bayer's avatar
Mike Bayer committed
"""Provides the Template class, a facade for parsing, generating and executing
template strings, as well as template runtime operations."""
Mike Bayer's avatar
Mike Bayer committed

from mako.lexer import Lexer
Mike Bayer's avatar
Mike Bayer committed
from mako import runtime, util, exceptions, codegen
import imp, os, re, shutil, stat, sys, tempfile, time, types, weakref
Mike Bayer's avatar
Mike Bayer committed

Mike Bayer's avatar
Mike Bayer committed
class Template(object):
    """Represents a compiled template.
    :class:`.Template` includes a reference to the original
    template source (via the ``.source`` attribute) 
    as well as the source code of the
    generated Python module (i.e. the ``.code`` attribute), 
    as well as a reference to an actual Python module.

    :class:`.Template` is constructed using either a literal string
    representing the template text, or a filename representing a filesystem
    path to a source file.
Mike Bayer's avatar
Mike Bayer committed
    :param text: textual template source.  This argument is mutually
     exclusive versus the "filename" parameter.

    :param filename: filename of the source template.  This argument is 
     mutually exclusive versus the "text" parameter.

    :param buffer_filters: string list of filters to be applied
     to the output of %defs which are buffered, cached, or otherwise
     filtered, after all filters
     defined with the %def itself have been applied. Allows the
     creation of default expression filters that let the output
     of return-valued %defs "opt out" of that filtering via
     passing special attributes or objects.
    :param cache_dir: Filesystem directory where cache files will be
     placed.  See :ref:`caching_toplevel`.
    :param cache_enabled: Boolean flag which enables caching of this
     template.  See :ref:`caching_toplevel`.
    :param cache_type: Type of Beaker caching to be applied to the 
     template. See :ref:`caching_toplevel`.
    :param cache_url: URL of a memcached server with which to use
     for caching.  See :ref:`caching_toplevel`.
    :param default_filters: List of string filter names that will
     be applied to all expressions.  See :ref:`filtering_default_filters`.
    :param disable_unicode: Disables all awareness of Python Unicode
     objects.  See :ref:`unicode_disabled`.
    :param encoding_errors: Error parameter passed to ``encode()`` when
     string encoding is performed. See :ref:`usage_unicode`.
    :param error_handler: Python callable which is called whenever
Mike Bayer's avatar
Mike Bayer committed
     compile or runtime exceptions occur. The callable is passed
     the current context as well as the exception. If the
     callable returns ``True``, the exception is considered to
     be handled, else it is re-raised after the function
     completes. Is used to provide custom error-rendering
     functions.
    :param format_exceptions: if ``True``, exceptions which occur during
     the render phase of this template will be caught and
     formatted into an HTML error page, which then becomes the
     rendered result of the :meth:`render` call. Otherwise,
     runtime exceptions are propagated outwards.
    :param imports: String list of Python statements, typically individual
Mike Bayer's avatar
Mike Bayer committed
     "import" lines, which will be placed into the module level
     preamble of all generated Python modules. See the example
     in :ref:`filtering_default_filters`.
    :param input_encoding: Encoding of the template's source code.  Can
Mike Bayer's avatar
Mike Bayer committed
     be used in lieu of the coding comment. See
     :ref:`usage_unicode` as well as :ref:`unicode_toplevel` for
     details on source encoding.
    :param lookup: a :class:`.TemplateLookup` instance that will be used
Mike Bayer's avatar
Mike Bayer committed
     for all file lookups via the ``<%namespace>``,
     ``<%include>``, and ``<%inherit>`` tags. See
     :ref:`usage_templatelookup`.
Mike Bayer's avatar
Mike Bayer committed
    :param module_directory: Filesystem location where generated 
     Python module files will be placed.
Mike Bayer's avatar
Mike Bayer committed
    :param module_filename: Overrides the filename of the generated 
     Python module file. For advanced usage only.
Mike Bayer's avatar
Mike Bayer committed
    :param output_encoding: The encoding to use when :meth:`.render` 
     is called. See :ref:`usage_unicode` as well as
     :ref:`unicode_toplevel`.
Mike Bayer's avatar
Mike Bayer committed
    :param preprocessor: Python callable which will be passed 
     the full template source before it is parsed. The return
     result of the callable will be used as the template source
     code.
Mike Bayer's avatar
Mike Bayer committed
    :param strict_undefined: Replaces the automatic usage of 
     ``UNDEFINED`` for any undeclared variables not located in
     the :class:`.Context` with an immediate raise of
     ``NameError``. The advantage is immediate reporting of
     missing variables which include the name. New in 0.3.6.
 
    :param uri: string uri or other identifier for this template. 
Mike Bayer's avatar
Mike Bayer committed
     If not provided, the uri is generated from the filesystem
     path, or from the in-memory identity of a non-file-based
     template. The primary usage of the uri is to provide a key
     within :class:`.TemplateLookup`, as well as to generate the
     file path of the generated Python module file, if
     ``module_directory`` is specified.
Mike Bayer's avatar
Mike Bayer committed
    def __init__(self, 
                    text=None, 
                    filename=None, 
                    uri=None, 
                    format_exceptions=False, 
                    error_handler=None, 
                    lookup=None, 
                    output_encoding=None, 
                    encoding_errors='strict', 
                    module_directory=None, 
                    cache_type=None, 
                    cache_dir=None, 
                    cache_url=None, 
                    module_filename=None, 
                    input_encoding=None, 
                    disable_unicode=False, 
                    default_filters=None, 
                    buffer_filters=(), 
                    strict_undefined=False,
Mike Bayer's avatar
Mike Bayer committed
                    imports=None, 
                    preprocessor=None, 
                    cache_enabled=True):
        if uri:
            self.module_id = re.sub(r'\W', "_", uri)
            self.uri = uri
Mike Bayer's avatar
Mike Bayer committed
        elif filename:
            self.module_id = re.sub(r'\W', "_", filename)
            drive, path = os.path.splitdrive(filename)
            path = os.path.normpath(path).replace(os.path.sep, "/")
            self.uri = path
Mike Bayer's avatar
Mike Bayer committed
        else:
            self.module_id = "memory:" + hex(id(self))
            self.uri = self.module_id
        self.input_encoding = input_encoding
        self.output_encoding = output_encoding
        self.encoding_errors = encoding_errors
        self.disable_unicode = disable_unicode
        self.strict_undefined = strict_undefined

        if util.py3k and disable_unicode:
            raise exceptions.UnsupportedError(
                                    "Mako for Python 3 does not "
                                    "support disabling Unicode")
        if default_filters is None:
            if util.py3k or self.disable_unicode:
                self.default_filters = ['str']
            else:
                self.default_filters = ['unicode']
        else:
            self.default_filters = default_filters
        self.buffer_filters = buffer_filters
Mike Bayer's avatar
Mike Bayer committed
        # if plain text, compile code in memory only
Mike Bayer's avatar
Mike Bayer committed
        if text is not None:
            (code, module) = _compile_text(self, text, filename)
            self._code = code
Mike Bayer's avatar
Mike Bayer committed
            self._source = text
            ModuleInfo(module, None, self, filename, code, text)
        elif filename is not None:
Mike Bayer's avatar
Mike Bayer committed
            # if template filename and a module directory, load
            # a filesystem-based module file, generating if needed
            if module_filename is not None:
                path = module_filename
            elif module_directory is not None:
Mike Bayer's avatar
Mike Bayer committed
                path = os.path.abspath(
                        os.path.join(
                            os.path.normpath(module_directory), 
                            os.path.normpath(u) + ".py"
            module = self._compile_from_file(path, filename)
Mike Bayer's avatar
Mike Bayer committed
        else:
Mike Bayer's avatar
Mike Bayer committed
            raise exceptions.RuntimeException(
                                "Template requires text or filename")
Mike Bayer's avatar
Mike Bayer committed
        self.module = module
Mike Bayer's avatar
Mike Bayer committed
        self.format_exceptions = format_exceptions
        self.error_handler = error_handler
Mike Bayer's avatar
Mike Bayer committed
        self.lookup = lookup
        self.cache_type = cache_type
        self.cache_dir = cache_dir
Mike Bayer's avatar
Mike Bayer committed
        self.cache_url = cache_url
Mike Bayer's avatar
Mike Bayer committed
        self.cache_enabled = cache_enabled
    def _compile_from_file(self, path, filename):
        if path is not None:
            util.verify_directory(os.path.dirname(path))
            filemtime = os.stat(filename)[stat.ST_MTIME]
            if not os.path.exists(path) or \
                        os.stat(path)[stat.ST_MTIME] < filemtime:
                _compile_module_file(
                            self, 
                            open(filename, 'rb').read(), 
                            filename, 
                            path)
            module = imp.load_source(self.module_id, path, open(path, 'rb'))
            del sys.modules[self.module_id]
            if module._magic_number != codegen.MAGIC_NUMBER:
                _compile_module_file(
                            self, 
                            open(filename, 'rb').read(), 
                            filename, 
                            path)
                module = imp.load_source(self.module_id, path, open(path, 'rb'))
                del sys.modules[self.module_id]
            ModuleInfo(module, path, self, filename, None, None)
        else:
            # template filename and no module directory, compile code
            # in memory
            code, module = _compile_text(
                                self, 
                                open(filename, 'rb').read(), 
                                filename)
            self._source = None
            self._code = code
            ModuleInfo(module, None, self, filename, code, None)
        return module
Mike Bayer's avatar
Mike Bayer committed
    @property
    def source(self):
        """return the template source code for this Template."""
        return _get_module_info_from_callable(self.callable_).source
Mike Bayer's avatar
Mike Bayer committed

    @property
    def code(self):
        """return the module source code for this Template"""
        return _get_module_info_from_callable(self.callable_).code
Mike Bayer's avatar
Mike Bayer committed
    @property
    def cache(self):
        return self.module._template_cache
Mike Bayer's avatar
Mike Bayer committed
    def render(self, *args, **data):
Mike Bayer's avatar
Mike Bayer committed
        """Render the output of this template as a string.
Mike Bayer's avatar
Mike Bayer committed
        if the template specifies an output encoding, the string
        will be encoded accordingly, else the output is raw (raw
        output uses cStringIO and can't handle multibyte
        characters). a Context object is created corresponding
        to the given data. Arguments that are explictly declared
        by this template's internal rendering method are also
        pulled from the given \*args, \**data members.
Mike Bayer's avatar
Mike Bayer committed
        """
        return runtime._render(self, self.callable_, args, data)
    def render_unicode(self, *args, **data):
        """render the output of this template as a unicode object."""
Mike Bayer's avatar
Mike Bayer committed
        return runtime._render(self, 
                                self.callable_, 
                                args, 
                                data, 
                                as_unicode=True)
Mike Bayer's avatar
Mike Bayer committed
    def render_context(self, context, *args, **kwargs):
        """Render this Template with the given context. 
 
Mike Bayer's avatar
Mike Bayer committed
        the data is written to the context's buffer.
Mike Bayer's avatar
Mike Bayer committed
        """
        if getattr(context, '_with_template', None) is None:
            context._with_template = self
Mike Bayer's avatar
Mike Bayer committed
        runtime._render_context(self, 
                                self.callable_, 
                                context, 
                                *args, 
                                **kwargs)
    def has_def(self, name):
        return hasattr(self.module, "render_%s" % name)
Mike Bayer's avatar
Mike Bayer committed
    def get_def(self, name):
Mike Bayer's avatar
Mike Bayer committed
        """Return a def of this template as a :class:`.DefTemplate`."""
Mike Bayer's avatar
Mike Bayer committed
        return DefTemplate(self, getattr(self.module, "render_%s" % name))

    def _get_def_callable(self, name):
        return getattr(self.module, "render_%s" % name)
Mike Bayer's avatar
Mike Bayer committed
    @property
    def last_modified(self): 
class ModuleTemplate(Template):
    """A Template which is constructed given an existing Python module.
Mike Bayer's avatar
Mike Bayer committed
        f = file("mymodule.py", "w")
Mike Bayer's avatar
Mike Bayer committed
                        module_filename=None, 
                        template=None, 
                        template_filename=None, 
                        module_source=None, 
                        template_source=None,
                        output_encoding=None, 
                        encoding_errors='strict',
                        disable_unicode=False, 
                        format_exceptions=False,
                        error_handler=None, 
                        lookup=None, 
                        cache_type=None,
                        cache_dir=None, 
                        cache_url=None, 
                        cache_enabled=True
    ):
        self.module_id = re.sub(r'\W', "_", module._template_uri)
        self.uri = module._template_uri
        self.input_encoding = module._source_encoding
        self.output_encoding = output_encoding
        self.encoding_errors = encoding_errors
        self.disable_unicode = disable_unicode
        self.module = module
        self.filename = template_filename
Mike Bayer's avatar
Mike Bayer committed
        ModuleInfo(module, 
                        module_filename, 
                        self, 
                        template_filename, 
                        module_source, 
                        template_source)
        self.callable_ = self.module.render_body
        self.format_exceptions = format_exceptions
        self.error_handler = error_handler
        self.lookup = lookup
        self.cache_type = cache_type
        self.cache_dir = cache_dir
        self.cache_url = cache_url
Mike Bayer's avatar
Mike Bayer committed
        self.cache_enabled = cache_enabled
Mike Bayer's avatar
Mike Bayer committed
class DefTemplate(Template):
Mike Bayer's avatar
Mike Bayer committed
    """a Template which represents a callable def in a parent
    template."""
Mike Bayer's avatar
Mike Bayer committed
    def __init__(self, parent, callable_):
        self.parent = parent
Mike Bayer's avatar
Mike Bayer committed
        self.callable_ = callable_
        self.output_encoding = parent.output_encoding
        self.module = parent.module
        self.encoding_errors = parent.encoding_errors
        self.format_exceptions = parent.format_exceptions
        self.error_handler = parent.error_handler
        self.lookup = parent.lookup

Mike Bayer's avatar
Mike Bayer committed
    def get_def(self, name):
        return self.parent.get_def(name)

class ModuleInfo(object):
Mike Bayer's avatar
Mike Bayer committed
    """Stores information about a module currently loaded into
    memory, provides reverse lookups of template source, module
    source code based on a module's identifier.
Mike Bayer's avatar
Mike Bayer committed
     """
    _modules = weakref.WeakValueDictionary()

Mike Bayer's avatar
Mike Bayer committed
    def __init__(self, 
                    module, 
                    module_filename, 
                    template, 
                    template_filename, 
                    module_source, 
                    template_source):
        self.module = module
        self.module_filename = module_filename
        self.template_filename = template_filename
        self.module_source = module_source
        self.template_source = template_source
        self._modules[module.__name__] = template._mmarker = self
        if module_filename:
            self._modules[module_filename] = self
Mike Bayer's avatar
Mike Bayer committed
    @property
    def code(self):
        if self.module_source is not None:
            return self.module_source
        else:
            return open(self.module_filename).read()
Mike Bayer's avatar
Mike Bayer committed
    @property
    def source(self):
        if self.template_source is not None:
Mike Bayer's avatar
Mike Bayer committed
            if self.module._source_encoding and \
                    not isinstance(self.template_source, unicode):
                return self.template_source.decode(
                                self.module._source_encoding)
            if self.module._source_encoding:
                return open(self.template_filename, 'rb').read().\
Mike Bayer's avatar
Mike Bayer committed
                                decode(self.module._source_encoding)
                return open(self.template_filename).read()
def _compile_text(template, text, filename):
    identifier = template.module_id
Mike Bayer's avatar
Mike Bayer committed
    lexer = Lexer(text, 
                    filename, 
                    disable_unicode=template.disable_unicode,
                    input_encoding=template.input_encoding,
                    preprocessor=template.preprocessor)
Mike Bayer's avatar
Mike Bayer committed
    source = codegen.compile(node, 
                            template.uri, 
                            filename,
                            default_filters=template.default_filters,
                            buffer_filters=template.buffer_filters, 
                            imports=template.imports, 
                            source_encoding=lexer.encoding,
                            generate_magic_comment=template.disable_unicode,
                            disable_unicode=template.disable_unicode,
                            strict_undefined=template.strict_undefined)
Mike Bayer's avatar
Mike Bayer committed
    cid = identifier
    if not util.py3k and isinstance(cid, unicode):
        cid = cid.encode()
    module = types.ModuleType(cid)
Mike Bayer's avatar
Mike Bayer committed
    code = compile(source, cid, 'exec')
Mike Bayer's avatar
Mike Bayer committed
    exec code in module.__dict__, module.__dict__
    return (source, module)
def _compile_module_file(template, text, filename, outputpath):
    identifier = template.module_id
Mike Bayer's avatar
Mike Bayer committed
    lexer = Lexer(text, 
                    filename, 
                    disable_unicode=template.disable_unicode,
                    input_encoding=template.input_encoding,
                    preprocessor=template.preprocessor)
Mike Bayer's avatar
Mike Bayer committed
    source = codegen.compile(node, 
                                template.uri, 
                                filename,
                                default_filters=template.default_filters,
                                buffer_filters=template.buffer_filters,
                                imports=template.imports,
                                source_encoding=lexer.encoding,
                                generate_magic_comment=True,
                                disable_unicode=template.disable_unicode,
                                strict_undefined=template.strict_undefined)
    # make tempfiles in the same location as the ultimate 
    # location.   this ensures they're on the same filesystem,
    # avoiding synchronization issues.
    (dest, name) = tempfile.mkstemp(dir=os.path.dirname(outputpath))
    if isinstance(source, unicode):
        source = source.encode(lexer.encoding or 'ascii')
    os.write(dest, source)
    os.close(dest)
    shutil.move(name, outputpath)
Mike Bayer's avatar
Mike Bayer committed
def _get_module_info_from_callable(callable_):
    return _get_module_info(callable_.func_globals['__name__'])
Mike Bayer's avatar
Mike Bayer committed
def _get_module_info(filename):
    return ModuleInfo._modules[filename]