o
    ~îj&“  ã                   @   sX  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ƒZ#G dd„ dƒZ$G dd„ de$ƒZ%G dd„ de$ƒZ&G d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'ƒZ0G d-d.„ d.e'ƒZ1G d/d0„ d0e1ƒZ2G d1d2„ d2e'ƒZ3G d3d4„ d4e4ƒZ5G d5d6„ d6ƒZ6G d7d8„ d8ƒZ7d9e!de!fd:d;„Z8		dBd<e7d=e#d>ee! d?ee! de)f
d@dA„Z9d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   úZ/raid/bulk_storage/M2HATS/real-image-venv/lib/python3.10/site-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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   r   rv   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   2  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   9  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   D  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`   K  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   S  r®   z_ApplyBlock.__init__r¥   c                 C   r´   rJ   rµ   rL   r   r   r   r¦   X  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› d|› d	�| j¡ d S )
Nz_tt_apply%dr’   z	def %s():r¯   r°   r±   z_tt_append(_tt_utf8(ú(z()))))Ú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Á   R  ó    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   j  r®   z_ControlBlock.__init__c                 C   r´   rJ   rµ   rL   r   r   r   r¦   o  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`   r  s
   
"ýz_ControlBlock.generater«   rÇ   r   r   r   r   rÉ   i  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Î   z  ó    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ƒ}|ƒ 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ï   r   )rE   rï   r   rL   r   r³   æ  s   	z_CodeWriter.indentrf   r­   c                    s0   ˆ j  ˆ j|f¡ |ˆ _G ‡ fdd„dƒ}|ƒ 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Ú   )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__/  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__2  s   



z_TemplateReader.__getitem__c                 C   s   | j | jd … S rJ   )r   rú   rL   r   r   r   rã   B  r  z_TemplateReader.__str__Úmsgc                 C   s   t || j| jƒ‚rJ   )rq   r'   r­   )rE   r  r   r   r   Úraise_parse_errorE  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ö   L  s     z _format_code.<locals>.<listcomp>)Ú
splitlinesr–   Úreprr”   Ú	enumerate)r=   Úlinesr   r  r   rB   I  s   rB   rF   rf   Úin_blockÚin_loopc                 C   s&  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¡\}}}| 
¡ }h d£dhdhdhdœ}| |¡}|d u�rX|�s>|  |› d|› d�¡ ||v�rN|  |› d|› d�¡ |j t|
|ƒ¡ q|dk�rg|�se|  d¡ |S |dv �r|d k�rrq|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d0�}n
|d1k�rt||ƒ}|j |¡ q|d2v �rn|d3v �r$t| |||ƒ}n|d4k�r1t| ||d ƒ}nt| |||ƒ}|d4k�rL|�sE|  d5¡ t|||ƒ}n|d6k�ra|�sY|  d7¡ t||||ƒ}nt|
||ƒ}|j |¡ q|d8v �r‹|�s�|  d9 |d:d;h¡¡ |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ÚtryÚwhiler  r!  )ÚelseÚelifÚexceptÚfinallyz	 outside z blockz block cannot be attached to rý   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Úcontinuez{} outside {} blockr   r"  zunknown operator: %r)r¬   rÿ   r  r  ro   rò   rÝ   r  r­   r,   Ústripr×   Ú	partitionÚgetrÎ   rp   rÖ   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:   O  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¥   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( =	:<	üÿþýüû