a
    õ£Be¡“  ã                   @   sP  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r²ddlmZmZ d	ZG d
d„ dƒZeƒ Z e!e!e!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(ƒ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-d.„ d.e2ƒZ3G d/d0„ d0e(ƒZ4G d1d2„ d2e5ƒZ6G d3d4„ d4e#ƒZ7G d5d6„ d6e#ƒZ8e!e!d7œd8d9„Z9d=e8e$ee! ee! e*d:œd;d<„Z:dS )>aŒ  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   úS/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/tornado/template.pyr   Ü   s   r   )ÚmodeÚtextÚreturnc                 C   sZ   | dkr|S | dkr4t  dd|¡}t  dd|¡}|S | dkrJt  dd|¡S td	|  ƒ‚d
S )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*)Ú
Zonelinez(\s+)zinvalid whitespace mode %sN)Ú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eeef eed ee	e
f eeee
f  ee ddœdd„Zeedœd	d
„Zed edœdd„Zed ed dœ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Ú
BaseLoader)Útemplate_stringÚnameÚloaderÚcompress_whitespaceÚ
autoescapeÚ
whitespacer   c           	      C   sP  t  |¡| _|tur0|dur$tdƒ‚|r,dnd}|du rh|rJ|jrJ|j}n| d¡s^| d¡rdd}nd}|dustJ ‚t|dƒ t|t	ƒs�|| _
n|rž|j
| _
nt| _
|r®|jni | _t|t  |¡|ƒ}t| t|| ƒƒ| _|  |¡| _|| _z,tt  | j¡d| j d	d
¡ ddd�| _W n4 t�yJ   t| jƒ ¡ }t d| j|¡ ‚ Y n0 dS )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   Z
native_strr'   Ú_UNSETr"   r+   Úendswithr#   Ú
isinstancer   r*   Ú_DEFAULT_AUTOESCAPEÚ	namespaceÚ_TemplateReaderÚ_FileÚ_parseÚfileÚ_generate_pythonÚcoder(   ÚcompileZ
to_unicodeÚreplaceÚcompiledÚ_format_codeÚrstripr   Úerror)	Úselfr&   r'   r(   r)   r*   r+   ÚreaderZformatted_coder   r   r   Ú__init__  sD    




üzTemplate.__init__)Úkwargsr   c                    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'   ©rB   r   r   Ú<lambda>`  ó    z#Template.generate.<locals>.<lambda>)Ú
get_source)r   r   Ú
url_escapeÚjson_encodeÚsqueezeÚlinkifyÚdatetimeZ_tt_utf8Z_tt_string_typesr   Ú
__loader__Z_tt_execute)r   r   rL   rM   rN   rO   rP   Úutf8r   Úbytesr'   r=   r   Úupdater5   r   r>   ÚtypingÚcastr
   Ú	linecacheÚ
clearcache)rB   rE   r5   Úexecuter   rH   r   ÚgenerateQ  s$    ó
zTemplate.generate)r(   r   c                 C   sv   t ƒ }z`i }|  |¡}| ¡  |D ]}| ||¡ q"t||||d jƒ}|d  |¡ | ¡ W | ¡  S | ¡  0 d S ©Nr   )	r   Ú_get_ancestorsÚreverseÚfind_named_blocksÚ_CodeWriterÚtemplaterZ   ÚgetvalueÚclose)rB   r(   ÚbufferÚnamed_blocksÚ	ancestorsZancestorÚwriterr   r   r   r:   l  s    
þzTemplate._generate_pythonr7   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)
r9   ÚbodyÚchunksr3   Ú_ExtendsBlockÚ
ParseErrorÚloadr'   Úextendr\   )rB   r(   re   Úchunkr`   r   r   r   r\   {  s    
ÿzTemplate._get_ancestors)r   r   r   Ú__doc__r1   r	   ÚstrrS   r   Úboolr   rD   r   rZ   r:   r   r\   r   r   r   r   r$   ü   s$   ù

øKr$   c                   @   sŠ   e Zd ZdZeddfeeeeef  ee ddœdd„Z	ddœdd„Z
deee ed	œd
d„Zdeee ed	œdd„Zeedœ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.
    N)r*   r5   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*   r5   r+   Ú	templatesÚ	threadingÚRLockÚlock)rB   r*   r5   r+   r   r   r   rD   �  s
    
zBaseLoader.__init__©r   c                 C   s0   | j � i | _W d  ƒ n1 s"0    Y  dS )z'Resets the cache of compiled templates.N)rt   rq   rH   r   r   r   Úreset°  s    zBaseLoader.reset©r'   Úparent_pathr   c                 C   s
   t ƒ ‚dS )z@Converts a possibly-relative path to absolute (used internally).N©ÚNotImplementedError©rB   r'   rx   r   r   r   Úresolve_pathµ  s    zBaseLoader.resolve_pathc                 C   s\   | j ||d�}| j�4 || jvr0|  |¡| j|< | j| W  d  ƒ S 1 sN0    Y  dS )zLoads a template.)rx   N)r|   rt   rq   Ú_create_templater{   r   r   r   rk   ¹  s
    
zBaseLoader.load©r'   r   c                 C   s
   t ƒ ‚d S rF   ry   ©rB   r'   r   r   r   r}   Á  s    zBaseLoader._create_template)N)N)r   r   r   rn   r4   ro   r   r   r   rD   rv   r|   r$   rk   r}   r   r   r   r   r%   ˆ  s   	üû r%   c                       sR   e Zd ZdZeeddœ‡ fdd„Zdeee edœdd„Zee	d	œd
d„Z
‡  ZS )ÚLoaderz:A template loader that loads from a single root directory.N)Úroot_directoryrE   r   c                    s$   t ƒ jf i |¤Ž tj |¡| _d S rF   )ÚsuperrD   ÚosÚpathÚabspathÚroot)rB   r�   rE   ©Ú	__class__r   r   rD   È  s    zLoader.__init__rw   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)rB   r'   rx   Úcurrent_pathÚfile_dirÚrelative_pathr   r   r   r|   Ì  s    ÿþýüzLoader.resolve_pathr~   c                 C   sT   t j | j|¡}t|dƒ�&}t| ¡ || d�}|W  d   ƒ S 1 sF0    Y  d S )NÚrb©r'   r(   )rƒ   r„   r�   r†   Úopenr$   Úread)rB   r'   r„   Úfr`   r   r   r   r}   Ú  s    zLoader._create_template)N)r   r   r   rn   ro   r   rD   r   r|   r$   r}   Ú__classcell__r   r   r‡   r   r€   Å  s   r€   c                       sZ   e Zd ZdZeeef eddœ‡ fdd„Zdeee edœdd„Z	ee
d	œd
d„Z‡  ZS )Ú
DictLoaderz/A template loader that loads from a dictionary.N)ÚdictrE   r   c                    s   t ƒ jf i |¤Ž || _d S rF   )r‚   rD   rš   )rB   rš   rE   r‡   r   r   rD   ä  s    zDictLoader.__init__rw   c                 C   sB   |r>|  d¡s>|  d¡s>|  d¡s>t |¡}t t ||¡¡}|S )Nr‰   rŠ   )rŒ   Ú	posixpathrŽ   Únormpathr�   )rB   r'   rx   r‘   r   r   r   r|   è  s    ÿþýü
zDictLoader.resolve_pathr~   c                 C   s   t | j| || d�S )Nr”   )r$   rš   r   r   r   r   r}   ó  s    zDictLoader._create_template)N)r   r   r   rn   r   ro   r   rD   r   r|   r$   r}   r˜   r   r   r‡   r   r™   á  s   r™   c                   @   sL   e Zd Zed  dœdd„Zdddœdd„Zee ee	d	f dd
œdd„Z
dS )Ú_Noderu   c                 C   s   dS )Nr   r   rH   r   r   r   Ú
each_childø  s    z_Node.each_childr_   N©rf   r   c                 C   s
   t ƒ ‚d S rF   ry   ©rB   rf   r   r   r   rZ   û  s    z_Node.generateÚ_NamedBlock©r(   rd   r   c                 C   s   |   ¡ D ]}| ||¡ qd S rF   )rž   r^   )rB   r(   rd   Úchildr   r   r   r^   þ  s    z_Node.find_named_blocks)r   r   r   r   rž   rZ   r   r%   r   ro   r^   r   r   r   r   r�   ÷  s
   þr�   c                   @   s@   e Zd Zedddœdd„Zdddœdd	„Zed
 dœdd„ZdS )r7   Ú
_ChunkListN)r`   rg   r   c                 C   s   || _ || _d| _d S r[   )r`   rg   Úline)rB   r`   rg   r   r   r   rD     s    z_File.__init__r_   rŸ   c                 C   sp   |  d| j¡ | ¡ �F |  d| j¡ |  d| j¡ | j |¡ |  d| j¡ W d   ƒ n1 sb0    Y  d S )Nzdef _tt_execute():ú_tt_buffer = []ú_tt_append = _tt_buffer.appendú$return _tt_utf8('').join(_tt_buffer))Ú
write_liner¥   Úindentrg   rZ   r    r   r   r   rZ     s    
z_File.generater�   ru   c                 C   s   | j fS rF   ©rg   rH   r   r   r   rž     s    z_File.each_child)r   r   r   r$   rD   rZ   r   rž   r   r   r   r   r7     s   r7   c                   @   sB   e Zd Zee ddœdd„Zdddœdd„Zed	 d
œdd„ZdS )r¤   N)rh   r   c                 C   s
   || _ d S rF   ©rh   )rB   rh   r   r   r   rD     s    z_ChunkList.__init__r_   rŸ   c                 C   s   | j D ]}| |¡ qd S rF   )rh   rZ   )rB   rf   rm   r   r   r   rZ     s    
z_ChunkList.generater�   ru   c                 C   s   | j S rF   r¬   rH   r   r   r   rž     s    z_ChunkList.each_child)	r   r   r   r   r�   rD   rZ   r   rž   r   r   r   r   r¤     s   r¤   c                   @   sb   e Zd Zeeeeddœdd„Zed dœdd„Z	d	dd
œdd„Z
ee eed f ddœdd„ZdS )r¡   N)r'   rg   r`   r¥   r   c                 C   s   || _ || _|| _|| _d S rF   )r'   rg   r`   r¥   )rB   r'   rg   r`   r¥   r   r   r   rD   $  s    z_NamedBlock.__init__r�   ru   c                 C   s   | j fS rF   r«   rH   r   r   r   rž   *  s    z_NamedBlock.each_childr_   rŸ   c                 C   sL   |j | j }| |j| j¡� |j |¡ W d   ƒ n1 s>0    Y  d S rF   )rd   r'   Úincluder`   r¥   rg   rZ   )rB   rf   Úblockr   r   r   rZ   -  s    z_NamedBlock.generater¢   c                 C   s   | || j < t | ||¡ d S rF   )r'   r�   r^   )rB   r(   rd   r   r   r   r^   2  s    
z_NamedBlock.find_named_blocks)r   r   r   ro   r�   r$   ÚintrD   r   rž   rZ   r   r%   r   r^   r   r   r   r   r¡   #  s   þr¡   c                   @   s   e Zd Zeddœdd„ZdS )ri   Nr~   c                 C   s
   || _ d S rF   rG   r   r   r   r   rD   :  s    z_ExtendsBlock.__init__)r   r   r   ro   rD   r   r   r   r   ri   9  s   ri   c                   @   sN   e Zd Zededdœdd„Zee eee	f ddœdd„Z
d	dd
œdd„ZdS )Ú_IncludeBlockr6   N)r'   rC   r¥   r   c                 C   s   || _ |j | _|| _d S rF   )r'   Útemplate_namer¥   )rB   r'   rC   r¥   r   r   r   rD   ?  s    z_IncludeBlock.__init__r¢   c                 C   s.   |d usJ ‚|  | j| j¡}|j ||¡ d S rF   )rk   r'   r±   r9   r^   )rB   r(   rd   Úincludedr   r   r   r^   D  s    z_IncludeBlock.find_named_blocksr_   rŸ   c                 C   s`   |j d usJ ‚|j  | j| j¡}| || j¡� |jj |¡ W d   ƒ n1 sR0    Y  d S rF   )	r(   rk   r'   r±   r­   r¥   r9   rg   rZ   )rB   rf   r²   r   r   r   rZ   K  s    z_IncludeBlock.generate)r   r   r   ro   r¯   rD   r   r%   r   r¡   r^   rZ   r   r   r   r   r°   >  s
   þr°   c                   @   sB   e Zd Zeeeddœdd„Zed dœdd„Zd	dd
œdd„Z	dS )Ú_ApplyBlockN)Úmethodr¥   rg   r   c                 C   s   || _ || _|| _d S rF   )r´   r¥   rg   )rB   r´   r¥   rg   r   r   r   rD   S  s    z_ApplyBlock.__init__r�   ru   c                 C   s   | j fS rF   r«   rH   r   r   r   rž   X  s    z_ApplyBlock.each_childr_   rŸ   c                 C   s¤   d|j  }| j d7  _ | d| | j¡ | ¡ �F | d| j¡ | d| j¡ | j |¡ | d| j¡ W d   ƒ n1 s~0    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ª   rg   rZ   r´   )rB   rf   Úmethod_namer   r   r   rZ   [  s    

,ÿz_ApplyBlock.generate©
r   r   r   ro   r¯   r�   rD   r   rž   rZ   r   r   r   r   r³   R  s   r³   c                   @   sB   e Zd Zeeeddœdd„Zee dœdd„Zddd	œd
d„Z	dS )Ú_ControlBlockN)Ú	statementr¥   rg   r   c                 C   s   || _ || _|| _d S rF   )r¹   r¥   rg   )rB   r¹   r¥   rg   r   r   r   rD   j  s    z_ControlBlock.__init__ru   c                 C   s   | j fS rF   r«   rH   r   r   r   rž   o  s    z_ControlBlock.each_childr_   rŸ   c                 C   sZ   |  d| j | j¡ | ¡ �* | j |¡ |  d| j¡ W d   ƒ n1 sL0    Y  d S )Nú%s:Úpass)r©   r¹   r¥   rª   rg   rZ   r    r   r   r   rZ   r  s    
z_ControlBlock.generater·   r   r   r   r   r¸   i  s   r¸   c                   @   s.   e Zd Zeeddœdd„Zdddœdd„ZdS )	Ú_IntermediateControlBlockN©r¹   r¥   r   c                 C   s   || _ || _d S rF   ©r¹   r¥   ©rB   r¹   r¥   r   r   r   rD   {  s    z"_IntermediateControlBlock.__init__r_   rŸ   c                 C   s0   |  d| j¡ |  d| j | j| ¡ d ¡ d S )Nr»   rº   r‹   )r©   r¥   r¹   Úindent_sizer    r   r   r   rZ     s    z"_IntermediateControlBlock.generate©r   r   r   ro   r¯   rD   rZ   r   r   r   r   r¼   z  s   r¼   c                   @   s.   e Zd Zeeddœdd„Zdddœdd„ZdS )	Ú
_StatementNr½   c                 C   s   || _ || _d S rF   r¾   r¿   r   r   r   rD   †  s    z_Statement.__init__r_   rŸ   c                 C   s   |  | j| j¡ d S rF   )r©   r¹   r¥   r    r   r   r   rZ   Š  s    z_Statement.generaterÁ   r   r   r   r   rÂ   …  s   rÂ   c                   @   s2   e Zd Zd
eeeddœdd„Zdddœdd	„ZdS )Ú_ExpressionFN)Ú
expressionr¥   Úrawr   c                 C   s   || _ || _|| _d S rF   )rÄ   r¥   rÅ   )rB   rÄ   r¥   rÅ   r   r   r   rD   �  s    z_Expression.__init__r_   rŸ   c                 C   sj   |  d| j | j¡ |  d| j¡ |  d| j¡ | jsX|jjd urX|  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   rZ   ”  s    þ
þz_Expression.generate)F)r   r   r   ro   r¯   rp   rD   rZ   r   r   r   r   rÃ   Ž  s   rÃ   c                       s&   e Zd Zeeddœ‡ fdd„Z‡  ZS )Ú_ModuleN)rÄ   r¥   r   c                    s   t ƒ jd| |dd� d S )Nz_tt_modules.T©rÅ   )r‚   rD   )rB   rÄ   r¥   r‡   r   r   rD   ¦  s    z_Module.__init__)r   r   r   ro   r¯   rD   r˜   r   r   r‡   r   rÇ   ¥  s   rÇ   c                   @   s0   e Zd Zeeeddœdd„Zdddœdd„ZdS )	Ú_TextN)Úvaluer¥   r+   r   c                 C   s   || _ || _|| _d S rF   )rÊ   r¥   r+   )rB   rÊ   r¥   r+   r   r   r   rD   «  s    z_Text.__init__r_   rŸ   c                 C   s:   | j }d|vrt| j|ƒ}|r6| dt |¡ | j¡ d S )Nz<pre>z_tt_append(%r))rÊ   r#   r+   r©   r   rR   r¥   )rB   rf   rÊ   r   r   r   rZ   °  s
    z_Text.generaterÁ   r   r   r   r   rÉ   ª  s   rÉ   c                   @   s8   e Zd ZdZd
eee eddœdd„Zedœdd	„ZdS )rj   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   s   || _ || _|| _d S rF   ©rË   rÌ   rÍ   )rB   rË   rÌ   rÍ   r   r   r   rD   Æ  s    zParseError.__init__ru   c                 C   s   d| j | j| jf S )Nz%s at %s:%drÎ   rH   r   r   r   Ú__str__Ï  s    zParseError.__str__)Nr   )	r   r   r   rn   ro   r   r¯   rD   rÏ   r   r   r   r   rj   ¼  s   
 ÿ
þ	rj   c                   @   sv   e Zd Zeeeef ee e	ddœdd„Z
edœdd„Zddœd	d
„Ze	eddœdd„Zdeeee ddœdd„ZdS )r_   N)r9   rd   r(   rÆ   r   c                 C   s.   || _ || _|| _|| _d| _g | _d| _d S r[   )r9   rd   r(   rÆ   rµ   Úinclude_stackÚ_indent)rB   r9   rd   r(   rÆ   r   r   r   rD   Ô  s    z_CodeWriter.__init__ru   c                 C   s   | j S rF   ©rÑ   rH   r   r   r   rÀ   ã  s    z_CodeWriter.indent_sizer   c                    s   G ‡ fdd„dt ƒ}|ƒ S )Nc                       s2   e Zd Zddœ‡ fdd„Zeddœ‡ fdd„ZdS )	z$_CodeWriter.indent.<locals>.Indenterr_   ru   c                    s   ˆ  j d7  _ ˆ S )Nr‹   rÒ   ©r.   rH   r   r   Ú	__enter__è  s    z._CodeWriter.indent.<locals>.Indenter.__enter__N©Úargsr   c                    s    ˆ j dksJ ‚ˆ  j d8  _ d S )Nr   r‹   rÒ   ©r.   rÖ   rH   r   r   Ú__exit__ì  s    z-_CodeWriter.indent.<locals>.Indenter.__exit__©r   r   r   rÔ   r   rØ   r   rH   r   r   ÚIndenterç  s   rÚ   )Úobject)rB   rÚ   r   rH   r   rª   æ  s    	z_CodeWriter.indent)r`   r¥   r   c                    s2   ˆ j  ˆ j|f¡ |ˆ _G ‡ fdd„dtƒ}|ƒ S )Nc                       s2   e Zd Zddœ‡ fdd„Zeddœ‡ fdd„ZdS )	z,_CodeWriter.include.<locals>.IncludeTemplater_   ru   c                    s   ˆ S rF   r   rÓ   rH   r   r   rÔ   ÷  s    z6_CodeWriter.include.<locals>.IncludeTemplate.__enter__NrÕ   c                    s   ˆ j  ¡ d ˆ _d S r[   )rÐ   ÚpoprÆ   r×   rH   r   r   rØ   ú  s    z5_CodeWriter.include.<locals>.IncludeTemplate.__exit__rÙ   r   rH   r   r   ÚIncludeTemplateö  s   rÝ   )rÐ   ÚappendrÆ   rÛ   )rB   r`   r¥   rÝ   r   rH   r   r­   ò  s    z_CodeWriter.include)r¥   Úline_numberrª   r   c                 C   sh   |d u r| j }d| jj|f }| jrJd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:%drG   )Ú.0ÚtmplrÍ   r   r   r   Ú
<listcomp>  s   z*_CodeWriter.write_line.<locals>.<listcomp>z	 (via %s)z, z    )r9   )rÑ   rÆ   r'   rÐ   r�   ÚreversedÚprintr9   )rB   r¥   rß   rª   Zline_commentre   r   r   r   r©   ÿ  s    ÿz_CodeWriter.write_line)N)r   r   r   r   r   ro   r¡   r   r%   r$   rD   r¯   rÀ   rª   r­   r©   r   r   r   r   r_   Ó  s   
ú ÿ
þr_   c                   @   s¢   e Zd Zeeeddœdd„Zdeeee edœdd„Zdee ed	œd
d„Zedœdd„Z	edœdd„Z
eeef edœdd„Zedœdd„Zeddœdd„ZdS )r6   N)r'   r   r+   r   c                 C   s"   || _ || _|| _d| _d| _d S )Nr‹   r   )r'   r   r+   r¥   Úpos)rB   r'   r   r+   r   r   r   rD     s
    z_TemplateReader.__init__r   )ÚneedleÚstartÚendr   c                 C   sn   |dksJ |ƒ‚| j }||7 }|d u r6| j ||¡}n$||7 }||ksJJ ‚| j |||¡}|dkrj||8 }|S )Nr   éÿÿÿÿ)rå   r   Úfind)rB   ræ   rç   rè   rå   Úindexr   r   r   rê     s    z_TemplateReader.find)Úcountr   c                 C   sX   |d u rt | jƒ| j }| j| }|  j| j d| j|¡7  _| j| j|… }|| _|S )Nr   )r�   r   rå   r¥   rì   )rB   rì   ZnewposÚsr   r   r   Úconsume#  s    
z_TemplateReader.consumeru   c                 C   s   t | jƒ| j S rF   )r�   r   rå   rH   r   r   r   Ú	remaining,  s    z_TemplateReader.remainingc                 C   s   |   ¡ S rF   )rï   rH   r   r   r   Ú__len__/  s    z_TemplateReader.__len__)Úkeyr   c                 C   s†   t |tƒr`t| ƒ}| |¡\}}}|d u r2| j}n
|| j7 }|d urN|| j7 }| jt|||ƒ S |dk rr| j| S | j| j|  S d S r[   )r3   Úslicer�   Úindicesrå   r   )rB   rñ   Úsizerç   ÚstopÚstepr   r   r   Ú__getitem__2  s    



z_TemplateReader.__getitem__c                 C   s   | j | jd … S rF   )r   rå   rH   r   r   r   rÏ   B  s    z_TemplateReader.__str__)Úmsgr   c                 C   s   t || j| jƒ‚d S rF   )rj   r'   r¥   )rB   rø   r   r   r   Úraise_parse_errorE  s    z!_TemplateReader.raise_parse_error)r   N)N)r   r   r   ro   rD   r¯   r   rê   rî   rï   rð   r	   rò   r÷   rÏ   rù   r   r   r   r   r6     s   	r6   )r;   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  rJ   z _format_code.<locals>.<listcomp>)Ú
splitlinesr�   Úreprr�   Ú	enumerate)r;   Úlinesr   rû   r   r?   I  s    r?   )rC   r`   Úin_blockÚin_loopr   c                 C   sT  t g ƒ}d}|  d|¡}|dks0|d |  ¡ krb|rB|  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qºq|dkrä|  |¡}|j t|| j| j	ƒ¡ |  d¡}| j}|  ¡ �r.| d dk�r.|  d¡ |j t||| j	ƒ¡ q|d	k�rp|  d
¡}	|	dk�rV|  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Ê|�s˜|  d||f ¡ ||v�r´|  d||f ¡ |j t|
|ƒ¡ qq|dk�rè|�sä|  d¡ |S |dv �rF|dk�rþq|dk�r4| 
d ¡ 
d!¡}|�s(|  d"¡ t|ƒ}�n|d#v �rZ|�sN|  d$¡ t|
|ƒ}nÜ|d%k�r’| 
d ¡ 
d!¡}|�s„|  d&¡ t|| |ƒ}n¤|d'k�r¸|�s¬|  d(¡ t||ƒ}n~|d)k�râ| 
¡ }|d*k�rØd }||_qnT|d+k�r| 
¡ }t|d,ƒ || _	qn.|d-k�r"t||d.d/�}n|d0k�r6t||ƒ}|j |¡ qq|d1v �r |d2v �rjt| |||ƒ}n(|d3k�r„t| ||d ƒ}nt| |||ƒ}|d3k�rº|�s¬|  d4¡ t|||ƒ}n6|d5k�rä|�sÔ|  d6¡ t||||ƒ}nt|
||ƒ}|j |¡ qq|d7v �r@|�s*|  d|td8d9gƒf ¡ |j t|
|ƒ¡ qq|  d:| ¡ qd S );Nr   Ú{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Å   TrÈ   r  )Úapplyr®   r  r  r	  r
  )r	  r
  r  zapply missing method namer®   zblock missing name)ÚbreakÚcontinuer	  r
  zunknown operator: %r)r¤   rê   rï   rù   rh   rÞ   rÉ   rî   r¥   r+   ÚstriprÃ   Ú	partitionr  Úgetr¼   ri   rÂ   r°   r*   r#   rÇ   r8   r³   r¡   r¸   )rC   r`   r  r  rg   ZcurlyZconsZstart_bracer¥   rè   ÚcontentsÚoperatorÚspaceÚsuffixZintermediate_blocksZallowed_parentsr®   Úfnr   Z
block_bodyr   r   r   r8   O  s    ÿÿÿþý



















ü


ÿ

ÿ


























ÿr8   )NN);rn   rP   Úior   rW   Úos.pathrƒ   r›   r    rr   Útornador   Ztornado.logr   Ztornado.utilr   r   r   rU   r   r	   r
   r   r   r   r   r   ÚTYPE_CHECKINGr   r   r4   r   r1   ro   r#   rÛ   r$   r%   r€   r™   r�   r7   r¤   r¡   ri   r°   r³   r¸   r¼   rÂ   rÃ   rÇ   rÉ   r"   rj   r_   r6   r?   r8   r   r   r   r   Ú<module>   sd    8( =	:<	  üû