o
    ì6Wj  ã                	   @   sì   d Z ddlZddlZddlZddlZddlmZ ddlm	Z	m
Z
 dejdejfdd	„Zdejdeje fd
d„Zdejdefdd„Zdejdejejeeje eje f  fdd„ZG dd„ dejƒZdejeef de	ddfdd„ZdS )zbAttribute docstrings parsing.

.. seealso:: https://peps.python.org/pep-0257/#what-is-a-docstring
é    N)Ú
ModuleTypeé   )Ú	DocstringÚDocstringParamÚnodeÚreturnc                 C   s
   t | dƒS )z<Return the constant's value if the given node is a constant.Úvalue)Úgetattr©r   © r   úg/home/esfera/Documents/content_generation/venv/lib/python3.10/site-packages/docstring_parser/attrdoc.pyÚast_get_constant_value   s   
r   c                 C   sB   t tdƒr
t | ¡S t| tjƒrtt| ƒƒS t| tjƒr| jS dS )z0Convert the AST node to source code as a string.ÚunparseN)	ÚhasattrÚastr   Ú
isinstanceÚConstantÚstrr   ÚNameÚidr
   r   r   r   Úast_unparse   s   

r   c                 C   s*   t | tjƒot | jtjƒot t| jƒtƒS )z2Return True if the given node is a literal string.)r   r   ÚExprr   r   r   r   r
   r   r   r   Úast_is_literal_str    s
   ÿýr   c                 C   sx   t | tjtjfƒr:t | tjƒr| jd n| j}t |tjƒr:d}t | tjƒr*t| jƒ}d}| j	r4t| j	ƒ}|j
||fS dS )z@Return name, type and default if the given node is an attribute.r   N)r   r   ÚAssignÚ	AnnAssignÚtargetsÚtargetr   r   Ú
annotationr   r   )r   r   Útype_strÚdefaultr   r   r   Úast_get_attribute)   s   ÿ

r    c                
   @   sV   e Zd ZdZdZdZdd„ Zdejdej	e
eje
eje
 eje
 f f fdd„ZdS )	ÚAttributeDocstringsz6An ast.NodeVisitor that collects attribute docstrings.Nc                 C   s^   | j rt|ƒr| j \}}}t|jƒ||f| j|< t|ƒ| _ t|tjtj	fƒr-|  
|¡ d S d S ©N)Ú	prev_attrr   r   r   Ú	attr_docsr    r   r   ÚClassDefÚModuleÚgeneric_visit)Úselfr   Ú	attr_nameÚ	attr_typeÚattr_defaultr   r   r   ÚvisitB   s   
ý
ÿzAttributeDocstrings.visitÚ	componentr   c                 C   s�   i | _ d| _z
t t |¡¡}W n ty   Y | j S w t |¡}t 	|¡r.|  
|¡ | j S t|tjƒrEt|jd tjƒrE|  
|jd ¡ | j S )zãGet attribute docstrings from the given component.

        :param component: component to process (class or module)
        :returns: for each attribute docstring, a tuple with (description,
            type, default)
        Nr   )r$   r#   ÚtextwrapÚdedentÚinspectÚ	getsourceÚOSErrorr   ÚparseÚismoduler,   r   r&   Úbodyr%   )r(   r-   ÚsourceÚtreer   r   r   Úget_attr_docsN   s"   		ö


üÿz!AttributeDocstrings.get_attr_docs)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r$   r#   r,   ÚTÚAnyÚDictr   ÚTupleÚOptionalr8   r   r   r   r   r!   <   s    ÿ$þr!   ÚobjÚ	docstringc              	   C   sh   t dd„ |jD ƒƒ}tƒ  | ¡ ¡ D ]\}\}}}||vr1td|g||||du|d�}|j |¡ qdS )zûAdd attribute docstrings found in the object's source code.

    :param obj: object from which to parse attribute docstrings
    :param docstring: Docstring object where found attributes are added
    :returns: list with names of added attributes
    c                 s   s   � | ]}|j V  qd S r"   )Úarg_name)Ú.0Úpr   r   r   Ú	<genexpr>q   s   € z+add_attribute_docstrings.<locals>.<genexpr>Ú	attributeN)ÚargsÚdescriptionrD   Ú	type_nameÚis_optionalr   )ÚsetÚparamsr!   r8   Úitemsr   ÚmetaÚappend)rB   rC   rN   rD   rJ   rK   r   Úparamr   r   r   Úadd_attribute_docstringsh   s   	ÿú€ôrS   )r<   r   r0   r.   Útypingr=   Útypesr   Úcommonr   r   ÚASTr>   r   rA   r   r   Úboolr   r@   r    ÚNodeVisitorr!   ÚUnionÚtyperS   r   r   r   r   Ú<module>   s.    	ÿ 
þ,ÿÿþ