a
    d(                     @  sr   d Z ddlmZ ddlZddlmZ i Zi Zdadd Z	dd Z
d	d
ddZdddddZdd Zdd ZdS )zNContains methods that generate documentation for Gradio functions and classes.    )annotationsN)Callablec                 C  s   | a | tvrg t| < d S N)documentation_groupclasses_to_document)m r   d/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/gradio_client/documentation.pyset_documentation_group   s    r
   c           
      C  s   t | j}dd |dD }d }t|D ]0\}}|d| d sX|d| d r, q^q,|d us|J d| d| j |d	|}|d	|d
 }t|d
 |D ]>}|| drJ d| d| j d||  d ||  qd	||d
 | }	|	S )Nc                 S  s   g | ]}|  qS r   )strip).0liner   r   r	   
<listcomp>       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__name__indexrangejoin)
clsattrcodelinesir   Z
start_lineZend_linejZ
doc_stringr   r   r	   extract_instance_attr_doc   s.    
r%   F)inheritc                   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"   rd t | < tt |  f | S r   )classes_inherit_documentationr   r   append)r   fnsr&   r   r	   	inner_doc6   s    zdocument.<locals>.inner_docr   )r&   r*   r+   r   r)   r	   document+   s    r,   r   z(tuple[str, list[dict], dict, str | None])fnreturnc                 C  sD  t | pd}|d}t | }g i g g f\}}}}d}	|D ]j}
|
 }
|
dkrZd}	q>|
drd}	d|
v rd	|
v r|
dd
 d	d }||jkrd}	q>|
dkrd}	q>|	dkr||
 r|
nd q>|
ds|
 dkst	|
 |
ds|
 dksJ d| j d|
 |
dd }
|	dkr|

d}|dks\J d| j d|
 |
d| }|
|d d }|||< q>|	dkr||
 q>|	dkr>||
 q>d|}g }|j D ]\}}|drܐq|dv r||vrq||j||d}||v r||= |jt jjkrn|j}t|tkrHd| d }|jjdkrd|jj d}||d< n:|d  durd!|d  v rd"|d!< d#|d  v rd"|d#< || qt|dksJ d| j d$d|  t|dkri }n$t|d
kr|j|d d%}ni }t|dkr4d|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:r.   <br>    Documentation format for  has format error in line:    Nr      r   _)kwargsargs)name
annotationdoc"builtinsz()defaultrB   r>   Tr?   z# documents nonexistent parameters: )rA   rB   )r   getdocr   	signaturerstripr   r   r(   r   printr   r   
parametersitemsrA   getrE   	Parameteremptytypestr	__class__
__module__lenkeysreturn_annotation)r-   r   doc_strZ	doc_linesrG   r0   rJ   returnsZexamplesmoder   cZcolon_indexr1   parameter_docdescription_docparameter_docs
param_nameparamrE   return_docsexamples_docr   r   r	   document_fn@   s    













ra   c                 C  s  t | }|d u rdi dfS i }g }d}|dD ]}| }|drjd|vrj|d d  }g ||< q2|dd dr|ds|d |d  }||dd	 d  }|||< q2|dkr||	 r|nd
 q2|ds
|	 r
J d| j
 d| || |dd   q2d|v rBd|d }|d= nd }| D ]$\}	}
t|
trNd
|
||	< qNd|dd
}|||fS )Nr/   r0   r   r   r   r;   r   r7   r<   r6   r8   r9   r:   r2   )r   rF   r   rH   endswithlowerr   r   r(   r   r   r   rK   
isinstancelistreplace)r   rV   tagsZdescription_linesrX   r   tagvaluer2   keyvalr0   r   r   r	   document_cls   sB    




rl   c                  C  s  i } t  D ]B\}}g | |< |D ]*\}}t|r:|n|j}t||\}}}}t|\}	}
}||j|	|
|||g d}|D ]}|d}|r|dd  }t	| |j
}n
t	||}t|st|}i }i }d}d|j d| }nt||\}}}}d }|r
t||}|d |||i ||||d q|| | | |tv r"|d t|< q"qt  D ]\}}t|D ]\}\}}tD ]}t|rzt||rz||krzt| D ]T}t|}zt||d	 |d
< W n ttfy   Y n0 | | | d | qqzqjqZ| S )N)classr@   r0   rg   rJ   rW   r2   r*   *r   r/   zgr..r*   )r-   r@   r0   rg   rJ   rW   r2   override_signaturer@   r0   )r   rK   r   
isfunctionr   ra   rl   r   r   getattr__call__callablerP   r%   r(   r'   r   isclass
issubclassdict
ValueErrorAssertionError)ZdocumentationrX   Z
class_listr   r*   Zfn_to_documentr=   rZ   Z
return_docZcls_descriptionZcls_tagsZcls_exampleZcls_documentationfn_nameZinstance_attribute_fnr-   r[   r\   r_   r`   rp   r#   Zsuper_classZinherited_fnr   r   r	   generate_documentation   s    



&r{   )__doc__
__future__r   r   typingr   r   r'   r   r
   r%   r,   ra   rl   r{   r   r   r   r	   <module>   s   [$