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mZ d dlmZ d dlmZ d d	lmZ d d
lmZ dZedœdd„Zdd„ Zdd„ Zeg d�dd„ ƒZdd„ Zdd„ Zedœdd„Zedœdd„Zd d!„ Zd"d#„ ZdS )$é    N)ÚPath)Úall_suffixes)Úinference_state_method_cache)ÚContextualizedNode)Ú	is_stringÚget_str_or_none)Úget_cached_code_lines)ÚFileIO)Úsettings)Údebugé
   )Ústr_pathc                 C   s<   t |ƒ}| ¡ r|S |  ¡ }|d u r(d S |j}| |¡ ¡ S ©N)r   Úis_absoluteZ
py__file__ÚparentÚjoinpathÚabsolute)Úmodule_contextr   ÚpathÚmodule_pathÚbase_dir© r   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jedi/inference/sys_path.pyÚ	_abs_path   s    r   c           
   	   c   s  t |jddd… |jddd… ƒD ]ò\}}z||dv s8J ‚|jdv rPt|jƒdksTJ ‚|j}|d jdkrv|d jdkszJ ‚|d }|jd d	kr |jd jd
ks¤J ‚W n ty¼   Y q"Y n0 t|  |¡|ƒ}| ¡  	|¡D ]6}| ¡ D ](}t
|ƒrèt| | ¡ ƒ}	|	durè|	V  qèqÜq"dS )a¾  
    Extracts the assigned strings from an assignment that looks as follows::

        sys.path[0:0] = ['module/path', 'another/module/path']

    This function is in general pretty tolerant (and therefore 'buggy').
    However, it's not a big issue usually to add more paths to Jedi's sys_path,
    because it will only affect Jedi in very random situations and by adding
    more paths than necessary, it usually benefits the general user.
    Né   é   )ú=z+=©ÚpowerZ	atom_exprr   ÚnameÚsysÚ.r   )ÚzipÚchildrenÚtypeÚlenÚvalueÚAssertionErrorr   Úcreate_contextZinferZiterater   r   Zget_safe_value)
r   Ú	expr_stmtZassigneeÚoperatorÚcÚtrailerZcnZ
lazy_valuer&   Úabs_pathr   r   r   Ú_paths_from_assignment    s(    *
ÿ "
r.   c                 c   sÈ   |j dkr>|jd dkr>|j dkr>|jd dkr>t|jƒdksBdS |jd j}|dvrZdS |jd }|d	kr„t|jƒd
v r„|jd }|  |¡ |¡D ].}t|ƒ}|du rªq”t| |ƒ}|dur”|V  q”dS )zE extract the path from either "sys.path.append" or "sys.path.insert" r,   r   r!   ú(é   Nr   )ÚinsertÚappendr1   )r0   é   r   )r$   r#   r%   r&   r(   Z
infer_noder   r   )r   Ztrailer1Ztrailer2r   Úargr&   Úpr-   r   r   r   Ú_paths_from_list_modificationsK   s*    ÿÿþ


r6   )Údefaultc                 C   s®   dd„ }| j du rg S g }z| j  ¡ d }W n ty>   Y nl0 ||ƒD ]`\}}|j}t|jƒdkr†| t| g|jdd… ¢R Ž ¡ qH|durH|jdkrH| t	| |ƒ¡ qH|S )z6
    Detect sys.path modifications within module.
    c                 s   s„   | D ]z}|j j }|d ur|jdv r|j}|d jdkr|d jdkr|d jdkr|d jd }|jdkr|jdkr||fV  qd S )Nr   r   r   r    r   r,   r   )r   r$   r#   r&   )Únamesr   r   r+   Únr   r   r   Úget_sys_path_powersi   s    ÿz9check_sys_path_modifications.<locals>.get_sys_path_powersNr   r3   r   r)   )
Z	tree_nodeZget_used_namesÚKeyErrorr   r%   r#   Úextendr6   r$   r.   )r   r:   ÚaddedZpossible_namesr   r   r)   r   r   r   Úcheck_sys_path_modificationsd   s*    
ÿÿÿr>   c                 C   sB   t ƒ }t|ƒD ].}t| |ƒD ]}| |¡ t|ƒtkr qqq|S r   )ÚsetÚ_get_buildout_script_pathsÚ_get_paths_from_buildout_scriptÚaddr%   Ú_BUILDOUT_PATH_INSERTION_LIMIT)Úinference_stateZscript_pathZbuildout_script_pathsÚbuildout_script_pathr   r   r   r   Údiscover_buildout_pathsŠ   s    
rF   c                 c   s€   t t|ƒƒ}z| j|dtjd�}W n  tyB   t d|¡ Y d S 0 ddlm	} || ||d t
| j|ƒd� ¡ }t|ƒE d H  d S )NT)Úfile_ioÚcacheÚ
cache_pathz(Error trying to read buildout_script: %sr   )ÚModuleValue)rG   Zstring_namesZ
code_lines)r	   ÚstrÚparser
   Zcache_directoryÚIOErrorr   ÚwarningZjedi.inference.valuerJ   r   ZgrammarZ
as_contextr>   )rD   rE   rG   Zmodule_noderJ   r   r   r   r   rA   –   s$    ý

ü
rA   )r   c              	   C   sD   | j D ]8}z| |¡ ¡ r$|W   S W q ty<   Y qY q0 qd S r   )Úparentsr   Úis_fileÚOSError)r   Úfilenamer   r   r   r   Ú_get_parent_dir_with_file¬   s    
rS   )Úsearch_pathc                 c   sØ   t | dƒ}|sdS | d¡}| ¡ s(dS t |¡D ] }zX| |¡}t|dƒ�0}| ¡ }| d¡rnd|v rn|V  W d  ƒ n1 s‚0    Y  W q2 tt	fyÐ } z&t
 t|ƒ¡ W Y d}~q2W Y d}~q2d}~0 0 q2dS )zü
    if there is a 'buildout.cfg' file in one of the parent directories of the
    given module it will return a list of all files in the buildout bin
    directory that look like python files.

    :param search_path: absolute path to the module.
    zbuildout.cfgNÚbinÚrz#!Úpython)rS   r   ÚexistsÚosÚlistdirÚopenÚreadlineÚ
startswithÚUnicodeDecodeErrorrM   r   rN   rK   )rT   Zproject_rootZbin_pathrR   ÚfilepathÚfÚ	firstlineÚer   r   r   r@   ¶   s     


(r@   c                 C   s0   t ƒ dg D ]}| j|kr|  | j¡}  q,q| S )Nz.pyi)r   ÚsuffixÚ	with_nameÚstem)r   rc   r   r   r   Úremove_python_path_suffixÓ   s
    
rf   c                    sd   t ˆ ƒ‰ ˆ j d¡rdS ˆ jdk}|r,ˆ j‰ ‡ ‡fdd„}t|ƒ ƒ}|sLdS t|dd„ d�d	 |fS )
aK  
    Returns the dotted path inside a sys.path as a list of names. e.g.

    >>> transform_path_to_dotted([str(Path("/foo").absolute())], Path('/foo/bar/baz.py').absolute())
    (('bar', 'baz'), False)

    Returns (None, False) if the path doesn't really resolve to anything.
    The second return part is if it is a package.
    r!   )NFÚ__init__c                  3   sˆ   ˆD ]~} t ˆ ƒ | ¡rt ˆ ƒt| ƒd … }| tjj¡sB| d¡rN|dd … }|r| tjj¡}t|ƒsn d S tdd„ |D ƒƒV  qd S )Nú/r   c                 s   s   | ]}t  d d|¡V  qdS )z-stubs$Ú N)ÚreÚsub)Ú.0Úsr   r   r   Ú	<genexpr>  ó    zMtransform_path_to_dotted.<locals>.iter_potential_solutions.<locals>.<genexpr>)	rK   r]   r%   rY   r   ÚsepÚsplitÚallÚtuple)r5   Úrestrq   ©r   Úsys_pathr   r   Úiter_potential_solutionsò   s    z:transform_path_to_dotted.<locals>.iter_potential_solutionsc                 S   s   t | ƒS r   )r%   )r5   r   r   r   Ú<lambda>  ro   z*transform_path_to_dotted.<locals>.<lambda>)Úkeyr   )rf   r   r]   r   rs   Úsorted)rv   r   Ú
is_packagerw   Zpotential_solutionsr   ru   r   Útransform_path_to_dottedÛ   s    

r|   ) rY   rj   Úpathlibr   Zimportlib.machineryr   Zjedi.inference.cacher   Zjedi.inference.base_valuer   Zjedi.inference.helpersr   r   Zjedi.parser_utilsr   Zjedi.file_ior	   Zjedir
   r   rC   rK   r   r.   r6   r>   rF   rA   rS   r@   rf   r|   r   r   r   r   Ú<module>   s,   +
%
