
    Mh&                     v   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          Z e            Z de!de!de!fdZ" G d d          Z# G d d          Z$ G d de$          Z% G d de$          Z& G d d          Z' G d de'          Z( G d de'          Z) G d de'          Z* G d  d!e'          Z+ G d" d#e'          Z, G d$ d%e'          Z- G d& d'e'          Z. G d( d)e'          Z/ G d* d+e'          Z0 G d, d-e'          Z1 G d. d/e1          Z2 G d0 d1e'          Z3 G d2 d3e4          Z5 G d4 d5          Z6 G d6 d7          Z7d8e!de!fd9Z8	 	 d?d:e7d;e#d<ee!         d=ee!         de)f
d>Z9d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                       e Zd ZdS )_UnsetMarkerN)__name__
__module____qualname__     P/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/tornado/template.pyr   r      s        Dr   r   modetextreturnc                     | dk    r|S | dk    r.t          j        dd|          }t          j        dd|          }|S | dk    rt          j        dd|          S t          d	| z            )
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   s     r   filter_whitespacer(      s{     u}}			vk3--vmT400			vhT***4t;<<<r   c                       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ZdedefdZded         defdZded         ded         fdZdS )TemplatezA 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                    t          j        |          | _        |t          ur|t	          d          |rdnd}|@|r|j        r|j        }n/|                    d          s|                    d          rd}nd}|J t          |d           t          |t                    s|| _
        n|r|j
        | _
        nt          | _
        |r|j        ni | _        t          |t          j        |          |          }t          | t          ||                     | _        |                     |          | _        || _        	 t)          t          j        | j                  d| j                            d	d
          z  dd          | _        dS # t          $ rC t1          | j                                                  }t5          j        d| j        |            w xY 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'   r1   endswithr(   
isinstancer   r0   _DEFAULT_AUTOESCAPE	namespace_TemplateReader_File_parsefile_generate_pythoncoder-   compile
to_unicodereplacecompiled_format_coderstripr   error)	selfr+   r,   r-   r/   r0   r1   readerformatted_codes	            r   __init__zTemplate.__init__  s   6 %d++	f,,% TUUU%8CeJ '&+ '#.

 ==)) 'T]]5-A-A '!)JJ!&J%%%*b)))*l33 	2(DOO 	2$/DOO1DO-3;)) v'8'I'I:VV$vt 4 455	))&11		
 $!$),,!DI$5$5c3$?$??!	  DMMM  	 	 	)$)44;;==NM.$)^DDD	s    AF AGkwargsc                 >    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}|                     j                   |                    |           t           j        |           t#          j        t&          g t          f         |d                   }t)          j                      |            S )z0Generate this template with the given arguments.r4   r5   c                     j         S N)rC   )r,   rK   s    r   <lambda>z#Template.generate.<locals>.<lambda>`  s	    TY r   )
get_source)r   r   
url_escapejson_encodesqueezelinkifydatetime_tt_utf8_tt_string_typesr   
__loader___tt_execute)r   r   rU   rV   rW   rX   rY   utf8r   bytesr,   rF   r   updater=   r   rG   typingcastr   	linecache
clearcache)rK   rO   r=   executes   `   r   generatezTemplate.generateQ  s     )"/ +!-~~ !-u 5 	))#s33$0F0F0F0FGGG
 
	 	(((   y)))+hr5y19]3KLL 	wyyr   c                    t                      }	 i }|                     |          }|                                 |D ]}|                    ||           t	          ||||d         j                  }|d                             |           |                                |                                 S # |                                 w xY wNr   )	r   _get_ancestorsreversefind_named_blocks_CodeWritertemplaterf   getvalueclose)rK   r-   buffernamed_blocks	ancestorsancestorwriters          r   rB   zTemplate._generate_pythonl  s    	L++F33I% A A**6<@@@@ vy|?TUUFaL!!&)))??$$LLNNNNFLLNNNNs   BB6 6Cr?   c                    | j         g}| j         j        j        D ]p}t          |t                    rY|st          d          |                    |j        | j                  }|                    |	                    |                     q|S )Nz1{% extends %} block found, but no template loader)
rA   bodychunksr;   _ExtendsBlock
ParseErrorloadr,   extendri   )rK   r-   rr   chunkrm   s        r   ri   zTemplate._get_ancestors{  s    YK	Y^* 	B 	BE%// B $N   ";;uz49==  !8!8!@!@AAAr   )r   r   r   __doc__r9   r
   strr_   r   boolr   rN   r	   rf   rB   r   ri   r   r   r   r*   r*      s8         )-9?9?$(I IsEz*I I &	I
 #4#56I U3#456I SMI 
I I I IV     6x'= #    
Xl%; 
W 
 
 
 
 
 
r   r*   c            	           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Z	ddZ
dd	ed
ee         defdZdd	ed
ee         defdZd	edefdZdS )r.   zBase 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.
    Nr0   r=   r1   r   c                 r    || _         |pi | _        || _        i | _        t	          j                    | _        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)r0   r=   r1   	templates	threadingRLocklock)rK   r0   r=   r1   s       r   rN   zBaseLoader.__init__  s9    * %"b$ O%%			r   c                 T    | j         5  i | _        ddd           dS # 1 swxY w Y   dS )z'Resets the cache of compiled templates.N)r   r   rK   s    r   resetzBaseLoader.reset  su    Y 	  	 DN	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 s   !!r,   parent_pathc                     t                      )z@Converts a possibly-relative path to absolute (used internally).NotImplementedErrorrK   r,   r   s      r   resolve_pathzBaseLoader.resolve_path  s    !###r   c                     |                      ||          }| j        5  || j        vr|                     |          | j        |<   | j        |         cddd           S # 1 swxY w Y   dS )zLoads a template.)r   N)r   r   r   _create_templater   s      r   rz   zBaseLoader.load  s      ; ??Y 	( 	(4>))'+'<'<T'B'Bt$>$'	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(s   3AA#&A#c                     t                      rR   r   rK   r,   s     r   r   zBaseLoader._create_template      !###r   )r   NrR   )r   r   r   r}   r<   r   r~   r   r	   rN   r   r   r*   rz   r   r   r   r   r.   r.     s         %8.2$(	& &SM& DcN+& SM	&
 
& & & &@       
$ $ $8C= $C $ $ $ $( ( (8C= (H ( ( ( ($S $X $ $ $ $ $ $r   r.   c                   b     e Zd ZdZdededdf fdZddedee         defd	Zdede	fd
Z
 xZS )Loaderz:A template loader that loads from a single root directory.root_directoryrO   r   Nc                      t                      j        di | t          j                            |          | _        d S Nr   )superrN   ospathabspathroot)rK   r   rO   	__class__s      r   rN   zLoader.__init__  s9    ""6"""GOON33			r   r,   r   c                 :   |r|                     d          s|                     d          s|                     d          st          j                            | j        |          }t          j                            t          j                            |                    }t          j                            t          j                            ||                    }|                     | j                  r|t          | j                  dz   d          }|S )N</   )
startswithr   r   joinr   dirnamer   len)rK   r,   r   current_pathfile_dirrelative_paths         r   r   zLoader.resolve_path  s    
	;**3//
	;  **3//
	; OOC((	
	; 7<<	;??Lwrw|'D'DEEHGOOBGLL4,H,HIIM''	22 ;$S^^a%7%9%9:r   c                     t           j                            | j        |          }t	          |d          5 }t          |                                ||           }|cd d d            S # 1 swxY w Y   d S )Nrbr,   r-   )r   r   r   r   openr*   read)rK   r,   r   frm   s        r   r   zLoader._create_template  s    w||DIt,,$ 	tDAAAH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   &A))A-0A-rR   )r   r   r   r}   r~   r	   rN   r   r   r*   r   __classcell__r   s   @r   r   r     s        DD4s 4c 4d 4 4 4 4 4 4  8C= C    S X        r   r   c                   r     e Zd ZdZdeeef         deddf fdZddedee         defd	Z	dede
fd
Z xZS )
DictLoaderz/A template loader that loads from a dictionary.dictrO   r   Nc                 H     t                      j        di | || _        d S r   )r   rN   r   )rK   r   rO   r   s      r   rN   zDictLoader.__init__  s+    ""6"""			r   r,   r   c                     |rz|                     d          se|                     d          sP|                     d          s;t          j        |          }t          j        t          j        ||                    }|S )Nr   r   )r   	posixpathr   normpathr   )rK   r,   r   r   s       r   r   zDictLoader.resolve_path  s    	F**3//	F  **3//	F OOC((		F !(55H%inXt&D&DEEDr   c                 <    t          | j        |         ||           S )Nr   )r*   r   r   s     r   r   zDictLoader._create_template  s    	$d4@@@@r   rR   )r   r   r   r}   r   r~   r	   rN   r   r   r*   r   r   r   s   @r   r   r     s        99T#s(^ s t      	 	 	8C= 	C 	 	 	 	AS AX A A A A A A A Ar   r   c                   ^    e Zd Zded          fdZddZdee         dee	d	f         ddfd
Z
dS )_Noder   c                     dS r   r   r   s    r   
each_childz_Node.each_child  s    rr   rt   rl   Nc                     t                      rR   r   rK   rt   s     r   rf   z_Node.generate  r   r   r-   rq   _NamedBlockc                 `    |                                  D ]}|                    ||           d S rR   )r   rk   )rK   r-   rq   childs       r   rk   z_Node.find_named_blocks  s@     __&& 	: 	:E##FL9999	: 	:r   rt   rl   r   N)r   r   r   r   r   rf   r   r.   r   r~   rk   r   r   r   r   r     s        HW-    $ $ $ $:z*::>sM?Q:R:	: : : : : :r   r   c                   B    e Zd ZdeddddfdZdd	Zded
         fdZdS )r?   rm   rv   
_ChunkListr   Nc                 0    || _         || _        d| _        d S rh   )rm   rv   line)rK   rm   rv   s      r   rN   z_File.__init__  s     				r   rt   rl   c                 l   |                     d| j                   |                                5  |                     d| j                   |                     d| j                   | j                            |           |                     d| j                   d d d            d S # 1 swxY w Y   d S )Nzdef _tt_execute():_tt_buffer = []_tt_append = _tt_buffer.append$return _tt_utf8('').join(_tt_buffer))
write_liner   indentrv   rf   r   s     r   rf   z_File.generate  s   .	:::]]__ 	Q 	Q/;;;>	JJJIv&&&DdiPPP		Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Qs   A,B))B-0B-r   c                     | j         fS rR   rv   r   s    r   r   z_File.each_child      	|r   r   )r   r   r   r*   rN   rf   r   r   r   r   r   r?   r?     su          $    
Q Q Q QHW-      r   r?   c                   J    e Zd Zdee         ddfdZd
dZded         fd	ZdS )r   rw   r   Nc                     || _         d S rR   rw   )rK   rw   s     r   rN   z_ChunkList.__init__  s    r   rt   rl   c                 D    | j         D ]}|                    |           d S rR   )rw   rf   )rK   rt   r|   s      r   rf   z_ChunkList.generate  s2    [ 	# 	#ENN6""""	# 	#r   r   c                     | j         S rR   r   r   s    r   r   z_ChunkList.each_child  s
    {r   r   )	r   r   r   r   r   rN   rf   r   r   r   r   r   r   r     sn        tE{ t    # # # #HW-      r   r   c            
       z    e Zd Zdededededdf
dZded         fd	Z	ddZ
dee         deed f         ddfdZdS )r   r,   rv   rm   r   r   Nc                 >    || _         || _        || _        || _        d S rR   )r,   rv   rm   r   )rK   r,   rv   rm   r   s        r   rN   z_NamedBlock.__init__$  s"    		 			r   r   c                     | j         fS rR   r   r   s    r   r   z_NamedBlock.each_child*  r   r   rt   rl   c                     |j         | j                 }|                    |j        | j                  5  |j                            |           d d d            d S # 1 swxY w Y   d S rR   )rq   r,   includerm   r   rv   rf   )rK   rt   blocks      r   rf   z_NamedBlock.generate-  s    #DI.^^ENDI66 	( 	(J'''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(s   AA"Ar-   rq   c                 R    | || j         <   t                              | ||           d S rR   )r,   r   rk   )rK   r-   rq   s      r   rk   z_NamedBlock.find_named_blocks2  s.     #'TYfl;;;;;r   r   )r   r   r   r~   r   r*   intrN   r   r   rf   r   r.   r   rk   r   r   r   r   r   #  s        S    QU    HW-    ( ( ( (
<z*<:>sM?Q:R<	< < < < < <r   r   c                       e Zd ZdeddfdZdS )rx   r,   r   Nc                     || _         d S rR   r,   r   s     r   rN   z_ExtendsBlock.__init__:  s    			r   )r   r   r   r~   rN   r   r   r   rx   rx   9  s6        S T      r   rx   c                   ^    e Zd ZdedddeddfdZdee         d	eee	f         ddfd
Z
ddZdS )_IncludeBlockr,   rL   r>   r   r   Nc                 :    || _         |j         | _        || _        d S rR   )r,   template_namer   )rK   r,   rL   r   s       r   rN   z_IncludeBlock.__init__?  s    	#[			r   r-   rq   c                     |J |                     | j        | j                  }|j                            ||           d S rR   )rz   r,   r   rA   rk   )rK   r-   rq   includeds       r   rk   z_IncludeBlock.find_named_blocksD  sF     !!!;;ty$*<==''=====r   rt   rl   c                    |j         J |j                             | j        | j                  }|                    || j                  5  |j        j                            |           d d d            d S # 1 swxY w Y   d S rR   )	r-   rz   r,   r   r   r   rA   rv   rf   )rK   rt   r   s      r   rf   z_IncludeBlock.generateK  s    }(((=%%di1CDD^^Hdi00 	0 	0M''///	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0s   
 A77A;>A;r   )r   r   r   r~   r   rN   r   r.   r   r   rk   rf   r   r   r   r   r   >  s        S *; 3 4    
>z*>:>sK?O:P>	> > > >0 0 0 0 0 0r   r   c                   F    e Zd ZdedededdfdZded         fdZddZ	dS )_ApplyBlockmethodr   rv   r   Nc                 0    || _         || _        || _        d S rR   )r   r   rv   )rK   r   r   rv   s       r   rN   z_ApplyBlock.__init__S  s    				r   r   c                     | j         fS rR   r   r   s    r   r   z_ApplyBlock.each_childX  r   r   rt   rl   c                    d|j         z  }|xj         dz  c_         |                    d|z  | j                   |                                5  |                    d| j                   |                    d| j                   | j                            |           |                    d| j                   d d d            n# 1 swxY 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   rv   rf   r   )rK   rt   method_names      r   rf   z_ApplyBlock.generate[  sU   #f&::!+3TY???]]__ 	Q 	Q/;;;>	JJJIv&&&DdiPPP		Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q 	Q
 	C4;CCCCCTY	
 	
 	
 	
 	
s   A,CC	C	r   
r   r   r   r~   r   r   rN   r   r   rf   r   r   r   r   r   R  sx        s # U t    
HW-    
 
 
 
 
 
r   r   c                   F    e Zd ZdedededdfdZdee         fdZdd
Z	dS )_ControlBlock	statementr   rv   r   Nc                 0    || _         || _        || _        d S rR   )r   r   rv   )rK   r   r   rv   s       r   rN   z_ControlBlock.__init__j  s    "				r   c                     | j         fS rR   r   r   s    r   r   z_ControlBlock.each_childo  r   r   rt   rl   c                    |                     d| j        z  | j                   |                                5  | j                            |           |                     d| j                   d d d            d S # 1 swxY w Y   d S )N%s:pass)r   r   r   r   rv   rf   r   s     r   rf   z_ControlBlock.generater  s    %$.0$)<<<]]__ 	1 	1Iv&&&fdi000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1s   6A;;A?A?r   r   r   r   r   r   r   i  sw        # S  $    
HUO    1 1 1 1 1 1r   r   c                   *    e Zd ZdededdfdZd	dZdS )
_IntermediateControlBlockr   r   r   Nc                 "    || _         || _        d S rR   r   r   rK   r   r   s      r   rN   z"_IntermediateControlBlock.__init__{      "			r   rt   rl   c                     |                     d| j                   |                     d| j        z  | j        |                                dz
             d S )Nr   r   r   )r   r   r   indent_sizer   s     r   rf   z"_IntermediateControlBlock.generate  sS    &$),,,%$.0$)V=O=O=Q=QTU=UVVVVVr   r   r   r   r   r~   r   rN   rf   r   r   r   r   r   z  sW        # S T    W W W W W Wr   r   c                   *    e Zd ZdededdfdZd	dZdS )

_Statementr   r   r   Nc                 "    || _         || _        d S rR   r   r   s      r   rN   z_Statement.__init__  r   r   rt   rl   c                 F    |                     | j        | j                   d S rR   )r   r   r   r   s     r   rf   z_Statement.generate  s"    $.$)44444r   r   r  r   r   r   r  r    sQ        # S T    5 5 5 5 5 5r   r  c            	       0    e Zd ZddedededdfdZdd
ZdS )_ExpressionF
expressionr   rawr   Nc                 0    || _         || _        || _        d S rR   )r	  r   r
  )rK   r	  r   r
  s       r   rN   z_Expression.__init__  s    $	r   rt   rl   c                 d   |                     d| j        z  | j                   |                     d| j                   |                     d| j                   | j        s4|j        j        (|                     d|j        j        z  | 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_templater0   r   s     r   rf   z_Expression.generate  s    .4?:DIFFFVI	
 	
 	
 	BDINNNx 	F3>J 1F4K4VV	   	/;;;;;r   )Fr   )r   r   r   r~   r   r   rN   rf   r   r   r   r  r    s]         3 c      
< < < < < <r   r  c                   ,     e Zd Zdededdf fdZ xZS )_Moduler	  r   r   Nc                 V    t                                          d|z   |d           d S )Nz_tt_modules.Tr
  )r   rN   )rK   r	  r   r   s      r   rN   z_Module.__init__  s-    *4dEEEEEr   )r   r   r   r~   r   rN   r   r   s   @r   r  r    s_        F3 Fc Fd F F F F F F F F F Fr   r  c                   .    e Zd ZdedededdfdZd
d	ZdS )_Textvaluer   r1   r   Nc                 0    || _         || _        || _        d S rR   )r  r   r1   )rK   r  r   r1   s       r   rN   z_Text.__init__  s    
	$r   rt   rl   c                     | j         }d|vrt          | j        |          }|r2|                    dt	          j        |          z  | j                   d S d S )Nz<pre>z_tt_append(%r))r  r(   r1   r   r   r^   r   )rK   rt   r  s      r   rf   z_Text.generate  sj    
 %%dou==E 	P.U1C1CCTYOOOOO	P 	Pr   r   r  r   r   r   r  r    s^        %c % %# %$ % % % %
	P 	P 	P 	P 	P 	Pr   r  c            	       F    e Zd ZdZ	 d
dedee         deddfdZdefd	ZdS )ry   zRaised 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                 0    || _         || _        || _        d S rR   r  r  r  )rK   r  r  r  s       r   rN   zParseError.__init__  s      !r   c                 0    d| j         | j        | j        fz  S )Nz%s at %s:%dr  r   s    r   __str__zParseError.__str__  s    dmT[IIIr   rh   )	r   r   r   r}   r~   r   r   rN   r  r   r   r   ry   ry     s          KL &.smDG	   J J J J J J Jr   ry   c            
           e Zd Zdedeeef         dee         de	ddf
dZ
defdZdd
Zde	dedd	fdZ	 ddededee         ddfdZdS )rl   rA   rq   r-   r  r   Nc                 h    || _         || _        || _        || _        d| _        g | _        d| _        d S rh   )rA   rq   r-   r  r   include_stack_indent)rK   rA   rq   r-   r  s        r   rN   z_CodeWriter.__init__  s=     	( 0r   c                     | j         S rR   r"  r   s    r   r  z_CodeWriter.indent_size  s
    |r   r   c                 6      G  fdd          } |            S )Nc                   0    e Zd Zd fdZdeddf fdZdS )$_CodeWriter.indent.<locals>.Indenterr   rl   c                 (    xj         dz  c_         S )Nr   r$  r5   rK   s    r   	__enter__z._CodeWriter.indent.<locals>.Indenter.__enter__  s    !r   argsNc                 B    j         dk    sJ xj         dz  c_         d S )Nr   r   r$  r5   r+  rK   s     r   __exit__z-_CodeWriter.indent.<locals>.Indenter.__exit__  s*    |a''''!r   r   rl   r   r   r   r*  r	   r.  r   s   r   Indenterr'    s_             "3 "4 " " " " " " " "r   r1  r   )rK   r1  s   ` r   r   z_CodeWriter.indent  sC    	" 	" 	" 	" 	" 	" 	" 	" 	" 	" xzzr   rm   r   c                       j                              j        |f           | _         G  fdd          } |            S )Nc                   0    e Zd Zd fdZdeddf fdZdS ),_CodeWriter.include.<locals>.IncludeTemplater   rl   c                     S rR   r   r)  s    r   r*  z6_CodeWriter.include.<locals>.IncludeTemplate.__enter__  s    r   r+  Nc                 P    j                                         d         _        d S rh   )r!  popr  r-  s     r   r.  z5_CodeWriter.include.<locals>.IncludeTemplate.__exit__  s%    (,(:(>(>(@(@(C%%%r   r/  r0  r   s   r   IncludeTemplater4    si             D3 D4 D D D D D D D Dr   r8  )r!  appendr  )rK   rm   r   r8  s   `   r   r   z_CodeWriter.include  sv    !!4#8$"?@@@ (	D 	D 	D 	D 	D 	D 	D 	D 	D 	D    r   line_numberr   c                     || j         }d| j        j        |fz  }| j        r9d | j        D             }|dd                    t          |                    z  z  }t          d|z  |z   |z   | j                   d S )Nz	  # %s:%dc                 ,    g | ]\  }}d |j         |fz  S )z%s:%dr   ).0tmplr  s      r   
<listcomp>z*_CodeWriter.write_line.<locals>.<listcomp>  s5       2@449f--  r   z	 (via %s)z, z    )rA   )r"  r  r,   r!  r   reversedprintrA   )rK   r   r:  r   line_commentrr   s         r   r   z_CodeWriter.write_line  s     >\F"d&;&@+%NN 	I DHDV  I K$))HY4G4G*H*HHHLfvo$|3$)DDDDDDr   )r   r   rR   )r   r   r   r   r   r~   r   r   r.   r*   rN   r   r  r   r   r   r   r   r   rl   rl     s        3+, $	
 # 
   S    
 
 
 
! ! !8H ! ! ! ! DHE EE&)E3;C=E	E E E E E Er   rl   c            	           e Zd ZdedededdfdZdded	ed
ee         defdZddee         defdZdefdZ	defdZ
deeef         defdZdefdZdeddfdZdS )r>   r,   r   r1   r   Nc                 L    || _         || _        || _        d| _        d| _        d S )Nr   r   )r,   r   r1   r   pos)rK   r,   r   r1   s       r   rN   z_TemplateReader.__init__  s)    		$	r   r   needlestartendc                     |dk    s
J |            | j         }||z  }|| j                            ||          }n)||z  }||k    sJ | j                            |||          }|dk    r||z  }|S )Nr   )rE  r   find)rK   rF  rG  rH  rE  indexs         r   rK  z_TemplateReader.find  s    zzz5zzzh;INN6511EE3JC%<<<<INN65#66EB;;SLEr   countc                     |t          | j                  | j        z
  }| j        |z   }| xj        | j                            d| j        |          z  c_        | j        | j        |         }|| _        |S )Nr#   )r   r   rE  r   rM  )rK   rM  newposss       r   consumez_TemplateReader.consume#  sj    =	NNTX-EE!		TY__T48V<<<		Idh'(r   c                 :    t          | j                  | j        z
  S rR   )r   r   rE  r   s    r   	remainingz_TemplateReader.remaining,  s    49~~((r   c                 *    |                                  S rR   )rS  r   s    r   __len__z_TemplateReader.__len__/  s    ~~r   keyc                 D   t          |t                    rdt          |           }|                    |          \  }}}|| j        }n
|| j        z  }|
|| j        z  }| j        t          |||                   S |dk     r| j        |         S | j        | j        |z            S rh   )r;   slicer   indicesrE  r   )rK   rV  sizerG  stopsteps         r   __getitem__z_TemplateReader.__getitem__2  s    c5!! 	-t99D #D 1 1E4}! 9U5$55661WW9S>!9TX^,,r   c                 *    | j         | j        d          S rR   )r   rE  r   s    r   r  z_TemplateReader.__str__B  s    y$$r   msgc                 8    t          || j        | j                  rR   )ry   r,   r   )rK   r_  s     r   raise_parse_errorz!_TemplateReader.raise_parse_errorE  s    di333r   )r   NrR   )r   r   r   r~   rN   r   r   rK  rQ  rS  rU  r
   rX  r]  r  ra  r   r   r   r>   r>     sF       S        3 s Xc] c     Xc] c    )3 ) ) ) )         -uS%Z0 -S - - - - % % % % %4S 4T 4 4 4 4 4 4r   r>   rC   c                     |                                  }dt          t          t          |          dz                       z  d                    fdt	          |          D                       S )Nz%%%dd  %%s
r   r3   c                 *    g | ]\  }}|d z   |fz  S )r   r   )r=  ir   formats      r   r?  z _format_code.<locals>.<listcomp>L  s*    MMMy4Fa!eT]*MMMr   )
splitlinesr   reprr   	enumerate)rC   linesre  s     @r   rH   rH   I  se    OOEc$s5zzA~"6"6777F77MMMMIe<L<LMMMNNNr   rL   rm   in_blockin_loopc                    t          g           }	 d}	 |                     d|          }|dk    s|dz   |                                 k    ra|r|                     d|z             |j                            t          |                                 | j        | j	                             |S | |dz            dvr|dz  }|dz   |                                 k     r$| |dz            dk    r| |dz            dk    r|dz  }	 |dk    rH|                     |          }|j                            t          || j        | j	                             |                     d          }| j        }|                                 rQ| d         d	k    rE|                     d           |j                            t          ||| j	                             |d
k    rn|                     d          }	|	dk    r|                     d           |                     |	          
                                }
|                     d           2|dk    r|                     d          }	|	dk    r|                     d           |                     |	          
                                }
|                     d           |
s|                     d           |j                            t          |
|                     |dk    s
J |            |                     d          }	|	dk    r|                     d           |                     |	          
                                }
|                     d           |
s|                     d           |
                    d          \  }}}|
                                }h ddhdhdhd}|                    |          }|f|s|                     | d| d           ||vr|                     | d| d           |j                            t          |
|                     .|dk    r|s|                     d           |S |dv r|d k    rZ|d!k    rP|
                    d"          
                    d#          }|s|                     d$           t          |          }n?|d%v r)|s|                     d&           t!          |
|          }n|d'k    rQ|
                    d"          
                    d#          }|s|                     d(           t#          || |          }n|d)k    r(|s|                     d*           t!          ||          }n|d+k    r%|
                                }|d,k    rd }||_        |d-k    r-|
                                }t'          |d.           || _	        |d/k    rt          ||d0          }n|d1k    rt)          ||          }|j                            |           |d2v r|d3v rt+          | |||          }n+|d4k    rt+          | ||d           }nt+          | |||          }|d4k    r)|s|                     d5           t-          |||          }nA|d6k    r*|s|                     d7           t/          ||||          }nt1          |
||          }|j                            |           |d8v rW|s+|                     d9                    |d:d;h                     |j                            t!          |
|                     8|                     d<|z             Q)=NTr   {rJ  r   z Missing {%% end %%} block for %s)rm  %#   !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whilerr  rt  )elseelifexceptfinallyz	 outside z blockz block cannot be attached to rH  zExtra {% end %} block)
extendsr   setimportfromcommentr0   r1   r
  moduler~  rz  "'zextends missing file path)r|  r}  zimport missing statementr   zinclude missing file pathr{  zset missing statementr0   Noner1   r3   r
  r  r  )applyr   rt  rr  rs  ru  )rs  ru  r  zapply missing method namer   zblock missing name)breakcontinuez{} outside {} blockrs  ru  zunknown operator: %r)r   rK  rS  ra  rw   r9  r  rQ  r   r1   stripr  	partitiongetr   rx   r  r   r0   r(   r  r@   r   r   r   re  )rL   rm   rj  rk  rv   curlyconsstart_bracer   rH  contentsoperatorspacesuffixintermediate_blocksallowed_parentsr   fnr   
block_bodys                       r   r@   r@   O  sV    b>>DH	KKU++E{{eai6+;+;+=+=== ,,:XE   ""&..**FK9JKK    eai 77

 	F,,....519%,,519%,,
 199>>%((DKuT6;8IJJKKKnnQ''{  	&)s"2"2NN1Ku[$8IJJKKK $++d##Cbyy(()ABBB~~c**0022HNN1 $++d##Cbyy(()DEEE~~c**0022HNN1 =(();<<<K{8T::;;; d"""K"""kk$"99$$%;<<<>>#&&,,..q 	@$$%>???"*"4"4S"9"9% 211Fgw	
 
 .11(;;& X((H)V)V)V)V)VWWW..((NNhNNN   K84HHIII  B(()@AAAK 
 
 
 9$$9$$c**0055 J,,-HIII%f--/// I,,-GHHH"8T22Y&&c**0055 J,,-HIII%ffd;;U"" F,,-DEEE"6400\))\\^^<<B&(#\))||~~!$+++$(!U""#FDd;;;X%%--Ku%%%HHH+++#FHhII

W$$ $FHhEE

#FHhHH
7"" J,,-HIII#FD*==W$$ C,,-ABBB#FJ$GG%hjAAKu%%%... (()00E7;KLL   Kz(D99::: $$%;h%FGGGHr   )NN):r}   rY   ior   rc   os.pathr   r   r%   r   tornador   tornado.logr   tornado.utilr   r   r   ra   r	   r
   r   r   r   r   r   r   TYPE_CHECKINGr   r   r<   r   r9   r~   r(   r*   r.   r   r   r   r?   r   r   rx   r   r   r   r   r  r  r  r  r'   ry   rl   r>   rH   r@   r   r   r   <module>r     s   u un                 				                 : : : : : : : : : : O O O O O O O O O O O O O O O O O O O O 	 -,,,,,,,,$ 	 	 	 	 	 	 	 	 
=C =s =s = = = =2I I I I I I I IX:$ :$ :$ :$ :$ :$ :$ :$z    Z   8A A A A A A A A,: : : : : : : :    E   $	 	 	 	 	 	 	 	< < < < <% < < <,    E   
0 0 0 0 0E 0 0 0(
 
 
 
 
% 
 
 
.1 1 1 1 1E 1 1 1"W W W W W W W W5 5 5 5 5 5 5 5< < < < <% < < <.F F F F Fk F F F
P P P P PE P P P$J J J J J J J J.7E 7E 7E 7E 7E 7E 7E 7Et94 94 94 94 94 94 94 94xOs Os O O O O #!	FH FHFHFH smFH c]	FH
 FH FH FH FH FH FHr   