관리-도구
편집 파일: template.cpython-38.pyc
U +�e1] � @ s d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl mZ ddl mZ ddl m Z ddl mZ ddl mZ dd lmZ G d d� d�ZG dd � d e�ZG dd� de�ZG dd� d�Zdd� Zdd� Zdd� Zdd� Zdd� ZdS )z�Provides the Template class, a facade for parsing, generating and executing template strings, as well as template runtime operations.� N)�cache)�codegen)�compat)� exceptions)�runtime)�util)�Lexerc @ s� e Zd ZdZeZd,dd �Zejd d� �Z dd � Z dd� Zedd� �Z edd� �Zejdd� �Zedd� �Zedd� �Zedd� �Zdd� Zdd� Zd d!� Zd"d#� Zd$d%� Zd&d'� Zd(d)� Zed*d+� �ZdS )-�Templatea| Represents a compiled template. :class:`.Template` includes a reference to the original template source (via the :attr:`.source` attribute) as well as the source code of the generated Python module (i.e. the :attr:`.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. :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 ``%def``\ s 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 ``%def``\ s "opt out" of that filtering via passing special attributes or objects. :param cache_args: Dictionary of cache configuration arguments that will be passed to the :class:`.CacheImpl`. See :ref:`caching_toplevel`. :param cache_dir: .. deprecated:: 0.6 Use the ``'dir'`` argument in the ``cache_args`` dictionary. See :ref:`caching_toplevel`. :param cache_enabled: Boolean flag which enables caching of this template. See :ref:`caching_toplevel`. :param cache_impl: String name of a :class:`.CacheImpl` caching implementation to use. Defaults to ``'beaker'``. :param cache_type: .. deprecated:: 0.6 Use the ``'type'`` argument in the ``cache_args`` dictionary. See :ref:`caching_toplevel`. :param cache_url: .. deprecated:: 0.6 Use the ``'url'`` argument in the ``cache_args`` dictionary. 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 enable_loop: When ``True``, enable the ``loop`` context variable. This can be set to ``False`` to support templates that may be making usage of the name "``loop``". Individual templates can re-enable the "loop" context by placing the directive ``enable_loop="True"`` inside the ``<%page>`` tag -- see :ref:`migrating_loop`. :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 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. .. seealso:: :paramref:`.Template.include_error_handler` - include-specific error handler function :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 "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 future_imports: String list of names to import from `__future__`. These will be concatenated into a comma-separated string and inserted into the beginning of the template, e.g. ``futures_imports=['FOO', 'BAR']`` results in ``from __future__ import FOO, BAR``. If you're interested in using features like the new division operator, you must use future_imports to convey that to the renderer, as otherwise the import will not appear as the first executed statement in the generated code and will therefore not have the desired effect. :param include_error_handler: An error handler that runs when this template is included within another one via the ``<%include>`` tag, and raises an error. Compare to the :paramref:`.Template.error_handler` option. .. versionadded:: 1.0.6 .. seealso:: :paramref:`.Template.error_handler` - top-level error handler function :param input_encoding: Encoding of the template's source code. Can 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 for all file lookups via the ``<%namespace>``, ``<%include>``, and ``<%inherit>`` tags. See :ref:`usage_templatelookup`. :param module_directory: Filesystem location where generated Python module files will be placed. :param module_filename: Overrides the filename of the generated Python module file. For advanced usage only. :param module_writer: A callable which overrides how the Python module is written entirely. The callable is passed the encoded source content of the module and the destination path to be written to. The default behavior of module writing uses a tempfile in conjunction with a file move in order to make the operation atomic. So a user-defined module writing function that mimics the default behavior would be: .. sourcecode:: python import tempfile import os import shutil def module_writer(source, outputpath): (dest, name) = \\ tempfile.mkstemp( dir=os.path.dirname(outputpath) ) os.write(dest, source) os.close(dest) shutil.move(name, outputpath) from mako.template import Template mytemplate = Template( filename="index.html", module_directory="/path/to/modules", module_writer=module_writer ) The function is provided for unusual configurations where certain platform-specific permissions or other special steps are needed. :param output_encoding: The encoding to use when :meth:`.render` is called. See :ref:`usage_unicode` as well as :ref:`unicode_toplevel`. :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. :param lexer_cls: A :class:`.Lexer` class used to parse the template. The :class:`.Lexer` class is used by default. .. versionadded:: 0.7.4 :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. .. versionadded:: 0.3.6 :param uri: string URI or other identifier for this template. 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. NF�strict�beakerT� c ! C s |rt �dd|�| _|| _n^|r`t �dd|�| _tj�|�\}}tj�|��tjj d�}|| _ndt t| �� | _| j| _| j}|�d�r�|dd � }tj�|�}|�d�r�t �d| j ��|| _|| _|| _|| _|| _|| _|d kr�dg| _n|| _|| _|| _|| _|| _|d k �r || _|d k �r\t| ||�\}} || _|| _t| d | ||||� nd|d k �r�|d k �rv|}n2| d k �r�tj�tj� tj�| �|d ��}nd }| �!||�} n t �"d ��| | _#|| _$| j#j%| _&|| _'|| _(|| _)|| _*| | _+| �,||| | ||� d S )N�\W�_�/zmemory:� z..zNTemplate uri "%s" is invalid - it cannot be relative outside of the root path.�strz.pyz"Template requires text or filename)-�re�sub� module_id�uri�os�path� splitdrive�normpath�replace�sep�hex�id� startswithr ZTemplateLookupException�input_encoding�output_encoding�encoding_errors�enable_loop�strict_undefined� module_writer�default_filters�buffer_filters�imports�future_imports�preprocessor� lexer_cls� _compile_text�_code�_source� ModuleInfo�abspath�join�_compile_from_fileZRuntimeException�module�filename�render_body� callable_�format_exceptions� error_handler�include_error_handler�lookup�module_directory�_setup_cache_args)!�self�textr3 r r6 r7 r9 r r! r: � cache_args� cache_impl� cache_enabled� cache_type� cache_dir� cache_url�module_filenamer r$ r% r&