o
    [hz                     @   s   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	 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 )    N)Request)Response)	BaseRouteMountRoutec                   @   s$   e Zd ZdZdejdefddZdS )OpenAPIResponsezapplication/vnd.oai.openapicontentreturnc                 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   x/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/starlette/schemas.pyrender   s   zOpenAPIResponse.renderN)__name__
__module____qualname__
media_typetypingAnybytesr   r   r   r   r   r      s    r   c                   @   s(   e Zd ZU eed< eed< ejed< dS )EndpointInfopathhttp_methodfuncN)r   r   r   str__annotations__r   Callabler   r   r   r   r      s   
 r   c                   @   sh   e Zd Zdeje defddZdeje deje fddZ	dej
defdd	Zd
edefddZdS )BaseSchemaGeneratorroutesr	   c                 C   s   t  N)NotImplementedError)r   r"   r   r   r   
get_schema    s   zBaseSchemaGenerator.get_schemac                    s   g }|D ]m t  tr" jpg } fdd| |D }|| qt  tr* js+qt j	s7t
 j	rS jp<dgD ]}|dkrDq=|t j|  j	 q=qdD ]}t j	|s^qUt j	|}|t j| | qUq|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|jf|j|jdqS ) )r   r   r   )r   joinr   r   r   ).0Zsub_endpointZrouter   r   
<listcomp>5   s    z5BaseSchemaGenerator.get_endpoints.<locals>.<listcomp>GETHEAD)getpostputpatchdeleteoptions)r   r   r"   get_endpointsextendr   Zinclude_in_schemainspect
isfunctionendpointismethodmethodsappendr   r   lowerhasattrgetattr)r   r"   endpoints_infoZsub_endpointsmethodr   r   r)   r   r3   #   s6   


z!BaseSchemaGenerator.get_endpointsfunc_or_methodc                 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   r@   	docstringparsedr   r   r   parse_docstringT   s   

z#BaseSchemaGenerator.parse_docstringrequestc                 C   s   |j j}| j|d}t|S )N)r"   )Zappr"   r%   r   )r   rG   r"   schemar   r   r   r   l   s   z#BaseSchemaGenerator.OpenAPIResponseN)r   r   r   r   Listr   r   r%   r   r3   r    rF   r   r   r   r   r   r   r   r!      s    
1r!   c                   @   s6   e Zd ZdeddfddZdeje defddZdS )	SchemaGeneratorbase_schemar	   Nc                 C   s
   || _ d S r#   )rK   )r   rK   r   r   r   __init__s   s   
zSchemaGenerator.__init__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   rK   
setdefaultr3   rF   r   r   r   )r   r"   rH   r>   r7   rE   r   r   r   r%   v   s   

zSchemaGenerator.get_schema)	r   r   r   r   rL   r   rI   r   r%   r   r   r   r   rJ   r   s    rJ   )r5   r   Zstarlette.requestsr   Zstarlette.responsesr   Zstarlette.routingr   r   r   r
   ImportErrorr   
NamedTupler   r!   rJ   r   r   r   r   <module>   s    S