HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/env/lib/python3.10/site-packages/tornado/__pycache__/template.cpython-310.pyc
o

we���@sbdZddlZddlmZddlZddlZddlZddlZddl	Z	ddl
mZddlm
Z
ddlmZmZmZddlmZmZmZmZmZmZmZmZddlZejrYddlmZmZd	ZGd
d�d�Ze�Z de!d
e!de!fdd�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(�Z/Gd)d*�d*e(�Z0Gd+d,�d,e(�Z1Gd-d.�d.e(�Z2Gd/d0�d0e2�Z3Gd1d2�d2e(�Z4Gd3d4�d4e5�Z6Gd5d6�d6e#�Z7Gd7d8�d8e#�Z8d9e!de!fd:d;�Z9		dBd<e8d=e$d>ee!d?ee!de*f
d@dA�Z:dS)Ca�A simple template system that compiles templates to Python code.

Basic usage looks like::

    t = template.Template("<html>{{ myvalue }}</html>")
    print(t.generate(myvalue="XXX"))

`Loader` is a class that loads templates from a root directory and caches
the compiled templates::

    loader = template.Loader("/home/btaylor")
    print(loader.load("test.html").generate(myvalue="XXX"))

We compile all templates to raw Python. Error-reporting is currently... uh,
interesting. Syntax for the templates::

    ### base.html
    <html>
      <head>
        <title>{% block title %}Default title{% end %}</title>
      </head>
      <body>
        <ul>
          {% for student in students %}
            {% block student %}
              <li>{{ escape(student.name) }}</li>
            {% end %}
          {% end %}
        </ul>
      </body>
    </html>

    ### bold.html
    {% extends "base.html" %}

    {% block title %}A bolder title{% end %}

    {% block student %}
      <li><span style="bold">{{ escape(student.name) }}</span></li>
    {% end %}

Unlike most other template systems, we do not put any restrictions on the
expressions you can include in your statements. ``if`` and ``for`` blocks get
translated exactly into Python, so you can do complex expressions like::

   {% for student in [p for p in people if p.student and p.age > 23] %}
     <li>{{ escape(student.name) }}</li>
   {% end %}

Translating directly to Python means you can apply functions to expressions
easily, like the ``escape()`` function in the examples above. You can pass
functions in to your template just like any other variable
(In a `.RequestHandler`, override `.RequestHandler.get_template_namespace`)::

   ### Python code
   def add(x, y):
      return x + y
   template.execute(add=add)

   ### The template
   {{ add(1, 2) }}

We provide the functions `escape() <.xhtml_escape>`, `.url_escape()`,
`.json_encode()`, and `.squeeze()` to all templates by default.

Typical applications do not create `Template` or `Loader` instances by
hand, but instead use the `~.RequestHandler.render` and
`~.RequestHandler.render_string` methods of
`tornado.web.RequestHandler`, which load templates automatically based
on the ``template_path`` `.Application` setting.

Variable names beginning with ``_tt_`` are reserved by the template
system and should not be used by application code.

Syntax Reference
----------------

Template expressions are surrounded by double curly braces: ``{{ ... }}``.
The contents may be any python expression, which will be escaped according
to the current autoescape setting and inserted into the output.  Other
template directives use ``{% %}``.

To comment out a section so that it is omitted from the output, surround it
with ``{# ... #}``.


To include a literal ``{{``, ``{%``, or ``{#`` in the output, escape them as
``{{!``, ``{%!``, and ``{#!``, respectively.


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

``{% whitespace *mode* %}``
    Sets the whitespace mode for the remainder of the current file
    (or until the next ``{% whitespace %}`` directive). See
    `filter_whitespace` for available options. New in Tornado 4.3.
�N)�StringIO)�escape)�app_log)�
ObjectDict�exec_in�unicode_type)�Any�Union�Callable�List�Dict�Iterable�Optional�TextIO)�Tuple�ContextManager�xhtml_escapec@seZdZdS)�_UnsetMarkerN)�__name__�
__module__�__qualname__�rr�I/home/arjun/projects/env/lib/python3.10/site-packages/tornado/template.pyr�sr�mode�text�returncCsV|dkr|S|dkrt�dd|�}t�dd|�}|S|dkr%t�dd|�Std	|��)
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+)zinvalid whitespace mode %s)�re�sub�	Exception)rrrrr�filter_whitespace�s
r$c@s�eZdZdZddeedfdeeefdededdee	e
fd	eeee
fd
eeddfdd
�Zdedefdd�Z
deddefdd�Zdeddedfdd�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�template_string�name�loader�
BaseLoader�compress_whitespace�
autoescape�
whitespacerc	CsHt�|�|_|tur|durtd��|rdnd}|dur4|r%|jr%|j}n|�d�s/|�d�r2d}nd}|dus:J�t|d�t|t	�sH||_
n
|rO|j
|_
nt|_
|rW|jni|_t
|t�|�|�}t|t||��|_|�|�|_||_ztt�|j�d|j�d	d
�ddd
�|_WdSty�t|j���}t�d|j|��w)a�Construct a Template.

        :arg str template_string: the contents of the template file.
        :arg str name: the filename from which the template was loaded
            (used for error message).
        :arg tornado.template.BaseLoader loader: the `~tornado.template.BaseLoader` responsible
            for this template, used to resolve ``{% include %}`` and ``{% extend %}`` directives.
        :arg bool compress_whitespace: Deprecated since Tornado 4.3.
            Equivalent to ``whitespace="single"`` if true and
            ``whitespace="all"`` if false.
        :arg str autoescape: The name of a function in the template
            namespace, or ``None`` to disable escaping by default.
        :arg str whitespace: A string specifying treatment of whitespace;
            see `filter_whitespace` for options.

        .. versionchanged:: 4.3
           Added ``whitespace`` parameter; deprecated ``compress_whitespace``.
        Nz2cannot set both whitespace and compress_whitespacerrz.htmlz.js�z%s.generated.py�.�_�execT)�dont_inheritz%s code:
%s)r�
native_strr'�_UNSETr#r,�endswithr$�
isinstancerr+�_DEFAULT_AUTOESCAPE�	namespace�_TemplateReader�_File�_parse�file�_generate_python�coder(�compile�
to_unicode�replace�compiled�_format_code�rstripr�error)	�selfr&r'r(r*r+r,�reader�formatted_coderrr�__init__sF




��zTemplate.__init__�kwargscs�tjtjtjtjtjtjttjtt	f�j
�dd�t�fdd�d�d�}|�
�j�|�
|�t�j|�t�tgt	f|d�}t��|�S)z0Generate this template with the given arguments.r.r/cs�jS�N)r=�r'�rErr�<lambda>`sz#Template.generate.<locals>.<lambda>)�
get_source)rr�
url_escape�json_encode�squeeze�linkify�datetime�_tt_utf8�_tt_string_typesr�
__loader__�_tt_execute)rrrOrPrQrRrS�utf8r�bytesr'r@r�updater7rrA�typing�castr
�	linecache�
clearcache)rErIr7�executerrLr�generateQs$�
zTemplate.generatecCsrt�}z0i}|�|�}|��|D]}|�||�qt||||dj�}|d�|�|��W|��S|��w�Nr)	r�_get_ancestors�reverse�find_named_blocks�_CodeWriter�templater`�getvalue�close)rEr(�buffer�named_blocks�	ancestors�ancestor�writerrrrr<ls
zTemplate._generate_pythonr9cCsR|jg}|jjjD]}t|t�r&|std��|�|j|j�}|�|�	|��q	|S)Nz1{% extends %} block found, but no template loader)
r;�body�chunksr5�
_ExtendsBlock�
ParseError�loadr'�extendrb)rEr(rk�chunkrfrrrrb{s
��zTemplate._get_ancestors)rrr�__doc__r3r	�strrYr�boolrrHrr`r<rrbrrrrr%�s2�
���
���
�Kr%c	@s�eZdZdZeddfdedeeeefdeeddfdd�Z	dd	d
�Z
ddedeedefd
d�Zddedeedefdd�Z
dedefdd�ZdS)r)z�Base class for template loaders.

    You must use a template loader to use template constructs like
    ``{% extends %}`` and ``{% include %}``. The loader caches all
    templates after they are loaded the first time.
    Nr+r7r,rcCs*||_|pi|_||_i|_t��|_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+r7r,�	templates�	threading�RLock�lock)rEr+r7r,rrrrH�s

zBaseLoader.__init__cCs2|j�i|_Wd�dS1swYdS)z'Resets the cache of compiled templates.N)r{rxrLrrr�reset�s"�zBaseLoader.resetr'�parent_pathcC�t��)z@Converts a possibly-relative path to absolute (used internally).��NotImplementedError�rEr'r}rrr�resolve_path�szBaseLoader.resolve_pathcCs\|j||d�}|j�||jvr|�|�|j|<|j|Wd�S1s'wYdS)zLoads a template.)r}N)r�r{rx�_create_templater�rrrrr�s
$�zBaseLoader.loadcCr~rJr�rEr'rrrr���zBaseLoader._create_template)rNrJ)rrrrur6rvrrrrHr|r�r%rrr�rrrrr)�s$	����
�
 r)cs\eZdZdZdededdf�fdd�Zdded	eedefd
d�Zdede	fdd
�Z
�ZS)�Loaderz:A template loader that loads from a single root directory.�root_directoryrIrNcs$t�jdi|��tj�|�|_dS�Nr)�superrH�os�path�abspath�root)rEr�rI��	__class__rrrH�szLoader.__init__r'r}cCs�|r?|�d�s?|�d�s?|�d�s?tj�|j|�}tj�tj�|��}tj�tj�||��}|�|j�r?|t|j�dd�}|S)N�<�/�)�
startswithr�r��joinr��dirnamer��len)rEr'r}�current_path�file_dir�
relative_pathrrrr��s����zLoader.resolve_pathcCsTtj�|j|�}t|d��}t|��||d�}|Wd�S1s#wYdS)N�rb�r'r()r�r�r�r��openr%�read)rEr'r��frfrrrr��s
$�zLoader._create_templaterJ)rrrrurvrrHrr�r%r��
__classcell__rrr�rr��s
r�csdeZdZdZdeeefdeddf�fdd�Zdded	eedefd
d�Z	dede
fdd
�Z�ZS)�
DictLoaderz/A template loader that loads from a dictionary.�dictrIrNcst�jdi|��||_dSr�)r�rHr�)rEr�rIr�rrrH�s
zDictLoader.__init__r'r}cCsB|r|�d�s|�d�s|�d�st�|�}t�t�||��}|S)Nr�r�)r��	posixpathr��normpathr�)rEr'r}r�rrrr��s����
zDictLoader.resolve_pathcCst|j|||d�S)Nr�)r%r�r�rrrr���zDictLoader._create_templaterJ)
rrrrurrvrrHrr�r%r�r�rrr�rr��s
"r�c@sJeZdZdedfdd�Zddd�Zd	eed
ee	dfddfdd
�Z
dS)�_NodercCsdSr�rrLrrr�
each_child��z_Node.each_childrmreNcCr~rJr�rErmrrrr`�r�z_Node.generater(rj�_NamedBlockcCs|��D]}|�||�qdSrJ)r�rd)rEr(rj�childrrrrd�s�z_Node.find_named_blocks�rmrerN)rrrr
r�r`rr)rrvrdrrrrr��s
�
��r�c@s>eZdZdeddddfdd�Zdd
d�Zdedfd
d�ZdS)r9rfrn�
_ChunkListrNcCs||_||_d|_dSra)rfrn�line)rErfrnrrrrH�
z_File.__init__rmrecCsr|�d|j�|���$|�d|j�|�d|j�|j�|�|�d|j�Wd�dS1s2wYdS)Nzdef _tt_execute():�_tt_buffer = []�_tt_append = _tt_buffer.append�$return _tt_utf8('').join(_tt_buffer))�
write_liner��indentrnr`r�rrrr`s
"�z_File.generater�cC�|jfSrJ�rnrLrrrr��z_File.each_childr�)rrrr%rHr`r
r�rrrrr9s
r9c@s>eZdZdeeddfdd�Zd
dd	�Zded
fdd�ZdS)r�rorNcC�
||_dSrJ�ro)rErorrrrH�
z_ChunkList.__init__rmrecCs|jD]}|�|�qdSrJ)ror`)rErmrtrrrr`s
�z_ChunkList.generater�cC�|jSrJr�rLrrrr�r�z_ChunkList.each_childr�)	rrrrr�rHr`r
r�rrrrr�s
r�c
@sheZdZdededededdf
dd�Zded	fd
d�Z	ddd�Z
deede
edfddfdd�ZdS)r�r'rnrfr�rNcCs||_||_||_||_dSrJ)r'rnrfr�)rEr'rnrfr�rrrrH$s
z_NamedBlock.__init__r�cCr�rJr�rLrrrr�*r�z_NamedBlock.each_childrmrecCsN|j|j}|�|j|j��|j�|�Wd�dS1s wYdSrJ)rjr'�includerfr�rnr`)rErm�blockrrrr`-s"�z_NamedBlock.generater(rjcCs|||j<t�|||�dSrJ)r'r�rd)rEr(rjrrrrd2s
z_NamedBlock.find_named_blocksr�)rrrrvr�r%�intrHr
r�r`rr)rrdrrrrr�#s
�
��r�c@seZdZdeddfdd�ZdS)rpr'rNcCr�rJrKr�rrrrH:r�z_ExtendsBlock.__init__)rrrrvrHrrrrrp9srpc@sReZdZdedddeddfdd�Zd	eed
eee	fddfdd�Z
ddd�ZdS)�
_IncludeBlockr'rFr8r�rNcCs||_|j|_||_dSrJ)r'�
template_namer�)rEr'rFr�rrrrH?s
z_IncludeBlock.__init__r(rjcCs.|dusJ�|�|j|j�}|j�||�dSrJ)rrr'r�r;rd)rEr(rj�includedrrrrdDsz_IncludeBlock.find_named_blocksrmrecCsb|jdusJ�|j�|j|j�}|�||j��|jj�|�Wd�dS1s*wYdSrJ)	r(rrr'r�r�r�r;rnr`)rErmr�rrrr`Ks
"�z_IncludeBlock.generater�)rrrrvr�rHrr)rr�rdr`rrrrr�>s�
�
�r�c@sBeZdZdedededdfdd�Zdedfd	d
�Zdd
d�Z	dS)�_ApplyBlock�methodr�rnrNcC�||_||_||_dSrJ)r�r�rn)rEr�r�rnrrrrHSr�z_ApplyBlock.__init__r�cCr�rJr�rLrrrr�Xr�z_ApplyBlock.each_childrmrecCs�d|j}|jd7_|�d||j�|���#|�d|j�|�d|j�|j�|�|�d|j�Wd�n1s?wY|�d|j|f|j�dS)Nz_tt_apply%dr�z	def %s():r�r�r�z_tt_append(_tt_utf8(%s(%s()))))�
apply_counterr�r�r�rnr`r�)rErm�method_namerrrr`[s

��z_ApplyBlock.generater��
rrrrvr�r�rHr
r�r`rrrrr�R�r�c@sBeZdZdedededdfdd�Zdeefdd	�Zddd
�Z	dS)�
_ControlBlock�	statementr�rnrNcCr�rJ)r�r�rn)rEr�r�rnrrrrHjr�z_ControlBlock.__init__cCr�rJr�rLrrrr�or�z_ControlBlock.each_childrmrecCs\|�d|j|j�|���|j�|�|�d|j�Wd�dS1s'wYdS)N�%s:�pass)r�r�r�r�rnr`r�rrrr`rs

"�z_ControlBlock.generater�r�rrrrr�ir�r�c@�,eZdZdededdfdd�Zdd	d
�ZdS)�_IntermediateControlBlockr�r�rNcC�||_||_dSrJ�r�r��rEr�r�rrrrH{�
z"_IntermediateControlBlock.__init__rmrecCs0|�d|j�|�d|j|j|��d�dS)Nr�r�r�)r�r�r��indent_sizer�rrrr`s"z"_IntermediateControlBlock.generater��rrrrvr�rHr`rrrrr�z�r�c@r�)�
_Statementr�r�rNcCr�rJr�r�rrrrH�r�z_Statement.__init__rmrecCs|�|j|j�dSrJ)r�r�r�r�rrrr`�r�z_Statement.generater�r�rrrrr��r�r�c	@s2eZdZd
dedededdfdd�Zddd�ZdS)�_ExpressionF�
expressionr��rawrNcCr�rJ)r�r�r�)rEr�r�r�rrrrH�r�z_Expression.__init__rmrecCsj|�d|j|j�|�d|j�|�d|j�|js,|jjdur,|�d|jj|j�|�d|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+r�rrrr`�s�
�z_Expression.generate)Fr�)rrrrvr�rwrHr`rrrrr��sr�cs*eZdZdededdf�fdd�Z�ZS)�_Moduler�r�rNcst�jd||dd�dS)Nz_tt_modules.T�r�)r�rH)rEr�r�r�rrrH�sz_Module.__init__)rrrrvr�rHr�rrr�rr��s"r�c@s0eZdZdedededdfdd�Zdd
d�ZdS)
�_Text�valuer�r,rNcCr�rJ)r�r�r,)rEr�r�r,rrrrH�r�z_Text.__init__rmrecCs>|j}d|vr
t|j|�}|r|�dt�|�|j�dSdS)Nz<pre>z_tt_append(%r))r�r$r,r�rrXr�)rErmr�rrrr`�s�z_Text.generater�r�rrrrr��sr�c	@s@eZdZdZ	ddedeededdfdd	�Zdefd
d�ZdS)
rqz�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�linenorcCr�rJ�r�r�r�)rEr�r�r�rrrrH�s
zParseError.__init__cCsd|j|j|jfS)Nz%s at %s:%dr�rLrrr�__str__�r�zParseError.__str__ra)	rrrrurvrr�rHr�rrrrrq�s
����
�	rqc
@s�eZdZdedeeefdeede	ddf
dd�Z
defd	d
�Zddd
�Z
de	deddfdd�Z	ddededeeddfdd�ZdS)rer;rjr(r�rNcCs.||_||_||_||_d|_g|_d|_dSra)r;rjr(r�r��
include_stack�_indent)rEr;rjr(r�rrrrH�s
z_CodeWriter.__init__cCr�rJ�r�rLrrrr��r�z_CodeWriter.indent_sizercsG�fdd�dt�}|�S)Nc�0eZdZd	�fdd�Zdeddf�fdd�ZdS)
z$_CodeWriter.indent.<locals>.Indenterrrecs�jd7_�S)Nr�r��r/rLrr�	__enter__�sz._CodeWriter.indent.<locals>.Indenter.__enter__�argsNcs �jdksJ��jd8_dS)Nrr�r��r/r�rLrr�__exit__�sz-_CodeWriter.indent.<locals>.Indenter.__exit__�rre�rrrr�rr�rrLrr�Indenter�sr�)�object)rEr�rrLrr��s	z_CodeWriter.indentrfr�cs2�j��j|f�|�_G�fdd�dt�}|�S)Ncr�)
z,_CodeWriter.include.<locals>.IncludeTemplaterrecs�SrJrr�rLrrr��r�z6_CodeWriter.include.<locals>.IncludeTemplate.__enter__r�Ncs�j��d�_dSra)r��popr�r�rLrrr��r�z5_CodeWriter.include.<locals>.IncludeTemplate.__exit__r�r�rrLrr�IncludeTemplate�sr�)r��appendr�r�)rErfr�r�rrLrr��sz_CodeWriter.include�line_numberr�cCsh|dur|j}d|jj|f}|jr%dd�|jD�}|dd�t|��7}td||||jd�dS)Nz	  # %s:%dcSsg|]\}}d|j|f�qS)z%s:%drK)�.0�tmplr�rrr�
<listcomp>s�z*_CodeWriter.write_line.<locals>.<listcomp>z	 (via %s)z, z    )r;)r�r�r'r�r��reversed�printr;)rEr�r�r��line_commentrkrrrr��s�z_CodeWriter.write_line)rrrJ)rrrrrrvr�rr)r%rHr�r�r�r�r�rrrrre�s2�
���
�
�����rec	@s�eZdZdedededdfdd�Zdd	ed
edeedefdd
�Zddeedefdd�Zdefdd�Z	defdd�Z
deeefdefdd�Z
defdd�Zdeddfdd�ZdS)r8r'rr,rNcCs"||_||_||_d|_d|_dS)Nr�r)r'rr,r��pos)rEr'rr,rrrrHs

z_TemplateReader.__init__r�needle�start�endcCsn|dksJ|��|j}||7}|dur|j�||�}n||7}||ks%J�|j�|||�}|dkr5||8}|S)Nr���)r�r�find)rEr�r�r�r��indexrrrr�sz_TemplateReader.find�countcCsX|durt|j�|j}|j|}|j|j�d|j|�7_|j|j|�}||_|S)Nr)r�rr�r�r)rEr�newpos�srrr�consume#s
z_TemplateReader.consumecCst|j�|jSrJ)r�rr�rLrrr�	remaining,�z_TemplateReader.remainingcCs|��SrJ)rrLrrr�__len__/r�z_TemplateReader.__len__�keycCs�t|t�r0t|�}|�|�\}}}|dur|j}n||j7}|dur'||j7}|jt|||�S|dkr9|j|S|j|j|Sra)r5�slicer��indicesr�r)rEr�sizer��stop�steprrr�__getitem__2s



z_TemplateReader.__getitem__cCs|j|jd�SrJ)rr�rLrrrr�Brz_TemplateReader.__str__�msgcCst||j|j��rJ)rqr'r�)rErrrr�raise_parse_errorErz!_TemplateReader.raise_parse_error)rNrJ)rrrrvrHr�rr�rrrr	r	rr�rrrrrr8
s 	r8r=cs<|��}dttt|�d���d��fdd�t|�D��S)Nz%%%dd  %%s
r�r-cs g|]\}}�|d|f�qS)r�r)r��ir���formatrrr�Ls z _format_code.<locals>.<listcomp>)�
splitlinesr��reprr��	enumerate)r=�linesrrrrBIsrBrFrf�in_block�in_loopcCs2tg�}	d}	|�d|�}|dks|d|��kr3|r#|�d|�|j�t|��|j|j	��|S||ddvr@|d7}q|d|��kr]||ddkr]||ddkr]|d7}q	|dkrs|�|�}|j�t||j|j	��|�d�}|j}|��r�|dd	kr�|�d�|j�t|||j	��q|d
kr�|�d�}	|	dkr�|�d�|�|	��
�}
|�d�q|d
kr�|�d�}	|	dkr�|�d�|�|	��
�}
|�d�|
s�|�d�|j�t|
|��q|dks�J|��|�d�}	|	dkr�|�d�|�|	��
�}
|�d�|
�s|�d�|
�d�\}}}
|
�
�}
t
gd��t
dg�t
dg�t
dg�d�}|�|�}|du�r\|�sD|�d||f�||v�rR|�d||f�|j�t|
|��q|dk�rk|�si|�d�|S|dv�r|dk�rvq|d k�r�|
�
d!��
d"�}
|
�s�|�d#�t|
�}n|d$v�r�|
�s�|�d%�t|
|�}nl|d&k�r�|
�
d!��
d"�}
|
�s�|�d'�t|
||�}nP|d(k�r�|
�s�|�d)�t|
|�}n=|d*k�r�|
�
�}|d+k�r�d}||_q|d,k�r�|
�
�}t|d-�||_	q|d.k�rt|
|dd/�}n
|d0k�rt|
|�}|j�|�q|d1v�rr|d2v�r(t||||�}n|d3k�r5t|||d�}nt||||�}|d3k�rP|
�sI|�d4�t|
||�}n|d5k�re|
�s]|�d6�t|
|||�}nt|
||�}|j�|�q|d7v�r�|�s�|�d|t
d8d9g�f�|j�t|
|��q|�d:|�q);NTr�{r�r�z Missing {%% end %%} block for %s)r�%�#��!z{#z#}zMissing end comment #}z{{z}}zMissing end expression }}zEmpty expressionz{%z%}zMissing end block %}zEmpty block tag ({% %})r)�if�for�while�tryrr")�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��moduler+r'�"�'zextends missing file path)r)r*zimport missing statementr�zinclude missing file pathr(zset missing statementr+�Noner,r-r�r�r,)�applyr�r"rr r!)r r!r0zapply missing method namer�zblock missing name)�break�continuer r!zunknown operator: %r)r�r�rrror�r�rr�r,�stripr��	partitionr(�getr�rpr�r�r+r$r�r:r�r�r�)rFrfrrrn�curly�cons�start_bracer�r��contents�operator�space�suffix�intermediate_blocks�allowed_parentsr��fnr�
block_bodyrrrr:Os"��














�


�

�



























���r:)NN);rurS�iorr]�os.pathr�r�r!ry�tornador�tornado.logr�tornado.utilrrrr[rr	r
rrr
rr�
TYPE_CHECKINGrrr6rr3rvr$r�r%r)r�r�r�r9r�r�rpr�r�r�r�r�r�r�r�r#rqrer8rBr:rrrr�<module>sn8(
=	:<	������