3
e                 @   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 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eededddZejeeje f ejedddZeejedddZdS )    N)
BaseLoader)Environment)Template)TemplateNotFound   )_app_ctx_stack)_request_ctx_stack)before_render_template)template_rendered)Flask)Scaffold)returnc              C   sB   t j} tj}i }|dk	r"|j|d< | dk	r>| j|d< | j|d< |S )zSDefault template context processor.  Injects `request`,
    `session` and `g`.
    Ngrequestsession)r   topr   r   r   r   )ZreqctxZappctxrv r   1/tmp/pip-build-3irwxpxt/Flask/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|j  |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   3   s    zDispatchingJinjaLoader.__init__)environmenttemplater   c             C   s$   | j jd r| j||S | j||S )NZEXPLAIN_TEMPLATE_LOADING)r   config_get_source_explained_get_source_fast)r   r#   r$   r   r   r   
get_source6   s    z!DispatchingJinjaLoader.get_sourcec       	      C   s   g }d }x\| j |D ]N\}}y|j||}|d kr6|}W n tk
rP   d }Y nX |j|||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:| j |D ],\}}y|j||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'   W   s    z'DispatchingJinjaLoader._get_source_fastr   )r$   r   c             c   sL   | j j}|d k	r| j |fV  x*| j 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*   a   s    z$DispatchingJinjaLoader._iter_loaders)r   c             C   sj   t  }| jj}|d k	r$|j|j  x<| jj D ].}|j}|d k	r0x|j D ]}|j| qLW q0W t|S )N)setr   r-   updatelist_templatesr.   addlist)r   resultr   r/   r$   r   r   r   r2   m   s    z%DispatchingJinjaLoader.list_templates)r   r   r   r   r   r   strr    TupleOptionalCallabler(   r&   r'   	Generatorr   r*   Listr2   r   r   r   r   r"   .   s   (((	$
r"   r   )r$   contextr   r   c             C   s.   t j|| |d | j|}tj|| |d |S )z)Renders the template and fires the signal)r$   r<   )r	   sendrenderr
   )r$   r<   r   r   r   r   r   _render|   s    
r?   )template_name_or_listr<   r   c             K   s*   t j}|jj| t|jjj| ||jS )a  Renders a template from the template folder with the given
    context.

    :param template_name_or_list: the name of the template to be
                                  rendered, or an iterable with template names
                                  the first one existing will be rendered
    :param context: the variables that should be available in the
                    context of the template.
    )r   r   r   update_template_contextr?   	jinja_envZget_or_select_template)r@   r<   ctxr   r   r   render_template   s    rD   )sourcer<   r   c             K   s*   t j}|jj| t|jjj| ||jS )aF  Renders a template from the given template source string
    with the given context. Template variables will be autoescaped.

    :param source: the source code of the template to be
                   rendered
    :param context: the variables that should be available in the
                    context of the template.
    )r   r   r   rA   r?   rB   Zfrom_string)rE   r<   rC   r   r   r   render_template_string   s    	rF   )typingr    Zjinja2r   r   r   r   r   globalsr   r   Zsignalsr	   r
   ZTYPE_CHECKINGr   r   Zscaffoldr   Dictr6   r!   r   r"   dictr?   Unionr;   rD   rF   r   r   r   r   <module>   s$   N
