B
    ;=[G                 @   sP   d Z ddlmZ ddlmZ ddlmZmZ G dd deZ	G dd	 d	eZ
d
S )u  
    flask.blueprints
    ~~~~~~~~~~~~~~~~

    Blueprints are the recommended way to implement larger or more
    pluggable applications in Flask 0.7 and later.

    :copyright: © 2010 by the Pallets team.
    :license: BSD, see LICENSE for more details.
    )update_wrapper)url_join   )_PackageBoundObject_endpoint_from_view_funcc               @   s"   e Zd ZdZdd ZdddZdS )BlueprintSetupStatezTemporary 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.
    c             C   s   || _ || _|| _|| _| jd}|d kr4| jj}|| _| jd}|d krV| jj}|| _t| jj| _	| j	
| jdd d S )N	subdomain
url_prefixurl_defaults )app	blueprintoptionsfirst_registrationgetr   r	   dicturl_values_defaultsr
   update)selfr   r   r   r   r   r	   r   r   /lib/python3.7/site-packages/flask/blueprints.py__init__   s    zBlueprintSetupState.__init__Nc             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|d| jj|f |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   defaultsz%s.%s)r	   joinrstriplstrip
setdefaultr   r   r
   r   popr   add_url_ruler   name)r   ruleendpoint	view_funcr   r   r   r   r   r   >   s    
z BlueprintSetupState.add_url_rule)NN)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s   %r   c               @   s0  e Zd ZdZdZdZdZdZdZdZ	dZ
d@ddZdd Zdd	 ZdAd
dZdBddZdd ZdCddZdd ZdDddZdEddZdFddZdGddZdHddZdId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 d0d1 Z!d2d3 Z"d4d5 Z#d6d7 Z$d8d9 Z%d:d; Z&d<d= Z'd>d? Z(dS )J	Blueprinta6  Represents a blueprint.  A blueprint is an object that records
    functions that will be called with the
    :class:`~flask.blueprints.BlueprintSetupState` later to register functions
    or other things on the main application.  See :ref:`blueprints` for more
    information.

    .. versionadded:: 0.7
    FNc
       
      C   sL   t j| |||	d || _|| _|| _|| _|| _g | _|d krBi }|| _d S )N)	root_path)	r   r   r   r	   r   static_folderstatic_url_pathdeferred_functionsr   )
r   r   import_namer)   r*   template_folderr	   r   r
   r(   r   r   r   r   v   s    
zBlueprint.__init__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   )warnzgThe blueprint was already registered once but is getting modified now.  These changes will not show up.N)_got_registered_oncewarn_on_modificationswarningsr.   Warningr+   append)r   funcr.   r   r   r   record   s    zBlueprint.recordc                s    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.
        c                s   | j r |  d S )N)r   )state)r4   r   r   wrapper   s    z&Blueprint.record_once.<locals>.wrapper)r5   r   )r   r4   r7   r   )r4   r   record_once   s    zBlueprint.record_oncec             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_statec             C   sN   d| _ | |||}| jr2|j| jd | jdd x| jD ]}|| q:W dS )a4  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`.
        :param first_registration: Whether this is the first time this
            blueprint has been registered on the application.
        Tz/<path:filename>Zstatic)r"   r!   N)r/   r9   Zhas_static_folderr   r*   Zsend_static_filer+   )r   r   r   r   r6   Zdeferredr   r   r   register   s    zBlueprint.registerc                s    fdd}|S )zLike :meth:`Flask.route` but for a blueprint.  The endpoint for the
        :func:`url_for` function is prefixed with the name of the blueprint.
        c                s$     d| j}j|| f  | S )Nr!   )r   r#   r   )fr!   )r   r    r   r   r   	decorator   s    z"Blueprint.route.<locals>.decoratorr   )r   r    r   r<   r   )r   r    r   r   route   s    zBlueprint.routec                sP    rd kst dr4tdr4djks4t 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.
        .z+Blueprint endpoints should not contain dotsr#   z4Blueprint view function name should not contain dotsc                s   | j  fS )N)r   )s)r!   r   r    r"   r   r   <lambda>   s    z(Blueprint.add_url_rule.<locals>.<lambda>N)AssertionErrorhasattrr#   r5   )r   r    r!   r"   r   r   )r!   r   r    r"   r   r      s
    zBlueprint.add_url_rulec                s    fdd}|S )a\  Like :meth:`Flask.endpoint` but for a blueprint.  This does not
        prefix the endpoint with the blueprint name, this has to be done
        explicitly by the user of this method.  If the endpoint is prefixed
        with a `.` it will be registered to the current blueprint, otherwise
        it's an application independent endpoint.
        c                s    fdd} |  S )Nc                s   | j j < d S )N)r   Zview_functions)r6   )r!   r;   r   r   register_endpoint   s    z@Blueprint.endpoint.<locals>.decorator.<locals>.register_endpoint)r8   )r;   rC   )r!   r   )r;   r   r<      s    
z%Blueprint.endpoint.<locals>.decoratorr   )r   r!   r<   r   )r!   r   r   r!      s    zBlueprint.endpointc                s    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.
        c                s   j |  d | S )N)r   )add_app_template_filter)r;   )r   r   r   r   r<      s    z0Blueprint.app_template_filter.<locals>.decoratorr   )r   r   r<   r   )r   r   r   app_template_filter   s    zBlueprint.app_template_filterc                s    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.
        c                s    | j jjp j< d S )N)r   	jinja_envfiltersr#   )r6   )r;   r   r   r   register_template   s    z<Blueprint.add_app_template_filter.<locals>.register_templateN)r8   )r   r;   r   rH   r   )r;   r   r   rD      s    z!Blueprint.add_app_template_filterc                s    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.
        c                s   j |  d | S )N)r   )add_app_template_test)r;   )r   r   r   r   r<     s    z.Blueprint.app_template_test.<locals>.decoratorr   )r   r   r<   r   )r   r   r   app_template_test   s    	zBlueprint.app_template_testc                s    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.
        c                s    | j jjp j< d S )N)r   rF   Ztestsr#   )r6   )r;   r   r   r   rH     s    z:Blueprint.add_app_template_test.<locals>.register_templateN)r8   )r   r;   r   rH   r   )r;   r   r   rI     s    
zBlueprint.add_app_template_testc                s    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.
        c                s   j |  d | S )N)r   )add_app_template_global)r;   )r   r   r   r   r<     s    z0Blueprint.app_template_global.<locals>.decoratorr   )r   r   r<   r   )r   r   r   app_template_global  s    	zBlueprint.app_template_globalc                s    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.
        c                s    | j jjp j< d S )N)r   rF   globalsr#   )r6   )r;   r   r   r   rH   ,  s    z<Blueprint.add_app_template_global.<locals>.register_templateN)r8   )r   r;   r   rH   r   )r;   r   r   rK   "  s    
z!Blueprint.add_app_template_globalc                s     fdd  S )zLike :meth:`Flask.before_request` but for a blueprint.  This function
        is only executed before each request that is handled by a function of
        that blueprint.
        c                s   | j jjg  S )N)r   before_request_funcsr   r   r3   )r?   )r;   r   r   r   r@   5  s   z*Blueprint.before_request.<locals>.<lambda>)r8   )r   r;   r   )r;   r   r   before_request0  s    zBlueprint.before_requestc                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 )N)r   rN   r   r3   )r?   )r;   r   r   r@   =  s   z.Blueprint.before_app_request.<locals>.<lambda>)r8   )r   r;   r   )r;   r   before_app_request9  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 )N)r   Zbefore_first_request_funcsr3   )r?   )r;   r   r   r@   E  s    z4Blueprint.before_app_first_request.<locals>.<lambda>)r8   )r   r;   r   )r;   r   before_app_first_requestA  s    z"Blueprint.before_app_first_requestc                s     fdd  S )zLike :meth:`Flask.after_request` but for a blueprint.  This function
        is only executed after each request that is handled by a function of
        that blueprint.
        c                s   | j jjg  S )N)r   after_request_funcsr   r   r3   )r?   )r;   r   r   r   r@   M  s   z)Blueprint.after_request.<locals>.<lambda>)r8   )r   r;   r   )r;   r   r   after_requestH  s    zBlueprint.after_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 )N)r   rR   r   r3   )r?   )r;   r   r   r@   U  s   z-Blueprint.after_app_request.<locals>.<lambda>)r8   )r   r;   r   )r;   r   after_app_requestQ  s    zBlueprint.after_app_requestc                s     fdd  S )a=  Like :meth:`Flask.teardown_request` but for a blueprint.  This
        function is only executed when tearing down requests handled by a
        function of that blueprint.  Teardown request functions are executed
        when the request context is popped, even when no actual request was
        performed.
        c                s   | j jjg  S )N)r   teardown_request_funcsr   r   r3   )r?   )r;   r   r   r   r@   `  s   z,Blueprint.teardown_request.<locals>.<lambda>)r8   )r   r;   r   )r;   r   r   teardown_requestY  s    zBlueprint.teardown_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 )N)r   rU   r   r3   )r?   )r;   r   r   r@   i  s   z0Blueprint.teardown_app_request.<locals>.<lambda>)r8   )r   r;   r   )r;   r   teardown_app_requestd  s    zBlueprint.teardown_app_requestc                s     fdd  S )zLike :meth:`Flask.context_processor` but for a blueprint.  This
        function is only executed for requests handled by a blueprint.
        c                s   | j jjg  S )N)r   template_context_processorsr   r   r3   )r?   )r;   r   r   r   r@   q  s   z-Blueprint.context_processor.<locals>.<lambda>)r8   )r   r;   r   )r;   r   r   context_processorm  s    zBlueprint.context_processorc                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 )N)r   rX   r   r3   )r?   )r;   r   r   r@   y  s   z1Blueprint.app_context_processor.<locals>.<lambda>)r8   )r   r;   r   )r;   r   app_context_processoru  s    zBlueprint.app_context_processorc                s    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.
        c                s     fdd  S )Nc                s   | j  S )N)r   errorhandler)r?   )coder;   r   r   r@     s    z?Blueprint.app_errorhandler.<locals>.decorator.<locals>.<lambda>)r8   )r;   )r\   r   )r;   r   r<     s    z-Blueprint.app_errorhandler.<locals>.decoratorr   )r   r\   r<   r   )r\   r   r   app_errorhandler}  s    zBlueprint.app_errorhandlerc                s     fdd  S )zRegisters a function as URL value preprocessor for this
        blueprint.  It's called before the view functions are called and
        can modify the url values provided.
        c                s   | j jjg  S )N)r   url_value_preprocessorsr   r   r3   )r?   )r;   r   r   r   r@     s   z2Blueprint.url_value_preprocessor.<locals>.<lambda>)r8   )r   r;   r   )r;   r   r   url_value_preprocessor  s    z Blueprint.url_value_preprocessorc                s     fdd  S )zCallback function for URL defaults for this blueprint.  It's called
        with the endpoint and values and should update the values passed
        in place.
        c                s   | j jjg  S )N)r   url_default_functionsr   r   r3   )r?   )r;   r   r   r   r@     s   z(Blueprint.url_defaults.<locals>.<lambda>)r8   )r   r;   r   )r;   r   r   r
     s    zBlueprint.url_defaultsc                s   |   fdd  S )zESame as :meth:`url_value_preprocessor` but application wide.
        c                s   | j jd g  S )N)r   r^   r   r3   )r?   )r;   r   r   r@     s   z6Blueprint.app_url_value_preprocessor.<locals>.<lambda>)r8   )r   r;   r   )r;   r   app_url_value_preprocessor  s    z$Blueprint.app_url_value_preprocessorc                s   |   fdd  S )z;Same as :meth:`url_defaults` but application wide.
        c                s   | j jd g  S )N)r   r`   r   r3   )r?   )r;   r   r   r@     s   z,Blueprint.app_url_defaults.<locals>.<lambda>)r8   )r   r;   r   )r;   r   app_url_defaults  s    zBlueprint.app_url_defaultsc                s    fdd}|S )a	  Registers an error handler that becomes active for this blueprint
        only.  Please be aware that routing does not happen local to a
        blueprint so an error handler for 404 usually is not handled by
        a blueprint unless it is caused inside a view function.  Another
        special case is the 500 internal server error which is always looked
        up from the application.

        Otherwise works as the :meth:`~flask.Flask.errorhandler` decorator
        of the :class:`~flask.Flask` object.
        c                s     fdd  S )Nc                s   | j j S )N)r   _register_error_handlerr   )r?   )code_or_exceptionr;   r   r   r   r@     s   z;Blueprint.errorhandler.<locals>.decorator.<locals>.<lambda>)r8   )r;   )rd   r   )r;   r   r<     s    z)Blueprint.errorhandler.<locals>.decoratorr   )r   rd   r<   r   )rd   r   r   r[     s    zBlueprint.errorhandlerc                s     fdd dS )a1  Non-decorator version of the :meth:`errorhandler` error attach
        function, akin to the :meth:`~flask.Flask.register_error_handler`
        application-wide function of the :class:`~flask.Flask` object but
        for error handlers limited to this blueprint.

        .. versionadded:: 0.11
        c                s   | j j S )N)r   rc   r   )r?   )rd   r;   r   r   r   r@     s   z2Blueprint.register_error_handler.<locals>.<lambda>N)r8   )r   rd   r;   r   )rd   r;   r   r   register_error_handler  s    z Blueprint.register_error_handler)NNNNNNN)F)F)NN)N)N)N)N)N)N))r#   r$   r%   r&   r0   r/   Zjson_encoderZjson_decoderr,   r-   r(   r   r5   r8   r9   r:   r=   r   r!   rE   rD   rJ   rI   rL   rK   rO   rP   rQ   rS   rT   rV   rW   rY   rZ   r]   r_   r
   ra   rb   r[   re   r   r   r   r   r'   S   sR      










						r'   N)r&   	functoolsr   Zwerkzeug.urlsr   Zhelpersr   r   objectr   r'   r   r   r   r   <module>   s
   A