U
    nuf,                     @   s  d dl Z d dl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
ZdZd/ddZdd Zdd Zdd Zdd Zdd Zd0ddZdd Zdd Zd d! Zd1d"d#Zd2d$d%Zd&d' Z d(d) Z!d3d+d,Z"d4d-d.Z#dS )5    N)python_bytes_to_unicode)dbg)KnownContentFileIOFolderIO)SubModuleName)load_module_from_path)ParserTreeFilter)convert_names)z.toxz.venvz.mypy_cachevenv__pycache__i      c                 c   sF   | D ]<}||krqt |ts"|V  |jdkrt| | E d H  qd S )Nmodule)
isinstancer   api_type_resolve_namesgoto)Zdefinition_namesZavoid_namesnamer   r   =/tmp/pip-unpacked-wheel-ol4ehs9f/jedi/inference/references.pyr      s    

r   c                 C   s   t dd | D S )Nc                 s   s&   | ]}|j d kr|n|j |fV  qd S N)	tree_name.0nr   r   r   	<genexpr>.   s   z _dictionarize.<locals>.<genexpr>)dict)namesr   r   r   _dictionarize-   s    r   c                 C   s   t | |}t|D ]&}|tt|g|   ddO }q|tt||jO }t|D ]>}|jdksV|j	d ksV|j	j
jdkr~qV|tt|j|jO }qVtt|S )NF)Z
only_stubsZprefer_stub_to_compiledparamtrailer)_find_nameslistsetr	   get_root_contextZis_stub_find_global_variablesvaluer   r   parenttype_add_names_in_same_contextparent_contextstring_namer   )module_contextr   found_namesr   r   r   r   _find_defining_names4   s    
r-   c                 C   s,   |  |}t| }|| tt|S r   )Zcreate_namer"   r   addr   )r+   r   r   r,   r   r   r   r    H   s    

r    c                 c   s\   | j d krd S d }t| |d}t||}|s2qX|E d H  t|dd d}|d j}qd S )N)r)   until_positionc                 S   s   | j S r   )	start_pos)xr   r   r   <lambda>^       z,_add_names_in_same_context.<locals>.<lambda>)keyr   )	tree_noder   r"   getsortedr0   )contextr*   r/   Zfilter_r   Zorderedr   r   r   r(   P   s    

r(   c              	   c   s|   | D ]r}|j d krq| }z
|j}W n tk
r>   Y qY qX | |D ](}|V  ||j }t||jE d H  qLqd S r   )r   r#   Zget_global_filterAttributeErrorr6   Zcreate_contextr(   r*   )r   search_namer   r+   methodZglobal_namecr   r   r   r$   b   s    


r$   Fc                    s  j }|j}zd|_t|}W 5 d|_X t| g}|stdd |D D ]2}|krN|jd k	rN|jj|	 j
krN|| qN|stdd |D r|}nt|||}i }	|D ]j |g D ]}
tt|
}t fdd|D rJ | |D ]F}|	|g D ]} | qz
|	|= W n tk
rB   Y nX q q|D ]}|	|g | qNqq  }|rfdd|D S |S )	NTFc                 s   s   | ]}|  V  qd S r   r#   )r   dr   r   r   r      s     z"find_references.<locals>.<genexpr>c                 s   s   | ]}|j d kV  qdS )r   N)r   r   r   r   r   r      s     c                 3   s   | ]}| kV  qd S r   r   )r   r   )found_names_dctr   r   r      s     c                    s   g | ]}|   kr|qS r   r=   r   )r+   r   r   
<listcomp>   s      z#find_references.<locals>.<listcomp>)inference_stater%   Zflow_analysis_enabledr-   r   r"   r5   projectpath
py__file__parentsappendany#get_module_contexts_containing_nameZget_used_namesr6   r    updateKeyError
setdefaultvalues)r+   r   Zonly_in_moduleinfr:   r,   module_contextsmZpotential_modulesZnon_matching_reference_mapsZ	name_leafnewdctr   resultr   )r?   r+   r   find_referencesr   sP    

rS   c                 C   sh   z|  }W n tk
r"   Y d S X t|dd}||s>d S t|j|}t| |}| r`d S | S )Nreplace)errors)	readFileNotFoundErrorr   searchr   rC   r   is_compiledZ
as_context)rA   file_ioregexcodeZnew_file_iorO   r   r   r   	_check_fs   s    

r]   c                 C   s   t  }t  }|  D ]v}|r|ds|dsd|kr>q|ddd}d|krz|d}|tj	
| j	| q|}|| j	|f q||fS )N   #   !   *zutf-8ignore/)r"   rV   
splitlines
startswithdecoderstriplstripr.   osrC   join)	folder_iorZ   ignored_paths_absignored_paths_rellpr   r   r   r   gitignored_paths   s     
ro   c                    s   | j   fdd|D S )Nc                    s,   h | ]$}  |d  rtj |d qS )r      )rd   rh   rC   ri   r   rn   Z	curr_pathr   r   	<setcomp>   s      z/expand_relative_ignore_paths.<locals>.<setcomp>rC   )rj   Zrelative_pathsr   rr   r   expand_relative_ignore_paths   s    ru   c           
      #   s   t   t  }|  D ]\}}}|D ]N}|j}|jdkrJ| krJd |fV  |jdkr$t||\}}	 |O  ||	O }q$t|| fdd|D |d d < |D ]} | d fV  qqd S )N)z.pyz.pyiz
.gitignorec                    s0   g | ](}|j  kr|j kr| tkr|qS r   )rC   Zget_base_name_IGNORE_FOLDERS)r   rj   except_pathsZexcept_paths_relative_expandedr   r   r@      s
   

z9recurse_find_python_folders_and_files.<locals>.<listcomp>)r"   walkrC   suffixr   ro   ru   )
rj   rx   Zexcept_paths_relativeZroot_folder_ioZ
folder_iosZfile_iosrZ   rC   rk   rl   r   rw   r   %recurse_find_python_folders_and_files   s0    


 
 r{   c                 c   s&   t | |D ]\} }|d k	r
|V  q
d S r   )r{   )rj   rx   rZ   r   r   r   recurse_find_python_files   s    r|   c                 #   s   |   }t }dd |D }|D ]z}| j}|d kr8q | }|j t fdd|D r |krfq t||D ]}|j|krp|V  qp|  | }q@q d S )Nc                 S   s   g | ]}|  qS r   rD   r   rO   r   r   r   r@      s     z2_find_python_files_in_sys_path.<locals>.<listcomp>c                 3   s   | ]}  |V  qd S r   )rd   rq   rt   r   r   r     s     z1_find_python_files_in_sys_path.<locals>.<genexpr>)	Zget_sys_pathr"   	get_valuerZ   Zget_parent_folderrC   rG   r|   r.   )rA   rN   sys_pathrx   Zyielded_pathsr+   rZ   rj   r   rt   r   _find_python_files_in_sys_path   s     


r   c                 c   s*   dd |D }t t| jj|E d H  d S )Nc                 S   s   g | ]}|  qS r   r}   r~   r   r   r   r@     s     z)_find_project_modules.<locals>.<listcomp>)r|   r   rB   rC   )rA   rN   Zexcept_r   r   r   _find_project_modules  s    r   rp   c                 c   sN   |D ]}|  rq|V  qt|dkr*dS t| |}t| |||dE dH  dS )z
    Search a name in the directories of modules.

    :param limit_reduction: Divides the limits on opening/parsing files by this
        factor.
       N)limit_reduction)rY   lenr   search_in_file_ios)rA   rN   r   r   r+   file_io_iteratorr   r   r   rH     s    	
rH   c                 c   s   t | }t| }d}d}tdt| |r0dnd }	|D ]Z}
|d7 }t| |
|	}|d k	r|d7 }|V  ||krtd|  q||kr<td|  qq<d S )Nr   z\b rp   zHit limit of parsed files: %szHit limit of opened files: %s)_PARSED_FILE_LIMIT_OPENED_FILE_LIMITrecompileescaper]   r   )rA   r   r   r   ZcompleteZparse_limitZ
open_limitZfile_io_countZparsed_file_countr[   rZ   rO   r   r   r   r   ,  s"     

r   )r   )F)r   )r   )rp   )rp   F)$rh   r   Zparsor   Z
jedi.debugr   Zjedi.file_ior   r   Zjedi.inference.namesr   Zjedi.inference.importsr   Zjedi.inference.filtersr   Z!jedi.inference.gradual.conversionr	   rv   r   r   r   r   r-   r    r(   r$   rS   r]   ro   ru   r{   r|   r   r   rH   r   r   r   r   r   <module>   s<   

6
"
 
   