o
    i0                     @  s   d dl mZ d dlZd dlZd dlmZ d dlmZm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yE   dZY nw G dd	 d	eZG d
d deZedZG dd dZG dd deZdS )    )annotationsN)Callable)Any
NamedTuple)Request)Response)	BaseRouteHostMountRoutec                   @  s   e Zd ZdZd	ddZdS )
OpenAPIResponsezapplication/vnd.oai.openapicontentr   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)Zdefault_flow_stylezutf-8)yaml
isinstancedictdumpencode)selfr    r   H/home/app/Keep/.python/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Callable[..., Any]funcN)r   r   r   __annotations__r   r   r   r   r      s   
 r   z:\w+}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, 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B 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"   ).0Z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   Zinclude_in_schemainspect
isfunctionendpointismethodmethodsappendr   lowerhasattrgetattr)r   r%   endpoints_infoZrouteZsub_endpointsmethodr"   r   r/   r   r:   (   s8   


z!BaseSchemaGenerator.get_endpointsr   r   c                 C  s   t 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.
        })_remove_converter_pattern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   splitZ	safe_loadr   r   )r   rJ   	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   rQ   r%   schemar   r   r   r   z   s   z#BaseSchemaGenerator.OpenAPIResponseNr%   r&   r   r'   )r%   r&   r   r+   )r   r   r   r   )rJ   r!   r   r'   )rQ   r   r   r   )r   r   r   r*   r:   r9   rP   r   r   r   r   r   r$   $   s    


1
	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(   )rV   )r   rV   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   rV   
setdefaultr:   rP   r"   r   r    )r   r%   rS   rE   r>   rO   r   r   r   r*      s   

zSchemaGenerator.get_schemaN)rV   r'   r   rW   rT   )r   r   r   rX   r*   r   r   r   r   rU      s    
rU   )
__future__r   r<   recollections.abcr   typingr   r   Zstarlette.requestsr   Zstarlette.responsesr   Zstarlette.routingr   r	   r
   r   r   ModuleNotFoundErrorr   r   compilerH   r$   rU   r   r   r   r   <module>   s$    	
\