
    g                        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j6                  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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(      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-e(      Z2 G d. d/e2      Z3 G d0 d1e(      Z4 G d2 d3e5      Z6 G d4 d5e#      Z7 G d6 d7e#      Z8d8e!de!fd9Z9	 	 d?d:e8d;e$d<ee!   d=ee!   de*f
d>Z:y)@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y)_UnsetMarkerN)__name__
__module____qualname__     E/var/www/openai/venv/lib/python3.12/site-packages/tornado/template.pyr   r      s    r   r   modetextreturnc                     | dk(  r|S | dk(  r0t        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(      sm     u}		vvk3-vvmT40		vvh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y)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}|B|r|j
                  r|j
                  }n'|j                  d      s|j                  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        ||             | _        | j#                  |      | _        || _        	 t)        t        j*                  | j$                        d| j                  j-                  d	d
      z  dd      | _        y# t        $ rF t1        | j$                        j3                         }t5        j6                  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%8eJ&++#..
 ==)T]]5-A!)J!&J%%%*b)*l3(DO$//DO1DO-3)) v'8'8'I:V$vt 45	))&1		
 $!!$)),!DII$5$5c3$??!	DM  	)$))4;;=NMM.$))^D	s   /AE> >AGkwargsc                 T    t         j                  t         j                  t         j                  t         j                  t         j                  t         j
                  t        t         j                  t        t        f j                  j                  dd      t         fd      d}|j                   j                         |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	    TYYr   )
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$0FG
	 	( y)++hr5y19]3KL 	yr   c                 X   t               }	 i }| j                  |      }|j                          |D ]  }|j                  ||        t	        ||||d   j
                        }|d   j                  |       |j                         |j                          S # |j                          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3I%**6<@ & vy|?T?TUFaL!!&)??$LLNFLLNs   A:B B)r?   c                 2   | j                   g}| j                   j                  j                  D ]f  }t        |t              s|st        d      |j                  |j                  | j                        }|j                  |j                  |             h |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}    YYK	YY^^**E%/$N  ";;uzz499=  !8!8!@A + r   )r   r   r   __doc__r9   r
   strr_   r   boolr   rN   r	   rf   rB   r   ri   r   r   r   r*   r*      s     )-9?9?$(IsEz*I I &	I
 #4#56I U3#456I SMI 
IV  6x'= # 
Xl%; 
W 
r   r*   c            	           e Zd ZdZeddfdedeeeef      dee   ddfd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y)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                 v    || _         |xs i | _        || _        i | _        t	        j
                         | _        y)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__  s4    * %"b$ OO%	r   c                 T    | j                   5  i | _        ddd       y# 1 sw Y   yxY w)z'Resets the cache of compiled templates.N)r   r   rK   s    r   resetzBaseLoader.reset  s    YYDN YYs   '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                  ||      }| j                  5  || j                  vr| j                  |      | j                  |<   | j                  |   cddd       S # 1 sw Y   yxY w)zLoads a template.)r   N)r   r   r   _create_templater   s      r   rz   zBaseLoader.load  sY      ; ?YY4>>)'+'<'<T'Bt$>>$' YYs   ;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     ..2$(	&& DcN+& SM	&
 
&@ 
$ $8C= $C $( (8C= (H ($S $X $r   r.   c                   \     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                 l    t        |   di | t        j                  j	                  |      | _        y Nr   )superrN   ospathabspathroot)rK   r   rO   	__class__s      r   rN   zLoader.__init__  s'    "6"GGOON3	r   r,   r   c                 $   |r|j                  d      s|j                  d      s|j                  d      st        j                  j                  | j                  |      }t        j                  j                  t        j                  j                  |            }t        j                  j                  t        j                  j                  ||            }|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(77<<		;?Lwwrww|'DEHGGOOBGGLL4,HIM''		2$S^a%7%9:r   c                     t         j                  j                  | j                  |      }t	        |d      5 }t        |j                         ||       }|cd d d        S # 1 sw Y   y xY w)Nrbr,   r-   )r   r   r   r   openr*   read)rK   r,   r   frm   s        r   r   zLoader._create_template  sI    ww||DIIt,$tDAH s   AA(rR   )r   r   r   r}   r~   r	   rN   r   r   r*   r   __classcell__r   s   @r   r   r     sQ    D4s 4c 4d 4 8C= C S X r   r   c                   f     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                 2    t        |   di | || _        y r   )r   rN   r   )rK   r   rO   r   s      r   rN   zDictLoader.__init__  s    "6"	r   r,   r   c                     |rq|j                  d      s`|j                  d      sO|j                  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]    **3/**3/OOC( ((5H%%innXt&DEDr   c                 8    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T#s(^ s t 	 	8C= 	C 	AS AX Ar   r   c                   J    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
y)
_Noder   c                      yr   r   r   s    r   
each_childz_Node.each_child  s    r   Nc                     t               rR   r   rK   rt   s     r   rf   z_Node.generate  r   r   r-   rq   _NamedBlockc                 R    | j                         D ]  }|j                  ||        y rR   )r   rk   )rK   r-   rq   childs       r   rk   z_Node.find_named_blocks  s%     __&E##FL9 'r   rt   rl   r   N)r   r   r   r   r   rf   r   r.   r   r~   rk   r   r   r   r   r     sD    HW- $:z*::>sM?Q:R:	:r   r   c                   :    e Zd ZdeddddfdZd
dZded   fd	Zy)r?   rm   rv   
_ChunkListr   Nc                 .    || _         || _        d| _        y rh   )rm   rv   line)rK   rm   rv   s      r   rN   z_File.__init__  s     		r   c                 d   |j                  d| j                         |j                         5  |j                  d| j                         |j                  d| j                         | j                  j	                  |       |j                  d| j                         d d d        y # 1 sw Y   y xY w)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{    .		:]]_/;>		JIIv&DdiiP	 __s   A0B&&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?     s3      $ 
QHW- r   r?   c                   <    e Zd Zdee   ddfdZddZded   fdZy)	r   rw   r   Nc                     || _         y rR   rw   )rK   rw   s     r   rN   z_ChunkList.__init__  s	    r   c                 H    | j                   D ]  }|j                  |        y rR   )rw   rf   )rK   rt   r|   s      r   rf   z_ChunkList.generate  s    [[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     s/    tE{ t #HW- r   r   c            
       f    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y)r   r,   rv   rm   r   r   Nc                 <    || _         || _        || _        || _        y 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   c                     |j                   | j                     }|j                  |j                  | j                        5  |j
                  j                  |       d d d        y # 1 sw Y   y xY wrR   )rq   r,   includerm   r   rv   rf   )rK   rt   blocks      r   rf   z_NamedBlock.generate-  sJ    ##DII.^^ENNDII6JJ' 766s    A%%A.r-   rq   c                 P    | || j                   <   t        j                  | ||       y rR   )r,   r   rk   )rK   r-   rq   s      r   rk   z_NamedBlock.find_named_blocks2  s$     #'TYYfl;r   r   )r   r   r   r~   r   r*   intrN   r   r   rf   r   r.   r   rk   r   r   r   r   r   #  sm    S    QU HW- (
<z*<:>sM?Q:R<	<r   r   c                       e Zd ZdeddfdZy)rx   r,   r   Nc                     || _         y rR   )r,   r   s     r   rN   z_ExtendsBlock.__init__:  s	    	r   )r   r   r   r~   rN   r   r   r   rx   rx   9  s    S T r   rx   c                   P    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y)_IncludeBlockr,   rL   r>   r   r   Nc                 B    || _         |j                   | _        || _        y rR   )r,   template_namer   )rK   r,   rL   r   s       r   rN   z_IncludeBlock.__init__?  s    	#[[	r   r-   rq   c                     |J |j                  | j                  | j                        }|j                  j	                  ||       y rR   )rz   r,   r   rA   rk   )rK   r-   rq   includeds       r   rk   z_IncludeBlock.find_named_blocksD  s>     !!!;;tyy$*<*<=''=r   c                 ,   |j                   J |j                   j                  | j                  | j                        }|j	                  || j
                        5  |j                  j                  j                  |       d d d        y # 1 sw Y   y xY wrR   )	r-   rz   r,   r   r   r   rA   rv   rf   )rK   rt   r   s      r   rf   z_IncludeBlock.generateK  sh    }}(((==%%dii1C1CD^^Hdii0MM''/ 100s   &B

Br   )r   r   r   r~   r   rN   r   r.   r   r   rk   rf   r   r   r   r   r   >  sU    S *; 3 4 
>z*>:>sK?O:P>	>0r   r   c                   >    e Zd ZdedededdfdZded   fdZd
d	Z	y)_ApplyBlockmethodr   rv   r   Nc                 .    || _         || _        || _        y 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   c                    d|j                   z  }|xj                   dz  c_         |j                  d|z  | j                         |j                         5  |j                  d| j                         |j                  d| j                         | j                  j                  |       |j                  d| j                         d d d        |j                  d| j                  d|d	| j                         y # 1 sw Y   7xY w)
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[  s    #f&:&::!+3TYY?]]_/;>		JIIv&DdiiP	 
 	04[I499	
 _s   A0C::Dr   
r   r   r   r~   r   r   rN   r   r   rf   r   r   r   r   r   R  s9    s # U t 
HW- 
r   r   c                   >    e Zd ZdedededdfdZdee   fdZd	dZ	y)
_ControlBlock	statementr   rv   r   Nc                 .    || _         || _        || _        y 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   c                    |j                  d| j                  z  | j                         |j                         5  | j                  j                  |       |j                  d| j                         d d d        y # 1 sw Y   y xY w)N%s:pass)r   r   r   r   rv   rf   r   s     r   rf   z_ControlBlock.generater  sX    %$..0$))<]]_IIv&fdii0 __s   8A;;Br   r   r   r   r   r   r   i  s8    # S  $ 
HUO 1r   r   c                   (    e Zd ZdededdfdZddZy)_IntermediateControlBlockr   r   r   Nc                      || _         || _        y rR   r   r   rK   r   r   s      r   rN   z"_IntermediateControlBlock.__init__{      "	r   c                     |j                  d| j                         |j                  d| j                  z  | j                  |j                         dz
         y )Nr   r   r   )r   r   r   indent_sizer   s     r   rf   z"_IntermediateControlBlock.generate  sD    &$)),%$..0$))V=O=O=QTU=UVr   r   r   r   r   r~   r   rN   rf   r   r   r   r   r   z  s"    # S T Wr   r   c                   (    e Zd ZdededdfdZddZy)
_Statementr   r   r   Nc                      || _         || _        y rR   r   r   s      r   rN   z_Statement.__init__  r   r   c                 P    |j                  | j                  | j                         y rR   )r   r   r   r   s     r   rf   z_Statement.generate  s    $..$))4r   r   r  r   r   r   r  r    s!    # S T 5r   r  c            	       .    e Zd ZddedededdfdZd	dZy)
_Expression
expressionr   rawr   Nc                 .    || _         || _        || _        y rR   )r  r   r	  )rK   r  r   r	  s       r   rN   z_Expression.__init__  s    $	r   c                    |j                  d| j                  z  | j                         |j                  d| j                         |j                  d| j                         | j                  sI|j                  j
                  3|j                  d|j                  j
                  z  | j                         |j                  d| j                         y )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??:DIIFVII	
 	BDIINxxF33>>J 1F4K4K4V4V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                 0    t         |   d|z   |d       y )Nz_tt_modules.Tr	  )r   rN   )rK   r  r   r   s      r   rN   z_Module.__init__  s    *4dEr   )r   r   r   r~   r   rN   r   r   s   @r   r  r    s'    F3 Fc Fd F Fr   r  c                   ,    e Zd ZdedededdfdZddZy)	_Textvaluer   r1   r   Nc                 .    || _         || _        || _        y rR   )r  r   r1   )rK   r  r   r1   s       r   rN   z_Text.__init__  s    
	$r   c                     | j                   }d|vrt        | j                  |      }|r3|j                  dt	        j
                  |      z  | j                         y y )Nz<pre>z_tt_append(%r))r  r(   r1   r   r   r^   r   )rK   rt   r  s      r   rf   z_Text.generate  sP    

 %%doou=E.U1CCTYYO r   r   r  r   r   r   r  r    s)    %c % %# %$ %
	Pr   r  c            	       >    e Zd ZdZ	 d	dedee   deddfdZdefdZy)
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.
    Nmessagefilenamelinenor   c                 .    || _         || _        || _        y rR   r  r  r  )rK   r  r  r  s       r   rN   zParseError.__init__  s      !r   c                 N    d| j                   | j                  | j                  fz  S )Nz%s at %s:%dr  r   s    r   __str__zParseError.__str__  s     dmmT[[IIIr   rh   )	r   r   r   r}   r~   r   r   rN   r  r   r   r   ry   ry     sE     KL&.smDG	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y)rl   rA   rq   r-   r  r   Nc                 f    || _         || _        || _        || _        d| _        g | _        d| _        y rh   )rA   rq   r-   r  r   include_stack_indent)rK   rA   rq   r-   r  s        r   rN   z_CodeWriter.__init__  s9     	( 0r   c                     | j                   S rR   r!  r   s    r   r   z_CodeWriter.indent_size  s    ||r   r   c                 4      G  fddt               } |       S )Nc                   .    e Zd Zd fdZdeddf fdZy)$_CodeWriter.indent.<locals>.Indenterr   c                 2    xj                   dz  c_         S )Nr   r#  r5   rK   s    r   	__enter__z._CodeWriter.indent.<locals>.Indenter.__enter__  s    !r   argsNc                 R    j                   dkD  sJ xj                   dz  c_         y )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   r0  )object)rK   r0  s   ` r   r   z_CodeWriter.indent  s    	"v 	" zr   rm   r   c                       j                   j                   j                  |f       | _         G  fddt              } |       S )Nc                   .    e Zd Zd fdZdeddf fdZy),_CodeWriter.include.<locals>.IncludeTemplater   c                     S rR   r   r(  s    r   r)  z6_CodeWriter.include.<locals>.IncludeTemplate.__enter__  s    r   r*  Nc                 J    j                   j                         d   _        y rh   )r   popr  r,  s     r   r-  z5_CodeWriter.include.<locals>.IncludeTemplate.__exit__  s    (,(:(:(>(>(@(C%r   r.  r/  r   s   r   IncludeTemplater4    s    D3 D4 Dr   r8  )r   appendr  r1  )rK   rm   r   r8  s   `   r   r   z_CodeWriter.include  sC    !!4#8#8$"?@ (	Df 	D   r   line_numberr   c                 T   || j                   }d| j                  j                  |fz  }| j                  rM| j                  D cg c]  \  }}d|j                  |fz   }}}|ddj	                  t        |            z  z  }t        d|z  |z   |z   | j                         y c c}}w )Nz	  # %s:%dz%s:%dz	 (via %s)z, z    )rA   )r!  r  r,   r   r   reversedprintrA   )rK   r   r:  r   line_commenttmplr  rr   s           r   r   z_CodeWriter.write_line  s     >\\F"d&;&;&@&@+%NNDHDVDVDV.4499f--DV   K$))HY4G*HHHLfvo$|3$))D	s   B$)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3;C=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y)r>   r,   r   r1   r   Nc                 J    || _         || _        || _        d| _        d| _        y )Nr   r   )r,   r   r1   r   pos)rK   r,   r   r1   s       r   rN   z_TemplateReader.__init__  s%    		$	r   needlestartendc                     |dk\  sJ |       | j                   }||z  }|| j                  j                  ||      }n)||z  }||k\  sJ | j                  j                  |||      }|dk7  r||z  }|S )Nr   )rB  r   find)rK   rC  rD  rE  rB  indexs         r   rH  z_TemplateReader.find  s~    z 5 zhh;IINN651E3JC%<<IINN65#6EB;SLEr   countc                     |"t        | j                        | j                  z
  }| j                  |z   }| xj                  | j                  j	                  d| j                  |      z  c_        | j                  | j                  | }|| _        |S )Nr#   )r   r   rB  r   rJ  )rK   rJ  newposss       r   consumez_TemplateReader.consume#  sn    =		NTXX-EE!		TYY__T488V<<	IIdhh(r   c                 F    t        | j                        | j                  z
  S rR   )r   r   rB  r   s    r   	remainingz_TemplateReader.remaining,  s    499~((r   c                 "    | j                         S rR   )rP  r   s    r   __len__z_TemplateReader.__len__/  s    ~~r   keyc                 T   t        |t              rit        |       }|j                  |      \  }}}|| j                  }n|| j                  z  }||| j                  z  }| j
                  t        |||         S |dk  r| j
                  |   S | j
                  | j                  |z      S rh   )r;   slicer   indicesrB  r   )rK   rS  sizerD  stopsteps         r   __getitem__z_TemplateReader.__getitem__2  s    c5!t9D #D 1E4}! 99U5$5661W99S>!99TXX^,,r   c                 4    | j                   | j                  d  S rR   )r   rB  r   s    r   r  z_TemplateReader.__str__B  s    yy$$r   msgc                 D    t        || j                  | j                        rR   )ry   r,   r   )rK   r\  s     r   raise_parse_errorz!_TemplateReader.raise_parse_errorE  s    dii33r   )r   NrR   )r   r   r   r~   rN   r   r   rH  rN  rP  rR  r
   rU  rZ  r  r^  r   r   r   r>   r>     s    S    3 s Xc] c Xc] c )3 )   -uS%Z0 -S - % %4S 4T 4r   r>   rC   c           	          | j                         }dt        t        t        |      dz               z  }dj                  t	        |      D cg c]  \  }}||dz   |fz   c}}      S c c}}w )Nz%%%dd  %%s
r   r3   )
splitlinesr   reprr   	enumerate)rC   linesformatir   s        r   rH   rH   I  sc    OOEc$s5zA~"677F77Ie<LM<Ly4Fa!eT]*<LMNNMs   A)
rL   rm   in_blockin_loopc                    t        g       }	 d}	 | j                  d|      }|dk(  s|dz   | j                         k(  r`|r| j                  d|z         |j                  j                  t        | j                         | j                  | j                               |S | |dz      dvr|dz  }|dz   | j                         k  r| |dz      dk(  r| |dz      dk(  r|dz  }	 |dkD  rK| j                  |      }|j                  j                  t        || j                  | j                               | j                  d      }| j                  }| j                         rK| d   d	k(  rC| j                  d       |j                  j                  t        ||| j                               |d
k(  rY| j                  d      }	|	dk(  r| j                  d       | j                  |	      j                         }
| j                  d       |dk(  r| j                  d      }	|	dk(  r| j                  d       | j                  |	      j                         }
| j                  d       |
s| j                  d       |j                  j                  t        |
|             |dk(  sJ |       | j                  d      }	|	dk(  r| j                  d       | j                  |	      j                         }
| j                  d       |
s| j                  d       |
j                  d      \  }}}|j                         }t        g d      t        dg      t        dg      t        dg      d}|j                  |      }|[|s| j                  |d|d       ||vr| j                  |d|d       |j                  j                  t        |
|             |dk(  r|s| j                  d       |S |dv rl|d k(  r|d!k(  r@|j                  d"      j                  d#      }|s| j                  d$       t!        |      }n|d%v r |s| j                  d&       t#        |
|      }n|d'k(  rA|j                  d"      j                  d#      }|s| j                  d(       t%        || |      }n|d)k(  r |s| j                  d*       t#        ||      }nt|d+k(  r |j                         }|d,k(  rd }||_        |d-k(  r%|j                         }t)        |d.       || _	        |d/k(  rt        ||d0      }n|d1k(  rt+        ||      }|j                  j                         T|d2v r|d3v rt-        | |||      }n"|d4k(  rt-        | ||d       }nt-        | |||      }|d4k(  r!|s| j                  d5       t/        |||      }n4|d6k(  r"|s| j                  d7       t1        ||||      }nt3        |
||      }|j                  j                  |       |d8v rK|s"| j                  |dt        d9d:g      d       |j                  j                  t#        |
|             S| j                  d;|z         h)<NTr   {rG  r   z Missing {%% end %%} block for %s)ri  %#   !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tryrn  rq  )elseelifexceptfinallyz	 outside z blockz block cannot be attached to rE  zExtra {% end %} block)
extendsr   setimportfromcommentr0   r1   r	  modulerz  rv  "'zextends missing file path)rx  ry  zimport missing statementr   zinclude missing file pathrw  zset missing statementr0   Noner1   r3   r	  r  r{  )applyr   rq  rn  ro  rp  )ro  rp  r  zapply missing method namer   zblock missing name)breakcontinuero  rp  zunknown operator: %r)r   rH  rP  r^  rw   r9  r  rN  r   r1   stripr  	partitionrw  getr   rx   r  r   r0   r(   r  r@   r   r   r   )rL   rm   rf  rg  rv   curlyconsstart_bracer   rE  contentsoperatorspacesuffixintermediate_blocksallowed_parentsr   fnr   
block_bodys                       r   r@   r@   O  sV    b>D
KKU+E{eai6+;+;+==,,:XE ""&..*FKK9J9JK  eai 7

 	F,,..519%,519%,
 19>>%(DKKuT6;;8I8IJKnnQ'{{ &)s"2NN1KKu[$8I8IJK $++d#Cby(()AB~~c*002HNN1 $++d#Cby(()DE~~c*002HNN1(();<KK{8T:; d"/K/"kk$"9$$%;<>>#&,,.q$$%>?"*"4"4S"9% 56K5'lE7|	
 .11(;&((-5G .((AI8T KK84HI (()@AK 
 
 9$9$c*005,,-HI%f-//,,-GH"8T2Y&c*005,,-HI%ffd;U",,-DE"640\)\\^<B&(#\)||~!$+$(!U"#FDd;X%-KKu%HH++#FHhI
W$ $FHhE
#FHhH
7",,-HI#FD*=W$,,-AB#FJ$G%hjAKKu%..((-5sE7;K7LM KKz(D9: $$%;h%FGC 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(   r1  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n      	    : : O O O 	,$ 	 	 
=C =s =s =2Iv IX:$ :$zZ 8A A,:F :E $	 	<% <,E 
0E 0(
% 
.1E 1"W W5 5<% <.Fk F
PE P$J J.7E& 7Et94f 94xOs Os O #!	HHHHHH smHH c]	HH
 HHr   