o
    Qœ_¦“  ã                   @   sb  d Z ddlZddlmZ ddlZddlZddlZddlZddl	Z	ddl
mZ ddlmZ ddlmZmZmZ ddlmZmZmZmZmZmZmZmZ ddlZejrYddlmZmZ d	ZG d
d„ dƒZeƒ Z de!de!de!fdd„Z"G dd„ de#ƒZ$G dd„ de#ƒZ%G dd„ de%ƒZ&G dd„ de%ƒZ'G dd„ de#ƒZ(G dd„ de(ƒZ)G dd„ de(ƒZ*G dd „ d e(ƒZ+G d!d"„ d"e(ƒZ,G d#d$„ d$e(ƒZ-G d%d&„ d&e(ƒZ.G d'd(„ d(e(ƒZ/G d)d*„ d*e(ƒZ0G d+d,„ d,e(ƒZ1G d-d.„ d.e(ƒZ2G d/d0„ d0e2ƒZ3G d1d2„ d2e(ƒZ4G d3d4„ d4e5ƒZ6G d5d6„ d6e#ƒZ7G d7d8„ d8e#ƒZ8d9e!de!fd:d;„Z9		dBd<e8d=e$d>ee! d?ee! de*f
d@dA„Z:dS )CaŒ  A simple template system that compiles templates to Python code.

Basic usage looks like::

    t = template.Template("<html>{{ myvalue }}</html>")
    print(t.generate(myvalue="XXX"))

`Loader` is a class that loads templates from a root directory and caches
the compiled templates::

    loader = template.Loader("/home/btaylor")
    print(loader.load("test.html").generate(myvalue="XXX"))

We compile all templates to raw Python. Error-reporting is currently... uh,
interesting. Syntax for the templates::

    ### base.html
    <html>
      <head>
        <title>{% block title %}Default title{% end %}</title>
      </head>
      <body>
        <ul>
          {% for student in students %}
            {% block student %}
              <li>{{ escape(student.name) }}</li>
            {% end %}
          {% end %}
        </ul>
      </body>
    </html>

    ### bold.html
    {% extends "base.html" %}

    {% block title %}A bolder title{% end %}

    {% block student %}
      <li><span style="bold">{{ escape(student.name) }}</span></li>
    {% end %}

Unlike most other template systems, we do not put any restrictions on the
expressions you can include in your statements. ``if`` and ``for`` blocks get
translated exactly into Python, so you can do complex expressions like::

   {% for student in [p for p in people if p.student and p.age > 23] %}
     <li>{{ escape(student.name) }}</li>
   {% end %}

Translating directly to Python means you can apply functions to expressions
easily, like the ``escape()`` function in the examples above. You can pass
functions in to your template just like any other variable
(In a `.RequestHandler`, override `.RequestHandler.get_template_namespace`)::

   ### Python code
   def add(x, y):
      return x + y
   template.execute(add=add)

   ### The template
   {{ add(1, 2) }}

We provide the functions `escape() <.xhtml_escape>`, `.url_escape()`,
`.json_encode()`, and `.squeeze()` to all templates by default.

Typical applications do not create `Template` or `Loader` instances by
hand, but instead use the `~.RequestHandler.render` and
`~.RequestHandler.render_string` methods of
`tornado.web.RequestHandler`, which load templates automatically based
on the ``template_path`` `.Application` setting.

Variable names beginning with ``_tt_`` are reserved by the template
system and should not be used by application code.

Syntax Reference
----------------

Template expressions are surrounded by double curly braces: ``{{ ... }}``.
The contents may be any python expression, which will be escaped according
to the current autoescape setting and inserted into the output.  Other
template directives use ``{% %}``.

To comment out a section so that it is omitted from the output, surround it
with ``{# ... #}``.


To include a literal ``{{``, ``{%``, or ``{#`` in the output, escape them as
``{{!``, ``{%!``, and ``{#!``, respectively.


``{% apply *function* %}...{% end %}``
    Applies a function to the output of all template code between ``apply``
    and ``end``::

        {% apply linkify %}{{name}} said: {{message}}{% end %}

    Note that as an implementation detail apply blocks are implemented
    as nested functions and thus may interact strangely with variables
    set via ``{% set %}``, or the use of ``{% break %}`` or ``{% continue %}``
    within loops.

``{% autoescape *function* %}``
    Sets the autoescape mode for the current file.  This does not affect
    other files, even those referenced by ``{% include %}``.  Note that
    autoescaping can also be configured globally, at the `.Application`
    or `Loader`.::

        {% autoescape xhtml_escape %}
        {% autoescape None %}

``{% block *name* %}...{% end %}``
    Indicates a named, replaceable block for use with ``{% extends %}``.
    Blocks in the parent template will be replaced with the contents of
    the same-named block in a child template.::

        <!-- base.html -->
        <title>{% block title %}Default title{% end %}</title>

        <!-- mypage.html -->
        {% extends "base.html" %}
        {% block title %}My page title{% end %}

``{% comment ... %}``
    A comment which will be removed from the template output.  Note that
    there is no ``{% end %}`` tag; the comment goes from the word ``comment``
    to the closing ``%}`` tag.

``{% extends *filename* %}``
    Inherit from another template.  Templates that use ``extends`` should
    contain one or more ``block`` tags to replace content from the parent
    template.  Anything in the child template not contained in a ``block``
    tag will be ignored.  For an example, see the ``{% block %}`` tag.

``{% for *var* in *expr* %}...{% end %}``
    Same as the python ``for`` statement.  ``{% break %}`` and
    ``{% continue %}`` may be used inside the loop.

``{% from *x* import *y* %}``
    Same as the python ``import`` statement.

``{% if *condition* %}...{% elif *condition* %}...{% else %}...{% end %}``
    Conditional statement - outputs the first section whose condition is
    true.  (The ``elif`` and ``else`` sections are optional)

``{% import *module* %}``
    Same as the python ``import`` statement.

``{% include *filename* %}``
    Includes another template file.  The included file can see all the local
    variables as if it were copied directly to the point of the ``include``
    directive (the ``{% autoescape %}`` directive is an exception).
    Alternately, ``{% module Template(filename, **kwargs) %}`` may be used
    to include another template with an isolated namespace.

``{% module *expr* %}``
    Renders a `~tornado.web.UIModule`.  The output of the ``UIModule`` is
    not escaped::

        {% module Template("foo.html", arg=42) %}

    ``UIModules`` are a feature of the `tornado.web.RequestHandler`
    class (and specifically its ``render`` method) and will not work
    when the template system is used on its own in other contexts.

``{% raw *expr* %}``
    Outputs the result of the given expression without autoescaping.

``{% set *x* = *y* %}``
    Sets a local variable.

``{% try %}...{% except %}...{% else %}...{% finally %}...{% end %}``
    Same as the python ``try`` statement.

``{% while *condition* %}... {% end %}``
    Same as the python ``while`` statement.  ``{% break %}`` and
    ``{% continue %}`` may be used inside the loop.

``{% whitespace *mode* %}``
    Sets the whitespace mode for the remainder of the current file
    (or until the next ``{% whitespace %}`` directive). See
    `filter_whitespace` for available options. New in Tornado 4.3.
é    N)ÚStringIO)Úescape)Úapp_log)Ú
ObjectDictÚexec_inÚunicode_type)ÚAnyÚUnionÚCallableÚListÚDictÚIterableÚOptionalÚTextIO)ÚTupleÚContextManagerÚxhtml_escapec                   @   s   e Zd ZdS )Ú_UnsetMarkerN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú2/usr/lib/python3/dist-packages/tornado/template.pyr   Ü   s    r   ÚmodeÚtextÚreturnc                 C   sV   | dkr|S | dkrt  dd|¡}t  dd|¡}|S | dkr%t  dd|¡S td	|  ƒ‚)
a’  Transform whitespace in ``text`` according to ``mode``.

    Available modes are:

    * ``all``: Return all whitespace unmodified.
    * ``single``: Collapse consecutive whitespace with a single whitespace
      character, preserving newlines.
    * ``oneline``: Collapse all runs of whitespace into a single space
      character, removing all newlines in the process.

    .. versionadded:: 4.3
    ÚallÚsinglez([\t ]+)ú z
(\s*\n\s*)Ú
Úonelinez(\s+)zinvalid whitespace mode %s)ÚreÚsubÚ	Exception)r   r   r   r   r   Úfilter_whitespaceã   s   r$   c                   @   s¨   e Zd ZdZddeedfdeeef deded dee	e
f d	eeee
f  d
ee ddfdd„Zdedefdd„Zded defdd„Zded ded fdd„ZdS )ÚTemplatez—A compiled template.

    We compile into Python from the given template_string. You can generate
    the template from variables with generate().
    z<string>NÚtemplate_stringÚnameÚloaderÚ
BaseLoaderÚcompress_whitespaceÚ
autoescapeÚ
whitespacer   c           	      C   sH  t  |¡| _|tur|durtdƒ‚|rdnd}|du r4|r%|jr%|j}n| d¡s/| d¡r2d}nd}|dus:J ‚t|dƒ t|t	ƒsH|| _
n
|rO|j
| _
nt| _
|rW|jni | _t|t  |¡|ƒ}t| t|| ƒƒ| _|  |¡| _|| _ztt  | j¡d| j d	d
¡ ddd�| _W dS  ty£   t| jƒ ¡ }t d| j|¡ ‚ w )a³  Construct a Template.

        :arg str template_string: the contents of the template file.
        :arg str name: the filename from which the template was loaded
            (used for error message).
        :arg tornado.template.BaseLoader loader: the `~tornado.template.BaseLoader` responsible
            for this template, used to resolve ``{% include %}`` and ``{% extend %}`` directives.
        :arg bool compress_whitespace: Deprecated since Tornado 4.3.
            Equivalent to ``whitespace="single"`` if true and
            ``whitespace="all"`` if false.
        :arg str autoescape: The name of a function in the template
            namespace, or ``None`` to disable escaping by default.
        :arg str whitespace: A string specifying treatment of whitespace;
            see `filter_whitespace` for options.

        .. versionchanged:: 4.3
           Added ``whitespace`` parameter; deprecated ``compress_whitespace``.
        Nz2cannot set both whitespace and compress_whitespacer   r   z.htmlz.jsÚ z%s.generated.pyÚ.Ú_ÚexecT)Údont_inheritz%s code:
%s)r   Ú
native_strr'   Ú_UNSETr#   r,   Úendswithr$   Ú
isinstancer   r+   Ú_DEFAULT_AUTOESCAPEÚ	namespaceÚ_TemplateReaderÚ_FileÚ_parseÚfileÚ_generate_pythonÚcoder(   ÚcompileÚ
to_unicodeÚreplaceÚcompiledÚ_format_codeÚrstripr   Úerror)	Úselfr&   r'   r(   r*   r+   r,   ÚreaderÚformatted_coder   r   r   Ú__init__  sF   




üýzTemplate.__init__Úkwargsc                    sŽ   t jt jt jt jt jt jtt jtt	fˆ j
 dd¡t‡ fdd„d�dœ}| ˆ j¡ | |¡ tˆ j|ƒ t tg t	f |d ¡}t ¡  |ƒ S )z0Generate this template with the given arguments.r.   r/   c                    s   ˆ j S ©N)r=   ©r'   ©rE   r   r   Ú<lambda>`  s    z#Template.generate.<locals>.<lambda>)Ú
get_source)r   r   Ú
url_escapeÚjson_encodeÚsqueezeÚlinkifyÚdatetimeÚ_tt_utf8Ú_tt_string_typesr   Ú
__loader__Ú_tt_execute)r   r   rO   rP   rQ   rR   rS   Úutf8r   Úbytesr'   r@   r   Úupdater7   r   rA   ÚtypingÚcastr
   Ú	linecacheÚ
clearcache)rE   rI   r7   Úexecuter   rL   r   ÚgenerateQ  s$   ó
zTemplate.generatec                 C   sr   t ƒ }z0i }|  |¡}| ¡  |D ]}| ||¡ qt||||d jƒ}|d  |¡ | ¡ W | ¡  S | ¡  w ©Nr   )	r   Ú_get_ancestorsÚreverseÚfind_named_blocksÚ_CodeWriterÚtemplater`   ÚgetvalueÚclose)rE   r(   ÚbufferÚnamed_blocksÚ	ancestorsÚancestorÚwriterr   r   r   r<   l  s   
zTemplate._generate_pythonr9   c                 C   sR   | j g}| j jjD ]}t|tƒr&|stdƒ‚| |j| j¡}| | 	|¡¡ q	|S )Nz1{% extends %} block found, but no template loader)
r;   ÚbodyÚchunksr5   Ú_ExtendsBlockÚ
ParseErrorÚloadr'   Úextendrb   )rE   r(   rk   Úchunkrf   r   r   r   rb   {  s   
ÿ€zTemplate._get_ancestors)r   r   r   Ú__doc__r3   r	   ÚstrrY   r   Úboolr   rH   r   r`   r<   r   rb   r   r   r   r   r%   ü   s2    ù
þýü
ûúù
øKr%   c                	   @   s–   e Zd ZdZeddfdedeeeef  dee ddfdd„Z	dd	d
„Z
ddedee defdd„Zddedee defdd„Zdedefdd„ZdS )r)   zàBase class for template loaders.

    You must use a template loader to use template constructs like
    ``{% extends %}`` and ``{% include %}``. The loader caches all
    templates after they are loaded the first time.
    Nr+   r7   r,   r   c                 C   s*   || _ |pi | _|| _i | _t ¡ | _dS )a�  Construct a template loader.

        :arg str autoescape: The name of a function in the template
            namespace, such as "xhtml_escape", or ``None`` to disable
            autoescaping by default.
        :arg dict namespace: A dictionary to be added to the default template
            namespace, or ``None``.
        :arg str whitespace: A string specifying default behavior for
            whitespace in templates; see `filter_whitespace` for options.
            Default is "single" for files ending in ".html" and ".js" and
            "all" for other files.

        .. versionchanged:: 4.3
           Added ``whitespace`` parameter.
        N)r+   r7   r,   Ú	templatesÚ	threadingÚRLockÚlock)rE   r+   r7   r,   r   r   r   rH   �  s
   
zBaseLoader.__init__c                 C   s2   | j � i | _W d  ƒ dS 1 sw   Y  dS )z'Resets the cache of compiled templates.N)r{   rx   rL   r   r   r   Úreset°  s   "ÿzBaseLoader.resetr'   Úparent_pathc                 C   ó   t ƒ ‚)z@Converts a possibly-relative path to absolute (used internally).©ÚNotImplementedError©rE   r'   r}   r   r   r   Úresolve_pathµ  s   zBaseLoader.resolve_pathc                 C   s\   | j ||d�}| j� || jvr|  |¡| j|< | j| W  d  ƒ S 1 s'w   Y  dS )zLoads a template.)r}   N)r‚   r{   rx   Ú_create_templater�   r   r   r   rr   ¹  s   
$ýzBaseLoader.loadc                 C   r~   rJ   r   ©rE   r'   r   r   r   rƒ   Á  ó   zBaseLoader._create_template)r   NrJ   )r   r   r   ru   r6   rv   r   r   r   rH   r|   r‚   r%   rr   rƒ   r   r   r   r   r)   ˆ  s$    	üþýü
û
 r)   c                       s\   e Zd ZdZdededdf‡ fdd„Zdded	ee defd
d„Zdede	fdd„Z
‡  ZS )ÚLoaderz?A template loader that loads from a single root directory.
    Úroot_directoryrI   r   Nc                    s$   t ƒ jdi |¤Ž tj |¡| _d S ©Nr   )ÚsuperrH   ÚosÚpathÚabspathÚroot)rE   r‡   rI   ©Ú	__class__r   r   rH   É  s   zLoader.__init__r'   r}   c                 C   s‚   |r?|  d¡s?|  d¡s?|  d¡s?tj | j|¡}tj tj |¡¡}tj tj ||¡¡}|  | j¡r?|t| jƒd d … }|S )Nú<ú/é   )Ú
startswithrŠ   r‹   Újoinr�   ÚdirnamerŒ   Úlen)rE   r'   r}   Úcurrent_pathÚfile_dirÚrelative_pathr   r   r   r‚   Í  s   ÿþýüzLoader.resolve_pathc                 C   sT   t j | j|¡}t|dƒ�}t| ¡ || d�}|W  d   ƒ S 1 s#w   Y  d S )NÚrb©r'   r(   )rŠ   r‹   r”   r�   Úopenr%   Úread)rE   r'   r‹   Úfrf   r   r   r   rƒ   Û  s
   $þzLoader._create_templaterJ   )r   r   r   ru   rv   r   rH   r   r‚   r%   rƒ   Ú__classcell__r   r   rŽ   r   r†   Å  s
    r†   c                       sd   e Zd ZdZdeeef deddf‡ fdd„Zdded	ee defd
d„Z	dede
fdd„Z‡  ZS )Ú
DictLoaderz/A template loader that loads from a dictionary.ÚdictrI   r   Nc                    s   t ƒ jdi |¤Ž || _d S rˆ   )r‰   rH   r¡   )rE   r¡   rI   rŽ   r   r   rH   å  s   
zDictLoader.__init__r'   r}   c                 C   sB   |r|  d¡s|  d¡s|  d¡st |¡}t t ||¡¡}|S )Nr�   r‘   )r“   Ú	posixpathr•   Únormpathr”   )rE   r'   r}   r˜   r   r   r   r‚   é  s   ÿþýü
zDictLoader.resolve_pathc                 C   s   t | j| || d�S )Nr›   )r%   r¡   r„   r   r   r   rƒ   ô  ó   zDictLoader._create_templaterJ   )r   r   r   ru   r   rv   r   rH   r   r‚   r%   rƒ   rŸ   r   r   rŽ   r   r    â  s
    "r    c                   @   sJ   e Zd Zded  fdd„Zddd„Zd	ee d
ee	df ddfdd„Z
dS )Ú_Noder   c                 C   s   dS rˆ   r   rL   r   r   r   Ú
each_childù  ó   z_Node.each_childrm   re   Nc                 C   r~   rJ   r   ©rE   rm   r   r   r   r`   ü  r…   z_Node.generater(   rj   Ú_NamedBlockc                 C   s   |   ¡ D ]}| ||¡ qd S rJ   )r¦   rd   )rE   r(   rj   Úchildr   r   r   rd   ÿ  s   ÿz_Node.find_named_blocks©rm   re   r   N)r   r   r   r   r¦   r`   r   r)   r   rv   rd   r   r   r   r   r¥   ø  s    
ÿ
ÿþr¥   c                   @   s>   e Zd Zdeddddfdd„Zdd
d„Zded fdd„ZdS )r9   rf   rn   Ú
_ChunkListr   Nc                 C   s   || _ || _d| _d S ra   )rf   rn   Úline)rE   rf   rn   r   r   r   rH     ó   
z_File.__init__rm   re   c                 C   sr   |  d| j¡ | ¡ �$ |  d| j¡ |  d| j¡ | j |¡ |  d| j¡ W d   ƒ d S 1 s2w   Y  d S )Nzdef _tt_execute():ú_tt_buffer = []ú_tt_append = _tt_buffer.appendú$return _tt_utf8('').join(_tt_buffer))Ú
write_liner­   Úindentrn   r`   r¨   r   r   r   r`     s   
"üz_File.generater¥   c                 C   ó   | j fS rJ   ©rn   rL   r   r   r   r¦     ó   z_File.each_childr«   )r   r   r   r%   rH   r`   r   r¦   r   r   r   r   r9     s    
r9   c                   @   s>   e Zd Zdee ddfdd„Zddd	„Zded
 fdd„ZdS )r¬   ro   r   Nc                 C   ó
   || _ d S rJ   ©ro   )rE   ro   r   r   r   rH     ó   
z_ChunkList.__init__rm   re   c                 C   s   | j D ]}| |¡ qd S rJ   )ro   r`   )rE   rm   rt   r   r   r   r`     s   
ÿz_ChunkList.generater¥   c                 C   ó   | j S rJ   r¸   rL   r   r   r   r¦      r…   z_ChunkList.each_childr«   )	r   r   r   r   r¥   rH   r`   r   r¦   r   r   r   r   r¬     s    
r¬   c                
   @   sh   e Zd Zdededededdf
dd„Zded	 fd
d„Z	ddd„Z
dee deed f ddfdd„ZdS )r©   r'   rn   rf   r­   r   Nc                 C   s   || _ || _|| _|| _d S rJ   )r'   rn   rf   r­   )rE   r'   rn   rf   r­   r   r   r   rH   %  s   
z_NamedBlock.__init__r¥   c                 C   r´   rJ   rµ   rL   r   r   r   r¦   +  r¶   z_NamedBlock.each_childrm   re   c                 C   sN   |j | j }| |j| j¡� |j |¡ W d   ƒ d S 1 s w   Y  d S rJ   )rj   r'   Úincluderf   r­   rn   r`   )rE   rm   Úblockr   r   r   r`   .  s   "ÿz_NamedBlock.generater(   rj   c                 C   s   | || j < t | ||¡ d S rJ   )r'   r¥   rd   )rE   r(   rj   r   r   r   rd   3  s   
z_NamedBlock.find_named_blocksr«   )r   r   r   rv   r¥   r%   ÚintrH   r   r¦   r`   r   r)   r   rd   r   r   r   r   r©   $  s    
ÿ
ÿþr©   c                   @   s   e Zd Zdeddfdd„ZdS )rp   r'   r   Nc                 C   r·   rJ   rK   r„   r   r   r   rH   ;  r¹   z_ExtendsBlock.__init__)r   r   r   rv   rH   r   r   r   r   rp   :  s    rp   c                   @   sR   e Zd Zdedddeddfdd„Zd	ee d
eee	f ddfdd„Z
ddd„ZdS )Ú_IncludeBlockr'   rF   r8   r­   r   Nc                 C   s   || _ |j | _|| _d S rJ   )r'   Útemplate_namer­   )rE   r'   rF   r­   r   r   r   rH   @  s   
z_IncludeBlock.__init__r(   rj   c                 C   s.   |d usJ ‚|  | j| j¡}|j ||¡ d S rJ   )rr   r'   r¿   r;   rd   )rE   r(   rj   Úincludedr   r   r   rd   E  s   z_IncludeBlock.find_named_blocksrm   re   c                 C   sb   |j d usJ ‚|j  | j| j¡}| || j¡� |jj |¡ W d   ƒ d S 1 s*w   Y  d S rJ   )	r(   rr   r'   r¿   r»   r­   r;   rn   r`   )rE   rm   rÀ   r   r   r   r`   L  s
   "ÿz_IncludeBlock.generater«   )r   r   r   rv   r½   rH   r   r)   r   r©   rd   r`   r   r   r   r   r¾   ?  s    ÿ
ÿ
þr¾   c                   @   sB   e Zd Zdedededdfdd„Zded fd	d
„Zddd„Z	dS )Ú_ApplyBlockÚmethodr­   rn   r   Nc                 C   ó   || _ || _|| _d S rJ   )rÂ   r­   rn   )rE   rÂ   r­   rn   r   r   r   rH   T  r®   z_ApplyBlock.__init__r¥   c                 C   r´   rJ   rµ   rL   r   r   r   r¦   Y  r¶   z_ApplyBlock.each_childrm   re   c                 C   s¤   d|j  }| j d7  _ | d| | j¡ | ¡ �# | d| j¡ | d| j¡ | j |¡ | d| j¡ W d   ƒ n1 s?w   Y  | d| j|f | j¡ d S )Nz_tt_apply%dr’   z	def %s():r¯   r°   r±   z_tt_append(_tt_utf8(%s(%s()))))Úapply_counterr²   r­   r³   rn   r`   rÂ   )rE   rm   Úmethod_namer   r   r   r`   \  s   

üÿz_ApplyBlock.generater«   ©
r   r   r   rv   r½   r¥   rH   r   r¦   r`   r   r   r   r   rÁ   S  ó    rÁ   c                   @   sB   e Zd Zdedededdfdd„Zdee fdd	„Zddd„Z	dS )Ú_ControlBlockÚ	statementr­   rn   r   Nc                 C   rÃ   rJ   )rÉ   r­   rn   )rE   rÉ   r­   rn   r   r   r   rH   k  r®   z_ControlBlock.__init__c                 C   r´   rJ   rµ   rL   r   r   r   r¦   p  r¶   z_ControlBlock.each_childrm   re   c                 C   s\   |  d| j | j¡ | ¡ � | j |¡ |  d| j¡ W d   ƒ d S 1 s'w   Y  d S )Nú%s:Úpass)r²   rÉ   r­   r³   rn   r`   r¨   r   r   r   r`   s  s
   
"ýz_ControlBlock.generater«   rÆ   r   r   r   r   rÈ   j  rÇ   rÈ   c                   @   ó,   e Zd Zdededdfdd„Zdd	d
„ZdS )Ú_IntermediateControlBlockrÉ   r­   r   Nc                 C   ó   || _ || _d S rJ   ©rÉ   r­   ©rE   rÉ   r­   r   r   r   rH   |  ó   
z"_IntermediateControlBlock.__init__rm   re   c                 C   s0   |  d| j¡ |  d| j | j| ¡ d ¡ d S )NrË   rÊ   r’   )r²   r­   rÉ   Úindent_sizer¨   r   r   r   r`   €  s   "z"_IntermediateControlBlock.generater«   ©r   r   r   rv   r½   rH   r`   r   r   r   r   rÍ   {  ó    rÍ   c                   @   rÌ   )Ú
_StatementrÉ   r­   r   Nc                 C   rÎ   rJ   rÏ   rÐ   r   r   r   rH   ‡  rÑ   z_Statement.__init__rm   re   c                 C   s   |  | j| j¡ d S rJ   )r²   rÉ   r­   r¨   r   r   r   r`   ‹  r¤   z_Statement.generater«   rÓ   r   r   r   r   rÕ   †  rÔ   rÕ   c                	   @   s2   e Zd Zddedededdfdd„Zddd„ZdS )Ú_ExpressionFÚ
expressionr­   Úrawr   Nc                 C   rÃ   rJ   )r×   r­   rØ   )rE   r×   r­   rØ   r   r   r   rH   �  r®   z_Expression.__init__rm   re   c                 C   sj   |  d| j | j¡ |  d| j¡ |  d| j¡ | js,|jjd ur,|  d|jj | j¡ |  d| j¡ d S )Nz_tt_tmp = %szEif isinstance(_tt_tmp, _tt_string_types): _tt_tmp = _tt_utf8(_tt_tmp)z&else: _tt_tmp = _tt_utf8(str(_tt_tmp))z_tt_tmp = _tt_utf8(%s(_tt_tmp))z_tt_append(_tt_tmp))r²   r×   r­   rØ   Úcurrent_templater+   r¨   r   r   r   r`   •  s   þ
þz_Expression.generate)Fr«   )r   r   r   rv   r½   rw   rH   r`   r   r   r   r   rÖ   �  s    rÖ   c                       s*   e Zd Zdededdf‡ fdd„Z‡  ZS )Ú_Moduler×   r­   r   Nc                    s   t ƒ jd| |dd� d S )Nz_tt_modules.T©rØ   )r‰   rH   )rE   r×   r­   rŽ   r   r   rH   §  s   z_Module.__init__)r   r   r   rv   r½   rH   rŸ   r   r   rŽ   r   rÚ   ¦  s    "rÚ   c                   @   s0   e Zd Zdedededdfdd„Zdd
d„ZdS )Ú_TextÚvaluer­   r,   r   Nc                 C   rÃ   rJ   )rÝ   r­   r,   )rE   rÝ   r­   r,   r   r   r   rH   ¬  r®   z_Text.__init__rm   re   c                 C   s>   | j }d|vrt| j|ƒ}|r| dt |¡ | j¡ d S d S )Nz<pre>z_tt_append(%r))rÝ   r$   r,   r²   r   rX   r­   )rE   rm   rÝ   r   r   r   r`   ±  s   ÿz_Text.generater«   rÓ   r   r   r   r   rÜ   «  s    rÜ   c                	   @   s@   e Zd ZdZ	ddedee deddfdd	„Zdefd
d„ZdS )rq   zíRaised for template syntax errors.

    ``ParseError`` instances have ``filename`` and ``lineno`` attributes
    indicating the position of the error.

    .. versionchanged:: 4.3
       Added ``filename`` and ``lineno`` attributes.
    Nr   ÚmessageÚfilenameÚlinenor   c                 C   rÃ   rJ   ©rÞ   rß   rà   )rE   rÞ   rß   rà   r   r   r   rH   Ç  s   
zParseError.__init__c                 C   s   d| j | j| jf S )Nz%s at %s:%drá   rL   r   r   r   Ú__str__Ð  r¤   zParseError.__str__ra   )	r   r   r   ru   rv   r   r½   rH   râ   r   r   r   r   rq   ½  s    
ÿÿÿÿ
þ	rq   c                
   @   s†   e Zd Zdedeeef dee de	ddf
dd„Z
defd	d
„Zddd„Zde	deddfdd„Z	ddededee ddfdd„ZdS )re   r;   rj   r(   rÙ   r   Nc                 C   s.   || _ || _|| _|| _d| _g | _d| _d S ra   )r;   rj   r(   rÙ   rÄ   Úinclude_stackÚ_indent)rE   r;   rj   r(   rÙ   r   r   r   rH   Õ  s   
z_CodeWriter.__init__c                 C   rº   rJ   ©rä   rL   r   r   r   rÒ   ä  r…   z_CodeWriter.indent_sizer   c                    s   G ‡ fdd„dt ƒ}|ƒ S )Nc                       ó0   e Zd Zd	‡ fdd„Zdeddf‡ fdd„ZdS )
z$_CodeWriter.indent.<locals>.Indenterr   re   c                    s   ˆ  j d7  _ ˆ S )Nr’   rå   ©r/   rL   r   r   Ú	__enter__é  s   z._CodeWriter.indent.<locals>.Indenter.__enter__ÚargsNc                    s    ˆ j dksJ ‚ˆ  j d8  _ d S )Nr   r’   rå   ©r/   ré   rL   r   r   Ú__exit__í  s   z-_CodeWriter.indent.<locals>.Indenter.__exit__©r   re   ©r   r   r   rè   r   rë   r   rL   r   r   ÚIndenterè  s    rî   )Úobject)rE   rî   r   rL   r   r³   ç  s   	z_CodeWriter.indentrf   r­   c                    s2   ˆ j  ˆ j|f¡ |ˆ _G ‡ fdd„dtƒ}|ƒ S )Nc                       ræ   )
z,_CodeWriter.include.<locals>.IncludeTemplater   re   c                    s   ˆ S rJ   r   rç   rL   r   r   rè   ø  r§   z6_CodeWriter.include.<locals>.IncludeTemplate.__enter__ré   Nc                    s   ˆ j  ¡ d ˆ _d S ra   )rã   ÚpoprÙ   rê   rL   r   r   rë   û  r¤   z5_CodeWriter.include.<locals>.IncludeTemplate.__exit__rì   rí   r   rL   r   r   ÚIncludeTemplate÷  s    rñ   )rã   ÚappendrÙ   rï   )rE   rf   r­   rñ   r   rL   r   r»   ó  s   z_CodeWriter.includeÚline_numberr³   c                 C   sh   |d u r| j }d| jj|f }| jr%dd„ | jD ƒ}|dd t|ƒ¡ 7 }td| | | | jd� d S )Nz	  # %s:%dc                 S   s   g | ]\}}d |j |f ‘qS )z%s:%drK   )Ú.0Útmplrà   r   r   r   Ú
<listcomp>  s    ÿz*_CodeWriter.write_line.<locals>.<listcomp>z	 (via %s)z, z    )r;   )rä   rÙ   r'   rã   r”   ÚreversedÚprintr;   )rE   r­   ró   r³   Úline_commentrk   r   r   r   r²      s   ÿz_CodeWriter.write_line)r   r   rJ   )r   r   r   r   r   rv   r©   r   r)   r%   rH   r½   rÒ   r³   r»   r²   r   r   r   r   re   Ô  s2    þ
ýüû
ú
ÿÿÿÿþre   c                	   @   s´   e Zd Zdedededdfdd„Zdd	ed
edee defdd„Zddee defdd„Zdefdd„Z	defdd„Z
deeef defdd„Zdefdd„Zdeddfdd„ZdS )r8   r'   r   r,   r   Nc                 C   s"   || _ || _|| _d| _d| _d S )Nr’   r   )r'   r   r,   r­   Úpos)rE   r'   r   r,   r   r   r   rH     s
   
z_TemplateReader.__init__r   ÚneedleÚstartÚendc                 C   sn   |dksJ |ƒ‚| j }||7 }|d u r| j ||¡}n||7 }||ks%J ‚| j |||¡}|dkr5||8 }|S )Nr   éÿÿÿÿ)rú   r   Úfind)rE   rû   rü   rý   rú   Úindexr   r   r   rÿ     s   z_TemplateReader.findÚcountc                 C   sX   |d u rt | jƒ| j }| j| }|  j| j d| j|¡7  _| j| j|… }|| _|S )Nr   )r–   r   rú   r­   r  )rE   r  ÚnewposÚsr   r   r   Úconsume$  s   
z_TemplateReader.consumec                 C   s   t | jƒ| j S rJ   )r–   r   rú   rL   r   r   r   Ú	remaining-  ó   z_TemplateReader.remainingc                 C   s   |   ¡ S rJ   )r  rL   r   r   r   Ú__len__0  r¶   z_TemplateReader.__len__Úkeyc                 C   s‚   t |tƒr0t| ƒ}| |¡\}}}|d u r| j}n|| j7 }|d ur'|| j7 }| jt|||ƒ S |dk r9| j| S | j| j|  S ra   )r5   Úslicer–   Úindicesrú   r   )rE   r  Úsizerü   ÚstopÚstepr   r   r   Ú__getitem__3  s   



z_TemplateReader.__getitem__c                 C   s   | j | jd … S rJ   )r   rú   rL   r   r   r   râ   C  r  z_TemplateReader.__str__Úmsgc                 C   s   t || j| jƒ‚rJ   )rq   r'   r­   )rE   r  r   r   r   Úraise_parse_errorF  r  z!_TemplateReader.raise_parse_error)r   NrJ   )r   r   r   rv   rH   r½   r   rÿ   r  r  r  r	   r	  r  râ   r  r   r   r   r   r8     s     	r8   r=   c                    s<   |   ¡ }dttt|ƒd ƒƒ ‰ d ‡ fdd„t|ƒD ƒ¡S )Nz%%%dd  %%s
r’   r-   c                    s    g | ]\}}ˆ |d  |f ‘qS )r’   r   )rô   Úir­   ©Úformatr   r   rö   M  s     z _format_code.<locals>.<listcomp>)Ú
splitlinesr–   Úreprr”   Ú	enumerate)r=   Úlinesr   r  r   rB   J  s   rB   rF   rf   Úin_blockÚin_loopc                 C   s2  t g ƒ}	 d}	 |  d|¡}|dks|d |  ¡ kr3|r#|  d| ¡ |j t|  ¡ | j| j	ƒ¡ |S | |d  dvr@|d7 }q|d |  ¡ k r]| |d  dkr]| |d  dkr]|d7 }q	 |dkrs|  |¡}|j t|| j| j	ƒ¡ |  d¡}| j}|  ¡ r–| d d	kr–|  d¡ |j t||| j	ƒ¡ q|d
krµ|  d¡}	|	dkr¨|  d¡ |  |	¡ 
¡ }
|  d¡ q|dkrä|  d¡}	|	dkrÇ|  d¡ |  |	¡ 
¡ }
|  d¡ |
sÚ|  d¡ |j t|
|ƒ¡ q|dksìJ |ƒ‚|  d¡}	|	dkrú|  d¡ |  |	¡ 
¡ }
|  d¡ |
�s|  d¡ |
 d¡\}}}| 
¡ }tg d¢ƒtdgƒtdgƒtdgƒdœ}| |¡}|d u�r\|�sD|  d||f ¡ ||v�rR|  d||f ¡ |j t|
|ƒ¡ q|dk�rk|�si|  d¡ |S |dv �r|dk�rvq|d k�r�| 
d!¡ 
d"¡}|�s‹|  d#¡ t|ƒ}n|d$v �r£|�s�|  d%¡ t|
|ƒ}nl|d&k�r¿| 
d!¡ 
d"¡}|�s¸|  d'¡ t|| |ƒ}nP|d(k�rÒ|�sÌ|  d)¡ t||ƒ}n=|d*k�ræ| 
¡ }|d+k�râd }||_q|d,k�rø| 
¡ }t|d-ƒ || _	q|d.k�rt||dd/�}n
|d0k�rt||ƒ}|j |¡ q|d1v �rr|d2v �r(t| |||ƒ}n|d3k�r5t| ||d ƒ}nt| |||ƒ}|d3k�rP|�sI|  d4¡ t|||ƒ}n|d5k�re|�s]|  d6¡ t||||ƒ}nt|
||ƒ}|j |¡ q|d7v �r‘|�s‡|  d|td8d9gƒf ¡ |j t|
|ƒ¡ q|  d:| ¡ q);NTr   ú{rþ   r’   z Missing {%% end %%} block for %s)r  ú%ú#é   ú!z{#z#}zMissing end comment #}z{{z}}zMissing end expression }}zEmpty expressionz{%z%}zMissing end block %}zEmpty block tag ({% %})r   )ÚifÚforÚwhileÚtryr  r"  )ÚelseÚelifÚexceptÚfinallyz%s outside %s blockz'%s block cannot be attached to %s blockrý   zExtra {% end %} block)
Úextendsr»   ÚsetÚimportÚfromÚcommentr+   r,   rØ   Úmoduler+  r'  ú"ú'zextends missing file path)r)  r*  zimport missing statementr»   zinclude missing file pathr(  zset missing statementr+   ÚNoner,   r-   rØ   rÛ   r,  )Úapplyr¼   r"  r  r   r!  )r   r!  r0  zapply missing method namer¼   zblock missing name)ÚbreakÚcontinuer   r!  zunknown operator: %r)r¬   rÿ   r  r  ro   rò   rÜ   r  r­   r,   ÚstriprÖ   Ú	partitionr(  ÚgetrÍ   rp   rÕ   r¾   r+   r$   rÚ   r:   rÁ   r©   rÈ   )rF   rf   r  r  rn   ÚcurlyÚconsÚstart_bracer­   rý   ÚcontentsÚoperatorÚspaceÚsuffixÚintermediate_blocksÚallowed_parentsr¼   Úfnr   Ú
block_bodyr   r   r   r:   P  s"  ÿÿ














ü


ÿ

ÿ



























ÿ �¾r:   )NN);ru   rS   Úior   r]   Úos.pathrŠ   r¢   r!   ry   Útornador   Útornado.logr   Útornado.utilr   r   r   r[   r   r	   r
   r   r   r   r   r   ÚTYPE_CHECKINGr   r   r6   r   r3   rv   r$   rï   r%   r)   r†   r    r¥   r9   r¬   r©   rp   r¾   rÁ   rÈ   rÍ   rÕ   rÖ   rÚ   rÜ   r#   rq   re   r8   rB   r:   r   r   r   r   Ú<module>   sn    8( =	:<	üÿþýüû