o
    Zh'                     @   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defddZdd Zdd Zeg ddd Zdd Zdd ZdefddZdef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   N/var/www/html/lang_env/lib/python3.10/site-packages/jedi/inference/sys_path.py	_abs_path   s   r   c           
   	   c   s   t |jddd |jddd D ]w\}}z>|dv sJ |jdv r)t|jdks+J |j}|d jdkr<|d jdks>J |d }|jd d	krQ|jd jd
ksSJ W n	 ty]   Y qw t| ||}| 	|D ]}| D ]}t
|rt| | }	|	dur|	V  qsqmq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    s2   *
 "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s"dS |jd j}|dvr.dS |jd }|d	krCt|jd
v rC|jd }| ||D ]}t|}|du rVqKt| |}|durb|V  qKd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 |S w ||D ]0\}}|j}t|jdkrD|t| g|jdd R   q%|durU|jdkrU|t	| | q%|S )z6
    Detect sys.path modifications within module.
    c                 s   s    | D ]=}|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   s0   
r>   c                 C   sB   t  }t|D ]}t| |D ]}|| t|tkr n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y"   td| Y d S w 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   s>   | j D ]}z|| r|W   S W q ty   Y qw 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 ]I}z,||}t|d}| }|dr8d|v r8|V  W d   n1 sBw   Y  W q tt	fyc } zt
t| W Y d}~qd}~ww d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   s2   t  dg D ]}| j|kr| | j}  | S 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| }|s&d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  | rBt  t| d  }|tjjs"|dr(|dd  }|rB|tjj}t|s8 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>  s    zMtransform_path_to_dotted.<locals>.iter_potential_solutions.<locals>.<genexpr>)	rK   r]   r%   rY   r   sepsplitalltuple)r5   restrp   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>  s    z*transform_path_to_dotted.<locals>.<lambda>)keyr   )rf   r   r]   r   rr   sorted)ru   r   
is_packagerv   Zpotential_solutionsr   rt   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.    +
%
