a
    ÷£Be!  ã                   @   s*  d dl Z d dlZ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 d	lmZmZ d
d„ Zdd„ Zdd„ Zdd„ Zd/dd„Zdd„ Zdd„ Zd0dd„Zdd„ Zdd„ Zdd„ Zd d!„ Z d1d#d$„Z!d2d%d&„Z"d'd(„ Z#d)d*„ Z$d+d,„ Z%G d-d.„ d.ƒZ&dS )3é    N)ÚPath)ÚZipFile)ÚzipimporterÚZipImportError)Úall_suffixes)Úaccess)Údebug)Úparser_utils)ÚKnownContentFileIOÚ	ZipFileIOc                   C   s   t jS ©N)ÚsysÚpath© r   r   úo/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jedi/inference/compiled/subprocess/functions.pyÚget_sys_path   s    r   c                 K   s   t j| fi |¤ŽS r   )r   Úload_module)Úinference_stateÚkwargsr   r   r   r      s    r   c                 O   s"   | j  |¡}t|j|ƒ|i |¤ŽS r   )Zcompiled_subprocessZget_access_handleÚgetattrr   )r   ÚidÚ	attributeÚargsr   Úhandler   r   r   Úget_compiled_method_return   s    r   c                 C   s   t  | |¡S r   )r   Zcreate_access_path)r   Úobjr   r   r   Úcreate_simple_object   s    r   c                 K   sˆ   |dur|t j t _}z\z$tf d|i|¤ŽW W |dur<|t _S  ty`   Y W |dur\|t _dS 0 W |dur„|t _n|dur‚|t _0 dS )zK
    Returns Tuple[Union[NamespaceInfo, FileIO, None], Optional[bool]]
    NÚ	full_name)NN)r   r   Ú_find_moduleÚImportError)r   Úsys_pathr   r   Útempr   r   r   Úget_module_info!   s     ûýÿr"   c                 C   s   t jS r   )r   Úbuiltin_module_names)r   r   r   r   Úget_builtin_module_names0   s    r$   c                 C   s   |‚dS )zE
    Raise an error to simulate certain problems for unit tests.
    Nr   )r   Zexception_typer   r   r   Ú_test_raise_error4   s    r%   c                 C   s>   |dur t |tjd� tj ¡  |dur:t |ƒ tj ¡  dS )zL
    Force some prints in the subprocesses. This exists for unit tests.
    N)Úfile)Úprintr   ÚstderrÚflushÚstdout)r   r(   r*   r   r   r   Ú_test_print;   s    
r+   c                 C   s6   t ƒ D ]*}tj | d| ¡}tj |¡r|  S qdS )z^
    The __init__ file can be searched in a directory. If found return it, else
    None.
    Ú__init__N)r   Úosr   ÚjoinÚexists)Zdirectory_pathÚsuffixr   r   r   r   Ú_get_init_pathG   s
    

r1   c                 C   s
   t  |¡S r   )r	   Úsafe_literal_eval)r   Úvaluer   r   r   r2   S   s    r2   c                  O   s   t t| i |¤ŽƒS r   )ÚlistÚ_iter_module_names)r   r   r   r   r   Úiter_module_namesW   s    r6   c                 c   sà   |D ]Ö}zdd„ t  |¡D ƒ}W nR tyr   zt|ƒ}t|j|jƒ}W n$ tyl   t 	d|¡ Y Y qY n0 Y n0 |D ]`\}}|rœ|dkrØ| 
¡ rØ|V  qx| d¡r´|d d… }n
t |¡}|rxd|vrx|dkrx|V  qxqd S )	Nc                 s   s   | ]}|j | ¡ fV  qd S r   )ÚnameÚis_dir)Ú.0Úentryr   r   r   Ú	<genexpr>_   ó    z%_iter_module_names.<locals>.<genexpr>z"Not possible to list directory: %sÚ__pycache__z.pyiéüÿÿÿÚ.r,   )r-   ÚscandirÚOSErrorr   Ú_zip_list_subdirectoryÚarchiveÚprefixr   r   ÚwarningÚisidentifierÚendswithÚinspectÚgetmodulename)r   Úpathsr   Zdir_entriesZzip_import_infor7   r8   Úmodnamer   r   r   r5   [   s,    ÿ

r5   Tc           
   	   C   s¸   d}d}t jD ]œ}|r(|tjjkr(d}n|}z
|j}W n tyL   Y qY n0 || |ƒ}|dur|jdkrlq|j}|du r¦|j	s¦|sˆ| n|}t
||jjƒ}	|	df  S  q¬qt| ||ƒS )aœ  
    Provides information about a module.

    This function isolates the differences in importing libraries introduced with
    python 3.3 on; it gets a module name and optionally a path. It will return a
    tuple containin an open file for the module (if not builtin), the filename
    or the name of the module if it is a builtin one and a boolean indicating
    if the module is contained in a package.
    NÚfrozenT)r   Ú	meta_pathÚ	importlibÚ	machineryÚ
PathFinderÚ	find_specÚAttributeErrorÚoriginÚloaderÚhas_locationÚImplicitNSInfoÚsubmodule_search_locationsÚ_pathÚ_find_module_py33)
Ústringr   r   Úis_global_searchÚspecrT   ÚfinderÚprQ   Zimplicit_ns_infor   r   r   r   ~   s*    





r   c              
   C   s¦   |s"t jj | |¡}|d ur"|j}|d u r†|d u r†zt j | ¡}|d urN|j}W n4 ty„ } ztdt|ƒ ƒ‚W Y d }~n
d }~0 0 |d u rœtd 	| ¡ƒ‚t
|| ƒS )NzOriginally  zCouldn't find a loader for {})rN   rO   rP   rQ   rT   ÚutilÚ
ValueErrorr   ÚreprÚformatÚ_from_loader)rZ   r   rT   r   r[   r\   Úer   r   r   rY   ¨   s    
&rY   c                 C   sä   z
| j }W n ty    d}Y n
0 ||ƒ}z
| j}W n tyN   d |f Y S 0 ||ƒ}zt| ƒj}W n ty€   tdƒ‚Y n0 |tjjjurœ|  |¡}n
t	| |ƒ}|d u r¶d |fS t
| tƒrÖt||t| jƒƒ|fS t||ƒ|fS )NFz$get_source was not defined on loader)Ú
is_packagerR   Úget_filenameÚtypeÚ
get_sourcer   rN   rO   ÚSourceFileLoaderÚ_get_sourceÚ
isinstancer   r   r   rC   r
   )rT   rZ   Zis_package_methodre   rf   Úmodule_pathÚfÚcoder   r   r   rc   ¾   s,    




rc   c                 C   s:   |   |¡}z|  |¡W S  ty4   td|d�‚Y n0 dS )z�
    This method is here as a replacement for SourceLoader.get_source. That
    method returns unicode, but we prefer bytes.
    z'source not available through get_data())r7   N)rf   Úget_datarA   r   )rT   Úfullnamer   r   r   r   rj   å   s    
ÿrj   c                 c   sT   t | ƒ}t|ƒ}| ¡ }|D ]2}t|ƒ}|j|kr| |¡}|j| d¡fV  qd S )Nú/)r   r   ÚnamelistÚparentÚrelative_tor7   rG   )Úzip_pathZzip_subdir_pathÚzip_fileZzip_content_file_pathsZraw_file_nameÚ	file_pathr   r   r   rB   ò   s    

rB   c                   @   s   e Zd ZdZdd„ ZdS )rV   z;Stores information returned from an implicit namespace specc                 C   s   || _ || _d S r   )r7   rJ   )Úselfr7   rJ   r   r   r   r,   ÿ   s    zImplicitNSInfo.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r,   r   r   r   r   rV   ý   s   rV   )NN)NN)NNT)NNNT)'r   r-   rH   rN   Úpathlibr   Úzipfiler   Ú	zipimportr   r   Zimportlib.machineryr   Zjedi.inference.compiledr   Zjedir   r	   Zjedi.file_ior
   r   r   r   r   r   r"   r$   r%   r+   r1   r2   r6   r5   r   rY   rc   rj   rB   rV   r   r   r   r   Ú<module>   s:   

#
*
'