o
    ˷e'                     @   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_absolute
py__file__parentjoinpathabsolute)module_contextr   pathmodule_pathbase_dir r   N/var/www/ideatree/venv/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+=power	atom_exprr   namesys.r   )zipchildrentypelenvalueAssertionErrorr   create_contextinferiterater   r   get_safe_value)
r   	expr_stmtassigneeoperatorctrailercn
lazy_valuer(   abs_pathr   r   r   _paths_from_assignment    s2   *
 "r6   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" r2   r   r#   (   Nr   )insertappendr9   )r8      r   )r&   r%   r'   r(   r*   
infer_noder   r   )r   trailer1trailer2r!   argr(   pr5   r   r   r   _paths_from_list_modificationsK   s(   


rA   )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   r2   r   )r   r&   r%   r(   )namesr!   r   r1   nr   r   r   get_sys_path_powersi   s   
z9check_sys_path_modifications.<locals>.get_sys_path_powersNr   r;   r   r.   )
	tree_nodeget_used_namesKeyErrorr   r'   r%   extendrA   r&   r6   )r   rE   addedpossible_namesr!   r   r.   r   r   r   check_sys_path_modificationsd   s0   
rL   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_statescript_pathbuildout_script_pathsbuildout_script_pathr   r   r   r   discover_buildout_paths   s   
rV   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)rW   string_names
code_lines)r	   strparser
   cache_directoryIOErrorr   warningjedi.inference.valuerZ   r   grammar
as_contextrL   )rR   rU   rW   module_noderZ   r   r   r   r   rO      s,   

rO   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   

rj   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)rj   r   existsoslistdiropenreadline
startswithUnicodeDecodeErrorr`   r   ra   r]   )rk   project_rootbin_pathri   filepathf	firstlineer   r   r   rN      s.   


rN   c                 C   s2   t  dg D ]}| j|kr| | j}  | S q| S )Nz.pyi)r   suffix	with_namestem)r   r|   r   r   r   remove_python_path_suffix   s   
r   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>)	r]   rt   r'   rp   r   sepsplitalltuple)r@   restr   r   sys_pathr   r   iter_potential_solutions   s   z:transform_path_to_dotted.<locals>.iter_potential_solutionsc                 S   s   t | S r   )r'   )r@   r   r   r   <lambda>  s    z*transform_path_to_dotted.<locals>.<lambda>)keyr   )r   r!   rt   r   r   sorted)r   r   
is_packager   potential_solutionsr   r   r   transform_path_to_dotted   s   

r   ) rp   r   pathlibr   importlib.machineryr   jedi.inference.cacher   jedi.inference.base_valuer   jedi.inference.helpersr   r   jedi.parser_utilsr   jedi.file_ior	   jedir
   r   rQ   r]   r   r6   rA   rL   rV   rO   rj   rN   r   r   r   r   r   r   <module>   s.    +
%
