3
՛gYm                 @   s@  d Z ddlmZmZmZ ddlmZ ddlZG dd deZ	G dd de
ZG d	d
 d
eZe ZdZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd5d&d'Zd6d)d*Zd+d, Zd-d. Zd/d0 Z d7d1d2Z!d3d4 Z"dS )8zdprovides runtime services for templates, including Context,
Namespace, and various helper functions.    )
exceptionsutilcompat)compat_builtinsNc               @   s   e Zd ZdZdd Zdd Zedd Zedd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zd'ddZdd Zdd  Zd!d" Zd#d$ Zd%d& ZdS )(ContextzProvides runtime namespace, output buffer, and various
    callstacks for templates.

    See :ref:`runtime_toplevel` for detail on the usage of
    :class:`.Context`.

     c             K   sR   |g| _ || _|j | _d | _d | _i | _tjt	| | jd< t
  | _| jd< d S )Ncapturecaller)_buffer_stack_datacopy_kwargs_with_template_outputting_as_unicode
namespacesr   partialr   CallerStackcaller_stack)selfbufferdata r   ./usr/lib/python3/dist-packages/mako/runtime.py__init__   s    
zContext.__init__c             C   s0   || _ |jj| j}|r,tjddj| d S )Nz%Reserved words passed to render(): %sz, )r   Zreserved_namesintersectionr
   r   ZNameConflictErrorjoin)r   tZillegal_namesr   r   r   _set_with_template*   s    zContext._set_with_templatec             C   s   | j jS )z]Return the :class:`.TemplateLookup` associated
        with this :class:`.Context`.

        )r   lookup)r   r   r   r   r   2   s    zContext.lookupc             C   s
   | j j S )a]  Return the dictionary of top level keyword arguments associated
        with this :class:`.Context`.

        This dictionary only includes the top-level arguments passed to
        :meth:`.Template.render`.  It does not include names produced within
        the template execution such as local variable names or special names
        such as ``self``, ``next``, etc.

        The purpose of this dictionary is primarily for the case that
        a :class:`.Template` accepts arguments via its ``<%page>`` tag,
        which are normally expected to be passed via :meth:`.Template.render`,
        except the template is being called in an inheritance context,
        using the ``body()`` method.   :attr:`.Context.kwargs` can then be
        used to propagate these arguments to the inheriting template::

            ${next.body(**context.kwargs)}

        )r   r   )r   r   r   r   kwargs:   s    zContext.kwargsc             C   s   | j j| dS )zQPush a ``caller`` callable onto the callstack for
        this :class:`.Context`.N)r   append)r   r   r   r   r   push_callerP   s    zContext.push_callerc             C   s   | j d= dS )zPPop a ``caller`` callable onto the callstack for this
        :class:`.Context`.   N)r   )r   r   r   r   
pop_callerV   s    zContext.pop_callerc             C   s   t | jj S )zAReturn a list of all names established in this :class:`.Context`.)listr
   keys)r   r   r   r   r%   \   s    zContext.keysc             C   s"   || j kr| j | S tj| S d S )N)r
   r   __dict__)r   keyr   r   r   __getitem__a   s    

zContext.__getitem__c             C   s   t j }| jj| |jS )zUpush a capturing buffer onto this Context and return
        the new writer function.)r   FastEncodingBufferr	   r   write)r   bufr   r   r   _push_writerg   s    zContext._push_writerc             C   s   | j j }|| j d jfS )zupop the most recent capturing buffer from this Context
        and return the current writer after the pop.

        r!   r"   )r	   popr*   )r   r+   r   r   r   _pop_buffer_and_writero   s    
zContext._pop_buffer_and_writerc             C   s   | j   dS )z*push a capturing buffer onto this Context.N)r,   )r   r   r   r   _push_bufferx   s    zContext._push_bufferc             C   s
   | j j S )z7pop the most recent capturing buffer from this Context.)r	   r-   )r   r   r   r   _pop_buffer}   s    zContext._pop_bufferNc             C   s   | j j|tjj||S )z+Return a value from this :class:`.Context`.)r
   getr   r&   )r   r'   defaultr   r   r   r1      s    zContext.getc             C   s   | j d j| dS )zSWrite a string to this :class:`.Context` object's
        underlying output buffer.r!   Nr"   )r	   r*   )r   stringr   r   r   r*      s    zContext.writec             C   s   | j d jS )z#Return the current writer function.r!   r"   )r	   r*   )r   r   r   r   writer   s    zContext.writerc             C   sJ   t jt }| j|_| jj |_| j|_| j|_| j|_| j|_| j	|_	|S )N)
r   __new__r	   r
   r   r   r   r   r   r   )r   cr   r   r   _copy   s    
zContext._copyc             C   s    |s| S | j  }|jj| |S )zCreate a new :class:`.Context` with a copy of this
        :class:`.Context`'s current state,
        updated with the given dictionary.

        The :attr:`.Context.kwargs` collection remains
        unaffected.


        )r7   r
   update)r   dr6   r   r   r   _locals   s
    zContext._localsc             C   s6   | j  }|j}|jdd |jdd |jdd |S )zfcreate a new copy of this :class:`.Context`. with
        tokens related to inheritance state removed.r   Nparentnext)r7   r
   r-   )r   r6   xr   r   r   _clean_inheritance_tokens   s    z!Context._clean_inheritance_tokens)N)__name__
__module____qualname____doc__r   r   propertyr   r   r    r#   r%   r(   r,   r.   r/   r0   r1   r*   r4   r7   r:   r>   r   r   r   r   r      s&   	
r   c               @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )r   c             C   s
   d | _ d S )N)
nextcaller)r   r   r   r   r      s    zCallerStack.__init__c             C   s   | j  S )N)__bool__)r   r   r   r   __nonzero__   s    zCallerStack.__nonzero__c             C   s   t | r| j rdpdS )NTF)len_get_caller)r   r   r   r   rE      s    zCallerStack.__bool__c             C   s   | d S )Nr!   r"   r   )r   r   r   r   rH      s    zCallerStack._get_callerc             C   s   t | j |S )N)getattrrH   )r   r'   r   r   r   __getattr__   s    zCallerStack.__getattr__c             C   s   | j pd }| j| d | _ |S )N)rD   r   )r   framer   r   r   _push_frame   s    

zCallerStack._push_framec             C   s   | j  | _d S )N)r-   rD   )r   r   r   r   
_pop_frame   s    zCallerStack._pop_frameN)
r?   r@   rA   r   rF   rE   rH   rJ   rL   rM   r   r   r   r   r      s   r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )		UndefinedzRepresents an undefined value in a template.

    All template modules have a constant value
    ``UNDEFINED`` present which is an instance of this
    object.

    c             C   s   t dd S )NrN   )	NameError)r   r   r   r   __str__   s    zUndefined.__str__c             C   s   | j  S )N)rE   )r   r   r   r   rF      s    zUndefined.__nonzero__c             C   s   dS )NFr   )r   r   r   r   rE      s    zUndefined.__bool__N)r?   r@   rA   rB   rP   rF   rE   r   r   r   r   rN      s   rN    c               @   sT   e Zd ZdZdd Zdd Zdd Zedd	 Zd
d Z	dd Z
dd Zdd ZdS )	LoopStackza stack for LoopContexts that implements the context manager protocol
    to automatically pop off the top of the stack on context exit
    c             C   s
   g | _ d S )N)stack)r   r   r   r   r      s    zLoopStack.__init__c             C   s   | j | | jS )N)_push_top)r   iterabler   r   r   _enter   s    
zLoopStack._enterc             C   s   | j   | jS )N)_poprU   )r   r   r   r   _exit   s    zLoopStack._exitc             C   s   | j r| j d S | S d S )Nr!   r"   )rS   )r   r   r   r   rU      s    
zLoopStack._topc             C   s
   | j j S )N)rS   r-   )r   r   r   r   rX     s    zLoopStack._popc             C   s&   t |}| jr| jd |_| jj|S )Nr!   r"   )LoopContextrS   r;   r   )r   rV   newr   r   r   rT   	  s    zLoopStack._pushc             C   s   t jdd S )NzNo loop context is established)r   RuntimeException)r   r'   r   r   r   rJ     s    zLoopStack.__getattr__c             C   s
   t | jS )N)iterrU   )r   r   r   r   __iter__  s    zLoopStack.__iter__N)r?   r@   rA   rB   r   rW   rY   rC   rU   rX   rT   rJ   r^   r   r   r   r   rR      s   rR   c               @   sr   e Zd ZdZdd Zdd Zejdd Ze	dd	 Z
e	d
d Ze	dd Ze	dd Ze	dd Zdd ZdS )rZ   a  A magic loop variable.
    Automatically accessible in any ``% for`` block.

    See the section :ref:`loop_context` for usage
    notes.

    :attr:`parent` -> :class:`.LoopContext` or ``None``
        The parent loop, if one exists.
    :attr:`index` -> `int`
        The 0-based iteration count.
    :attr:`reverse_index` -> `int`
        The number of iterations remaining.
    :attr:`first` -> `bool`
        ``True`` on the first iteration, ``False`` otherwise.
    :attr:`last` -> `bool`
        ``True`` on the last iteration, ``False`` otherwise.
    :attr:`even` -> `bool`
        ``True`` when ``index`` is even.
    :attr:`odd` -> `bool`
        ``True`` when ``index`` is odd.
    c             C   s   || _ d| _d | _d S )Nr   )	_iterableindexr;   )r   rV   r   r   r   r   .  s    zLoopContext.__init__c             c   s(   x"| j D ]}|V  |  jd7  _qW d S )Nr!   )r_   r`   )r   ir   r   r   r^   3  s    zLoopContext.__iter__c             C   s
   t | jS )N)rG   r_   )r   r   r   r   __len__8  s    zLoopContext.__len__c             C   s   t | | j d S )Nr!   )rG   r`   )r   r   r   r   reverse_index<  s    zLoopContext.reverse_indexc             C   s
   | j dkS )Nr   )r`   )r   r   r   r   first@  s    zLoopContext.firstc             C   s   | j t| d kS )Nr!   )r`   rG   )r   r   r   r   lastD  s    zLoopContext.lastc             C   s   | j  S )N)odd)r   r   r   r   evenH  s    zLoopContext.evenc             C   s   t | jd S )N   )boolr`   )r   r   r   r   rf   L  s    zLoopContext.oddc             G   s   |st d|| jt|  S )z5Cycle through values as the loop progresses.
        z(You must provide values to cycle through)
ValueErrorr`   rG   )r   valuesr   r   r   cycleP  s    zLoopContext.cycleN)r?   r@   rA   rB   r   r^   r   Zmemoized_instancemethodrb   rC   rc   rd   re   rg   rf   rl   r   r   r   r   rZ     s   rZ   c               @   s   e Zd Zdd Zdd ZdS )_NSAttrc             C   s
   || _ d S )N)_NSAttr__parent)r   r;   r   r   r   r   Z  s    z_NSAttr.__init__c             C   s:   | j }x&|r,t|j|r$t|j|S |j}qW t|d S )N)rn   hasattrmodulerI   inheritsAttributeError)r   r'   nsr   r   r   rJ   ]  s    
z_NSAttr.__getattr__N)r?   r@   rA   r   rJ   r   r   r   r   rm   X  s   rm   c               @   s   e Zd ZdZdddZf ZdZdZdZdZ	dZ
dZejdd Zdd	 Zd
d Zdd Zedd Zdd Zdd Zdd Zdd ZdS )	Namespacea  Provides access to collections of rendering methods, which
      can be local, from other templates, or from imported modules.

      To access a particular rendering method referenced by a
      :class:`.Namespace`, use plain attribute access:

      .. sourcecode:: mako

        ${some_namespace.foo(x, y, z)}

      :class:`.Namespace` also contains several built-in attributes
      described here.

      NTc             C   s2   || _ || _|| _|d k	r.tdd |D | _d S )Nc             S   s   g | ]}|j |fqS r   )r?   ).0r6   r   r   r   
<listcomp>  s    z&Namespace.__init__.<locals>.<listcomp>)namecontextrq   dict	callables)r   rw   rx   rz   rq   populate_selfcalling_urir   r   r   r   x  s
    zNamespace.__init__c             C   s   t | S )a-  Access module level attributes by name.

        This accessor allows templates to supply "scalar"
        attributes which are particularly handy in inheritance
        relationships.

        .. seealso::

            :ref:`inheritance_attr`

            :ref:`namespace_attr_for_includes`

        )rm   )r   r   r   r   attr  s    zNamespace.attrc             C   sL   | |f}|| j jkr | j j| S t|| j j || jd}|| j j|< |S dS )a]  Return a :class:`.Namespace` corresponding to the given ``uri``.

        If the given ``uri`` is a relative URI (i.e. it does not
        contain a leading slash ``/``), the ``uri`` is adjusted to
        be relative to the ``uri`` of the namespace itself. This
        method is therefore mostly useful off of the built-in
        ``local`` namespace, described in :ref:`namespace_local`.

        In
        most cases, a template wouldn't need this function, and
        should instead use the ``<%namespace>`` tag to load
        namespaces. However, since all ``<%namespace>`` tags are
        evaluated before the body of a template ever runs,
        this method can be used to locate namespaces using
        expressions that were generated within the body code of
        the template, or to conditionally use a particular
        namespace.

        )templateurir|   N)rx   r   TemplateNamespacer7   _templateuri)r   urir'   rs   r   r   r   get_namespace  s    
zNamespace.get_namespacec             C   s   t | j|| jS )zReturn a :class:`.Template` from the given ``uri``.

        The ``uri`` resolution is relative to the ``uri`` of this
        :class:`.Namespace` object's :class:`.Template`.

        )_lookup_templaterx   r   )r   r   r   r   r   get_template  s    zNamespace.get_templatec             K   s   | j j|f|S )a  Return a value from the :class:`.Cache` referenced by this
        :class:`.Namespace` object's :class:`.Template`.

        The advantage to this method versus direct access to the
        :class:`.Cache` is that the configuration parameters
        declared in ``<%page>`` take effect here, thereby calling
        up the same configured backend as that configured
        by ``<%page>``.

        )cacher1   )r   r'   r   r   r   r   
get_cached  s    zNamespace.get_cachedc             C   s   | j jS )zReturn the :class:`.Cache` object referenced
        by this :class:`.Namespace` object's
        :class:`.Template`.

        )templater   )r   r   r   r   r     s    zNamespace.cachec             K   s   t | j|| jf| dS )z$Include a file at the given ``uri``.N)_include_filerx   r   )r   r   r   r   r   r   include_file  s    zNamespace.include_filec             C   sH   xB|D ]:}|dkr2x,| j  D ]\}}|||< qW qt| |||< qW d S )N*)	_get_starrI   )r   r9   lZidentkvr   r   r   	_populate  s
    
zNamespace._populatec             c   s*   | j r&x| j D ]}|| j | fV  qW d S )N)rz   )r   r'   r   r   r   r     s    zNamespace._get_starc             C   sL   || j kr| j | }n&| jr*t| j|}ntd| j|f t| || |S )Nz!Namespace '%s' has no member '%s')rz   rq   rI   rr   rw   setattr)r   r'   valr   r   r   rJ     s    
zNamespace.__getattr__)NNTN)r?   r@   rA   rB   r   rz   rp   r   rx   filenamer   r   r   Zmemoized_propertyr}   r   r   r   rC   r   r   r   r   rJ   r   r   r   r   rt   g  s&    
				rt   c               @   sN   e Zd ZdZdddZedd Zedd	 Zed
d Zdd Z	dd Z
dS )r   z@A :class:`.Namespace` specific to a :class:`.Template` instance.NTc	             C   s   || _ || _|| _|d k	r.tdd |D | _|d k	rRt|||| _| jjj| _	n"|d k	rl|| _|jj| _	nt
d|rt|| j| d\}	}
d S )Nc             S   s   g | ]}|j |fqS r   )r?   )ru   r6   r   r   r   rv   ,  s    z.TemplateNamespace.__init__.<locals>.<listcomp>z 'template' argument is required.)self_ns)rw   rx   rq   ry   rz   r   r   rp   Z_template_urir   	TypeError_populate_self_namespace)r   rw   rx   r   r~   rz   rq   r{   r|   Zlclcallable
lclcontextr   r   r   r   %  s     zTemplateNamespace.__init__c             C   s   | j jS )zThe Python module referenced by this :class:`.Namespace`.

        If the namespace references a :class:`.Template`, then
        this module is the equivalent of ``template.module``,
        i.e. the generated module for the template.

        )r   rp   )r   r   r   r   rp   =  s    	zTemplateNamespace.modulec             C   s   | j jS )zhThe path of the filesystem file used for this
        :class:`.Namespace`'s module or template.
        )r   r   )r   r   r   r   r   H  s    zTemplateNamespace.filenamec             C   s   | j jS )zThe URI for this :class:`.Namespace`'s template.

        I.e. whatever was sent to :meth:`.TemplateLookup.get_template()`.

        This is the equivalent of :attr:`.Template.uri`.

        )r   r   )r   r   r   r   r   O  s    	zTemplateNamespace.uric             #   sX    j r&x j D ]}| j | fV  qW  fdd}x  jjjD ]}|||fV  q>W d S )Nc                s    j j| }tj| jS )N)r   _get_def_callabler   r   rx   )r'   	callable_)r   r   r   r1   _  s    z(TemplateNamespace._get_star.<locals>.get)rz   r   rp   Z_exports)r   r'   r1   r   r   )r   r   r   Z  s    zTemplateNamespace._get_starc             C   st   || j kr| j | }nN| jj|r>| jj|}tj|| j}n&| jrRt| j|}nt	d| j
|f t| || |S )Nz!Namespace '%s' has no member '%s')rz   r   Zhas_defr   r   r   rx   rq   rI   rr   rw   r   )r   r'   r   r   r   r   r   rJ   e  s    
zTemplateNamespace.__getattr__)NNNNTN)r?   r@   rA   rB   r   rC   rp   r   r   r   rJ   r   r   r   r   r   !  s     
r   c               @   s6   e Zd ZdZdddZedd Zdd	 Zd
d ZdS )ModuleNamespacez;A :class:`.Namespace` specific to a Python module instance.NTc       
      C   sf   || _ || _|| _|d k	r.tdd |D | _t|}x$|jddd  D ]}	t||	}qJW || _d S )Nc             S   s   g | ]}|j |fqS r   )r?   )ru   r6   r   r   r   rv     s    z,ModuleNamespace.__init__.<locals>.<listcomp>.r!   )	rw   rx   rq   ry   rz   
__import__splitrI   rp   )
r   rw   rx   rp   rz   rq   r{   r|   modtokenr   r   r   r   z  s    zModuleNamespace.__init__c             C   s   | j jS )zhThe path of the filesystem file used for this
        :class:`.Namespace`'s module or template.
        )rp   __file__)r   r   r   r   r     s    zModuleNamespace.filenamec             c   st   | j r&x| j D ]}|| j | fV  qW xHt| jD ]:}|d dkr2t| j|}tj|r2|tj|| jfV  q2W d S )Nr   _)rz   dirrp   rI   r   callabler   rx   )r   r'   r   r   r   r   r     s    
zModuleNamespace._get_starc             C   st   || j kr| j | }nNt| j|r>t| j|}tj|| j}n&| jrRt| j|}ntd| j	|f t
| || |S )Nz!Namespace '%s' has no member '%s')rz   ro   rp   rI   r   r   rx   rq   rr   rw   r   )r   r'   r   r   r   r   r   rJ     s    
zModuleNamespace.__getattr__)NNTN)	r?   r@   rA   rB   r   rC   r   r   rJ   r   r   r   r   r   v  s    

r   c                s    fdd}|S )zApply a caller_stack compatibility decorator to a plain
    Python function.

    See the example in :ref:`namespaces_python_modules`.

    c          
      s,   | j j  z | f||S | j j  X d S )N)r   rL   rM   )rx   argsr   )funcr   r   wrap_stackframe  s    
z(supports_caller.<locals>.wrap_stackframer   )r   r   r   )r   r   supports_caller  s    r   c          
   O   s>   t j|stjd| j  z||| W d| j }X |j S )zExecute the given template def, capturing the output into
    a buffer.

    See the example in :ref:`namespaces_python_modules`.

    z[capture() function expects a callable as its argument (i.e. capture(func, *args, **kwargs))N)r   r   r   r\   r/   r0   getvalue)rx   r   r   r   r+   r   r   r   r     s    

r   c                s    fdd}|S )Nc                s    fdd}|S )Nc                sL    fdd}yj dd  |_ W n tk
r6   Y nX | f||S )Nc                 s    f| |S )Nr   )r   kw)rx   	render_fnr   r   y  s    zB_decorate_toplevel.<locals>.decorate_render.<locals>.go.<locals>.y   )r?   r   )rx   r   r   r   )fnr   )rx   r   go  s    z7_decorate_toplevel.<locals>.decorate_render.<locals>.gor   )r   r   )r   )r   r   decorate_render  s    	z+_decorate_toplevel.<locals>.decorate_renderr   )r   r   r   )r   r   _decorate_toplevel  s    r   c                s    fdd}|S )Nc                s   |   fdd}|S )Nc                 s    f| |S )Nr   )r   r   )rx   decr   r   r     s    z5_decorate_inline.<locals>.decorate_render.<locals>.gor   )r   r   )rx   r   )r   r   r     s    z)_decorate_inline.<locals>.decorate_renderr   )rx   r   r   r   )rx   r   r   _decorate_inline  s    r   c             K   s   t | ||}t| j |\}}t|| jf|}|jr~y||f| W q tk
rz   |j|tj }|svtj	t
j   Y qX n||f| dS )zPlocate the template from the given uri and include it in
    the current output.N)r   r   r>   _kwargs_for_includer
   Zinclude_error_handler	Exceptionr   exception_asreraisesysexc_info)rx   r   r|   r   r   r   Zctxresultr   r   r   r     s    
r   c       
      C   s   |dkrdS t | ||}| d }|}x|jdk	r8|j}q&W | jd|i}td|j ||dd|_|j | jd< |jd< t|jd	d}|dk	r|||}|r|S t|jd
d}	|	dk	r|	|  |j|fS )zcalled by the _inherit method in template modules to set
    up the inheritance chain at the start of a template's
    execution.Nr   r<   zself:%sF)r   r{   r;   local_mako_inheritZ_mako_generate_namespaces)	r   rq   r:   r   r   r
   rI   rp   r   )
rx   r   r|   r   r   Zihr   r   retZgen_nsr   r   r   _inherit_from  s,    



r   c             C   sf   | j j}|d kr"tjd| j j |j||}y
|j|S  tjk
r`   tjtt	j
 Y nX d S )Nz.Template '%s' has no TemplateLookup associated)r   r   r   ZTemplateLookupExceptionr   Z
adjust_urir   ZTopLevelLookupExceptionstrr   r   )rx   r   Z
relativetor   r   r   r   r     s    
r   c             C   s^   |d krt d|j | |dd}| | jd< | jd< t|jdrT|jj|| }|rT|S |j| fS )Nzself:%sF)r   r{   r   r   r   )r   r   r
   ro   rp   r   r   )rx   r   r   r   r   r   r   r   '  s    
r   Fc             C   sx   |rt jdd}n$| jr"tj }nt j|| j| jd}t|f|}||_|j	|  t
| ||f|t|| |j j S )z^create a Context and return the string
    output of the given template and template callable.T)
as_unicode)r   encodingerrors)r   r)   Zbytestring_passthroughr   StringIOoutput_encodingencoding_errorsr   r   r   _render_context_kwargs_for_callabler0   r   )r   r   r   r   r   r+   rx   r   r   r   _render4  s    


r   c             C   sn   t j| }|d r|S |d dd |dd D  }i }x0|D ](}|dkr>||kr>||kr>|| ||< q>W |S )Nrh   r   c             S   s   g | ]}|d k	r|qS )Nr   )ru   r   r   r   r   rv   Q  s    z(_kwargs_for_callable.<locals>.<listcomp>r!      rx   )r   inspect_func_args)r   r   argspec	namedargsr   argr   r   r   r   J  s    

r   c             K   s^   t j| }|d dd |dd D  }x0|D ](}|dkr.||kr.||kr.|| ||< q.W |S )Nr   c             S   s   g | ]}|d k	r|qS )Nr   )ru   r   r   r   r   rv   [  s    z'_kwargs_for_include.<locals>.<listcomp>r!   r   rx   )r   r   )r   r   r   r   r   r   r   r   r   r   Y  s    

r   c             O   sZ   dd l j}t| |js6t|| \}}t||||d n t|| j\}}t||||d d S )Nr   )r   r   )Zmako.templater   
isinstanceZDefTemplater   _exec_templater;   )Ztmplr   rx   r   r   r   Zinheritr   r   r   r   r   b  s    
r   c             C   s   |j }|dk	rx|js|jrxy| |f|| W q tk
rR   t||tj  Y q   tj d }t||| Y qX n| |f|| dS )zexecute a rendering callable given the callable, a
    Context, and optional explicit arguments

    the contextual Template will be located if it exists, and
    the error handling options specified on that Template will
    be interpreted here.
    Nr   )	r   Zformat_exceptionserror_handlerr   _render_errorr   r   r   r   )r   rx   r   r   r   er   r   r   r   p  s    r   c             C   s   | j r&| j ||}|stjtj   n\tj }|jrNtj	ddg|j
d d < ntj	|j|jg|j
d d < |j| |j||d d S )NT)r   )error)r   r   r   r   r   r   Zhtml_error_templater   r   r)   r	   r   r   r   Zrender_context)r   rx   r   r   Zerror_templater   r   r   r     s    
r   )N)F)NN)#rB   Zmakor   r   r   Zmako.compatr   r   objectr   r$   r   rN   Z	UNDEFINEDZSTOP_RENDERINGrR   rZ   rm   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s<    ,(B ;U3


	
