U
    Ha[                     @   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 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 ejdgejf ZG dd dZG dd de	ZdS )    N)defaultdict)update_wrapper   )_endpoint_from_view_func)	_sentinel)Scaffold)AfterRequestCallable)BeforeRequestCallableErrorHandlerCallable)TeardownCallable) TemplateContextProcessorCallableTemplateFilterCallableTemplateGlobalCallableTemplateTestCallable)URLDefaultCallable)URLValuePreprocessorCallable)FlaskBlueprintSetupStatec                   @   sP   e Zd ZdZddejeddddZdeej	e ej	ej
 ejddd	d
ZdS )r   zTemporary holder object for registering a blueprint with the
    application.  An instance of this class is created by the
    :meth:`~flask.Blueprint.make_setup_state` method and later passed
    to all register callback functions.
    	Blueprintr   N)	blueprintappoptionsfirst_registrationreturnc                 C   s   || _ || _|| _|| _| jd}|d kr4| jj}|| _| jd}|d krV| jj}|| _| jd|j| _| jdd| _t	| jj
| _| j| jdd d S )N	subdomain
url_prefixnamename_prefix url_defaults )r   r   r   r   getr   r   r    r!   dicturl_values_defaultsr#   update)selfr   r   r   r   r   r   r$   r$   4/tmp/pip-unpacked-wheel-99zf6fhi/flask/blueprints.py__init__    s     zBlueprintSetupState.__init__)ruleendpoint	view_funcr   r   c                 K   s   | j dk	r2|r,d| j d|df}n| j }|d| j |dkrPt|}| j}d|krpt|f|	d}| j
j|| j d| j d| d|fd|i| dS )zA helper method to register a rule (and optionally a view function)
        to the application.  The endpoint is automatically prefixed with the
        blueprint's name.
        N/r   defaults.)r   joinrstriplstrip
setdefaultr   r   r#   r&   popr   add_url_ruler!   r    )r)   r,   r-   r.   r   r0   r$   r$   r*   r7   N   s&    
z BlueprintSetupState.add_url_rule)NN)__name__
__module____qualname____doc__tAnyboolr+   strOptionalCallabler7   r$   r$   r$   r*   r      s    1  
c                       st  e Zd ZdZdZdZdZdZdddddddefe	e	e
je	 e
je	 e
je	 e
je	 e
je	 e
je e
je	 e
je	 d
 fddZeddd	Ze
jdd
ddZe
jdd
ddZd<deeedddZd e
jddddZdeddddZd=e	e
je	 e
je
j e
je e
jddddZd>e
je	 e
jegef dddZd?ee
je	 ddd d!Zd@e
je	 e
jegef dd"d#ZdAee
je	 ddd$d%ZdBe
je	 e
jegef dd&d'Z dCee
je	 ddd(d)Z!e"e"d*d+d,Z#e"e"d*d-d.Z$e%e%d*d/d0Z&e'e'd*d1d2Z(e)e)d*d3d4Z*e
j+e
j,e- e.f e
jd5d6d7Z/e0e0d*d8d9Z1e2e2d*d:d;Z3  Z4S )Dr   a	  Represents a blueprint, a collection of routes and other
    app-related functions that can be registered on a real application
    later.

    A blueprint is an object that allows defining application functions
    without requiring an application object ahead of time. It uses the
    same decorators as :class:`~flask.Flask`, but defers the need for an
    application by recording them for later registration.

    Decorating a function with a blueprint creates a deferred function
    that is called with :class:`~flask.blueprints.BlueprintSetupState`
    when the blueprint is registered on an application.

    See :doc:`/blueprints` for more information.

    :param name: The name of the blueprint. Will be prepended to each
        endpoint name.
    :param import_name: The name of the blueprint package, usually
        ``__name__``. This helps locate the ``root_path`` for the
        blueprint.
    :param static_folder: A folder with static files that should be
        served by the blueprint's static route. The path is relative to
        the blueprint's root path. Blueprint static files are disabled
        by default.
    :param static_url_path: The url to serve static files from.
        Defaults to ``static_folder``. If the blueprint does not have
        a ``url_prefix``, the app's static route will take precedence,
        and the blueprint's static files won't be accessible.
    :param template_folder: A folder with templates that should be added
        to the app's template search path. The path is relative to the
        blueprint's root path. Blueprint templates are disabled by
        default. Blueprint templates have a lower precedence than those
        in the app's templates folder.
    :param url_prefix: A path to prepend to all of the blueprint's URLs,
        to make them distinct from the rest of the app's routes.
    :param subdomain: A subdomain that blueprint routes will match on by
        default.
    :param url_defaults: A dict of default values that blueprint routes
        will receive by default.
    :param root_path: By default, the blueprint will automatically set
        this based on ``import_name``. In certain situations this
        automatic detection can fail, so the path can be specified
        manually instead.

    .. versionchanged:: 1.1.0
        Blueprints have a ``cli`` group to register nested CLI commands.
        The ``cli_group`` parameter controls the name of the group under
        the ``flask`` command.

    .. versionadded:: 0.7
    FN)
r    import_namestatic_folderstatic_url_pathtemplate_folderr   r   r#   	root_path	cli_groupc                    s`   t  j|||||	d d|kr&td|| _|| _|| _g | _|d krJi }|| _|
| _g | _	d S )N)rB   rC   rD   rE   rF   r1   z+'name' may not contain a dot '.' character.)
superr+   
ValueErrorr    r   r   deferred_functionsr'   rG   _blueprints)r)   r    rB   rC   rD   rE   r   r   r#   rF   rG   	__class__r$   r*   r+      s$    zBlueprint.__init__)r   c                 C   s   | j o
| jS N)warn_on_modifications_got_registered_oncer)   r$   r$   r*   _is_setup_finished   s    zBlueprint._is_setup_finished)funcr   c                 C   s4   | j r$| jr$ddlm} |td | j| dS )zRegisters a function that is called when the blueprint is
        registered on the application.  This function is called with the
        state as argument as returned by the :meth:`make_setup_state`
        method.
        r   )warnzfThe blueprint was already registered once but is getting modified now. These changes will not show up.N)rP   rO   warningsrT   WarningrJ   append)r)   rS   rT   r$   r$   r*   record   s    zBlueprint.recordc                    s$   t dd fdd}| t| S )zWorks like :meth:`record` but wraps the function in another
        function that will ensure the function is only called once.  If the
        blueprint is registered a second time on the application, the
        function passed is not called.
        Nstater   c                    s   | j r |  d S rN   )r   rZ   rS   r$   r*   wrapper   s    z&Blueprint.record_once.<locals>.wrapper)r   rX   r   )r)   rS   r]   r$   r\   r*   record_once   s    zBlueprint.record_oncer   )r   r   r   r   c                 C   s   t | |||S )zCreates an instance of :meth:`~flask.blueprints.BlueprintSetupState`
        object that is later passed to the register callback functions.
        Subclasses can override this to return a subclass of the setup state.
        )r   )r)   r   r   r   r$   r$   r*   make_setup_state   s    zBlueprint.make_setup_state)r   r   r   c                 K   s$   || krt d| j||f dS )a  Register a :class:`~flask.Blueprint` on this blueprint. Keyword
        arguments passed to this method will override the defaults set
        on the blueprint.

        .. versionchanged:: 2.0.1
            The ``name`` option can be used to change the (pre-dotted)
            name the blueprint is registered with. This allows the same
            blueprint to be registered multiple times with unique names
            for ``url_for``.

        .. versionadded:: 2.0
        z%Cannot register a blueprint on itselfN)rI   rK   rW   )r)   r   r   r$   r$   r*   register_blueprint   s    zBlueprint.register_blueprint)r   r   r   c                    s   t fdd|j D  }|dd}|dj}| d| d  |jkr| krjd  dnd}|j  k	rtd	| d
| dn$ddl}|jd	| d| ddd |j < d_	
|||}jr|jj djdd |rЇ fdd}	j D ]H\}
}|
dkr( n  d|
 }
ttdd | D }||j|
< qj D ]\}}||j|< qf|	j|j |	j|j |	j|j |	j|j |	j|j |	j|j jD ]}|| q|dj}jjrT|dkr|jjjj n8|tkr> j_|j j n|j_|j j j!D ]\}}|" }|d}|dkr|j#}|j#dk	r|dk	r|j#$dd |d |d< n*|dk	r||d< n|j#dk	r|j#|d<  |d< |%|| qZdS )a\  Called by :meth:`Flask.register_blueprint` to register all
        views and callbacks registered on the blueprint with the
        application. Creates a :class:`.BlueprintSetupState` and calls
        each :meth:`record` callback with it.

        :param app: The application this blueprint is being registered
            with.
        :param options: Keyword arguments forwarded from
            :meth:`~Flask.register_blueprint`.

        .. versionchanged:: 2.0.1
            Nested blueprints are registered with their dotted name.
            This allows different blueprints with the same name to be
            nested at different locations.

        .. versionchanged:: 2.0.1
            The ``name`` option can be used to change the (pre-dotted)
            name the blueprint is registered with. This allows the same
            blueprint to be registered multiple times with unique names
            for ``url_for``.

        .. versionchanged:: 2.0.1
            Registering the same blueprint with the same name multiple
            times is deprecated and will become an error in Flask 2.1.
        c                 3   s   | ]}| kV  qd S rN   r$   ).0ZbprQ   r$   r*   	<genexpr>'  s     z%Blueprint.register.<locals>.<genexpr>r!   r"   r    r1   z ''z
The name 'z1' is already registered for a different blueprintz'. Use 'name=' to provide a unique name.r   Nz*' is already registered for this blueprintzO. Use 'name=' to provide a unique name. This will become an error in Flask 2.1.   )
stacklevelTz/<path:filename>Zstatic)r.   r-   c                    s>   |   D ]0\}}|d kr n  d| }|| | qd S )Nr1   )itemsextend)Zbp_dictZparent_dictkeyvaluesr    r$   r*   rg   N  s    z"Blueprint.register.<locals>.extendc                 S   s$   i | ]\}}|d d |  D qS )c                 S   s   i | ]\}}||qS r$   r$   )ra   	exc_classrS   r$   r$   r*   
<dictcomp>X  s     z1Blueprint.register.<locals>.<dictcomp>.<dictcomp>)rf   )ra   codeZcode_valuesr$   r$   r*   rl   W  s
    z&Blueprint.register.<locals>.<dictcomp>rG   r   r/   )&anyZ
blueprintsri   r%   r    r4   rI   rU   rT   rP   r_   Zhas_static_folderr7   rD   Zsend_static_fileZerror_handler_specrf   r   r&   Zview_functionsbefore_request_funcsafter_request_funcsteardown_request_funcsurl_default_functionsurl_value_preprocessorstemplate_context_processorsrJ   rG   clicommandsr(   r   Zadd_commandrK   copyr   r3   register)r)   r   r   r   r!   Z	self_nameZexisting_atrU   rZ   rg   rh   valuer-   rS   deferredZcli_resolved_groupr   Z
bp_optionsZbp_url_prefixr$   r    r)   r*   rx     s    


	








zBlueprint.register)r,   r-   r.   provide_automatic_optionsr   r   c                    sR    rd krt dr4tdr4djkr4t d|  fdd dS )zLike :meth:`Flask.add_url_rule` but for a blueprint.  The endpoint for
        the :func:`url_for` function is prefixed with the name of the blueprint.
        r1   z/'endpoint' may not contain a dot '.' character.r8   z5'view_func' name may not contain a dot '.' character.c                    s   | j  fdiS )Nr|   )r7   sr-   r   r|   r,   r.   r$   r*   <lambda>  s   z(Blueprint.add_url_rule.<locals>.<lambda>N)rI   hasattrr8   rX   )r)   r,   r-   r.   r|   r   r$   r   r*   r7     s    zBlueprint.add_url_rule)r    r   c                    s   t t d fdd}|S )zRegister a custom template filter, available application wide.  Like
        :meth:`Flask.template_filter` but for a blueprint.

        :param name: the optional name of the filter, otherwise the
                     function name will be used.
        fr   c                    s   j |  d | S Nrj   )add_app_template_filterr   r{   r$   r*   	decorator  s    z0Blueprint.app_template_filter.<locals>.decoratorr   r)   r    r   r$   r{   r*   app_template_filter  s    
zBlueprint.app_template_filter)r   r    r   c                    s$   t dd fdd}| | dS )aI  Register a custom template filter, available application wide.  Like
        :meth:`Flask.add_template_filter` but for a blueprint.  Works exactly
        like the :meth:`app_template_filter` decorator.

        :param name: the optional name of the filter, otherwise the
                     function name will be used.
        NrY   c                    s    | j jjp j< d S rN   )r   	jinja_envfiltersr8   r[   r   r    r$   r*   register_template  s    z<Blueprint.add_app_template_filter.<locals>.register_templater   r^   r)   r   r    r   r$   r   r*   r     s    z!Blueprint.add_app_template_filterc                    s   t t d fdd}|S )a  Register a custom template test, available application wide.  Like
        :meth:`Flask.template_test` but for a blueprint.

        .. versionadded:: 0.10

        :param name: the optional name of the test, otherwise the
                     function name will be used.
        r   c                    s   j |  d | S r   )add_app_template_testr   r{   r$   r*   r     s    z.Blueprint.app_template_test.<locals>.decoratorr   r   r$   r{   r*   app_template_test  s    zBlueprint.app_template_testc                    s$   t dd fdd}| | dS )aa  Register a custom template test, available application wide.  Like
        :meth:`Flask.add_template_test` but for a blueprint.  Works exactly
        like the :meth:`app_template_test` decorator.

        .. versionadded:: 0.10

        :param name: the optional name of the test, otherwise the
                     function name will be used.
        NrY   c                    s    | j jjp j< d S rN   )r   r   testsr8   r[   r   r$   r*   r     s    z:Blueprint.add_app_template_test.<locals>.register_templater   r   r$   r   r*   r     s    zBlueprint.add_app_template_testc                    s   t t d fdd}|S )a  Register a custom template global, available application wide.  Like
        :meth:`Flask.template_global` but for a blueprint.

        .. versionadded:: 0.10

        :param name: the optional name of the global, otherwise the
                     function name will be used.
        r   c                    s   j |  d | S r   )add_app_template_globalr   r{   r$   r*   r     s    z0Blueprint.app_template_global.<locals>.decoratorr   r   r$   r{   r*   app_template_global  s    zBlueprint.app_template_globalc                    s$   t dd fdd}| | dS )ai  Register a custom template global, available application wide.  Like
        :meth:`Flask.add_template_global` but for a blueprint.  Works exactly
        like the :meth:`app_template_global` decorator.

        .. versionadded:: 0.10

        :param name: the optional name of the global, otherwise the
                     function name will be used.
        NrY   c                    s    | j jjp j< d S rN   )r   r   globalsr8   r[   r   r$   r*   r     s    z<Blueprint.add_app_template_global.<locals>.register_templater   r   r$   r   r*   r      s    z!Blueprint.add_app_template_globalr   c                    s   |   fdd  S )zLike :meth:`Flask.before_request`.  Such a function is executed
        before each request, even if outside of a blueprint.
        c                    s   | j jd g  S rN   )r   ro   r5   rW   r}   r   r$   r*   r         z.Blueprint.before_app_request.<locals>.<lambda>r^   r)   r   r$   r   r*   before_app_request  s    
zBlueprint.before_app_requestc                    s   |   fdd  S )zLike :meth:`Flask.before_first_request`.  Such a function is
        executed before the first request to the application.
        c                    s   | j j S rN   )r   Zbefore_first_request_funcsrW   r}   r   r$   r*   r   !  r   z4Blueprint.before_app_first_request.<locals>.<lambda>r   r   r$   r   r*   before_app_first_request  s    z"Blueprint.before_app_first_requestc                    s   |   fdd  S )zLike :meth:`Flask.after_request` but for a blueprint.  Such a function
        is executed after each request, even if outside of the blueprint.
        c                    s   | j jd g  S rN   )r   rp   r5   rW   r}   r   r$   r*   r   )  r   z-Blueprint.after_app_request.<locals>.<lambda>r   r   r$   r   r*   after_app_request$  s    
zBlueprint.after_app_requestc                    s   |   fdd  S )zLike :meth:`Flask.teardown_request` but for a blueprint.  Such a
        function is executed when tearing down each request, even if outside of
        the blueprint.
        c                    s   | j jd g  S rN   )r   rq   r5   rW   r}   r   r$   r*   r   3  r   z0Blueprint.teardown_app_request.<locals>.<lambda>r   r   r$   r   r*   teardown_app_request-  s    
zBlueprint.teardown_app_requestc                    s   |   fdd  S )zLike :meth:`Flask.context_processor` but for a blueprint.  Such a
        function is executed each request, even if outside of the blueprint.
        c                    s   | j jd g  S rN   )r   rt   r5   rW   r}   r   r$   r*   r   >  r   z1Blueprint.app_context_processor.<locals>.<lambda>r   r   r$   r   r*   app_context_processor7  s    
zBlueprint.app_context_processor)rm   r   c                    s   t t d fdd}|S )zLike :meth:`Flask.errorhandler` but for a blueprint.  This
        handler is used for all requests, even if outside of the blueprint.
        r   c                    s     fdd  S )Nc                    s   | j  S rN   )r   Zerrorhandlerr}   )rm   r   r$   r*   r   H  r   z?Blueprint.app_errorhandler.<locals>.decorator.<locals>.<lambda>r   r   rm   r)   r   r*   r   G  s    z-Blueprint.app_errorhandler.<locals>.decoratorr
   )r)   rm   r   r$   r   r*   app_errorhandlerB  s    zBlueprint.app_errorhandlerc                    s   |   fdd  S )z<Same as :meth:`url_value_preprocessor` but application wide.c                    s   | j jd g  S rN   )r   rs   r5   rW   r}   r   r$   r*   r   R  r   z6Blueprint.app_url_value_preprocessor.<locals>.<lambda>r   r   r$   r   r*   app_url_value_preprocessorM  s    
z$Blueprint.app_url_value_preprocessorc                    s   |   fdd  S )z2Same as :meth:`url_defaults` but application wide.c                    s   | j jd g  S rN   )r   rr   r5   rW   r}   r   r$   r*   r   Y  r   z,Blueprint.app_url_defaults.<locals>.<lambda>r   r   r$   r   r*   app_url_defaultsV  s    
zBlueprint.app_url_defaults)F)NNN)N)N)N)N)N)N)5r8   r9   r:   r;   rO   rP   Zjson_encoderZjson_decoderr   r?   r<   r@   r&   r+   r>   rR   rA   rX   r^   r   r_   r=   r`   rx   r7   r   r   r   r   r   r   r   r   r   r	   r   r   r   r   r   r   r   r   UnionType	Exceptionintr   r   r   r   r   __classcell__r$   r$   rL   r*   r   n   s   4$   	    
         
		"	r   )typingr<   collectionsr   	functoolsr   Zscaffoldr   r   r   r   r	   r   r   r   r   r   r   r   r   TYPE_CHECKINGr   r   rA   ZDeferredSetupFunctionr   r   r$   r$   r$   r*   <module>   s(   U