o
    w!gc                     @  s  d dl mZ d dlZd dlmZ d dlmZmZ d dlm	Z	m
Z
 d dlmZmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZmZ d dlmZ d dlmZmZmZ d dl m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& d dl'm(Z(m)Z) d dl*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 erd dl4m5Z5 d dl6m7Z7 d dlm8Z8 ddddZ9	 dhdhddhddhdhdhdZ:	 ddhZ;	 dddddddd6d2d3Z<G d4d5 d5Z=dS )7    )annotationsN)suppress)TYPE_CHECKINGAny)get_instance_names	get_names)ast_childrenast_kindast_next)get_docstring)safe_get__all__)relative_to_absolute)get_parameters)LinesCollectionModulesCollection)Kind)AliasResolutionErrorCyclicAliasErrorLastNodeError)ExprExprNamesafe_get_annotationsafe_get_base_classsafe_get_conditionsafe_get_expression)
Extensionsload_extensions)	Alias	AttributeClass	Decorator	DocstringFunctionModule	Parameter
Parameters)Path)DocstringStyle)Parserpropertystaticmethodclassmethod)r)   r*   r+   abstractmethodcached	dataclass)zabc.abstractmethodzfunctools.cachezfunctools.cached_propertyzcached_property.cached_propertyzfunctools.lru_cachezdataclasses.dataclassztyping.overloadztyping_extensions.overload)
extensionsparentdocstring_parserdocstring_optionslines_collectionmodules_collectionmodule_namestrfilepathr&   coder/   Extensions | Noner0   Module | Noner1   DocstringStyle | Parser | Noner2   dict[str, Any] | Noner3   LinesCollection | Noner4   ModulesCollection | Nonereturnr#   c          	      C  s$   t | |||pt |||||d	 S )a  Parse and visit a module file.

    We provide this function for static analysis. It uses a [`NodeVisitor`][ast.NodeVisitor]-like class,
    the [`Visitor`][griffe.Visitor], to compile and parse code (using [`compile`][])
    then visit the resulting AST (Abstract Syntax Tree).

    Important:
        This function is generally not used directly.
        In most cases, users can rely on the [`GriffeLoader`][griffe.GriffeLoader]
        and its accompanying [`load`][griffe.load] shortcut and their respective options
        to load modules using static analysis.

    Parameters:
        module_name: The module name (as when importing [from] it).
        filepath: The module file path.
        code: The module contents.
        extensions: The extensions to use when visiting the AST.
        parent: The optional parent of this module.
        docstring_parser: The docstring parser to use. By default, no parsing is done.
        docstring_options: Additional docstring parsing options.
        lines_collection: A collection of source code lines.
        modules_collection: A collection of modules.

    Returns:
        The module, with its members populated.
    )r1   r2   r3   r4   )Visitorr   
get_module)	r5   r7   r8   r/   r0   r1   r2   r3   r4    rB   X/mnt/skqttb/ctump_chatbot/chatbot/lib/python3.10/site-packages/_griffe/agents/visitor.pyvisitA   s   &
rD   c                      s   e Zd ZdZ					dZd[ fddZddd\dd Zd]d"d#Zd^d$d%Zd^d&d'Zd_d)d*Z	d`d,d-Z
dad1d2Zdbd6d7Zdcddd;d<Zded>d?ZdfdAdBZdgdDdEZdhdGdHZ	dcdidLdMZdjdOdPZdkdRdSZdldUdVZdmdXdYZ  ZS )nr@   zoThis class is used to instantiate a visitor.

    Visitors iterate on AST nodes to extract data from them.
    Nr5   r6   r7   r&   r8   r/   r   r0   r:   r1   r;   r2   r<   r3   r=   r4   r>   r?   Nonec
           
        st   t    || _	 || _	 || _	 || _	 || _	 d| _	 || _	 |p$i | _		 |p+t
 | _	 |	p2t | _	 d| _dS )a(  Initialize the visitor.

        Parameters:
            module_name: The module name.
            filepath: The module filepath.
            code: The module source code.
            extensions: The extensions to use when visiting.
            parent: An optional parent for the final module object.
            docstring_parser: The docstring parser to use.
            docstring_options: The docstring parsing options.
            lines_collection: A collection of source code lines.
            modules_collection: A collection of modules.
        NF)super__init__r5   r7   r8   r/   r0   currentr1   r2   r   r3   r   r4   type_guarded)
selfr5   r7   r8   r/   r0   r1   r2   r3   r4   	__class__rB   rC   rG   z   s.   

zVisitor.__init__Fstrictnodeast.ASTrN   boolDocstring | Nonec                C  s4   t ||d\}}}|d u rd S t|||| j| jdS )NrM   )lineno	endlinenoparserparser_options)r   r!   r1   r2   )rJ   rO   rN   valuerS   rT   rB   rB   rC   _get_docstring   s   zVisitor._get_docstringr#   c                 C  s.   t | jdt| jtjdd}| | | jjS )zBuild and return the object representing the module attached to this visitor.

        This method triggers a complete visit of the module nodes.

        Returns:
            A module instance.
        exec   )modefilenameflagsoptimize)	compiler8   r6   r7   astPyCF_ONLY_ASTrD   rH   module)rJ   top_noderB   rB   rC   rA      s   

zVisitor.get_modulec                 C  s    t | dt| | j| dS )ziExtend the base visit with extensions.

        Parameters:
            node: The node to visit.
        visit_N)getattrr	   generic_visitrJ   rO   rB   rB   rC   rD      s    zVisitor.visitc                 C  s   t |D ]}| | qdS )zqExtend the base generic visit with extensions.

        Parameters:
            node: The node to visit.
        N)r   rD   )rJ   rO   childrB   rB   rC   rf      s   zVisitor.generic_visit
ast.Modulec                 C  s   | j jd|| d | j jd|| d t| j| j| j| || j| jd | _	}| j jd||| d | j jd||| d | 
| | j jd	||| d | j jd
||| d dS )zWVisit a module node.

        Parameters:
            node: The node to visit.
        on_noderO   agenton_module_node)namer7   r0   	docstringr3   r4   on_instancerO   objrl   on_module_instance)rO   modrl   
on_memberson_module_membersN)r/   callr#   r5   r7   r0   rX   r3   r4   rH   rf   )rJ   rO   rb   rB   rB   rC   visit_module   s   
zVisitor.visit_moduleast.ClassDefc              	     s&   j jd| d  j jd| d g }|jr+|jd j}| fdd|jD  n|j} fdd|jD }t|j||j 	||| j
 d	}| j |O  _ j|j| | _ j jd
|| d  j jd|| d  |  j jd|| d  j jd|| d  jj _dS )zaVisit a class definition node.

        Parameters:
            node: The node to visit.
        rj   rk   on_class_noder   c                 3  s.    | ]}t t| jd d|j|jdV  qdS )Fr0   parse_stringsrS   rT   N)r    r   rH   rS   
end_lineno).0decorator_noderJ   rB   rC   	<genexpr>  s    
z)Visitor.visit_classdef.<locals>.<genexpr>c                   s   g | ]	}t | jd qS r0   )r   rH   )r   baser   rB   rC   
<listcomp>  s    z*Visitor.visit_classdef.<locals>.<listcomp>)rn   rS   rT   ro   
decoratorsbasesruntimerp   rq   on_class_instance)rO   clsrl   ru   on_class_membersN)r/   rw   decorator_listrS   extendr   r   rn   r~   rX   rI   labelsdecorators_to_labelsrH   
set_memberrf   r0   )rJ   rO   r   rS   r   class_rB   r   rC   visit_classdef   s8   
		
zVisitor.visit_classdefr   list[Decorator]set[str]c                 C  sF   t  }|D ]}|j}|tv r|t|  q|tv r |t| O }q|S )zBuild and return a set of labels based on decorators.

        Parameters:
            decorators: The decorators to check.

        Returns:
            A set of labels.
        )setcallable_pathbuiltin_decoratorsaddstdlib_decorators)rJ   r   r   	decoratorr   rB   rB   rC   r   %  s   	zVisitor.decorators_to_labelsfunctionr"   
str | Nonec              	   C  sj   |D ]0}z|j dd\}}W n	 ty   Y qw |dv o+||jko+| j|jd}|r2|  S qdS )a<  Check decorators to return the base property in case of setters and deleters.

        Parameters:
            decorators: The decorators to check.

        Returns:
            base_property: The property for which the setter/deleted is set.
            property_function: Either `"setter"` or `"deleter"`.
        .rZ   >   setterdeleterr)   N)r   rsplit
ValueErrorpathrH   
get_memberrn   
has_labels)rJ   r   r   r   r   prop_functionproperty_setter_or_deleterrB   rB   rC   get_base_property7  s   
zVisitor.get_base_property&ast.AsyncFunctionDef | ast.FunctionDefr   
set | Nonec                   s   j jd| d  j jd| d |pt }g }d}|jrM|jd j}|jD ]$}t| jdd}|du r6q't||j|jd}|	| ||j
tv O }q'n|j}| |O }d	|v rt|jdt|j jd
|j|j | j d}	|	 j|O  _ j|j|	  j jd||	 d  j jd||	 d dS t fddt|jD  }
t|j||j|
t|j jd
| | j  jd	} ||}|rՈ jj|j 	| nN|r jj|j }|dkr||_|jd n7|dkr||_|jd n) j|j|  jjt j!t j"hv r# jj|j r# jj|j |_ jj|j= | j|O  _ j jd|| d  j jd|| d  jjt j"u r[|jdkr]| _ #|  jj$ _dS dS dS )zHandle a function definition node.

        Parameters:
            node: The node to visit.
            labels: Labels to add to the data object.
        rj   rk   on_function_nodeFr   r{   Nr}   r)   r   rn   rW   
annotationrS   rT   ro   r   rp   rq   on_attribute_instancerO   attrrl   c                   sH   g | ] \}}}}t ||t| jd t|tr|nt| jdddqS )r   Fr{   )kindr   default)r$   r   rH   
isinstancer6   r   )r   rn   r   r   r   r   rB   rC   r     s    
	z+Visitor.handle_function.<locals>.<listcomp>)	rn   rS   rT   
parametersreturnsr   ro   r   r0   r   writabler   	deletableon_function_instance)rO   funcrl   rG   )%r/   rw   r   r   rS   r   rH   r    r~   appendr   typing_overloadr   r   rn   r   r   rX   rI   r   r   r%   r   argsr"   r   	overloadsmembersr   r   r   r   r   MODULECLASSrf   r0   )rJ   rO   r   r   overloadrS   r   decorator_valuer   	attributer   r   property_functionbase_propertyrB   r   rC   handle_functionO  s   


	
	&
zVisitor.handle_functionast.FunctionDefc                 C     |  | dS )zdVisit a function definition node.

        Parameters:
            node: The node to visit.
        Nr   rg   rB   rB   rC   visit_functiondef     zVisitor.visit_functiondefast.AsyncFunctionDefc                 C  s   | j |dhd dS )zkVisit an async function definition node.

        Parameters:
            node: The node to visit.
        async)r   Nr   rg   rB   rB   rC   visit_asyncfunctiondef  s   zVisitor.visit_asyncfunctiondef
ast.Importc                 C  s   |j D ]@}|jr|jn|jddd }|jp|ddd }|| jj|< t|||j|j| j	 d}| j
|| | jjd||| d qdS )zXVisit an import node.

        Parameters:
            node: The node to visit.
        r   rZ   r   rS   rT   r   on_aliasaliasrO   rl   N)namesasnamern   splitrH   importsr   rS   r~   rI   r   r/   rw   rJ   rO   rn   
alias_path
alias_namer   rB   rB   rC   visit_import  s   
zVisitor.visit_importast.ImportFromc                 C  s   |j D ]b}|js|jdkr|js| jjjrqt||| jj}|jdkr0|dd}|dd}n|jp5|j}|| jj	|< || jj
 d| kret|||j|j| j d}| j|| | jjd||| d	 qd
S )z_Visit an "import from" node.

        Parameters:
            node: The node to visit.
        rZ   *r   /z.* r   r   r   N)r   rb   levelr   rH   is_init_moduler   rn   replacer   r   r   rS   r~   rI   r   r/   rw   r   rB   rB   rC   visit_importfrom  s*   
 
zVisitor.visit_importfromast.Assign | ast.AnnAssignr   str | Expr | Nonec              
   C  s  | j jd|| d | j jd|| d | j}t }|jtju r5zt|}W n
 ty.   Y dS w |	d nb|jtj
u rqzt|}W n
 tyK   Y dS w t|tr]|jr]|j}|	d n:|jrk|	d |	d n,|	d n&|jtju r|jdkr~dS zt|}W n
 ty   Y dS w |j}|	d |sdS t|j| jd	d
}z| jt|dd}W n ttfy   d}Y nw |D ]}d|v rq||jv rt|jtjtjfrq|j| }	ttt2 ||	j O }|	j!r|s|	j!}tt |	j"r|s|	j"}W d   n	1 sw   Y  W d   n	1 sw   Y  t#||||j$|j%|| j& d}
|
 j |O  _ |'||
 |dkr^tt dd t(|| jD |_)W d   n	1 sYw   Y  | j jd||
| d | j jd||
| d qdS )zHandle an attribute (assignment) node.

        Parameters:
            node: The node to visit.
            annotation: A potential annotation.
        rj   rk   on_attribute_nodeNzmodule-attributezclass-attributezinstance-attributerG   Fr{   TrM   r   r   __all__c                 S  *   g | ]}t |tr|nt|j|jd qS r   r   r6   r   rn   r0   r   rn   rB   rB   rC   r   ]      z,Visitor.handle_attribute.<locals>.<listcomp>rp   rq   r   r   )*r/   rw   rH   r   r   r   r   r   KeyErrorr   r   r   r   is_classvarslicerW   FUNCTIONrn   r   r0   r   rX   r
   r   AttributeErrorr   r`   IfExceptHandlerr   r   r   r   ro   r   r   rS   r~   rI   r   r   exports)rJ   rO   r   r0   r   r   rW   ro   rn   existing_memberr   rB   rB   rC   handle_attribute  s   







		



zVisitor.handle_attribute
ast.Assignc                 C  r   )z\Visit an assignment node.

        Parameters:
            node: The node to visit.
        N)r   rg   rB   rB   rC   visit_assignd  r   zVisitor.visit_assignast.AnnAssignc                 C  s   |  |t|j| jd dS )zfVisit an annotated assignment node.

        Parameters:
            node: The node to visit.
        r   N)r   r   r   rH   rg   rB   rB   rC   visit_annassignl  s   zVisitor.visit_annassignast.AugAssignc                 C  s   t t4 |jjdko| jjot|jtj	}|r0| jj
dd t|| jD  W d   dS W d   dS 1 s;w   Y  dS )zfVisit an augmented assignment node.

        Parameters:
            node: The node to visit.
        r   c                 S  r   r   r   r   rB   rB   rC   r     r   z+Visitor.visit_augassign.<locals>.<listcomp>N)r   r   targetidrH   	is_moduler   opr`   Addr   r   r   )rJ   rO   all_augmentrB   rB   rC   visit_augassignt  s   

"zVisitor.visit_augassignast.Ifc                 C  sL   t |jtjtjfrt|j| jdd}t|dv rd| _	| 
| d| _	dS )zVVisit an "if" node.

        Parameters:
            node: The node to visit.
        N)r0   	log_level>   typing.TYPE_CHECKINGr   TF)r   r0   r`   r#   ClassDefr   testrH   r6   rI   rf   )rJ   rO   	conditionrB   rB   rC   visit_if  s   

zVisitor.visit_if)NNNNN)r5   r6   r7   r&   r8   r6   r/   r   r0   r:   r1   r;   r2   r<   r3   r=   r4   r>   r?   rE   )rO   rP   rN   rQ   r?   rR   )r?   r#   )rO   rP   r?   rE   )rO   ri   r?   rE   )rO   ry   r?   rE   )r   r   r?   r   )r   r   r   r"   r?   r   )N)rO   r   r   r   r?   rE   )rO   r   r?   rE   )rO   r   r?   rE   )rO   r   r?   rE   )rO   r   r?   rE   )rO   r   r   r   r?   rE   )rO   r   r?   rE   )rO   r   r?   rE   )rO   r   r?   rE   )rO   r  r?   rE   )__name__
__module____qualname____doc__rG   rX   rA   rD   rf   rx   r   r   r   r   r   r   r   r   r   r   r   r  r  __classcell__rB   rB   rK   rC   r@   t   s6    <



	

.

g


&
g

r@   )r5   r6   r7   r&   r8   r6   r/   r9   r0   r:   r1   r;   r2   r<   r3   r=   r4   r>   r?   r#   )>
__future__r   r`   
contextlibr   typingr   r    _griffe.agents.nodes.assignmentsr   r   _griffe.agents.nodes.astr   r	   r
   _griffe.agents.nodes.docstringsr   _griffe.agents.nodes.exportsr   _griffe.agents.nodes.importsr   _griffe.agents.nodes.parametersr   _griffe.collectionsr   r   _griffe.enumerationsr   _griffe.exceptionsr   r   r   _griffe.expressionsr   r   r   r   r   r   _griffe.extensions.baser   r   _griffe.modelsr   r   r   r    r!   r"   r#   r$   r%   pathlibr&   _griffe.docstrings.parsersr'   r(   r   r   r   rD   r@   rB   rB   rB   rC   <module>   sV    ,3