a
    ÷£Be”*  ã                   @   s   d dl Z d dl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 d dlmZ h d£Zd	Zd4dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd5dd„Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd6d$d%„ZeeƒZd&d'„ Zd(d)„ Zd*d+„ Z d,d-„ Z!d.d/„ Z"e"d0ƒZ#e"d1ƒZ$e"d2d3ƒZ%dS )7é    N)Úliteral_eval)Úcleandoc©ÚWeakKeyDictionary)Útree)Úparser_cache)Úsplit_lines>   Z
arith_exprZand_exprÚclassdefZ
shift_exprZor_testZ
comparisonZxor_exprÚimport_nameÚexprZnot_testÚtermÚfactorÚpowerZand_testZimport_fromÚtestÚ	atom_exprÚatomÚfuncdef)	ÚtryÚexceptÚfinallyÚelseÚifÚelifÚwithÚforÚwhileFc                 C   sú   g }| j }|dkrB|  ¡ }|du rö| jj dkrö|dkrö| | ¡ n´|dkrr| | ¡ | jD ]}|t|dd�7 }qZn„|dkr¨| jd	 d
krö| jd } | dkrö|t| ƒ7 }nNz
| j}W n tyÄ   Y n20 | j tv rÞ|sÞ| | ¡ |D ]}|t||ƒ7 }qâ|S )z
    For static analysis.
    ÚnameFÚparamú=Ú	expr_stmtT)Ú
last_addedÚ	decoratoréþÿÿÿú)éýÿÿÿú()ÚtypeÚget_next_leafÚparentÚappendÚchildrenÚget_executable_nodesÚAttributeErrorÚ_EXECUTE_NODES)Únoder    ÚresultÚtypZ	next_leafÚchildr*   © r2   úT/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jedi/parser_utils.pyr+      s0    




r+   c                 c   sV   | V  | j d }|jdkr(|j d V  n|jdkr:|V  n|jdksFqR|j d }qd S )NéÿÿÿÿÚcomp_foré   Úsync_comp_forZcomp_if©r*   r&   )r5   Úlastr2   r2   r3   Úget_sync_comp_fors:   s    



r:   c                 C   s   | j d jdkS )z¢
    Returns True if only one name is returned: ``for x in y``.
    Returns False if the for loop is more complicated: ``for x, z in y``.

    :returns: bool
    r6   r   r8   )Úfor_stmtr2   r2   r3   Úfor_stmt_defines_one_nameG   s    r<   c                 C   sj   |j }| j |  k r| jks(n tdƒ‚d }t| jƒD ].\}}||j k rP|  S | ¡ }|tv r6|}q6d S )Nz!The node is not part of the flow.)Ú	start_posÚend_posÚ
ValueErrorÚ	enumerater*   Úget_first_leafÚ_FLOW_KEYWORDS)Z	flow_noder.   r=   ÚkeywordÚir1   Z
first_leafr2   r2   r3   Úget_flow_branch_keywordQ   s    
rE   c                 C   s"   |   ¡ }|durtt|jƒƒS dS )z3 Returns a cleaned version of the docstring token. NÚ )Zget_doc_noder   Úsafe_literal_evalÚvalue)Z
scope_noder.   r2   r2   r3   Úclean_scope_docstring`   s    rI   c                 C   sP   | j dkrL| j} |  ¡ }|d urL|j dkrL|jd }|j dkrLtt|jƒƒS dS )Nr   Zsimple_stmtr   ÚstringrF   )r&   r(   Zget_next_siblingr*   r   rG   rH   )Z	tree_nodeZmaybe_stringr2   r2   r3   Úfind_statement_documentationl   s    



rK   c                 C   s0   | d d…   ¡ }|d dks$|dv r(dS t| ƒS )Né   r   Úf)ÚfrÚrfrF   )Úlowerr   )rH   Z	first_twor2   r2   r3   rG   x   s    rG   éH   c           	      C   s¢   |du r | j dkrd}n| jj}|  ¡ }|r8|dd… }dd dd„ |D ƒ¡ ¡  d	 }t d
d|¡}| jr€|s€d| j 	¡  }nd}|| | }d t
 ||¡¡S )zí
    Generate a string signature of a function.

    :param width: Fold lines if a line is longer than this value.
    :type width: int
    :arg func_name: Override function name when given.
    :type func_name: str

    :rtype: str
    NÚlambdefz<lambda>r6   r%   rF   c                 s   s   | ]}|  ¡ V  qd S ©N)Úget_code)Ú.0r   r2   r2   r3   Ú	<genexpr>—   ó    z get_signature.<locals>.<genexpr>r#   z\s+ú z ->Ú
)r&   r   rH   Ú
get_paramsÚjoinÚstripÚreÚsubÚ
annotationrT   ÚtextwrapÚwrap)	r   ÚwidthZcall_stringZomit_first_paramZomit_return_annotationÚparamsÚpZrtypeÚcoder2   r2   r3   Úget_signature‚   s    
 
rf   c                 C   sD   z
| j }W n  ty*   |  j|7  _Y n0 |D ]}t||ƒ q0dS )z$
    Move the `Node` start_pos.
    N)r*   r,   ÚlineÚmove)r.   Úline_offsetr*   Úcr2   r2   r3   rh   £   s    
rh   c                 C   sè   zj| j dkr| jd  ¡ j}nJ| j dkr:| jd  ¡ j}n.| j dkrZ| jd  ¡  ¡ j}n|  ¡  ¡ j}W n& ty~   Y dS  ty�   Y dS 0 d|vrždS || d¡d… }d	|v rÊ|d| d	¡… }d
|v rä|d| d
¡… }|S )zl
    returns (as string) any comment that appears on the same line,
    after the node, including the #
    r;   é   Z	with_stmté   r   é   Nú#úrY   )	r&   r*   rA   Úprefixr'   Zget_last_leafr,   r?   Úindex)r.   Ú
whitespaceÚcommentr2   r2   r3   Úget_following_comment_same_line°   s(    


rt   c                 C   s&   | j }|dkr| jd j dkS |dv S )Nr5   r6   r7   )Z
file_inputr	   r   rR   r7   )r&   r*   )r.   Útr2   r2   r3   Úis_scopeÏ   s    rv   c                    s   t ƒ ‰ d‡ ‡fdd„	}|S )NFc                    sv   | d u rˆ||ƒS zˆ |  }W n t y<   i  }ˆ | < Y n0 z
|| W S  t yp   ˆ||ƒ }||< | Y S 0 d S rS   )ÚKeyError)Zparso_cache_noder.   Úinclude_flowsZ
for_moduler/   ©ÚcacheÚfuncr2   r3   ÚwrapperÛ   s    

z(_get_parent_scope_cache.<locals>.wrapper)Fr   )r{   r|   r2   ry   r3   Ú_get_parent_scope_cacheØ   s    r}   c                    sÊ   ˆ j }|du rdS t|ƒr†|jdv r‚|j d¡}|j| jˆ jkr‚ˆ j jdkr\ˆ j jˆ kr\n&ˆ j jdkrzˆ j jd ˆ krzn|j }q|S |r¾t|tj	ƒr¾|jdkrºt
‡ fdd	„| ¡ D ƒƒs¾|S |j }qdS )
z'
    Returns the underlying scope.
    N)r	   r   rR   ú:r   Ztfpdefr   Zif_stmtc                 3   s,   | ]$}|j ˆ j   ko|jk n  V  qd S rS   )r=   r>   )rU   Ún©r.   r2   r3   rV     s   ÿz#get_parent_scope.<locals>.<genexpr>)r(   rv   r&   r*   rq   r=   r   Ú
isinstancer   ZFlowÚanyZget_test_nodes)r.   rx   Úscoperq   r2   r€   r3   Úget_parent_scopeì   s,    

ÿÿr„   c                 C   s   t | |ƒjS )z‘
    Basically access the cached code lines in parso. This is not the nicest way
    to do this, but we avoid splitting all the lines again.
    )Úget_parso_cache_nodeÚlines©ZgrammarÚpathr2   r2   r3   Úget_cached_code_lines  s    r‰   c                 C   s   t | j | S )zß
    This is of course not public. But as long as I control parso, this
    shouldn't be a problem. ~ Dave

    The reason for this is mostly caching. This is obviously also a sign of a
    broken caching architecture.
    )r   Z_hashedr‡   r2   r2   r3   r…     s    r…   c                 C   sj   t | jdd�d|d | j d … }|d }| j|d krD|| j8 }|sLdS |d d|… |d< d |¡S )z3
    Cuts of the value of the leaf at position
    T)ÚkeependsNr   r6   rF   r4   )r   rH   rg   Úcolumnr[   )ÚleafÚpositionr†   r‹   r2   r2   r3   Úcut_value_at_position"  s    $
rŽ   c                 C   sŒ   | j dkr8t| jƒdkr4| jd dkr4t| jd ƒS dS | j dkr‚| j}|d dkrXdS t|d ƒshdS td	d
„ |dd… D ƒƒS | j dkS )zP
    Checks if a path looks like `name` or `name.foo.bar` and not `name()`.
    r   rl   r   r%   r6   Fr   Úawaitc                 s   s   | ]}|j d  dkV  qdS )r   Ú.N)r*   )rU   rj   r2   r2   r3   rV   ?  rW   z!expr_is_dotted.<locals>.<genexpr>Nr   )r&   Úlenr*   Úexpr_is_dottedÚall)r.   r*   r2   r2   r3   r’   0  s    

r’   c                     s   ‡ fdd„}|S )Nc                    s.   |   ¡ D ] }|jd }| ¡ ˆ v r dS qdS )zý
        This is a heuristic. It will not hold ALL the times, but it will be
        correct pretty much for anyone that doesn't try to beat it.
        staticmethod/classmethod are builtins and unless overwritten, this will
        be correct.
        r6   TF)Zget_decoratorsr*   rT   )Zfunction_noder!   Zdotted_name©Úmethod_namesr2   r3   r|   D  s
    
z&_function_is_x_method.<locals>.wrapperr2   )r•   r|   r2   r”   r3   Ú_function_is_x_methodC  s    r–   ÚstaticmethodÚclassmethodÚpropertyÚcached_property)F)rQ   NFF)F)&r]   r`   Úastr   Úinspectr   Úweakrefr   Zparso.pythonr   Zparso.cacher   Zparsor   r-   rB   r+   r:   r<   rE   rI   rK   rG   rf   rh   rt   rv   r}   r„   Zget_cached_parent_scoper‰   r…   rŽ   r’   r–   Zfunction_is_staticmethodZfunction_is_classmethodZfunction_is_propertyr2   r2   r2   r3   Ú<module>   sB   
%

  ÿ
!	
 