B
    5d                 @   s  d dl Zd dlmZ d dlmZ d dlmZ d dlmZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ejrddlmZ ddlmZ ejeejf dddZG dd deZG dd deZdeejeejf edddZejeeejejeef  f ejedddZeejeddd Z deejeejf ej!e dd!d"Z"ejeeejejeef  f ejej!e dd#d$Z#eejej!e dd%d&Z$dS )'    N)
BaseLoader)Environment)Template)TemplateNotFound   )_cv_app)_cv_request)current_app)request)stream_with_context)before_render_template)template_rendered)Flask)Scaffold)returnc              C   sJ   t d} td}i }| dk	r*| j|d< |dk	rF|j|d< |j|d< |S )zSDefault template context processor.  Injects `request`,
    `session` and `g`.
    Ngr
   session)r   getr   r   r
   r   )ZappctxZreqctxrv r   4/tmp/pip-unpacked-wheel-qgtjwo79/flask/templating.py_default_template_ctx_processor   s    




r   c               @   s$   e Zd ZdZdejddddZdS )r   zWorks like a regular Jinja2 environment but has some additional
    knowledge of how Flask's blueprint works so that it can prepend the
    name of the blueprint to referenced templates if necessary.
    r   N)appoptionsr   c             K   s,   d|kr|  |d< tj| f| || _d S )Nloader)Zcreate_global_jinja_loaderBaseEnvironment__init__r   )selfr   r   r   r   r   r   *   s    zEnvironment.__init__)__name__
__module____qualname____doc__tAnyr   r   r   r   r   r   $   s   r   c               @   s   e Zd ZdZdddddZeeejeej	e ej	ej
 f ddd	Zeeejeej	e ej	ej
 f dd
dZeeejeej	e ej	ej
 f dddZeejejdef ddf dddZeje dddZdS )DispatchingJinjaLoaderz\A loader that looks for templates in the application and all
    the blueprint folders.
    r   N)r   r   c             C   s
   || _ d S )N)r   )r   r   r   r   r   r   6   s    zDispatchingJinjaLoader.__init__)environmenttemplater   c             C   s$   | j jd r| ||S | ||S )NZEXPLAIN_TEMPLATE_LOADING)r   config_get_source_explained_get_source_fast)r   r%   r&   r   r   r   
get_source9   s    z!DispatchingJinjaLoader.get_sourcec       	   	   C   s   g }d }x\|  |D ]N\}}y|||}|d kr6|}W n tk
rP   d }Y nX ||||f qW ddlm} || j|| |d k	r|S t|d S )Nr   )!explain_template_loading_attempts)_iter_loadersr*   r   appendZdebughelpersr+   r   )	r   r%   r&   attemptsZtrvZsrcobjr   r   r+   r   r   r   r(   @   s    
z,DispatchingJinjaLoader._get_source_explainedc          	   C   sH   x:|  |D ],\}}y|||S  tk
r6   wY qX qW t|d S )N)r,   r*   r   )r   r%   r&   Z_srcobjr   r   r   r   r)   Z   s    z'DispatchingJinjaLoader._get_source_fastr   )r&   r   c             c   sL   | j j}|d k	r| j |fV  x*| j  D ]}|j}|d k	r(||fV  q(W d S )N)r   jinja_loaderiter_blueprints)r   r&   r   	blueprintr   r   r   r,   d   s    z$DispatchingJinjaLoader._iter_loaders)r   c             C   sj   t  }| jj}|d k	r$||  x<| j D ].}|j}|d k	r0x| D ]}|| qLW q0W t|S )N)setr   r/   updatelist_templatesr0   addlist)r   resultr   r1   r&   r   r   r   r4   p   s    z%DispatchingJinjaLoader.list_templates)r   r   r    r!   r   r   strr"   TupleOptionalCallabler*   r(   r)   	Generatorr   r,   Listr4   r   r   r   r   r$   1   s   (((	$
r$   r   )r   r&   contextr   c             C   s8   |  | tj| ||d ||}tj| ||d |S )N)r&   r>   )update_template_contextr   sendrenderr   )r   r&   r>   r   r   r   r   _render   s
    

rB   )template_name_or_listr>   r   c             K   s    t  }|j| }t|||S )a  Render a template by name with the given context.

    :param template_name_or_list: The name of the template to render. If
        a list is given, the first name to exist will be rendered.
    :param context: The variables to make available in the template.
    )r	   _get_current_object	jinja_envget_or_select_templaterB   )rC   r>   r   r&   r   r   r   render_template   s    
rG   )sourcer>   r   c             K   s    t  }|j| }t|||S )zRender a template from the given source string with the given
    context.

    :param source: The source code of the template to render.
    :param context: The variables to make available in the template.
    )r	   rD   rE   from_stringrB   )rH   r>   r   r&   r   r   r   render_template_string   s    rJ   c                sL      tj d tjt d fdd}| }trHt|}|S )N)r&   r>   )r   c               3   s$    E d H  tj d d S )N)r&   r>   )generater   r@   r   )r   r>   r&   r   r   rK      s    z_stream.<locals>.generate)r?   r   r@   r"   Iteratorr8   r
   r   )r   r&   r>   rK   r   r   )r   r>   r&   r   _stream   s    
rM   c             K   s    t  }|j| }t|||S )a  Render a template by name with the given context as a stream.
    This returns an iterator of strings, which can be used as a
    streaming response from a view.

    :param template_name_or_list: The name of the template to render. If
        a list is given, the first name to exist will be rendered.
    :param context: The variables to make available in the template.

    .. versionadded:: 2.2
    )r	   rD   rE   rF   rM   )rC   r>   r   r&   r   r   r   stream_template   s    rN   c             K   s    t  }|j| }t|||S )aZ  Render a template from the given source string with the given
    context as a stream. This returns an iterator of strings, which can
    be used as a streaming response from a view.

    :param source: The source code of the template to render.
    :param context: The variables to make available in the template.

    .. versionadded:: 2.2
    )r	   rD   rE   rI   rM   )rH   r>   r   r&   r   r   r   stream_template_string   s    
rO   )%typingr"   Zjinja2r   r   r   r   r   globalsr   r   r	   r
   Zhelpersr   Zsignalsr   r   TYPE_CHECKINGr   r   Zscaffoldr   Dictr8   r#   r   r$   rB   Unionr=   rG   rJ   rL   rM   rN   rO   r   r   r   r   <module>   s8   N 	