
[c           @` s  d  Z  d d l m Z m Z 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 Z d d l m Z d d l m Z d d l m Z m Z m Z m Z e r d d l m Z n d d l m Z d Z e   Z d	   Z d
 e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ  d e  f d     YZ! d e  f d     YZ" d e  f d     YZ# d e  f d     YZ$ d e  f d     YZ% d e  f d     YZ& d  e  f d!     YZ' d" e  f d#     YZ( d$ e  f d%     YZ) d& e  f d'     YZ* d( e* f d)     YZ+ d* e  f d+     YZ, d, e- f d-     YZ. d. e f d/     YZ/ d0 e f d1     YZ0 d2   Z1 d d d3  Z3 d S(4   s  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.
i    (   t   absolute_importt   divisiont   print_functionN(   t   escape(   t   app_log(   t
   ObjectDictt   exec_int   unicode_typet   PY3(   t   StringIOt   xhtml_escapec         C` s}   |  d k r | S|  d k rJ t  j d d |  } t  j d d |  } | S|  d k ri t  j d d |  St d	 |    d
 S(   s  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
    t   allt   singles   ([\t ]+)t    s
   (\s*\n\s*)s   
t   onelines   (\s+)s   invalid whitespace mode %sN(   t   ret   subt	   Exception(   t   modet   text(    (    s/   lib/python2.7/site-packages/tornado/template.pyt   filter_whitespace   s    t   Templatec           B` sA   e  Z d  Z d d e e d d  Z d   Z d   Z d   Z RS(   s   A compiled template.

    We compile into Python from the given template_string. You can generate
    the template from variables with generate().
    s   <string>c   	      C` s  t  j |  |  _ | t k	 rN | d k	 r9 t d   n  | rE d n d } n  | d k r | ru | j ru | j } q | j d  s | j d  r d } q d } n  t | d  | t k	 r | |  _	 n | r | j	 |  _	 n	 t
 |  _	 | r | j n i  |  _ t | t  j |  |  } t |  t | |    |  _ |  j |  |  _ | |  _ y> t t  j |  j  d |  j j d d	  d
 d t |  _ Wn? t k
 rt |  j  j   } t j d |  j |    n Xd S(   s  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``.
        s2   cannot set both whitespace and compress_whitespaceR   R   s   .htmls   .jst    s   %s.generated.pyt   .t   _t   exect   dont_inherits   %s code:
%sN(   R   t
   native_strt   namet   _UNSETt   NoneR   t
   whitespacet   endswithR   t
   autoescapet   _DEFAULT_AUTOESCAPEt	   namespacet   _TemplateReadert   _Filet   _parset   filet   _generate_pythont   codet   loadert   compilet
   to_unicodet   replacet   Truet   compiledt   _format_codet   rstripR   t   error(	   t   selft   template_stringR   R*   t   compress_whitespaceR!   R   t   readert   formatted_code(    (    s/   lib/python2.7/site-packages/tornado/template.pyt   __init__   s@    					c         ` s   i t  j d 6t  j d 6t  j d 6t  j d 6t  j d 6t  j d 6t d 6t  j d 6t t	 f d	 6  j
 j d
 d  d 6t d   f d    d 6} | j   j  | j |  t   j |  | d } t j   |   S(   s0   Generate this template with the given arguments.R   R
   t
   url_escapet   json_encodet   squeezet   linkifyt   datetimet   _tt_utf8t   _tt_string_typesR   R   t   __name__t
   get_sourcec         ` s     j  S(   N(   R)   (   R   (   R3   (    s/   lib/python2.7/site-packages/tornado/template.pyt   <lambda>P  s    t
   __loader__t   _tt_execute(   R   R
   R9   R:   R;   R<   R=   t   utf8R   t   bytesR   R-   R   t   updateR#   R   R/   t	   linecachet
   clearcache(   R3   t   kwargsR#   t   execute(    (   R3   s/   lib/python2.7/site-packages/tornado/template.pyt   generateA  s$    








c         C` s   t    } z{ i  } |  j |  } | j   x | D] } | j | |  q2 Wt | | | | d j  } | d j |  | j   SWd  | j   Xd  S(   Ni    (	   R	   t   _get_ancestorst   reverset   find_named_blockst   _CodeWritert   templateRL   t   getvaluet   close(   R3   R*   t   buffert   named_blockst	   ancestorst   ancestort   writer(    (    s/   lib/python2.7/site-packages/tornado/template.pyR(   \  s    	
c         C` s   |  j  g } xl |  j  j j D][ } t | t  r | sF t d   n  | j | j |  j  } | j | j	 |   q q W| S(   Ns1   {% extends %} block found, but no template loader(
   R'   t   bodyt   chunkst
   isinstancet   _ExtendsBlockt
   ParseErrort   loadR   t   extendRM   (   R3   R*   RV   t   chunkRQ   (    (    s/   lib/python2.7/site-packages/tornado/template.pyRM   l  s    N(	   R@   t
   __module__t   __doc__R   R   R8   RL   R(   RM   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyR      s   A		t
   BaseLoaderc           B` sJ   e  Z d  Z e d d d  Z d   Z d d  Z d d  Z d   Z	 RS(   s   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.
    c         C` s=   | |  _  | p i  |  _ | |  _ i  |  _ t j   |  _ d S(   s  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!   R#   R   t	   templatest	   threadingt   RLockt   lock(   R3   R!   R#   R   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR8     s
    			c         C` s   |  j   i  |  _ Wd QXd S(   s'   Resets the cache of compiled templates.N(   Rg   Rd   (   R3   (    (    s/   lib/python2.7/site-packages/tornado/template.pyt   reset  s    
c         C` s   t     d S(   s@   Converts a possibly-relative path to absolute (used internally).N(   t   NotImplementedError(   R3   R   t   parent_path(    (    s/   lib/python2.7/site-packages/tornado/template.pyt   resolve_path  s    c         C` s\   |  j  | d | } |  j 8 | |  j k rG |  j |  |  j | <n  |  j | SWd QXd S(   s   Loads a template.Rj   N(   Rk   Rg   Rd   t   _create_template(   R3   R   Rj   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR^     s
    
c         C` s   t     d  S(   N(   Ri   (   R3   R   (    (    s/   lib/python2.7/site-packages/tornado/template.pyRl     s    N(
   R@   Ra   Rb   R"   R   R8   Rh   Rk   R^   Rl   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyRc   x  s   	t   Loaderc           B` s,   e  Z d  Z d   Z d d  Z d   Z RS(   s?   A template loader that loads from a single root directory.
    c         K` s/   t  t |   j |   t j j |  |  _ d  S(   N(   t   superRm   R8   t   ost   patht   abspatht   root(   R3   t   root_directoryRJ   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR8     s    c         C` s   | r | j  d  r | j  d  r | j  d  r t j j |  j |  } t j j t j j |   } t j j t j j | |   } | j  |  j  r | t |  j  d } q n  | S(   Nt   <t   /i   (   t
   startswithRo   Rp   t   joinRr   t   dirnameRq   t   len(   R3   R   Rj   t   current_patht   file_dirt   relative_path(    (    s/   lib/python2.7/site-packages/tornado/template.pyRk     s    !c      
   C` sV   t  j j |  j |  } t | d  ) } t | j   d | d |  } | SWd  QXd  S(   Nt   rbR   R*   (   Ro   Rp   Rw   Rr   t   openR   t   read(   R3   R   Rp   t   fRQ   (    (    s/   lib/python2.7/site-packages/tornado/template.pyRl     s    N(   R@   Ra   Rb   R8   R   Rk   Rl   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyRm     s   	t
   DictLoaderc           B` s,   e  Z d  Z d   Z d d  Z d   Z RS(   s/   A template loader that loads from a dictionary.c         K` s#   t  t |   j |   | |  _ d  S(   N(   Rn   R   R8   t   dict(   R3   R   RJ   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR8     s    c         C` sg   | rc | j  d  rc | j  d  rc | j  d  rc t j |  } t j t j | |   } n  | S(   NRt   Ru   (   Rv   t	   posixpathRx   t   normpathRw   (   R3   R   Rj   R{   (    (    s/   lib/python2.7/site-packages/tornado/template.pyRk     s    c         C` s   t  |  j | d | d |  S(   NR   R*   (   R   R   (   R3   R   (    (    s/   lib/python2.7/site-packages/tornado/template.pyRl     s    N(   R@   Ra   Rb   R8   R   Rk   Rl   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyR     s   	t   _Nodec           B` s#   e  Z d    Z d   Z d   Z RS(   c         C` s   d S(   N(    (    (   R3   (    (    s/   lib/python2.7/site-packages/tornado/template.pyt
   each_child  s    c         C` s   t     d  S(   N(   Ri   (   R3   RX   (    (    s/   lib/python2.7/site-packages/tornado/template.pyRL     s    c         C` s+   x$ |  j    D] } | j | |  q Wd  S(   N(   R   RO   (   R3   R*   RU   t   child(    (    s/   lib/python2.7/site-packages/tornado/template.pyRO     s    (   R@   Ra   R   RL   RO   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyR     s   		R%   c           B` s#   e  Z d    Z d   Z d   Z RS(   c         C` s   | |  _  | |  _ d |  _ d  S(   Ni    (   RQ   RY   t   line(   R3   RQ   RY   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR8     s    		c         C` ss   | j  d |  j  | j   N | j  d |  j  | j  d |  j  |  j j |  | j  d |  j  Wd  QXd  S(   Ns   def _tt_execute():s   _tt_buffer = []s   _tt_append = _tt_buffer.appends$   return _tt_utf8('').join(_tt_buffer)(   t
   write_lineR   t   indentRY   RL   (   R3   RX   (    (    s/   lib/python2.7/site-packages/tornado/template.pyRL     s    c         C` s
   |  j  f S(   N(   RY   (   R3   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR     s    (   R@   Ra   R8   RL   R   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyR%     s   		t
   _ChunkListc           B` s#   e  Z d    Z d   Z d   Z RS(   c         C` s   | |  _  d  S(   N(   RZ   (   R3   RZ   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR8     s    c         C` s%   x |  j  D] } | j |  q
 Wd  S(   N(   RZ   RL   (   R3   RX   R`   (    (    s/   lib/python2.7/site-packages/tornado/template.pyRL     s    c         C` s   |  j  S(   N(   RZ   (   R3   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR     s    (   R@   Ra   R8   RL   R   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyR     s   		t   _NamedBlockc           B` s,   e  Z d    Z d   Z d   Z d   Z RS(   c         C` s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   R   RY   RQ   R   (   R3   R   RY   RQ   R   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR8     s    			c         C` s
   |  j  f S(   N(   RY   (   R3   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR     s    c         C` sC   | j  |  j } | j | j |  j   | j j |  Wd  QXd  S(   N(   RU   R   t   includeRQ   R   RY   RL   (   R3   RX   t   block(    (    s/   lib/python2.7/site-packages/tornado/template.pyRL     s    c         C` s$   |  | |  j  <t j |  | |  d  S(   N(   R   R   RO   (   R3   R*   RU   (    (    s/   lib/python2.7/site-packages/tornado/template.pyRO     s    (   R@   Ra   R8   R   RL   RO   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyR     s   			R\   c           B` s   e  Z d    Z RS(   c         C` s   | |  _  d  S(   N(   R   (   R3   R   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR8     s    (   R@   Ra   R8   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyR\     s   t   _IncludeBlockc           B` s#   e  Z d    Z d   Z d   Z RS(   c         C` s"   | |  _  | j  |  _ | |  _ d  S(   N(   R   t   template_nameR   (   R3   R   R6   R   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR8     s    	c         C` s/   | j  |  j |  j  } | j j | |  d  S(   N(   R^   R   R   R'   RO   (   R3   R*   RU   t   included(    (    s/   lib/python2.7/site-packages/tornado/template.pyRO   $  s    c         C` sN   | j  j |  j |  j  } | j | |  j   | j j j |  Wd  QXd  S(   N(	   R*   R^   R   R   R   R   R'   RY   RL   (   R3   RX   R   (    (    s/   lib/python2.7/site-packages/tornado/template.pyRL   (  s    (   R@   Ra   R8   RO   RL   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyR     s   		t   _ApplyBlockc           B` s&   e  Z d d   Z d   Z d   Z RS(   c         C` s   | |  _  | |  _ | |  _ d  S(   N(   t   methodR   RY   (   R3   R   R   RY   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR8   /  s    		c         C` s
   |  j  f S(   N(   RY   (   R3   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR   4  s    c         C` s   d | j  } | j  d 7_  | j d | |  j  | j   N | j d |  j  | j d |  j  |  j j |  | j d |  j  Wd  QX| j d |  j | f |  j  d  S(   Ns   _tt_apply%di   s	   def %s():s   _tt_buffer = []s   _tt_append = _tt_buffer.appends$   return _tt_utf8('').join(_tt_buffer)s   _tt_append(_tt_utf8(%s(%s())))(   t   apply_counterR   R   R   RY   RL   R   (   R3   RX   t   method_name(    (    s/   lib/python2.7/site-packages/tornado/template.pyRL   7  s    	N(   R@   Ra   R   R8   R   RL   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyR   .  s   	t   _ControlBlockc           B` s&   e  Z d d   Z d   Z d   Z RS(   c         C` s   | |  _  | |  _ | |  _ d  S(   N(   t	   statementR   RY   (   R3   R   R   RY   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR8   E  s    		c         C` s
   |  j  f S(   N(   RY   (   R3   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR   J  s    c         C` sT   | j  d |  j |  j  | j   ( |  j j |  | j  d |  j  Wd  QXd  S(   Ns   %s:t   pass(   R   R   R   R   RY   RL   (   R3   RX   (    (    s/   lib/python2.7/site-packages/tornado/template.pyRL   M  s    N(   R@   Ra   R   R8   R   RL   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyR   D  s   	t   _IntermediateControlBlockc           B` s   e  Z d    Z d   Z RS(   c         C` s   | |  _  | |  _ d  S(   N(   R   R   (   R3   R   R   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR8   V  s    	c         C` s>   | j  d |  j  | j  d |  j |  j | j   d  d  S(   NR   s   %s:i   (   R   R   R   t   indent_size(   R3   RX   (    (    s/   lib/python2.7/site-packages/tornado/template.pyRL   Z  s    (   R@   Ra   R8   RL   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyR   U  s   	t
   _Statementc           B` s   e  Z d    Z d   Z RS(   c         C` s   | |  _  | |  _ d  S(   N(   R   R   (   R3   R   R   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR8   a  s    	c         C` s   | j  |  j |  j  d  S(   N(   R   R   R   (   R3   RX   (    (    s/   lib/python2.7/site-packages/tornado/template.pyRL   e  s    (   R@   Ra   R8   RL   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyR   `  s   	t   _Expressionc           B` s   e  Z e d   Z d   Z RS(   c         C` s   | |  _  | |  _ | |  _ d  S(   N(   t
   expressionR   t   raw(   R3   R   R   R   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR8   j  s    		c         C` s   | j  d |  j |  j  | j  d |  j  | j  d |  j  |  j r| | j j d  k	 r| | j  d | j j |  j  n  | j  d |  j  d  S(   Ns   _tt_tmp = %ssE   if isinstance(_tt_tmp, _tt_string_types): _tt_tmp = _tt_utf8(_tt_tmp)s&   else: _tt_tmp = _tt_utf8(str(_tt_tmp))s   _tt_tmp = _tt_utf8(%s(_tt_tmp))s   _tt_append(_tt_tmp)(   R   R   R   R   t   current_templateR!   R   (   R3   RX   (    (    s/   lib/python2.7/site-packages/tornado/template.pyRL   o  s    	
	(   R@   Ra   t   FalseR8   RL   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyR   i  s   t   _Modulec           B` s   e  Z d    Z RS(   c         C` s'   t  t |   j d | | d t d  S(   Ns   _tt_modules.R   (   Rn   R   R8   R.   (   R3   R   R   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR8   }  s    (   R@   Ra   R8   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyR   |  s   t   _Textc           B` s   e  Z d    Z d   Z RS(   c         C` s   | |  _  | |  _ | |  _ d  S(   N(   t   valueR   R   (   R3   R   R   R   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR8     s    		c         C` sW   |  j  } d | k r* t |  j |  } n  | rS | j d t j |  |  j  n  d  S(   Ns   <pre>s   _tt_append(%r)(   R   R   R   R   R   RE   R   (   R3   RX   R   (    (    s/   lib/python2.7/site-packages/tornado/template.pyRL     s
    	(   R@   Ra   R8   RL   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyR     s   	R]   c           B` s&   e  Z d  Z d d d  Z d   Z RS(   s   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.
    i    c         C` s   | |  _  | |  _ | |  _ d  S(   N(   t   messaget   filenamet   lineno(   R3   R   R   R   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR8     s    		c         C` s   d |  j  |  j |  j f S(   Ns   %s at %s:%d(   R   R   R   (   R3   (    (    s/   lib/python2.7/site-packages/tornado/template.pyt   __str__  s    N(   R@   Ra   Rb   R   R8   R   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyR]     s   RP   c           B` s8   e  Z d    Z d   Z d   Z d   Z d d  Z RS(   c         C` sC   | |  _  | |  _ | |  _ | |  _ d |  _ g  |  _ d |  _ d  S(   Ni    (   R'   RU   R*   R   R   t   include_stackt   _indent(   R3   R'   RU   R*   R   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR8     s    						c         C` s   |  j  S(   N(   R   (   R3   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR     s    c         ` s#   d t  f   f d     Y} |   S(   Nt   Indenterc           ` s&   e  Z   f d    Z   f d   Z RS(   c         ` s     j  d 7_    S(   Ni   (   R   (   R   (   R3   (    s/   lib/python2.7/site-packages/tornado/template.pyt	   __enter__  s    c         ` s(     j  d k s t    j  d 8_  d  S(   Ni    i   (   R   t   AssertionError(   R   t   args(   R3   (    s/   lib/python2.7/site-packages/tornado/template.pyt   __exit__  s    (   R@   Ra   R   R   (    (   R3   (    s/   lib/python2.7/site-packages/tornado/template.pyR     s   (   t   object(   R3   R   (    (   R3   s/   lib/python2.7/site-packages/tornado/template.pyR     s    	c         ` sE     j  j   j | f  |   _ d t f   f d     Y} |   S(   Nt   IncludeTemplatec           ` s&   e  Z   f d    Z   f d   Z RS(   c         ` s     S(   N(    (   R   (   R3   (    s/   lib/python2.7/site-packages/tornado/template.pyR     s    c         ` s     j  j   d   _ d  S(   Ni    (   R   t   popR   (   R   R   (   R3   (    s/   lib/python2.7/site-packages/tornado/template.pyR     s    (   R@   Ra   R   R   (    (   R3   (    s/   lib/python2.7/site-packages/tornado/template.pyR     s   (   R   t   appendR   R   (   R3   RQ   R   R   (    (   R3   s/   lib/python2.7/site-packages/tornado/template.pyR     s    	c         C` s   | d  k r |  j } n  d |  j j | f } |  j r g  |  j D] \ } } d | j | f ^ qA } | d d j t |   7} n  t d | | | d |  j d  S(   Ns	     # %s:%ds   %s:%ds	    (via %s)s   , s       R'   (	   R   R   R   R   R   Rw   t   reversedt   printR'   (   R3   R   t   line_numberR   t   line_commentt   tmplR   RV   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR     s    	, N(   R@   Ra   R8   R   R   R   R   R   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyRP     s
   					R$   c           B` sY   e  Z d    Z d d	 d  Z d	 d  Z d   Z d   Z d   Z d   Z	 d   Z
 RS(
   c         C` s1   | |  _  | |  _ | |  _ d |  _ d |  _ d  S(   Ni   i    (   R   R   R   R   t   pos(   R3   R   R   R   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR8     s
    				i    c         C` s   | d k s t  |   |  j } | | 7} | d  k rO |  j j | |  } n4 | | 7} | | k sk t   |  j j | | |  } | d k r | | 8} n  | S(   Ni    i(   R   R   R   R   t   find(   R3   t   needlet   startt   endR   t   index(    (    s/   lib/python2.7/site-packages/tornado/template.pyR     s    	

c         C` sv   | d  k r% t |  j  |  j } n  |  j | } |  j |  j j d |  j |  7_ |  j |  j | !} | |  _ | S(   Ns   
(   R   Ry   R   R   R   t   count(   R3   R   t   newpost   s(    (    s/   lib/python2.7/site-packages/tornado/template.pyt   consume  s    $	c         C` s   t  |  j  |  j S(   N(   Ry   R   R   (   R3   (    (    s/   lib/python2.7/site-packages/tornado/template.pyt	   remaining  s    c         C` s
   |  j    S(   N(   R   (   R3   (    (    s/   lib/python2.7/site-packages/tornado/template.pyt   __len__  s    c         C` s   t  |  t k r t |   } | j |  \ } } } | d  k rN |  j } n | |  j 7} | d  k	 rw | |  j 7} n  |  j t | | |  S| d k  r |  j | S|  j |  j | Sd  S(   Ni    (   t   typet   sliceRy   t   indicesR   R   R   (   R3   t   keyt   sizeR   t   stopt   step(    (    s/   lib/python2.7/site-packages/tornado/template.pyt   __getitem__  s    c         C` s   |  j  |  j S(   N(   R   R   (   R3   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR     s    c         C` s   t  | |  j |  j   d  S(   N(   R]   R   R   (   R3   t   msg(    (    s/   lib/python2.7/site-packages/tornado/template.pyt   raise_parse_error  s    N(   R@   Ra   R8   R   R   R   R   R   R   R   R   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyR$     s   						c         C` sf   |  j    } d t t t |  d   } d j g  t |  D]  \ } } | | d | f ^ q?  S(   Ns   %%%dd  %%s
i   R   (   t
   splitlinesRy   t   reprRw   t	   enumerate(   R)   t   linest   formatt   iR   (    (    s/   lib/python2.7/site-packages/tornado/template.pyR0     s     c         C` s  t  g   } xt rd } x t r|  j d |  } | d k sX | d |  j   k r | rr |  j d |  n  | j j t |  j   |  j	 |  j
   | S|  | d d; k r | d 7} q n  | d |  j   k  r|  | d d k r|  | d d k r| d 7} q n  Pq W| d k rU|  j |  } | j j t | |  j	 |  j
   n  |  j d  } |  j	 } |  j   r|  d d	 k r|  j d  | j j t | | |  j
   q n  | d
 k r|  j d  }	 |	 d k r|  j d  n  |  j |	  j   }
 |  j d  q n  | d k r|  j d  }	 |	 d k rQ|  j d  n  |  j |	  j   }
 |  j d  |
 s|  j d  n  | j j t |
 |   q n  | d k st |   |  j d  }	 |	 d k r|  j d  n  |  j |	  j   }
 |  j d  |
 s#|  j d  n  |
 j d  \ } } } | j   } i t d d d d g  d 6t d g  d 6t d g  d 6t d g  d 6} | j |  } | d  k	 r| s|  j d | | f  n  | | k r|  j d | | f  n  | j j t |
 |   q q | d  k r<| s8|  j d!  n  | S| d< k r"| d' k rZq n  | d" k r| j d,  j d-  } | s|  j d.  n  t |  } ni| d= k r| s|  j d/  n  t |
 |  } n5| d# k r&| j d,  j d-  } | s|  j d0  n  t | |  |  } n | d$ k rZ| sH|  j d1  n  t | |  } n | d( k r| j   } | d2 k rd  } n  | | _ q nv | d) k r| j   } t | d3  | |  _
 q nB | d* k rt | | d* t } n | d+ k rt | |  } n  | j j |  q q | d> k r$| d? k rRt |  | | |  } n9 | d4 k rvt |  | | d   } n t |  | | |  } | d4 k r| s|  j d6  n  t | | |  } nL | d5 k r| s|  j d7  n  t | | | |  } n t |
 | |  } | j j |  q q | d@ k r{| s\|  j d | t d d g  f  n  | j j t |
 |   q q |  j d: |  q Wd  S(A   Ni    t   {ii   s    Missing {%% end %%} block for %st   %t   #i   t   !s   {#s   #}s   Missing end comment #}s   {{s   }}s   Missing end expression }}s   Empty expressions   {%s   %}s   Missing end block %}s   Empty block tag ({% %})R   t   ift   fort   whilet   tryt   elset   elift   exceptt   finallys   %s outside %s blocks'   %s block cannot be attached to %s blockR   s   Extra {% end %} blockt   extendsR   t   sett   importt   fromt   commentR!   R   R   t   modulet   "t   's   extends missing file paths   import missing statements   include missing file paths   set missing statementR   R   t   applyR   s   apply missing method names   block missing namet   breakt   continues   unknown operator: %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   RZ   R   R   R   R   R   t   stripR   R   t	   partitionR   t   getR   R   R\   R   R   R!   R   R   R&   R   R   R   (   R6   RQ   t   in_blockt   in_loopRY   t   curlyt   const   start_braceR   R   t   contentst   operatort   spacet   suffixt   intermediate_blockst   allowed_parentsR   t   fnR   t
   block_body(    (    s/   lib/python2.7/site-packages/tornado/template.pyR&     s   		"
(
		 					(4   Rb   t
   __future__R    R   R   R=   RH   t   os.pathRo   R   R   Re   t   tornadoR   t   tornado.logR   t   tornado.utilR   R   R   R   t   ioR	   t	   cStringIOR"   R   R   R   R   Rc   Rm   R   R   R%   R   R   R\   R   R   R   R   R   R   R   R   R   R]   RP   R$   R0   R   R&   (    (    (    s/   lib/python2.7/site-packages/tornado/template.pyt   <module>   sL   "		8	1<	