B
    u9aÕS  ã               @   st  d dl mZ d dlmZ d dlZd dlZd dlZd dlZd dl	Zd dl
Z
d dlmZ d dlZddddd	d
ddddddgZeddƒZde_dd„ Zdd„ Zd*dd	„Zd+dd
„Zed,dd„ƒZd-dd„Ze ejje¡ dd„ ZG dd„ dƒZG d d„ dƒZy.d dlZd d!lmZ d.d"d#„Ze ee¡ W n e k
r<   Y nX d$d„ Z!d/d%d„Z"d&d„ Z#d'd„ Z$d(d„ Z%d)d„ Z&dS )0é    )Ú
namedtuple)ÚsingledispatchN)Ú
ModuleTypeÚget_importerÚiter_importersÚ
get_loaderÚfind_loaderÚwalk_packagesÚiter_modulesÚget_dataÚImpImporterÚ	ImpLoaderÚ	read_codeÚextend_pathÚ
ModuleInfozmodule_finder name ispkgz.A namedtuple with minimal info about a module.c             C   sL   y
| j }W n4 tk
r>   |  |¡}|d kr0d S tj ||¡S X ||ƒS d S )N)Ú	find_specÚAttributeErrorÚfind_moduleÚ	importlibÚutilÚspec_from_loader)ÚfinderÚnamer   Úloader© r   ú/usr/lib/python3.7/pkgutil.pyÚ	_get_spec   s    

r   c             C   s6   dd l }|  d¡}|tjjkr"d S |  d¡ | | ¡S )Nr   é   é   )ÚmarshalÚreadr   r   ÚMAGIC_NUMBERÚload)Ústreamr   Úmagicr   r   r   r   (   s    

Ú c          	   #   sÐ   i fdd„‰ x¾t | |ƒD ]°}|V  |jryt|jƒ W nN tk
r\   |d k	rX||jƒ Y q tk
r„   |d k	r~||jƒ n‚ Y qX ttj|j dd ƒpœg } ‡ fdd„| D ƒ} t	| |jd |ƒE d H  qW d S )Nc             S   s   | |krdS d|| < d S )NTr   )ÚpÚmr   r   r   ÚseenR   s    zwalk_packages.<locals>.seenÚ__path__c                s   g | ]}ˆ |ƒs|‘qS r   r   )Ú.0r&   )r(   r   r   ú
<listcomp>i   s    z!walk_packages.<locals>.<listcomp>Ú.)
r
   ÚispkgÚ
__import__r   ÚImportErrorÚ	ExceptionÚgetattrÚsysÚmodulesr	   )ÚpathÚprefixÚonerrorÚinfor   )r(   r   r	   5   s     c             c   sz   | d krt ƒ }nt| tƒr$tdƒ‚n
tt| ƒ}i }xB|D ]:}x4t||ƒD ]&\}}||krHd||< t|||ƒV  qHW q8W d S )Nz9path must be None or list of paths to look for modules iné   )r   Ú
isinstanceÚstrÚ
ValueErrorÚmapr   Úiter_importer_modulesr   )r4   r5   Z	importersÚyieldedÚir   r-   r   r   r   r
   n   s    




c             C   s   t | dƒsg S |  |¡S )Nr
   )Úhasattrr
   )Úimporterr5   r   r   r   r=   ˆ   s    
r=   c          	   c   s0  | j d kstj  | j ¡sd S i }dd l}yt | j ¡}W n tk
rP   g }Y nX | ¡  xÐ|D ]È}| |¡}|dks`||kr€q`tj  | j |¡}d}|stj  |¡rd|kr|}yt |¡}	W n tk
rÜ   g }	Y nX x&|	D ]}| |¡}
|
dkräd}P qäW q`|r`d|kr`d||< || |fV  q`W d S )Nr   Ú__init__Fr,   Tr8   )	r4   ÚosÚisdirÚinspectÚlistdirÚOSErrorÚsortÚgetmodulenameÚjoin)rA   r5   r>   rE   Ú	filenamesÚfnÚmodnamer4   r-   ÚdircontentsÚsubnamer   r   r   Ú_iter_file_finder_modules   s<    





rP   c            	   C   s.   t  ¡  t  dt¡ t d¡aW d Q R X d S )NÚignoreÚimp)ÚwarningsÚcatch_warningsÚsimplefilterÚDeprecationWarningr   Úimport_modulerR   r   r   r   r   Ú_import_imp¼   s    
rX   c               @   s*   e Zd Zd	dd„Zd
dd„Zddd„ZdS )r   Nc             C   s   t  dt¡ tƒ  || _d S )Nz5This emulation is deprecated, use 'importlib' instead)rS   ÚwarnrV   rX   r4   )Úselfr4   r   r   r   rB   Í   s    zImpImporter.__init__c             C   s~   |  d¡d }||kr$| jd kr$d S | jd kr4d }ntj | j¡g}yt ||¡\}}}W n tk
rn   d S X t||||ƒS )Nr,   éÿÿÿÿ)Úsplitr4   rC   ÚrealpathrR   r   r/   r   )rZ   Úfullnamer4   rO   ÚfileÚfilenameÚetcr   r   r   r   Ô   s    
zImpImporter.find_moduler%   c          	   c   s0  | j d kstj  | j ¡sd S i }dd l}yt | j ¡}W n tk
rP   g }Y nX | ¡  xÐ|D ]È}| |¡}|dks`||kr€q`tj  | j |¡}d}|stj  |¡rd|kr|}yt |¡}	W n tk
rÜ   g }	Y nX x&|	D ]}| |¡}
|
dkräd}P qäW q`|r`d|kr`d||< || |fV  q`W d S )Nr   rB   Fr,   Tr8   )	r4   rC   rD   rE   rF   rG   rH   rI   rJ   )rZ   r5   r>   rE   rK   rL   rM   r4   r-   rN   rO   r   r   r   r
   ã   s<    





zImpImporter.iter_modules)N)N)r%   )Ú__name__Ú
__module__Ú__qualname__rB   r   r
   r   r   r   r   r   Â   s   

c               @   sj   e Zd Zd ZZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
ddd„Zddd„Zdd„ Zddd„ZdS )r   Nc             C   s.   t  dt¡ tƒ  || _|| _|| _|| _d S )Nz5This emulation is deprecated, use 'importlib' instead)rS   rY   rV   rX   r_   r`   r^   ra   )rZ   r^   r_   r`   ra   r   r   r   rB     s    zImpLoader.__init__c             C   s:   |   ¡  zt || j| j| j¡}W d | jr4| j ¡  X |S )N)Ú_reopenrR   Úload_moduler_   r`   ra   Úclose)rZ   r^   Úmodr   r   r   rf     s    zImpLoader.load_modulec          	   C   s   t |dƒ
}| ¡ S Q R X d S )NÚrb)Úopenr    )rZ   Úpathnamer_   r   r   r   r   %  s    zImpLoader.get_datac             C   sT   | j rP| j jrP| jd }|tjkr2t| jdƒ| _ n|tjtjfkrPt| jdƒ| _ d S )Né   Úrri   )	r_   Úclosedra   rR   Ú	PY_SOURCErj   r`   ÚPY_COMPILEDÚC_EXTENSION)rZ   Úmod_typer   r   r   re   )  s    

zImpLoader._reopenc             C   s0   |d kr| j }n|| j kr,td| j |f ƒ‚|S )Nz,Loader for module %s cannot handle module %s)r^   r/   )rZ   r^   r   r   r   Ú	_fix_name1  s    
zImpLoader._fix_namec             C   s   |   |¡}| jd tjkS )Nrl   )rs   ra   rR   ÚPKG_DIRECTORY)rZ   r^   r   r   r   Ú
is_package9  s    
zImpLoader.is_packagec             C   s”   |   |¡}| jd krŽ| jd }|tjkrD|  |¡}t|| jdƒ| _nJ|tjkrv|  	¡  zt
| jƒ| _W d | j ¡  X n|tjkrŽ|  ¡  ¡ | _| jS )Nrl   Úexec)rs   Úcodera   rR   ro   Ú
get_sourceÚcompiler`   rp   re   r   r_   rg   rt   Ú_get_delegateÚget_code)rZ   r^   rr   Úsourcer   r   r   r{   =  s    






zImpLoader.get_codec          	   C   sº   |   |¡}| jd kr´| jd }|tjkrP|  ¡  z| j ¡ | _W d | j ¡  X nd|tj	krœt
j | jd d… ¡r´t| jd d… dƒ}| ¡ | _W d Q R X n|tjkr´|  ¡  ¡ | _| jS )Nrl   r[   rm   )rs   r|   ra   rR   ro   re   r_   r    rg   rp   rC   r4   Úexistsr`   rj   rt   rz   rx   )rZ   r^   rr   Úfr   r   r   rx   N  s    





zImpLoader.get_sourcec             C   s   t | jƒ}t|dƒ}|jS )NrB   )r   r`   r   r   )rZ   r   Úspecr   r   r   rz   `  s    

zImpLoader._get_delegatec             C   sH   |   |¡}| jd }|tjkr*|  ¡  ¡ S |tjtjtjfkrD| j	S d S )Nrl   )
rs   ra   rR   rt   rz   Úget_filenamero   rp   rq   r`   )rZ   r^   rr   r   r   r   r€   e  s    


zImpLoader.get_filename)N)N)N)rb   rc   rd   rw   r|   rB   rf   r   re   rs   ru   r{   rx   rz   r€   r   r   r   r   r     s   	

)Úzipimporterc       	      c   sð   t tj| j ƒ}| j}t|ƒ}i }dd l}xÀ|D ]¸}| |¡s@q0||d …  t	j
¡}t|ƒdkr˜|d  d¡r˜|d |kr˜d||d < ||d  dfV  t|ƒdkr¦q0| |d ¡}|dkr¾q0|r0d|kr0||kr0d||< || dfV  q0W d S )	Nr   rl   r8   z__init__.pyTrB   r,   F)ÚsortedÚ	zipimportÚ_zip_directory_cacheÚarchiver5   ÚlenrE   Ú
startswithr\   rC   ÚseprI   )	rA   r5   ZdirlistÚ_prefixZplenr>   rE   rL   rM   r   r   r   Úiter_zipimport_moduless  s*    

rŠ   c             C   sn   yt j|  }W nZ tk
rh   xDt jD ]6}y|| ƒ}t j | |¡ P W q& tk
rZ   Y q&X q&W d }Y nX |S )N)r2   Úpath_importer_cacheÚKeyErrorÚ
path_hooksÚ
setdefaultr/   )Z	path_itemrA   Ú	path_hookr   r   r   r   •  s    	

c             c   s„   |   d¡rd | ¡}t|ƒ‚d| krV|  d¡d }t |¡}t|dd ƒ}|d krhd S ntjE d H  tj	}x|D ]}t
|ƒV  qnW d S )Nr,   z'Relative module name {!r} not supportedr   r)   )r‡   Úformatr/   Ú
rpartitionr   rW   r1   r2   Ú	meta_pathr4   r   )r^   ÚmsgZpkg_nameZpkgr4   Úitemr   r   r   r   ­  s    



c             C   sn   | t jkr t j|  } | d kr d S t| tƒrb| }t|dd ƒ}|d k	rF|S t|dd ƒd krZd S |j}n| }t|ƒS )NÚ
__loader__Ú__spec__)r2   r3   r9   r   r1   rb   r   )Zmodule_or_nameÚmoduler   r^   r   r   r   r   Ê  s    


c             C   s†   |   d¡rd | ¡}t|ƒ‚ytj | ¡}W nF ttttfk
rr } z d}t| | t	|ƒ|¡ƒ|‚W d d }~X Y nX |d k	r‚|j
S d S )Nr,   z'Relative module name {!r} not supportedz,Error while finding loader for {!r} ({}: {}))r‡   r   r/   r   r   r   r   Ú	TypeErrorr;   Útyper   )r^   r“   r   Zexr   r   r   r   â  s    

*c             C   s¨  t | tƒs| S |d }| d d … } | d¡\}}}|rbytj| j}W qh ttfk
r^   | S X ntj}x8|D ].}t |t	ƒs‚qpt
|ƒ}|d k	rög }	t|dƒr¾| |¡}
|
d k	rÖ|
jpºg }	nt|dƒrÖ| |¡\}}	x|	D ]}|| krÜ|  |¡ qÜW tj ||¡}tj |¡rpyt|ƒ}W n8 tk
rT } ztj d||f ¡ W d d }~X Y qpX |@ x8|D ]0}| d¡}|rb| d¡r†qb|  |¡ qbW W d Q R X qpW | S )Nz.pkgr,   r   r   zCan't open %s: %s
Ú
ú#)r9   Úlistr‘   r2   r3   r)   rŒ   r   r4   r:   r   r@   r   Úsubmodule_search_locationsr   ÚappendrC   rJ   Úisfilerj   rG   ÚstderrÚwriteÚrstripr‡   )r4   r   Z	sname_pkgZparent_packageÚ_Z
final_nameZsearch_pathÚdirr   Úportionsr   ZportionZpkgfiler~   r“   Úliner   r   r   r   ÷  sP    !







c             C   s˜   t j | ¡}|d krd S |j}|d ks0t|dƒs4d S tj | ¡pJt j 	|¡}|d ks^t|dƒsbd S | 
d¡}| dtj |j¡¡ tjj|Ž }| |¡S )Nr   Ú__file__ú/r   )r   r   r   r   r@   r2   r3   ÚgetÚ
_bootstrapÚ_loadr\   ÚinsertrC   r4   Údirnamer§   rJ   r   )ÚpackageÚresourcer   r   rh   ÚpartsZresource_namer   r   r   r   U  s    
)Nr%   N)Nr%   )r%   )r%   )r%   )r%   )'Úcollectionsr   Ú	functoolsr   Zsimplegenericr   Úimportlib.utilZimportlib.machineryrC   Zos.pathr2   Útypesr   rS   Ú__all__r   Ú__doc__r   r   r	   r
   r=   rP   ÚregisterÚ	machineryÚ
FileFinderrX   r   r   rƒ   r   rŠ   r/   r   r   r   r   r   r   r   r   r   r   Ú<module>   sP   

9

(Jc

^