o
    ï›h*Z  ã                   @   s°  d 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
 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 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mZ ddlmZm Z m!Z! ddl"m#Z# ddl$m%Z% G dd„ dƒZ&eed�dd„ ƒZ'eg d�dd„ ƒZ(dd„ Z)dd„ Z*d d!„ Z+G d"d#„ d#ƒZ,		$d6d%d&„Z-e% .¡ ed'd(„ ƒƒZ/	)d7d*d+„Z0d8d,d-„Z1d8d.d/„Z2d0d1„ Z3d2d3„ Z4ed$fd4d5„Z5dS )9aå  
:mod:`jedi.inference.imports` is here to resolve import statements and return
the modules/classes/functions/whatever, which they stand for. However there's
not any actual importing done. This module is about finding modules in the
filesystem. This can be quite tricky sometimes, because Python imports are not
always that simple.

This module also supports import autocompletion, which means to complete
statements like ``from datetim`` (cursor at the end would return ``datetime``).
é    N)ÚPath)Útree)Úsearch_ancestor)Údebug)Úsettings)ÚFolderIO)Úget_cached_code_lines)Úsys_path)Úhelpers)Úcompiled)Úanalysis)Úunite)Úinference_state_method_cache)Ú
ImportNameÚSubModuleName)ÚValueSetÚ	NO_VALUES)Úimport_module_decoratorÚcreate_stub_moduleÚparse_stub_module)ÚImplicitNSInfo)Úplugin_managerc                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚModuleCachec                 C   s
   i | _ d S ©N©Ú_name_cache©Úself© r   úLc:\Users\shubh\Videos\agent\nova\Lib\site-packages\jedi/inference/imports.pyÚ__init__$   s   
zModuleCache.__init__c                 C   s   |d ur|| j |< d S d S r   r   )r   Ústring_namesÚ	value_setr   r   r   Úadd'   s   ÿzModuleCache.addc                 C   s   | j  |¡S r   )r   Úget)r   r!   r   r   r   r$   +   s   zModuleCache.getN)Ú__name__Ú
__module__Ú__qualname__r    r#   r$   r   r   r   r   r   #   s    r   )Údefaultc           	      C   sl   |   ¡ }t||ƒ\}}}}|r.|d ur.|j|| dd�}|s.||f }t| j|||ƒ}| ¡ }t d|¡ |S )NF©Zname_contextZanalysis_errorszafter import: %s)Úget_root_contextÚ_prepare_infer_importZpy__getattribute__ÚImporterÚinference_stateÚfollowr   Údbg)	ÚcontextÚ	tree_nameÚmodule_contextÚfrom_import_nameÚimport_pathÚlevelÚvaluesÚpathÚimporterr   r   r   Úinfer_import1   s    
ÿý
r9   c           	         s”   ˆ   ¡ }t|ˆƒ\‰}}}|sg S ˆd urAt‡ ‡fdd„|D ƒƒ}|r0t‡fdd„|D ƒƒs0|S |ˆf }tˆ j|||ƒ}| ¡ }tdd„ |D ƒƒS )Nc                    s   g | ]
}|j ˆˆ d d�‘qS )Fr)   )Zgoto)Ú.0Úc)r0   r3   r   r   Ú
<listcomp>P   s    üýÿzgoto_import.<locals>.<listcomp>c                 3   s   � | ]}|j ˆ u V  qd S r   ©r1   )r:   Únr=   r   r   Ú	<genexpr>X   s   € zgoto_import.<locals>.<genexpr>c                 s   s   � | ]}|j V  qd S r   )Úname)r:   Úsr   r   r   r?   ^   s   € )r*   r+   r   Úanyr,   r-   r.   Úset)	r0   r1   r2   r4   r5   r6   Únamesr7   r8   r   )r0   r3   r1   r   Úgoto_importG   s   
ÿû
rE   c                 C   s†   t |ddƒ}| |¡}d }z| ¡ }W n	 ty   Y nw t|ƒd t|ƒkr-|d }|}t| jt|ƒ| |jƒ}|t|ƒ|j| 	¡ fS )NÚimport_nameZimport_fromé   éÿÿÿÿ)
r   Zget_path_for_nameZget_from_namesÚAttributeErrorÚlenr,   r-   Útupler5   r.   )r2   r1   Zimport_noder4   r3   Z
from_namesr8   r   r   r   r+   a   s   
þÿr+   c                 C   s8   t |dƒr| d urt | d||¡ d S t d| ¡ d S )NÚparentzimport-errorzImportError without origin: )Úhasattrr   r#   r   Úwarning)Úvaluer@   Úmessager   r   r   Ú
_add_errorw   s   rQ   c                 C   s~   t |d ƒD ]}|}tj |¡}||kr dS q|}g }	 || kr%||fS tj |¡}|r:| d|¡ tj |¡}nd|fS q)z«
    In case the level is outside of the currently known package (something like
    import .....foo), we can still try our best to help the user for
    completions.
    rG   ©NNTr   N)ÚrangeÚosr7   ÚdirnameÚbasenameÚinsert)Úproject_pathÚ	directoryr5   ÚiÚoldÚdZlevel_import_pathsÚdir_namer   r   r   Ú_level_to_base_import_path~   s"   ÿør^   c                   @   sF   e Zd Zddd„Zedd„ ƒZdd„ Zdd	„ Zddd„Zddd„Z	d
S )r,   r   c                 C   s
  t  d||f ¡ || _|| _|| _d| _d| _|r€| ¡  ¡ }|t	|ƒkr>t
|ƒ}|dkr7|d| d … }|t
|ƒ }nB| ¡ }| jjj}t|ƒ}|du rR|}ntj |¡}t|||ƒ\}	}
|
du rhd| _n|
g| _|	du r||r{t||d dd� n|	| }|| _dS )	a  
        An implementation similar to ``__import__``. Use `follow`
        to actually follow the imports.

        *level* specifies whether to use absolute or relative imports. 0 (the
        default) means only perform absolute imports. Positive values for level
        indicate the number of parent directories to search relative to the
        directory of the module calling ``__import__()`` (see PEP 328 for the
        details).

        :param import_path: List of namespaces (strings or Names).
        zimport %s %sNTrG   Fr   z3Attempted relative import beyond top-level package.)rP   )r   ÚspeedÚ_inference_stater5   Ú_module_contextÚ_fixed_sys_pathÚ_infer_possibleÚ	get_valueZpy__package__rJ   rK   Z
py__file__Úprojectr7   ÚlistrT   rU   r^   rQ   r4   )r   r-   r4   r2   r5   Úbaser7   rX   rY   Zbase_import_pathZbase_directoryr   r   r   r    š   sD   

ÿþ€
zImporter.__init__c                 C   s   t dd„ | jD ƒƒS )z:Returns the import path as pure strings instead of `Name`.c                 s   ó&   � | ]}t |tjƒr|jn|V  qd S r   ©Ú
isinstancer   ÚNamerO   )r:   r@   r   r   r   r?   ã   ó
   € ÿ
ÿz,Importer._str_import_path.<locals>.<genexpr>)rK   r4   r   r   r   r   Ú_str_import_pathà   s   þzImporter._str_import_pathc                 C   s6   | j d ur| j S | jj| d�dd„ t | j¡D ƒ S )N)Zadd_init_pathsc                 S   s   g | ]}t |ƒ‘qS r   )Ústr©r:   Úpr   r   r   r<   ñ   s    ÿz9Importer._sys_path_with_modifications.<locals>.<listcomp>)rb   r`   Úget_sys_pathr	   Zcheck_sys_path_modificationsra   )r   Úis_completionr   r   r   Ú_sys_path_with_modificationsè   s   

þÿüz%Importer._sys_path_with_modificationsc                 C   s´   | j s&| jr$ddlm} tj | jd ¡f}|| j|| jd�}t|hƒS t	S | j
s+t	S | jj | j¡}|d ur<t|hƒS | jj | j¡}|d urJ|S | jdd�}t| j| j || jƒS )Nr   ©ÚImplicitNamespaceValue©r!   ÚpathsF©rr   )r4   rb   Újedi.inference.value.namespaceru   rT   r7   rV   r`   r   r   rc   Zstub_module_cacher$   rm   Úmodule_cachers   Úimport_module_by_namesra   )r   ru   r4   ÚnsÚ
from_cacher	   r   r   r   r.   ÷   s.   ý

ÿzImporter.followNc                 C   sL   |du r| j dd�}n|}tt| j| j||du rtnt|du o"|du d�ƒS )z„
        Get the names of all modules in the search_path. This means file names
        and not names defined in the files.
        NTrx   )Ú
module_clsÚadd_builtin_modules)rs   rf   Úiter_module_namesr`   ra   r   r   )r   Úsearch_pathZ	in_moduler	   r   r   r   Ú_get_module_names,  s   
ýzImporter._get_module_namesFc                 C   s>  | j sg S g }| jrŠ| jdkrM|  ¡ D ]}|j}| d¡r.|tdƒd… }| t| j	|ƒ¡ q| j
dd�D ]}tj |d¡}tj |¡rL||  |g¡7 }q5|  ¡ }	|	D ]}
|
jdvr[qS|
 ¡ sg||
 ¡  ¡ 7 }qS|sˆdd	lm} |	||	ƒB }|D ]}| ¡ D ]}|| ¡ 7 }q~qx|S | jr—||  | j¡7 }|S ||  ¡ 7 }|S )
z‰
        :param only_modules: Indicates wheter it's possible to import a
            definition that is not defined in a module.
        )ZflaskÚextZflask_NTrx   Úflaskext)ÚmoduleÚ	namespacer   )Úconvert_values)rc   r4   rm   r‚   Zstring_nameÚ
startswithrJ   Úappendr   ra   rs   rT   r7   ÚjoinÚisdirr.   Zapi_typeZis_compiledZsub_modules_dictr6   Z!jedi.inference.gradual.conversionr‡   Zget_filtersr5   rb   )r   r-   Zonly_modulesrD   ÚmodÚmodnameZextnameÚdirr„   r6   rO   r‡   Zboth_valuesr;   Úfilterr   r   r   Úcompletion_names;  sH   

€€
€ÿ	úÿzImporter.completion_names)r   rR   )F)
r%   r&   r'   r    Úpropertyrm   rs   r.   r‚   r�   r   r   r   r   r,   ™   s    
F

5r,   Tc                    sž   ˆd u rˆ  ¡ ‰tdd„ |D ƒƒ‰d g}t|ƒD ]4\‰ }t ‡ ‡‡‡‡fdd„|D ƒ¡}|sLdd ˆ¡ }|d urCt|||ƒ t	  S t |¡ t	  S q|S )Nc                 s   rh   r   ri   )r:   rZ   r   r   r   r?   r  rl   z)import_module_by_names.<locals>.<genexpr>c              	      s*   g | ]}t ˆˆd ˆ d … |ˆˆd�‘qS )NrG   )Úprefer_stubs)Úimport_module)r:   Úparent_module_value©rZ   r-   r’   Zstr_import_namesr	   r   r   r<   x  s    úûÿz*import_module_by_names.<locals>.<listcomp>zNo module named Ú.)
rq   rK   Ú	enumerater   Z	from_setsrŠ   rQ   r   rN   r   )r-   Úimport_namesr	   r2   r’   r"   r@   rP   r   r•   r   r{   m  s&   þù	
ÿúr{   c           
      C   sD  |d t jv rt| ||ƒ}|du rtS t|gƒS d |¡}|du r6| jj|d ||dd�\}}|du r5tS n| ¡ }|du r@tS | jj|d ||dd�\}}|du rTtS t	|t
ƒrndd	lm}	 |	| t|j d¡ƒ|jd
�}n|du rt| ||ƒ}|du r~tS nt| |||d�}|du r•t d|d |¡ nt d|||¡ t|gƒS )zC
    This method is very similar to importlib's `_gcd_import`.
    r   Nr–   rH   T)ÚstringÚ	full_namer	   Úis_global_searchF)r™   r7   rš   r›   rt   rv   ©r˜   Ú
is_packagezglobal search_module %s: %sz search_module %s in paths %s: %s)r   Zauto_import_modulesÚ_load_builtin_moduler   r   rŠ   Úcompiled_subprocessZget_module_infoZ
py__path__rj   r   ry   ru   rK   r@   Úsplitrw   Ú_load_python_moduler   r/   )
r-   r˜   r”   r	   r…   Úmodule_nameZfile_io_or_nsZis_pkgrw   ru   r   r   r   r“   ‹  s`   


üÿ
ü
ýÿý
r“   Fc                 C   s@   | j |dtjtjd�}ddlm} || |||t| j|jƒ|d�S )NT)Úfile_ioÚcacheZ
diff_cacheÚ
cache_pathr   )ÚModuleValue)r£   r!   Z
code_linesr�   )	Úparser   Zfast_parserZcache_directoryZjedi.inference.valuer¦   r   Zgrammarr7   )r-   r£   r˜   r�   Zmodule_noder¦   r   r   r   r¡   Ë  s   üûr¡   c                    sn   | j }|d u r|  ¡ }|js| | ¡‰ ‡ fdd„|D ƒ}d |¡}|d us'J ‚tj| ||d�}|d u r5d S |S )Nc                    s   g | ]}|ˆ v r|‘qS r   r   ro   ©Z
safe_pathsr   r   r<   ä  s    z(_load_builtin_module.<locals>.<listcomp>r–   )Údotted_namer	   )re   rq   Z_load_unsafe_extensionsZ_get_base_sys_pathrŠ   r   Úload_module)r-   r˜   r	   re   r©   r…   r   r¨   r   rž   Þ  s   

rž   c                 C   s  t |jƒ}|du r|  ¡ }t ||¡\}}nt|tƒsJ ‚|jdk}|rs| ¡ }|j 	d¡r7t
|jdd… ƒ}|jdkrB| d¡}n	| |d d ¡}zt| |||d	�}	t|	gƒ}
W n tye   t}
Y nw t| | j|
t| |ƒ||ƒS t| |||d
�}| j |t|gƒ¡ |S )z·
    This should pretty much only be used for get_modules_containing_name. It's
    here to ensure that a random path is still properly loaded into the Jedi
    module structure.
    Nz.pyiz-stubsiúÿÿÿz__init__.pyiz__init__.pyrH   z.py)r�   rœ   )r   r7   rq   r	   Útransform_path_to_dottedrj   ÚboolÚsuffixZget_parent_folderÚendswithr   r@   Zget_file_ioÚload_module_from_pathr   ÚFileNotFoundErrorr   r   Zlatest_grammarr   r¡   rz   r#   )r-   r£   r˜   r�   r7   Z
e_sys_pathZis_stubÚ	folder_ioZpython_file_ioÚvr6   r…   r   r   r   r¯   ð  sB   


þÿþýr¯   c                 C   s6   t  |  ¡ t|jƒ¡\}}ddlm} || ||jgƒS )Nr   rt   )r	   r«   rq   r   r7   ry   ru   )r-   r±   r˜   r�   ru   r   r   r   Úload_namespace_from_path  s   þr³   c                 C   s¨   t  |d¡}|d urRd}t|jƒD ]\}}|j|jkr n	|dkr%|d }q|j|d … }|d  ¡ j}|dv rR|dk}tj|||d�\}	}
t	| j
|
|  ¡ |	ƒ ¡ S d S )NÚ
error_noder   ú;rG   )ÚfromÚimportr¶   )Úis_import_fromZ
until_node)r   r   r—   ÚchildrenÚ	start_posZget_first_leafrO   r
   Zparse_dotted_namesr,   r-   r*   r.   )r0   r@   r´   Zstart_indexÚindexr>   ÚnodesZ
first_namer¸   r5   rD   r   r   r   Ú%follow_error_node_imports_if_possible'  s0   €
ýÿÿr½   c                 c   sD   � |r| j  ¡ D ]}|||ƒV  q| j  |¡D ]}|||ƒV  qdS )zx
    Get the names of all modules in the search_path. This means file names
    and not names defined in the files.
    N)rŸ   Zget_builtin_module_namesr€   )r-   r2   r�   r~   r   r@   r   r   r   r€   D  s   €ÿr€   )NNT)NFrR   )6Ú__doc__rT   Úpathlibr   Zparso.pythonr   Z
parso.treer   Zjedir   r   Zjedi.file_ior   Zjedi.parser_utilsr   Zjedi.inferencer	   r
   r   r   Zjedi.inference.utilsr   Zjedi.inference.cacher   Zjedi.inference.namesr   r   Zjedi.inference.base_valuer   r   Zjedi.inference.gradual.typeshedr   r   r   Z,jedi.inference.compiled.subprocess.functionsr   Zjedi.pluginsr   r   r9   rE   r+   rQ   r^   r,   r{   Zdecorater“   r¡   rž   r¯   r³   r½   r€   r   r   r   r   Ú<module>   sX    


 U
ÿ?
ÿ

.	ÿ