
_S                 @   so   d  Z  d d l m Z d d l m Z d d l m Z e   Z Gd d   d e  Z Gd d	   d	 e  Z	 d
 S)z
    flask.blueprints
    ~~~~~~~~~~~~~~~~

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

    :copyright: 2010 Pallets
    :license: BSD-3-Clause
    )update_wrapper   )_endpoint_from_view_func)_PackageBoundObjectc               @   s4   e  Z d  Z d Z d d   Z d 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 j d  } | d  k rN |  j j } | |  _ |  j j d  } | d  k r |  j j } | |  _ t |  j j  |  _	 |  j	 j
 |  j j d f    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   1/tmp/pip-build-5gj8f0j9/flask/flask/blueprints.py__init__   s    						zBlueprintSetupState.__init__Nc             K   s   |  j  d k	 rK | rB d j |  j  j d  | j d  f  } n	 |  j  } | j d |  j  | d k rv t |  } |  j } d | k r t | | j	 d   } |  j
 j | d |  j j | f | d | | 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   A   s     -			z BlueprintSetupState.add_url_rule)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s   %r   c            
   @   s  e  Z d  Z d Z d Z d Z d Z d Z d Z d Z	 d Z
 d d d d d d d e d d  Z d d   Z d d	   Z d d
 d  Z d d d  Z d d   Z d d d d  Z d d   Z d d d  Z d d d  Z d d d  Z d d d  Z d d d  Z d d 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! d0 d1   Z" d2 d3   Z# d4 d5   Z$ d6 d7   Z% d8 d9   Z& d: d;   Z' d< d=   Z( d> d?   Z) d S)@	Blueprinta	  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 :ref:`blueprints` for more information.

    .. 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

    :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 this
        based on ``import_name``. In certain situations this automatic
        detection can fail, so the path can be specified manually
        instead.
    FNc             C   sw   t  j |  | | d |	 | |  _ | |  _ | |  _ | |  _ | |  _ g  |  _ | d  k ra i  } | |  _ |
 |  _	 d  S)N	root_path)
r   r   r   r   r   static_folderstatic_url_pathdeferred_functionsr   	cli_group)r   r   import_namer(   r)   template_folderr   r   r	   r'   r+   r   r   r   r      s    							zBlueprint.__init__c             C   sF   |  j  r2 |  j r2 d d l m } | t d   |  j 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   } |  j  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   s	  d |  _  |  j | | |  } |  j rJ | j |  j d d |  j d d x |  j D] } | |  qT W| j d |  j  } |  j	 j
 s d S| d k r | j	 j
 j |  j	 j
  nP | t k r |  j |  j	 _ | j	 j |  j	  n | |  j	 _ | j	 j |  j	  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>r!   r    Zstaticr+   N)r/   r9   Zhas_static_folderr   r)   Zsend_static_filer*   r   r+   clicommandsr   	_sentinelr   Zadd_command)r   r
   r   r   r6   deferredZcli_resolved_groupr   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/     j  d |  j  }  j  | |     |  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                st     r d   k s t  d    rN t  d  rN d  j k sN t  d   |  j      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        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   }  j  |    S)Nc                s    |  j  j   <d  S)N)r
   Zview_functions)r6   )r    r?   r   r   register_endpoint1  s    z@Blueprint.endpoint.<locals>.decorator.<locals>.register_endpoint)r8   )r?   rG   )r    r   )r?   r   r@   0  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)Nr   )add_app_template_filter)r?   )r   r   r   r   r@   A  s    z0Blueprint.app_template_filter.<locals>.decoratorr   )r   r   r@   r   )r   r   r   app_template_filter9  s    zBlueprint.app_template_filterc                s&      f d d   } |  j  |  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_templateP  s    z<Blueprint.add_app_template_filter.<locals>.register_templateN)r8   )r   r?   r   rL   r   )r?   r   r   rH   G  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)Nr   )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_testU  s    
zBlueprint.app_template_testc                s&      f d d   } |  j  |  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
   rJ   testsr"   )r6   )r?   r   r   r   rL   p  s    z:Blueprint.add_app_template_test.<locals>.register_templateN)r8   )r   r?   r   rL   r   )r?   r   r   rM   e  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)Nr   )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_globalu  s    
zBlueprint.app_template_globalc                s&      f d d   } |  j  |  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
   rJ   globalsr"   )r6   )r?   r   r   r   rL     s    z<Blueprint.add_app_template_global.<locals>.register_templateN)r8   )r   r?   r   rL   r   )r?   r   r   rP     s    z!Blueprint.add_app_template_globalc                s     j     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  j g   j    S)N)r
   before_request_funcsr   r   r3   )rC   )r?   r   r   r   rD     s    z*Blueprint.before_request.<locals>.<lambda>)r8   )r   r?   r   )r?   r   r   before_request  s    zBlueprint.before_requestc                s   |  j    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 j d  g   j    S)N)r
   rS   r   r3   )rC   )r?   r   r   rD     s    z.Blueprint.before_app_request.<locals>.<lambda>)r8   )r   r?   r   )r?   r   before_app_request  s    zBlueprint.before_app_requestc                s   |  j    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 j    S)N)r
   Zbefore_first_request_funcsr3   )rC   )r?   r   r   rD     s    z4Blueprint.before_app_first_request.<locals>.<lambda>)r8   )r   r?   r   )r?   r   before_app_first_request  s    z"Blueprint.before_app_first_requestc                s     j     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  j g   j    S)N)r
   after_request_funcsr   r   r3   )rC   )r?   r   r   r   rD     s    z)Blueprint.after_request.<locals>.<lambda>)r8   )r   r?   r   )r?   r   r   after_request  s    zBlueprint.after_requestc                s   |  j    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 j d  g   j    S)N)r
   rW   r   r3   )rC   )r?   r   r   rD     s    z-Blueprint.after_app_request.<locals>.<lambda>)r8   )r   r?   r   )r?   r   after_app_request  s    zBlueprint.after_app_requestc                s     j     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  j g   j    S)N)r
   teardown_request_funcsr   r   r3   )rC   )r?   r   r   r   rD     s    z,Blueprint.teardown_request.<locals>.<lambda>)r8   )r   r?   r   )r?   r   r   teardown_request  s    zBlueprint.teardown_requestc                s   |  j    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 j d  g   j    S)N)r
   rZ   r   r3   )rC   )r?   r   r   rD     s    z0Blueprint.teardown_app_request.<locals>.<lambda>)r8   )r   r?   r   )r?   r   teardown_app_request  s    zBlueprint.teardown_app_requestc                s     j     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  j g   j    S)N)r
   template_context_processorsr   r   r3   )rC   )r?   r   r   r   rD     s   z-Blueprint.context_processor.<locals>.<lambda>)r8   )r   r?   r   )r?   r   r   context_processor  s    zBlueprint.context_processorc                s   |  j    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 j d  g   j    S)N)r
   r]   r   r3   )rC   )r?   r   r   rD     s    z1Blueprint.app_context_processor.<locals>.<lambda>)r8   )r   r?   r   )r?   r   app_context_processor  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     j     f d d      S)Nc                s   |  j  j      S)N)r
   errorhandler)rC   )coder?   r   r   rD     s    z?Blueprint.app_errorhandler.<locals>.decorator.<locals>.<lambda>)r8   )r?   )ra   r   )r?   r   r@     s    z-Blueprint.app_errorhandler.<locals>.decoratorr   )r   ra   r@   r   )ra   r   r   app_errorhandler  s    zBlueprint.app_errorhandlerc                s     j     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  j g   j    S)N)r
   url_value_preprocessorsr   r   r3   )rC   )r?   r   r   r   rD     s    z2Blueprint.url_value_preprocessor.<locals>.<lambda>)r8   )r   r?   r   )r?   r   r   url_value_preprocessor  s    z Blueprint.url_value_preprocessorc                s     j     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  j g   j    S)N)r
   url_default_functionsr   r   r3   )rC   )r?   r   r   r   rD     s    z(Blueprint.url_defaults.<locals>.<lambda>)r8   )r   r?   r   )r?   r   r   r	     s    zBlueprint.url_defaultsc                s   |  j    f d d      S)zESame as :meth:`url_value_preprocessor` but application wide.
        c                s   |  j  j j d  g   j    S)N)r
   rc   r   r3   )rC   )r?   r   r   rD     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   |  j    f d d      S)z;Same as :meth:`url_defaults` but application wide.
        c                s   |  j  j j d  g   j    S)N)r
   re   r   r3   )rC   )r?   r   r   rD     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#    j      f d d      S)Nc                s   |  j  j  j     S)N)r
   _register_error_handlerr   )rC   )code_or_exceptionr?   r   r   r   rD   )  s    z;Blueprint.errorhandler.<locals>.decorator.<locals>.<lambda>)r8   )r?   )ri   r   )r?   r   r@   '  s    z)Blueprint.errorhandler.<locals>.decoratorr   )r   ri   r@   r   )ri   r   r   r`     s    zBlueprint.errorhandlerc                s#    j      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  j     S)N)r
   rh   r   )rC   )ri   r?   r   r   r   rD   8  s    z2Blueprint.register_error_handler.<locals>.<lambda>N)r8   )r   ri   r?   r   )ri   r?   r   r   register_error_handler/  s    z Blueprint.register_error_handler)*r"   r#   r$   r%   r0   r/   Zjson_encoderZjson_decoderr,   r-   r'   r<   r   r5   r8   r9   r>   rA   r   r    rI   rH   rN   rM   rQ   rP   rT   rU   rV   rX   rY   r[   r\   r^   r_   rb   rd   r	   rf   rg   r`   rj   r   r   r   r   r&   Z   sZ   3'
	
	
	

r&   N)
r%   	functoolsr   helpersr   r   objectr<   r   r&   r   r   r   r   <module>   s   	E