a
    ÷£Be7  ã                   @   sÐ   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 d dlm	Z	 dd„ Z
dd„ Zd$d
d„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZG dd„ deƒZedd„ ƒZd%dd„Zd d!„ Zd"d#„ ZdS )&é    N)Úchain)Úcontextmanager)Útreec                 C   sJ   | j }d|v sd|v rdS tj tjdd¡}tt t 	|¡d t
| ƒ¡ƒS )Nzdist-packageszsite-packagesFÚlibÚpythonz\d.\d)ÚpartsÚosÚpathÚjoinÚsysÚprefixÚboolÚreÚmatchÚescapeÚstr)r	   r   Ú	base_path© r   úY/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jedi/inference/helpers.pyÚis_stdlib_path   s
    r   c                 C   sZ   t   | ¡}g }| jD ]:}t|tjƒr6t   |¡}||_nt|ƒ}||_| |¡ q||_|S )zO
    Much, much faster than copy.deepcopy, but just for parser tree nodes.
    )ÚcopyÚchildrenÚ
isinstancer   ZLeafÚparentÚdeep_ast_copyÚappend)ÚobjZnew_objZnew_childrenÚchildÚ	new_childr   r   r   r      s    


r   Fc                 C   sN  |j }|jdkr(ddlm} | | j¡S |jdksJ||jd |jd fvr€|dkrbddlm} |S |jdkrv|  	|¡S |  	|¡S |j }|j 
|¡}|rœ|}n|d	 }|jd
kræ|}	|	d	8 }	|j|	 }
|
jdkr²qÒq²|j|	d	 |… }n|jd }
|jd	|… }|
dk�r|d }
|d	d… }|  	|
¡}ddlm} |D ]}|| ||ƒ}�q6|S )a®  
    Creates a "call" node that consist of all ``trailer`` and ``power``
    objects.  E.g. if you call it with ``append``::

        list([]).append(3) or None

    You would get a node with the content ``list([]).append`` back.

    This generates a copy of the original ast node.

    If you're using the leaf, e.g. the bracket `)` it will return ``list([])``.

    We use this function for two purposes. Given an expression ``bar.foo``,
    we may want to
      - infer the type of ``foo`` to offer completions after foo
      - infer the type of ``bar`` to be able to jump to the definition of foo
    The option ``cut_own_trailer`` must be set to true for the second purpose.
    Zfstringr   )ÚcompiledÚtraileréÿÿÿÿú:)Ú	NO_VALUESZatomé   Z
error_nodeÚawaitN)Úinfer_trailer)r   ÚtypeZjedi.inferencer   Zget_string_value_setÚinference_stater   Zjedi.inference.base_valuer#   Z
infer_nodeÚindexZjedi.inference.syntax_treer&   )ÚcontextÚleafZcut_own_trailerr    r   r#   Úpowerr)   ÚcutÚstartÚbaseZtrailersÚvaluesr&   r   r   r   Úinfer_call_of_leaf-   sB    
"








r1   c                 C   sV   z
| j }W n. ty8   | jdkr,| g Y S g  Y S Y n0 tt dd„ |D ƒ¡ƒS d S )NÚnamec                 s   s   | ]}t |ƒV  qd S ©N)Úget_names_of_node)Ú.0Úcr   r   r   Ú	<genexpr>x   ó    z$get_names_of_node.<locals>.<genexpr>)r   ÚAttributeErrorr'   Úlistr   Úfrom_iterable)Únoder   r   r   r   r4   o   s    


r4   c                 C   s   |   ¡ ot| jd d�tƒS ©N)Údefault)Zis_compiledr   Úget_safe_valuer   ©Úvaluer   r   r   Ú	is_string{   s    rB   c                 C   s   t | ƒpt| ƒS r3   )Ú	is_numberrB   r@   r   r   r   Ú
is_literal   s    rD   c                 C   s   | j d d�} t| |ƒr| S d S r=   )r?   r   )rA   Úacceptr   r   r   Ú_get_safe_value_or_noneƒ   s    
rF   c                 C   s
   t | tƒS r3   )rF   Úintr@   r   r   r   Úget_int_or_none‰   s    rH   c                 C   s
   t | tƒS r3   )rF   r   r@   r   r   r   Úget_str_or_none�   s    rI   c                 C   s   t | ttfƒd uS r3   )rF   rG   Úfloatr@   r   r   r   rC   ‘   s    rC   c                   @   s   e Zd ZdS )ÚSimpleGetItemNotFoundN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   rK   •   s   rK   c               
   g   s<   z
d V  W n, | y6 } zt |ƒ‚W Y d }~n
d }~0 0 d S r3   )rK   )Zexception_classesÚer   r   r   Úreraise_getitem_errors™   s    
rP   c                 C   s´   d}g }| dd … D ]–}|dv r4|sª|t |jƒ7 }q|jdkrr|jd d d… D ]}| |¡ ||u rN qlqNq q¬q|jdkr”| |¡ ||u rª q¬q|dkr¦|sªg }q q¬q||fS )Nr   r$   )Ú.z...Zdotted_nameé   r2   ú,)ÚlenrA   r'   r   r   )ÚnodesZis_import_fromZ
until_nodeÚlevelÚnamesr<   Únr   r   r   Úparse_dotted_names¡   s,    



rY   c                 G   s   |   |d d… ¡ |d ¡S )Nr!   )Úimport_moduleZpy__getattribute__)r(   rW   r   r   r   Úvalues_from_qualified_names¾   s    r[   c                 C   s"   |   ¡ j}|d u rdS |d dv S )NFr   )ZpandasÚnumpyZ
tensorflowZ
matplotlib)Zget_root_contextÚstring_names)r*   r]   r   r   r   Úis_big_annoying_libraryÂ   s    
r^   )F)N)r   r   r   r   Ú	itertoolsr   Ú
contextlibr   Zparso.pythonr   r   r   r1   r4   rB   rD   rF   rH   rI   rC   Ú	ExceptionrK   rP   rY   r[   r^   r   r   r   r   Ú<module>   s,   
B

