o
    Þ­jN  ã                   @  s¼   d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
mZmZmZ zd dlZW n ey;   dZY nw G dd„ deƒZG dd	„ d	ejƒZG d
d„ dƒZG dd„ deƒZdS )é    )ÚannotationsN)ÚRequest)ÚResponse)Ú	BaseRouteÚHostÚMountÚRoutec                   @  s   e Zd ZdZd	dd„ZdS )
ÚOpenAPIResponsezapplication/vnd.oai.openapiÚcontentú
typing.AnyÚreturnÚbytesc                 C  s6   t d usJ dƒ‚t|tƒsJ dƒ‚t j|dd� d¡S )Nz2`pyyaml` must be installed to use OpenAPIResponse.z<The schema passed to OpenAPIResponse should be a dictionary.F)Údefault_flow_stylezutf-8)ÚyamlÚ
isinstanceÚdictÚdumpÚencode)Úselfr
   © r   úN/var/www/html/CropPilot/venv/lib/python3.10/site-packages/starlette/schemas.pyÚrender   s   ÿþzOpenAPIResponse.renderN)r
   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú
media_typer   r   r   r   r   r	      s    r	   c                   @  s&   e Zd ZU ded< ded< ded< dS )ÚEndpointInfoÚstrÚpathÚhttp_methodú typing.Callable[..., typing.Any]ÚfuncN)r   r   r   Ú__annotations__r   r   r   r   r      s   
 r   c                   @  s>   e Zd Zddd„Zddd	„Zddd„Zddd„Zddd„ZdS )ÚBaseSchemaGeneratorÚroutesúlist[BaseRoute]r   údict[str, typing.Any]c                 C  s   t ƒ ‚©N)ÚNotImplementedError)r   r$   r   r   r   Ú
get_schema#   s   zBaseSchemaGenerator.get_schemaúlist[EndpointInfo]c                   s  g }|D ]‡}t |ttfƒr2|jpg }t |tƒr|  |j¡‰ nd‰ ‡ fdd„|  |¡D ƒ}| |¡ qt |tƒr:|j	s;qt
 |j¡sGt
 |j¡rh|  |j¡‰ |jpRdgD ]}|dkrZqS| tˆ | ¡ |jƒ¡ qSq|  |j¡‰ dD ]}t|j|ƒsyqpt|j|ƒ}| tˆ | ¡ |ƒ¡ qpq|S )a
  
        Given the routes, yields the following information:

        - path
            eg: /users/
        - http_method
            one of 'get', 'post', 'put', 'patch', 'delete', 'options'
        - func
            method ready to extract the docstring
        Ú c                   s*   g | ]}t d  ˆ |jf¡|j|jd�‘qS )r+   )r   r   r!   )r   Újoinr   r   r!   )Ú.0Úsub_endpoint©r   r   r   Ú
<listcomp>:   s    ûýÿz5BaseSchemaGenerator.get_endpoints.<locals>.<listcomp>ÚGETÚHEAD)ÚgetÚpostÚputÚpatchÚdeleteÚoptions)r   r   r   r$   Ú_remove_converterr   Úget_endpointsÚextendr   Úinclude_in_schemaÚinspectÚ
isfunctionÚendpointÚismethodÚmethodsÚappendr   ÚlowerÚhasattrÚgetattr)r   r$   Úendpoints_infoÚrouteÚsub_endpointsÚmethodr!   r   r/   r   r:   &   s<   


úÿýüz!BaseSchemaGenerator.get_endpointsr   r   c                 C  s   t  dd|¡S )zì
        Remove the converter from the path.
        For example, a route like this:
            Route("/users/{id:int}", endpoint=get_user, methods=["GET"])
        Should be represented as `/users/{id}` in the OpenAPI schema.
        z:\w+}Ú})ÚreÚsub)r   r   r   r   r   r9   Y   s   z%BaseSchemaGenerator._remove_converterÚfunc_or_methodr    c                 C  sH   |j }|si S tdusJ dƒ‚| d¡d }t |¡}t|tƒs"i S |S )z`
        Given a function, parse the docstring as YAML and return a dictionary of info.
        Nz2`pyyaml` must be installed to use parse_docstring.z---éÿÿÿÿ)Ú__doc__r   ÚsplitÚ	safe_loadr   r   )r   rM   Ú	docstringÚparsedr   r   r   Úparse_docstringb   s   

z#BaseSchemaGenerator.parse_docstringÚrequestr   r   c                 C  s   |j j}| j|d�}t|ƒS )N)r$   )Úappr$   r)   r	   )r   rU   r$   Úschemar   r   r   r	   |   s   z#BaseSchemaGenerator.OpenAPIResponseN©r$   r%   r   r&   )r$   r%   r   r*   )r   r   r   r   )rM   r    r   r&   )rU   r   r   r   )r   r   r   r)   r:   r9   rT   r	   r   r   r   r   r#   "   s    


3
	r#   c                   @  s    e Zd Zddd„Zdd	d
„ZdS )ÚSchemaGeneratorÚbase_schemar&   r   ÚNonec                 C  s
   || _ d S r'   )rZ   )r   rZ   r   r   r   Ú__init__ƒ   s   
zSchemaGenerator.__init__r$   r%   c                 C  sp   t | jƒ}| di ¡ |  |¡}|D ]#}|  |j¡}|sq|j|d vr+i |d |j< ||d |j |j< q|S )NÚpaths)r   rZ   Ú
setdefaultr:   rT   r!   r   r   )r   r$   rW   rF   r?   rS   r   r   r   r)   †   s   

zSchemaGenerator.get_schemaN)rZ   r&   r   r[   rX   )r   r   r   r\   r)   r   r   r   r   rY   ‚   s    
rY   )Ú
__future__r   r=   rK   ÚtypingÚstarlette.requestsr   Ústarlette.responsesr   Ústarlette.routingr   r   r   r   r   ÚModuleNotFoundErrorr	   Ú
NamedTupler   r#   rY   r   r   r   r   Ú<module>   s     ÿ`