
¾«‘_K  ã               @   sÖ   d  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 „  Z Gd d „  d e ƒ Z Gd d „  d e ƒ Z d d „  Z d d „  Z d d „  Z d S)zŠ
    flask.templating
    ~~~~~~~~~~~~~~~~

    Implements the bridge to Jinja2.

    :copyright: 2010 Pallets
    :license: BSD-3-Clause
é    )Ú
BaseLoader)ÚEnvironment)ÚTemplateNotFoundé   )Ú_app_ctx_stack)Ú_request_ctx_stack)Úbefore_render_template)Útemplate_renderedc              C   s[   t  j }  t j } i  } | d k	 r1 | j | d <|  d k	 rW |  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-5gj8f0j9/flask/flask/templating.pyÚ_default_template_ctx_processor   s    		r   c               @   s"   e  Z d  Z d Z d d „  Z d S)r   zÌWorks 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.
    c             K   s9   d | k r | j  ƒ  | d <t j |  | � | |  _ d  S)NÚloader)Zcreate_global_jinja_loaderÚBaseEnvironmentÚ__init__Úapp)Úselfr   Úoptionsr   r   r   r   *   s    zEnvironment.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   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 d	 „  Z d
 d „  Z d d „  Z	 d S)ÚDispatchingJinjaLoaderz\A loader that looks for templates in the application and all
    the blueprint folders.
    c             C   s   | |  _  d  S)N)r   )r   r   r   r   r   r   6   s    zDispatchingJinjaLoader.__init__c             C   s0   |  j  j d r  |  j | | ƒ S|  j | | ƒ S)NZEXPLAIN_TEMPLATE_LOADING)r   ÚconfigÚ_get_source_explainedÚ_get_source_fast)r   ÚenvironmentÚtemplater   r   r   Ú
get_source9   s    z!DispatchingJinjaLoader.get_sourcec       	      C   sÈ   g  } d  } xv |  j  | ƒ D]e \ } } y( | j | | ƒ } | d  k rO | } Wn t k
 rj d  } Yn X| j | | | f ƒ q Wd d l m } | |  j | | ƒ | d  k	 r¸ | St | ƒ ‚ 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   s\   xI |  j  | ƒ D]8 \ } } y | j | | ƒ SWq t k
 rG w Yq Xq Wt | ƒ ‚ d  S)N)r$   r"   r   )r   r    r!   Z_srcobjr   r   r   r   r   S   s    z'DispatchingJinjaLoader._get_source_fastc             c   sd   |  j  j } | d  k	 r& |  j  | f Vx7 |  j  j ƒ  D]& } | j } | d  k	 r6 | | f Vq6 Wd  S)N)r   Újinja_loaderÚiter_blueprints)r   r!   r   Ú	blueprintr   r   r   r$   [   s    	z$DispatchingJinjaLoader._iter_loadersc             C   s‘   t  ƒ  } |  j j } | d  k	 r4 | j | j ƒ  ƒ xP |  j j ƒ  D]? } | j } | d  k	 rD x! | j ƒ  D] } | j | ƒ ql WqD Wt | ƒ S)N)Úsetr   r'   ÚupdateÚlist_templatesr(   ÚaddÚlist)r   Úresultr   r)   r!   r   r   r   r,   e   s    		z%DispatchingJinjaLoader.list_templatesN)
r   r   r   r   r   r"   r   r   r$   r,   r   r   r   r   r   1   s   
r   c             C   sE   t  j | d |  d | ƒ|  j | ƒ } t j | d |  d | ƒ| S)z)Renders the template and fires the signalr!   Úcontext)r   ÚsendÚrenderr	   )r!   r0   r   r   r   r   r   Ú_rendert   s    r3   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_contextr3   Ú	jinja_envZget_or_select_template)Ztemplate_name_or_listr0   Úctxr   r   r   Úrender_template}   s    
	r7   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   r4   r3   r5   Zfrom_string)Úsourcer0   r6   r   r   r   Úrender_template_string�   s    		r9   N)r   Zjinja2r   r   r   r   Úglobalsr   r   Zsignalsr   r	   r   r   r3   r7   r9   r   r   r   r   Ú<module>
   s   C	