
    >|h.              	           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
lmZ ddlmZmZ  G d deeeee      Zy)a  API Blueprint

This is a subclass of Flask's Blueprint

It provides added features:

- Decorators to specify Marshmallow schema for view functions I/O

- API documentation registration

Documentation process works in several steps:

- At import time

  - When a MethodView or a view function is decorated, relevant information
    is automatically added to the object's ``_apidoc`` attribute.

  - The ``Blueprint.doc`` decorator stores additional information in there that
    flask-smorest can not - or does not yet - infer from the code.

  - The ``Blueprint.route`` decorator registers the endpoint in the Blueprint
    and gathers all documentation information about the endpoint in
    ``Blueprint._docs[endpoint]``.

- At initialization time

  - Schema instances are replaced by their reference in the `schemas` section
    of the spec components.

  - The ``Blueprint.register_blueprint`` method merges nested blueprint
    documentation into the parent blueprint documentation.

  - Documentation is finalized using the information stored in
    ``Blueprint._docs``, with adaptations to parameters only known at init
    time, such as OAS version.

  - Manual documentation is deep-merged with automatic documentation.

  - Endpoints documentation is registered in the APISpec object.
    )deepcopy)wraps)	Blueprint)
MethodView   )ArgumentsMixin)	EtagMixin)PaginationMixin)ResponseMixin)
deepupdateload_info_from_docstringc                        e Zd ZdZg dZddddZdZ fdZ	 	 	 dd	d	d
 fdZd	d	d
dZ	 fdZ
d Zd Zed        Zd Z xZS )r   z2Blueprint that registers info in API documentation)OPTIONSHEADGETPOSTPUTPATCHDELETEzapplication/jsonz!application/x-www-form-urlencodedzmultipart/form-data)jsonformfilesz---c                     |j                  dd      | _        t        |   |i | i | _        g | _        | j                  | j                  | j                  | j                  g| _
        y )Ndescription )popr   super__init___docs
_endpoints_prepare_arguments_doc_prepare_response_doc_prepare_pagination_doc_prepare_etag_doc_prepare_doc_cbks)selfargskwargs	__class__s      V/var/www/html/test/engine/venv/lib/python3.12/site-packages/flask_smorest/blueprint.pyr   zBlueprint.__init__G   sf    !::mR8$)&)$ 
''&&((""	"
    N
parameterstagsc                l   |t        d      ||j                  }|| j                  v r| dt        | j                         }| j                  j	                  |       t        |t        t                    r|j                  |      }n|}t        	| (  |||fi |  | j                  ||||fi | y)a2  Register url rule in application

        Also stores doc info for later registration

        Use this to register a :class:`MethodView <flask.views.MethodView>` or
        a resource function.

        :param str rule: URL rule as string.
        :param str endpoint: Endpoint for the registered URL rule (defaults
            to function name).
        :param callable|MethodView view_func: View function or MethodView class
        :param list parameters: List of parameter descriptions relevant to all
            operations in this path. Only used to document the resource.
        :param list tags: List of tags for the resource.
            If None, ``Blueprint`` name is used.
        :param options: Options to be forwarded to the underlying
            :class:`werkzeug.routing.Rule <Rule>` object.
        Nzview_func must be provided_)	TypeError__name__r    lenappend
isinstancetyper   as_viewr   add_url_rule_store_endpoint_docs)
r&   ruleendpoint	view_funcprovide_automatic_optionsr-   r.   optionsfuncr)   s
            r*   r8   zBlueprint.add_url_rulee   s    : 899 ))H
 t&"1S%9$:;Hx(ij!12$$X.DD 	T8T=W=!!!(Iz4S7Sr+   c                "      fd}|S )zDecorator to register view function in application and documentation

        Calls :meth:`add_url_rule <Blueprint.add_url_rule>`.
        c                 \    j                  dd       } j                  || fd | S )Nr;   r,   )r   r8   )r?   r;   r>   r-   r:   r&   r.   s     r*   	decoratorz"Blueprint.route.<locals>.decorator   sC    {{:t4HDh1;$JQ Kr+    )r&   r:   r-   r.   r>   rB   s   ````` r*   routezBlueprint.route   s    	 	 r+   c           
         |j                  d|j                        }| j                  j                  |j                  j	                         D ci c]  \  }}dj                  ||f      | c}}       t        |   |fi |S c c}}w )a  Register a nested blueprint in application

        Also stores doc info from the nested bluepint for later registration.

        Use this to register a nested :class:`Blueprint <Blueprint>`.

        :param Blueprint blueprint: Blueprint to register under this blueprint.
        :param options: Options to be forwarded to the underlying
            :meth:`flask.Blueprint.register_blueprint` method.

        See :ref:`register-nested-blueprints`.
        name.)getrF   r   updateitemsjoinr   register_blueprint)r&   	blueprintr>   blp_nameendpoint_namedocr)   s         r*   rL   zBlueprint.register_blueprint   s     ;;vy~~6 	

 +4//*?*?*A&M3 (M23S8	
 w))?w??s   B
c                    	  j                   j                  |i       		 fd}t        |t        t                    rP j
                  D ]@  }||j                  v sd|vs||d   v st        ||j                               } |||       B n/ j
                  D ]   }||j                  dd      v s |||       " |	d<   y)zStore view or function doc infoc                     t        t        |di             }t        |j                  j                        |d<   |d<   || j                         <   y)z7Add auto and manual doc to table for later registration_apidoc)	delimiter	docstringr.   N)r   getattrr   __doc__DOCSTRING_INFO_DELIMITERlower)methodfunctionrP   endpoint_doc_infor&   r.   s      r*   store_method_docsz9Blueprint._store_endpoint_docs.<locals>.store_method_docs   sT    
 78Y;<C7  D,I,I C CK03flln-r+   methods)r   r-   N)
r   
setdefaultr5   r6   r   HTTP_METHODSr^   rV   rY   rH   )
r&   r;   objr-   r.   r>   r]   rZ   r?   r\   s
   `   `    @r*   r9   zBlueprint._store_endpoint_docs   s     !JJ11(B?	4  c4
+,++ 8S[[( /6WY=O3O&sFLLN;)&$7	8 ++ 3W[[H==%fc23
 +5,'r+   c                F   |xs g }t        | j                        j                         D ]  \  }}|j                  d      xs g }	||	z   }
i }|j                         D ]v  \  }}|j                  d      }i }| j                  D ]  } |||||||      } |j                  |d          ||n|g|d<   |j                  di       }t        ||      ||<   x dj                  ||f      }t        |j                  j                  |            }|j                  |||
        y)	a#  Register views information in documentation

        If a schema in a parameter or a response appears in the spec
        `schemas` section, it is replaced by a reference in the parameter or
        response documentation:

        "schema":{"$ref": "#/components/schemas/MySchema"}
        r-   r.   )apiappspecrZ   rU   N
manual_docrG   )r:   
operationsr-   )r   r   rJ   r   r%   rI   rH   r   rK   nexturl_map
iter_rulespath)r&   rc   rd   re   rF   r-   url_prefix_parametersr;   r\   endpoint_route_parametersendpoint_parametersrP   method_loperation_doc_infor.   operation_docr?   rf   full_endpointr:   s                       r*   register_views_in_doczBlueprint.register_views_in_doc   sd    !+ 0b ,4DJJ+?+E+E+G !	Q'H'(9(=(=l(K(Qr%"7:S"SC0A0G0G0I F,,)--f5 " 22 D$(%*!'%M $$%7%DE '   f% 033L"E
 *=* EH/F4  HHdH%56M..}=>DII4C<OIPC!	Qr+   c                        fd}|S )aY  Decorator adding description attributes to a view function

        Values passed as kwargs are copied verbatim in the docs

            Example: ::

                @blp.doc(description="Return pets based on ID",
                         summary="Find pets by ID"
                )
                def get(...):
                    ...
        c                      t                fd       }t        t        |di             |_        t	        t        |j                  j                  di                   |j                  d<   |S )Nc                       | i |S )NrC   )f_argsf_kwargsr?   s     r*   wrapperz1Blueprint.doc.<locals>.decorator.<locals>.wrapper'  s    V0x00r+   rS   rf   )r   r   rV   rS   r   rH   )r?   ry   r(   s   ` r*   rB   z Blueprint.doc.<locals>.decorator&  se    4[1 1 'ww	2'FGGO,6,,\2>?-GOOL) Nr+   rC   )r(   rB   s   ` r*   rP   zBlueprint.doc  s    
	 r+   c                     t        |t        t                    rQ| j                  D ]@  }||j                  v s|j                         }t        ||      }t        || ||             B |S  ||      S )z7Apply decorator to view func or MethodView HTTP methods)r5   r6   r   r`   r^   rY   rV   setattr)r&   rB   ra   rZ   ro   r?   s         r*   "_decorate_view_func_or_method_viewz,Blueprint._decorate_view_func_or_method_view4  sl     c4
+,++ <S[[(%||~H"31DC9T?;	<
 J~r+   )NNN)r2   
__module____qualname__rW   r`   %DEFAULT_LOCATION_CONTENT_TYPE_MAPPINGrX   r   r8   rD   rL   r9   rs   staticmethodrP   r|   __classcell__)r)   s   @r*   r   r   7   s     = PL #3&-)  %
B "&1T 1Tf )-4 @2"5H1Qf  8r+   r   N)rW   copyr   	functoolsr   flaskr   FlaskBlueprintflask.viewsr   	argumentsr   etagr	   
paginationr
   responser   utilsr   r   rC   r+   r*   <module>r      s?   'R   - " %  ' # 7INM?IIr+   