o
    An0j6                     @  s   d Z 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 eeZi Zdd Zd	d
 Zg dZedddddZdddddZd ddZdd Zdd ZdS )!zNContains methods that generate documentation for Gradio functions and classes.    )annotationsN)defaultdict)Callable)	lru_cachec                 C  s   dS )zRA no-op for backwards compatibility of custom components published prior to 4.16.0N )mr   r   b/home/wildlama/miniconda3/envs/lam_a2e/lib/python3.10/site-packages/gradio_client/documentation.pyset_documentation_group   s   r	   c           
      C  s   t | j}dd |dD }d }t|D ]\}}|d| d s,|d| d r. nq|d u r>td| d| j |d	|}|d	|d
 }t	|d
 |D ]}|| drrt
d| d| j d||  d ||  qSd||d
 | }	|	S )Nc                 S  s   g | ]}|  qS r   )strip).0liner   r   r   
<listcomp>   s    z-extract_instance_attr_doc.<locals>.<listcomp>
zself.:z =zCould not find z in z"""   z-Found another attribute before docstring for : z
 start: )inspect	getsource__init__split	enumerate
startswith	NameError__name__indexrange
ValueErrorjoin)
clsattrcodelinesir   
start_lineend_linejZ
doc_stringr   r   r   extract_instance_attr_doc   s6   
r'   ))zgradio._simple_templates	component)zgradio.blockblock)zgradio.chatZchatinterface)zgradio.componentr(   )zgradio.eventshelpers)zgradio.data_classesr*   )zgradio.exceptionsr*   )zgradio.externalr*   )zgradio.flagZflagging)zgradio.helpersr*   )zgradio.interfaceZ	interface)zgradio.layoutZlayout)zgradio.routeZroutes)zgradio.themethemes)gradio_client.z	py-client)zgradio.utilsr*   )zgradio.renderable
renderable
   )maxsizereturnstrc                 C  s.   t D ]\}}| |r|  S qtd| )Nz(No known documentation group for module )_module_prefixesr   r   )modnameprefixgroupr   r   r   _get_module_documentation_groupC   s
   
r6   F)inheritdocumentation_groupc                   s   |  fdd}|S )a  
    Defines the @document decorator which adds classes or functions to the Gradio
    documentation at www.gradio.app/docs.

    Usage examples:
    - Put @document() above a class to document the class and its constructor.
    - Put @document("fn1", "fn2") above a class to also document methods fn1 and fn2.
    - Put @document("*fn3") with an asterisk above a class to document the instance attribute methods f3.
    c              
     s   t }t| dr|| j7 }rd t| <  } d u rPzt| j}|ds+|dr0t|}n	 W n t	yO } zt
d|  d|  W Y d }~nd }~ww t| | |f | S )NEVENTSzgradio.r,   z&Could not get documentation group for r   )listhasattrr9   classes_inherit_documentationr   	getmoduler   r   r6   	Exceptionwarningswarnclasses_to_documentappend)r   Z	functionsr8   r3   excZ_documentation_groupfnsr7   r   r   	inner_docW   s*   


"zdocument.<locals>.inner_docr   )r7   r8   rE   rF   r   rD   r   documentK   s   
rG   fnr   (tuple[str, list[dict], dict, str | None]c              
   C  s>  t | pd}|d}t | }g i g g f\}}}}d}	|D ]}
|
 }
|
dkr,d}	q|
drOd}	d|
v rNd	|
v rN|
dd
 d	d }||jkrNd}	q|
dkrVd}	q|	dkrf||
 rb|
nd q|
dsu|
 dksut	|
 |
ds|
 dkst
d| j d|
 |
dd }
|	dkr|
d}|dk rt
d| j d|
 |
d| }|
|d d }|||< q|	dkr||
 q|	dkr||
 qd|}g }|j D ]v\}}|drq|dkrq|dv r||vrq||j||d}||v r||= |jt jjkr0|j}t|trd| d }|jjdkr+|jj d}||d < n|d! durMd"|d! v rBd#|d"< d$|d! v rMd#|d$< || q|rotd| j d%d&|  d'd&|j  t|dkryi }nt|d
kr|j|d d(}ni }t|dkrd|nd}||||fS ))a  
    Generates documentation for any function.
    Parameters:
        fn: Function to document
    Returns:
        description: General description of fn
        parameters: A list of dicts for each parameter, storing data for the parameter name, annotation and doc
        return: A dict storing data for the returned annotation and doc
        example: Code for an example use of the fn
     r   descriptionzParameters:	parameterzExample:example()r   r   ignorezReturns:r0   <br>    Documentation format for  has format error in line:    Nr      r   _self)kwargsargs)name
annotationdoc"builtinsz()defaultr^   rZ   Tr[   z# documents nonexistent parameters: z, z. Valid parameters: )r]   r^   )r   getdocr   	signaturerstripr   r   rB   r
   printSyntaxErrorr   r   
parametersitemsr]   getra   	Parameterempty
isinstancer1   	__class__
__module__r   keyslenreturn_annotation)rH   r   doc_strZ	doc_linesrc   rK   rg   returnsZexamplesmoder   cZcolon_indexrL   parameter_docdescription_docparameter_docs
param_nameparamra   return_docsexamples_docr   r   r   document_fnq   s   











r}   c                 C  s  t | }|d u rdi dfS i }g }d}|dD ]u}| }|dr5d|vr5|d d  }g ||< q|dd dr_|ds_|d |d  }||dd	 d  }|||< q|dkro||	 rk|nd
 q|ds|	 rt
d| j d| || |dd   qd|v rd|d }|d= nd }| D ]\}	}
t|
trd
|
||	< qd|dd
}|||fS )NrJ   rK   r   r   r   rV   r   rR   rW   rQ   rS   rT   rU   rM   )r   rb   r   rd   endswithlowerr   r   rB   r
   rf   r   r   rh   rl   r:   replace)r   rr   tagsZdescription_linesrt   r   tagvaluerM   keyvalrK   r   r   r   document_cls   s@   





r   c                  C  s  i } t  D ]\}}g | |< |D ]\}}t|st|r!|n|j}t||\}}}}t|dr`t	|j
r`t|dr`t	|jr`t|j
|}	t|j|}
|	d |	d d|
d |
d d}	}
t|\}}}||j|||||g d}t|drt	|j
rt|drt	|jr|	|d< |
|d< |D ]d}|d}|r|dd  }t| |j}nt||}t	|st|}i }i }d}d	|j d
| }nt||\}}}}|t|dg v r|dd  }d }|rt||}|d |||i ||||d q| | | |tv r|d t|< qqt  D ]W\}}t|D ]M\}\}}t D ]A\}}t|r`t||r`||kr`|D ](}t|}zt||d |d< W n
 tyR   Y nw | | | d | q7q qq| S )N
preprocesspostprocessr   rW   )rv   
return_doc)classr\   rK   r   rg   rs   rM   rE   *rJ   zgr..r9   rE   )rH   r\   rK   r   rg   rs   rM   override_signaturer\   rK   )rA   rh   r   
isfunctiondataclassesis_dataclassr   r}   r;   callabler   r   r   r   r   getattr__call__r1   r'   rB   r<   r   isclass
issubclassdictr   )Zdocumentationrt   Z
class_listr   rE   Zfn_to_documentrX   rv   r   Zpreprocess_docZpostprocess_docZcls_descriptionZcls_tagsZcls_exampleZcls_documentationfn_nameZinstance_attribute_fnrH   rw   rx   r{   r|   r   r#   Zsuper_classZinherited_fnr   r   r   generate_documentation   s   




V
r   )r0   r1   )rH   r   r0   rI   )__doc__
__future__r   r   r   r?   collectionsr   collections.abcr   	functoolsr   r:   rA   r<   r	   r'   r2   r6   rG   r}   r   r   r   r   r   r   <module>   s&    
&b$