BYPASS SHELL BY ./RAZORGANZ
Server: nginx/1.20.1
System: Linux iZdzfnv9mwfppeZ 5.10.134-19.2.al8.x86_64 #1 SMP Wed Oct 29 22:47:09 CST 2025 x86_64
User: apache (48)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: //lib64/python3.6/site-packages/tornado/__pycache__/template.cpython-36.pyc
3

	��f'��@sHdZddlZddlmZddlZddlZddlZddlZddl	Z	ddl
mZddlm
Z
ddlmZmZmZddlmZmZmZmZmZmZmZmZddlZejr�ddlmZmZd	ZGd
d�d�Ze�Z e!e!e!d�d
d�Z"Gdd�de#�Z$Gdd�de#�Z%Gdd�de%�Z&Gdd�de%�Z'Gdd�de#�Z(Gdd�de(�Z)Gdd�de(�Z*Gdd�de(�Z+Gdd �d e(�Z,Gd!d"�d"e(�Z-Gd#d$�d$e(�Z.Gd%d&�d&e(�Z/Gd'd(�d(e(�Z0Gd)d*�d*e(�Z1Gd+d,�d,e(�Z2Gd-d.�d.e2�Z3Gd/d0�d0e(�Z4Gd1d2�d2e5�Z6Gd3d4�d4e#�Z7Gd5d6�d6e#�Z8e!e!d7�d8d9�Z9d=e8e$e!e!e*d:�d;d<�Z:dS)>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 ``{# ... #}``.

These tags may be escaped as ``{{!``, ``{%!``, and ``{#!``
if you need to include a literal ``{{``, ``{%``, or ``{#`` in the output.


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

``{% whitespace *mode* %}``
    Sets the whitespace mode for the remainder of the current file
    (or until the next ``{% whitespace %}`` directive). See
    `filter_whitespace` for available options. New in Tornado 4.3.
�N)�StringIO)�escape)�app_log)�
ObjectDict�exec_in�unicode_type)�Any�Union�Callable�List�Dict�Iterable�Optional�TextIO)�Tuple�ContextManager�xhtml_escapec@seZdZdS)�_UnsetMarkerN)�__name__�
__module__�__qualname__�rr� /usr/lib64/python3.6/template.pyr�sr)�mode�text�returncCsZ|dkr|S|dkr4tjdd|�}tjdd|�}|S|dkrJtjdd|�Std	|��d
S)a�Transform whitespace in ``text`` according to ``mode``.

    Available modes are:

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

    .. versionadded:: 4.3
    �all�singlez([\t ]+)� z
(\s*\n\s*)�
Zonelinez(\s+)zinvalid whitespace mode %sN)�re�sub�	Exception)rrrrr�filter_whitespace�s
r#c	@s�eZdZdZddeedfeeefedeee	feee	fedd�dd�Z
eed�d	d
�Ze
ded�dd
�Ze
dedd�dd�ZdS)�Templatez�A compiled template.

    We compile into Python from the given template_string. You can generate
    the template from variables with generate().
    z<string>N�
BaseLoader)�template_string�name�loader�compress_whitespace�
autoescape�
whitespacerc	CsRtj|�|_|tk	r0|dk	r$td��|r,dnd}|dkrh|rJ|jrJ|j}n|jd�s^|jd�rdd}nd}|dk	stt�t|d�t	|t
�s�||_n|r�|j|_nt|_|r�|j
ni|_
t|tj|�|�}t|t||��|_|j|�|_||_y,ttj|j�d|jjd	d
�ddd
�|_Wn6tk
�rLt|j�j�}tjd|j|��YnXdS)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_whitespacerrz.htmlz.js�z%s.generated.py�.�_�execT)�dont_inheritz%s code:
%s)rZ
native_strr'�_UNSETr"r+�endswith�AssertionErrorr#�
isinstancerr*�_DEFAULT_AUTOESCAPE�	namespace�_TemplateReader�_File�_parse�file�_generate_python�coder(�compileZ
to_unicode�replace�compiled�_format_code�rstripr�error)	�selfr&r'r(r)r*r+�readerZformatted_coderrr�__init__sB




zTemplate.__init__)�kwargsrcs�tjtjtjtjtjtjttjtt	f�j
jdd�t�fdd�d�d�}|j
�j�|j
|�t�j|�tjtgt	f|d�}tj�|�S)z0Generate this template with the given arguments.r-r.cs�jS)N)r<)r')rCrr�<lambda>_sz#Template.generate.<locals>.<lambda>)�
get_source)rr�
url_escape�json_encode�squeeze�linkify�datetimeZ_tt_utf8Z_tt_string_typesr�
__loader__Z_tt_execute)rrrIrJrKrLrM�utf8r�bytesr'r>r�updater6rr?�typing�castr
�	linecache�
clearcache)rCrFr6Zexecuter)rCr�generatePs"
zTemplate.generate)r(rcCspt�}zZi}|j|�}|j�x|D]}|j||�q$Wt||||dj�}|dj|�|j�S|j�XdS)Nr)	r�_get_ancestors�reverse�find_named_blocks�_CodeWriter�templaterV�getvalue�close)rCr(�buffer�named_blocks�	ancestorsZancestor�writerrrrr;ks

zTemplate._generate_pythonr8cCsV|jg}xH|jjjD]:}t|t�r|s.td��|j|j|j�}|j|j	|��qW|S)Nz1{% extends %} block found, but no template loader)
r:�body�chunksr4�
_ExtendsBlock�
ParseError�loadr'�extendrW)rCr(r`�chunkr[rrrrWzs
zTemplate._get_ancestors)rrr�__doc__r1r	�strrP�boolrrErrVrr;rrWrrrrr$�s(Cr$c@szeZdZdZeddfeeeefedd�dd�Zdd�dd�Z	deeed	�d
d�Z
deeed	�dd
�Zeed�dd�Z
dS)r%z�Base class for template loaders.

    You must use a template loader to use template constructs like
    ``{% extends %}`` and ``{% include %}``. The loader caches all
    templates after they are loaded the first time.
    N)r*r6r+rcCs*||_|pi|_||_i|_tj�|_dS)a�Construct a template loader.

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

        .. versionchanged:: 4.3
           Added ``whitespace`` parameter.
        N)r*r6r+�	templates�	threading�RLock�lock)rCr*r6r+rrrrE�s

zBaseLoader.__init__)rc	Cs|j�i|_WdQRXdS)z'Resets the cache of compiled templates.N)rorl)rCrrr�reset�szBaseLoader.reset)r'�parent_pathrcCs
t��dS)z@Converts a possibly-relative path to absolute (used internally).N)�NotImplementedError)rCr'rqrrr�resolve_path�szBaseLoader.resolve_pathc
CsD|j||d�}|j�&||jkr0|j|�|j|<|j|SQRXdS)zLoads a template.)rqN)rsrorl�_create_template)rCr'rqrrrrf�s

zBaseLoader.load)r'rcCs
t��dS)N)rr)rCr'rrrrt�szBaseLoader._create_template)N)N)rrrrir5rjrrrErprsr$rfrtrrrrr%�sr%csNeZdZdZeedd��fdd�Zdeeed�dd�Zeed	�d
d�Z	�Z
S)
�Loaderz?A template loader that loads from a single root directory.
    N)�root_directoryrFrcs$tt|�jf|�tjj|�|_dS)N)�superrurE�os�path�abspath�root)rCrvrF)�	__class__rrrE�szLoader.__init__)r'rqrcCs�|r�|jd�r�|jd�r�|jd�r�tjj|j|�}tjjtjj|��}tjjtjj||��}|j|j�r�|t|j�dd�}|S)N�<�/�)�
startswithrxry�joinr{�dirnamerz�len)rCr'rqZcurrent_path�file_dirZ
relative_pathrrrrs�szLoader.resolve_path)r'rcCs<tjj|j|�}t|d��}t|j�||d�}|SQRXdS)N�rb)r'r()rxryr�r{�openr$�read)rCr'ry�fr[rrrrt�szLoader._create_template)N)rrrrirjrrErsr$rt�
__classcell__rr)r|rru�srucsVeZdZdZeeefedd��fdd�Zdeeed�dd�Zee	d	�d
d�Z
�ZS)
�
DictLoaderz/A template loader that loads from a dictionary.N)�dictrFrcstt|�jf|�||_dS)N)rwr�rEr�)rCr�rF)r|rrrE�szDictLoader.__init__)r'rqrcCsH|rD|jd�rD|jd�rD|jd�rDtj|�}tjtj||��}|S)Nr}r~)r��	posixpathr��normpathr�)rCr'rqr�rrrrs�s
zDictLoader.resolve_path)r'rcCst|j|||d�S)N)r'r()r$r�)rCr'rrrrt�szDictLoader._create_template)N)rrrrirrjrrErsr$rtr�rr)r|rr��sr�c@sLeZdZedd�dd�Zddd�dd�Zeeee	d	fdd
�dd�Z
dS)
�_Node)rcCsfS)Nr)rCrrr�
each_child�sz_Node.each_childrZN)rarcCs
t��dS)N)rr)rCrarrrrV�sz_Node.generate�_NamedBlock)r(r_rcCs"x|j�D]}|j||�q
WdS)N)r�rY)rCr(r_ZchildrrrrY�sz_Node.find_named_blocks)rrrr
r�rVrr%rrjrYrrrrr��sr�c@s@eZdZeddd�dd�Zddd�dd	�Zed
d�dd
�ZdS)r8�
_ChunkListN)r[rbrcCs||_||_d|_dS)Nr)r[rb�line)rCr[rbrrrrEsz_File.__init__rZ)rarc
Cs\|jd|j�|j��<|jd|j�|jd|j�|jj|�|jd|j�WdQRXdS)Nzdef _tt_execute():z_tt_buffer = []z_tt_append = _tt_buffer.appendz$return _tt_utf8('').join(_tt_buffer))�
write_liner��indentrbrV)rCrarrrrVs
z_File.generater�)rcCs|jfS)N)rb)rCrrrr�sz_File.each_child)rrrr$rErVr
r�rrrrr8sr8c@sBeZdZeedd�dd�Zddd�dd�Zed	d
�dd�ZdS)
r�N)rcrcCs
||_dS)N)rc)rCrcrrrrEsz_ChunkList.__init__rZ)rarcCsx|jD]}|j|�qWdS)N)rcrV)rCrarhrrrrVsz_ChunkList.generater�)rcCs|jS)N)rc)rCrrrr�sz_ChunkList.each_child)	rrrrr�rErVr
r�rrrrr�sr�c@sbeZdZeeeedd�dd�Zedd�dd�Z	d	dd
�dd�Z
eee
edfdd
�dd�ZdS)r�N)r'rbr[r�rcCs||_||_||_||_dS)N)r'rbr[r�)rCr'rbr[r�rrrrE$sz_NamedBlock.__init__r�)rcCs|jfS)N)rb)rCrrrr�*sz_NamedBlock.each_childrZ)rarc	Cs8|j|j}|j|j|j��|jj|�WdQRXdS)N)r_r'�includer[r�rbrV)rCra�blockrrrrV-sz_NamedBlock.generate)r(r_rcCs|||j<tj|||�dS)N)r'r�rY)rCr(r_rrrrY2s
z_NamedBlock.find_named_blocks)rrrrjr�r$�intrEr
r�rVrr%rrYrrrrr�#s
r�c@seZdZedd�dd�ZdS)rdN)r'rcCs
||_dS)N)r')rCr'rrrrE:sz_ExtendsBlock.__init__)rrrrjrErrrrrd9srdc@sNeZdZededd�dd�Zeeeee	fdd�dd�Z
d	dd
�dd�ZdS)
�
_IncludeBlockr7N)r'rDr�rcCs||_|j|_||_dS)N)r'�
template_namer�)rCr'rDr�rrrrE?sz_IncludeBlock.__init__)r(r_rcCs.|dk	st�|j|j|j�}|jj||�dS)N)r3rfr'r�r:rY)rCr(r_�includedrrrrYDsz_IncludeBlock.find_named_blocksrZ)rarc	CsL|jdk	st�|jj|j|j�}|j||j��|jjj	|�WdQRXdS)N)
r(r3rfr'r�r�r�r:rbrV)rCrar�rrrrVKsz_IncludeBlock.generate)rrrrjr�rErr%rr�rYrVrrrrr�>sr�c@sBeZdZeeedd�dd�Zedd�dd�Zd	dd
�dd�Z	dS)
�_ApplyBlockN)�methodr�rbrcCs||_||_||_dS)N)r�r�rb)rCr�r�rbrrrrESsz_ApplyBlock.__init__r�)rcCs|jfS)N)rb)rCrrrr�Xsz_ApplyBlock.each_childrZ)rarcCs�d|j}|jd7_|jd||j�|j��<|jd|j�|jd|j�|jj|�|jd|j�WdQRX|jd|j|f|j�dS)Nz_tt_apply%drz	def %s():z_tt_buffer = []z_tt_append = _tt_buffer.appendz$return _tt_utf8('').join(_tt_buffer)z_tt_append(_tt_utf8(%s(%s()))))�
apply_counterr�r�r�rbrVr�)rCraZmethod_namerrrrV[s

z_ApplyBlock.generate)
rrrrjr�r�rEr
r�rVrrrrr�Rsr�c@sBeZdZeeedd�dd�Zeed�dd�Zddd	�d
d�Z	dS)�
_ControlBlockN)�	statementr�rbrcCs||_||_||_dS)N)r�r�rb)rCr�r�rbrrrrEjsz_ControlBlock.__init__)rcCs|jfS)N)rb)rCrrrr�osz_ControlBlock.each_childrZ)rarc
CsF|jd|j|j�|j�� |jj|�|jd|j�WdQRXdS)Nz%s:�pass)r�r�r�r�rbrV)rCrarrrrVrs
z_ControlBlock.generate)
rrrrjr�r�rEr
r�rVrrrrr�isr�c@s.eZdZeedd�dd�Zddd�dd�ZdS)	�_IntermediateControlBlockN)r�r�rcCs||_||_dS)N)r�r�)rCr�r�rrrrE{sz"_IntermediateControlBlock.__init__rZ)rarcCs0|jd|j�|jd|j|j|j�d�dS)Nr�z%s:r)r�r�r��indent_size)rCrarrrrVsz"_IntermediateControlBlock.generate)rrrrjr�rErVrrrrr�zsr�c@s.eZdZeedd�dd�Zddd�dd�ZdS)	�
_StatementN)r�r�rcCs||_||_dS)N)r�r�)rCr�r�rrrrE�sz_Statement.__init__rZ)rarcCs|j|j|j�dS)N)r�r�r�)rCrarrrrV�sz_Statement.generate)rrrrjr�rErVrrrrr��sr�c@s2eZdZd
eeedd�dd�Zddd�dd	�ZdS)�_ExpressionFN)�
expressionr��rawrcCs||_||_||_dS)N)r�r�r�)rCr�r�r�rrrrE�sz_Expression.__init__rZ)rarcCsl|jd|j|j�|jd|j�|jd|j�|jrZ|jjdk	rZ|jd|jj|j�|jd|j�dS)Nz_tt_tmp = %szEif isinstance(_tt_tmp, _tt_string_types): _tt_tmp = _tt_utf8(_tt_tmp)z&else: _tt_tmp = _tt_utf8(str(_tt_tmp))z_tt_tmp = _tt_utf8(%s(_tt_tmp))z_tt_append(_tt_tmp))r�r�r�r��current_templater*)rCrarrrrV�s
z_Expression.generate)F)rrrrjr�rkrErVrrrrr��sr�cs&eZdZeedd��fdd�Z�ZS)�_ModuleN)r�r�rcstt|�jd||dd�dS)Nz_tt_modules.T)r�)rwr�rE)rCr�r�)r|rrrE�sz_Module.__init__)rrrrjr�rEr�rr)r|rr��sr�c@s0eZdZeeedd�dd�Zddd�dd�ZdS)	�_TextN)�valuer�r+rcCs||_||_||_dS)N)r�r�r+)rCr�r�r+rrrrE�sz_Text.__init__rZ)rarcCs:|j}d|krt|j|�}|r6|jdtj|�|j�dS)Nz<pre>z_tt_append(%r))r�r#r+r�rrOr�)rCrar�rrrrV�s
z_Text.generate)rrrrjr�rErVrrrrr��sr�c@s4eZdZdZd
eeedd�dd�Zed�dd	�ZdS)rez�Raised for template syntax errors.

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

    .. versionchanged:: 4.3
       Added ``filename`` and ``lineno`` attributes.
    Nr)�message�filename�linenorcCs||_||_||_dS)N)r�r�r�)rCr�r�r�rrrrE�szParseError.__init__)rcCsd|j|j|jfS)Nz%s at %s:%d)r�r�r�)rCrrr�__str__�szParseError.__str__)Nr)rrrrirjr�rEr�rrrrre�srec@sreZdZeeeefeee	dd�dd�Z
ed�dd�Zdd�d	d
�Z
e	edd�dd
�Zdeeedd�dd�ZdS)rZN)r:r_r(r�rcCs.||_||_||_||_d|_g|_d|_dS)Nr)r:r_r(r�r��
include_stack�_indent)rCr:r_r(r�rrrrE�sz_CodeWriter.__init__)rcCs|jS)N)r�)rCrrrr��sz_CodeWriter.indent_sizercsG�fdd�dt�}|�S)Ncs2eZdZdd��fdd�Zedd��fdd�ZdS)	z$_CodeWriter.indent.<locals>.IndenterrZ)rcs�jd7_�S)Nr)r�)r.)rCrr�	__enter__�sz._CodeWriter.indent.<locals>.Indenter.__enter__N)�argsrcs �jdkst��jd8_dS)Nrr)r�r3)r.r�)rCrr�__exit__�sz-_CodeWriter.indent.<locals>.Indenter.__exit__)rrrr�rr�r)rCrr�Indenter�sr�)�object)rCr�r)rCrr��s	z_CodeWriter.indent)r[r�rcs2�jj�j|f�|�_G�fdd�dt�}|�S)Ncs2eZdZdd��fdd�Zedd��fdd�ZdS)	z,_CodeWriter.include.<locals>.IncludeTemplaterZ)rcs�S)Nr)r.)rCrrr��sz6_CodeWriter.include.<locals>.IncludeTemplate.__enter__N)r�rcs�jj�d�_dS)Nr)r��popr�)r.r�)rCrrr��sz5_CodeWriter.include.<locals>.IncludeTemplate.__exit__)rrrr�rr�r)rCrr�IncludeTemplate�sr�)r��appendr�r�)rCr[r�r�r)rCrr��sz_CodeWriter.include)r��line_numberr�rcCsh|dkr|j}d|jj|f}|jrJdd�|jD�}|ddjt|��7}td||||jd�dS)Nz	  # %s:%dcSsg|]\}}d|j|f�qS)z%s:%d)r')�.0Ztmplr�rrr�
<listcomp>sz*_CodeWriter.write_line.<locals>.<listcomp>z	 (via %s)z, z    )r:)r�r�r'r�r��reversed�printr:)rCr�r�r�Zline_commentr`rrrr��sz_CodeWriter.write_line)N)rrrrrrjr�rr%r$rEr�r�r�r�r�rrrrrZ�s
	
rZc@s�eZdZeeedd�dd�Zdeeeed�dd�Zdeed	�d
d�Zed�d
d�Zed�dd�Z	e
eefed�dd�Zed�dd�Z
edd�dd�ZdS)r7N)r'rr+rcCs"||_||_||_d|_d|_dS)Nrr)r'rr+r��pos)rCr'rr+rrrrE
s
z_TemplateReader.__init__r)�needle�start�endrcCsn|dkst|��|j}||7}|dkr6|jj||�}n$||7}||ksJt�|jj|||�}|dkrj||8}|S)Nrr���)r3r�r�find)rCr�r�r�r��indexrrrr�sz_TemplateReader.find)�countrcCsX|dkrt|j�|j}|j|}|j|jjd|j|�7_|j|j|�}||_|S)Nr)r�rr�r�r�)rCr�Znewpos�srrr�consumes
z_TemplateReader.consume)rcCst|j�|jS)N)r�rr�)rCrrr�	remaining(sz_TemplateReader.remainingcCs|j�S)N)r�)rCrrr�__len__+sz_TemplateReader.__len__)�keyrcCs�t|t�r`t|�}|j|�\}}}|dkr2|j}n
||j7}|dk	rN||j7}|jt|||�S|dkrr|j|S|j|j|SdS)Nr)r4�slicer��indicesr�r)rCr��sizer��stop�steprrr�__getitem__.s



z_TemplateReader.__getitem__cCs|j|jd�S)N)rr�)rCrrrr�>sz_TemplateReader.__str__)�msgrcCst||j|j��dS)N)rer'r�)rCr�rrr�raise_parse_errorAsz!_TemplateReader.raise_parse_error)rN)N)rrrrjrEr�r�r�r�r�r	r�r�r�r�rrrrr7	s	r7)r<rcs<|j�}dttt|�d���dj�fdd�t|�D��S)Nz%%%dd  %%s
rr,cs g|]\}}�|d|f�qS)rr)r��ir�)�formatrrr�Hsz _format_code.<locals>.<listcomp>)�
splitlinesr��reprr��	enumerate)r<�linesr)r�rr@Esr@)rDr[�in_block�in_looprcCsbtg�}�xRd}x�|jd|�}|d9ks6|d|j�krh|rH|jd|�|jjt|j�|j|j	��|S||dd:kr�|d7}q|d|j�kr�||ddkr�||ddkr�|d7}qPqW|dkr�|j|�}|jjt||j|j	��|jd�}|j}|j��r6|ddk�r6|jd�|jjt|||j	��q|d	k�rx|jd
�}	|	d;k�r^|jd�|j|	�j
�}
|jd�q|dk�r�|jd
�}	|	d<k�r�|jd�|j|	�j
�}
|jd�|
�s�|jd�|jjt|
|��q|dk�s�t|��|jd�}	|	d=k�r|jd�|j|	�j
�}
|jd�|
�s4|jd�|
j
d�\}}}
|
j
�}
tddddg�tdg�tdg�tdg�d�}|j|�}|dk	�r�|�s�|jd||f�||k�r�|jd||f�|jjt|
|��qq|dk�r�|�s�|jd�|S|d>k�rR|d#k�r
q|dk�r@|
j
d(�j
d)�}
|
�s4|jd*�t|
�}�n|d?k�rf|
�sZ|jd+�t|
|�}n�|dk�r�|
j
d(�j
d)�}
|
�s�|jd,�t|
||�}n�|d k�r�|
�s�|jd-�t|
|�}n~|d$k�r�|
j
�}|d.k�r�d}||_qnT|d%k�r|
j
�}t|d/�||_	qn.|d&k�r.t|
|d0d1�}n|d'k�rBt|
|�}|jj|�qq|d@k�r|dAk�rvt||||�}n(|d2k�r�t|||d�}nt||||�}|d2k�r�|
�s�|jd4�t|
||�}n6|d3k�r�|
�s�|jd5�t|
|||�}nt|
||�}|jj|�qq|dBk�rL|�s6|jd|tddg�f�|jjt|
|��qq|jd8|�qWdS)CNr�{rz Missing {%% end %%} block for %s�%�#��!z{#z#}zMissing end comment #}z{{z}}zMissing end expression }}zEmpty expressionz{%z%}zMissing end block %}zEmpty block tag ({% %})r�if�for�while�try)�else�elif�except�finallyz%s outside %s blockz'%s block cannot be attached to %s blockr�zExtra {% end %} block�extendsr��set�import�from�commentr*r+r��module�"�'zextends missing file pathzimport missing statementzinclude missing file pathzset missing statement�Noner,T)r��applyr�zapply missing method namezblock missing name�break�continuezunknown operator: %rr�)r�r�r�r�r�r�)
r�r�r�r�r�r�r*r+r�r�)r�r�)r�r�r�r�r�r�)r�r�)r�r�)r�r�r�r�rcr�r�r�r�r+�stripr�r3�	partitionr��getr�rdr�r�r*r#r�r9r�r�r�)rDr[r�r�rbZcurlyZconsZstart_bracer�r��contents�operatorZspace�suffixZintermediate_blocksZallowed_parentsr��fnrZ
block_bodyrrrr9Ks&
















































r9)NN);rirM�iorrTZos.pathrxr�r rmZtornadorZtornado.logrZtornado.utilrrrrRrr	r
rrr
rrZ
TYPE_CHECKINGrrr5rr1rjr#r�r$r%rur�r�r8r�r�rdr�r�r�r�r�r�r�r�r"rerZr7r@r9rrrr�<module>�sV(
=	8<