a
    ze^                     @   s  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Zd dlmZ d dlZg dZeddZde_d	d
 Zdd Zd1ddZd2ddZed3ddZd4ddZeejje dd ZG dd dZG dd dZz.d dlZd dlmZ d5ddZ eee  W n e!y.   Y n0 dd  Z"d6d!d"Z#d#d$ Z$d%d& Z%d'd( Z&d)d* Z'd+Z(e
)d,e( d-e( d.e
j*Z+[(d/d0 Z,dS )7    )
namedtuple)singledispatchN)
ModuleType)get_importeriter_importers
get_loaderfind_loaderwalk_packagesiter_modulesget_dataImpImporter	ImpLoader	read_codeextend_path
ModuleInfor   zmodule_finder name ispkgz.A namedtuple with minimal info about a module.c                 C   sP   z
| j }W n8 tyB   | |}|d u r0Y d S tj|| Y S 0 ||S d S N)	find_specAttributeErrorfind_module	importlibutilspec_from_loader)findernamer   loader r   /usr/lib/python3.9/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    

r    c              	   #   s   i fdd t | |D ]}|V  |jrzt|j W nJ tyX   |d urT||j Y q ty~   |d urx||j n Y q0 ttj|j dd pg }  fdd| D } t	| |jd |E d H  qd S )Nc                 S   s   | |v rdS d|| < d S )NTr   )pmr   r   r   seenS   s    zwalk_packages.<locals>.seen__path__c                    s   g | ]} |s|qS r   r   ).0r'   r)   r   r   
<listcomp>j       z!walk_packages.<locals>.<listcomp>.)
r
   ispkg
__import__r   ImportError	Exceptiongetattrsysmodulesr	   )pathprefixonerrorinfor   r,   r   r	   6   s     r	   c                 c   sr   | d u rt  }nt| tr$tdn
tt| }i }|D ]6}t||D ]&\}}||vrDd||< t|||V  qDq6d S )Nz9path must be None or list of paths to look for modules in   )r   
isinstancestr
ValueErrormapr   iter_importer_modulesr   )r7   r8   Z	importersyieldedir   r0   r   r   r   r
   o   s    



r
   c                 C   s   t | dsg S | |S )Nr
   )hasattrr
   )importerr8   r   r   r   r@      s    
r@   c              	   c   s   | j d u stj | j sd S i }dd l}zt| j }W n tyN   g }Y n0 |  |D ]}||}|dks\||v r|q\tj | j |}d}|stj |rd|vr|}zt|}	W n ty   g }	Y n0 |	D ]}||}
|
dkrd} qqq\|r\d|vr\d||< || |fV  q\d S Nr   __init__Fr/   Tr;   	r7   osisdirinspectlistdirOSErrorsortgetmodulenamejoin)rD   r8   rA   rJ   	filenamesfnmodnamer7   r0   dircontentssubnamer   r   r   _iter_file_finder_modules   s<    



rU   c                   C   sB   t  & t dt tdaW d    n1 s40    Y  d S )Nignoreimp)warningscatch_warningssimplefilterDeprecationWarningr   import_modulerW   r   r   r   r   _import_imp   s    
r]   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)rX   warnr[   r]   r7   )selfr7   r   r   r   rF      s
    zImpImporter.__init__c                 C   s~   | dd }||kr$| jd u r$d S | jd u r4d }ntj| jg}zt||\}}}W n tyn   Y d S 0 t||||S )Nr/   )splitr7   rH   realpathrW   r   r2   r   )r`   fullnamer7   rT   filefilenameetcr   r   r   r      s    
zImpImporter.find_moduler&   c              	   c   s   | j d u stj | j sd S i }dd l}zt| j }W n tyN   g }Y n0 |  |D ]}||}|dks\||v r|q\tj | j |}d}|stj |rd|vr|}zt|}	W n ty   g }	Y n0 |	D ]}||}
|
dkrd} qqq\|r\d|vr\d||< || |fV  q\d S rE   rG   )r`   r8   rA   rJ   rP   rQ   rR   r7   r0   rS   rT   r   r   r   r
      s<    



zImpImporter.iter_modules)N)N)r&   )__name__
__module____qualname__rF   r   r
   r   r   r   r   r      s   

r   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 r^   )rX   r_   r[   r]   re   rf   rd   rg   )r`   rd   re   rf   rg   r   r   r   rF     s    zImpLoader.__init__c                 C   sJ   |    z*t|| j| j| j}W | jrF| j  n| jrD| j  0 |S r   )_reopenrW   load_modulere   rf   rg   close)r`   rd   modr   r   r   rl     s    zImpLoader.load_modulec                 C   s6   t |d}| W  d    S 1 s(0    Y  d S )Nrb)openr!   )r`   pathnamere   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v rPt| jd| _ d S )N   rro   )	re   closedrg   rW   	PY_SOURCErp   rf   PY_COMPILEDC_EXTENSION)r`   mod_typer   r   r   rk   *  s    

zImpLoader._reopenc                 C   s0   |d u r| j }n|| j kr,td| j |f |S )Nz,Loader for module %s cannot handle module %s)rd   r2   r`   rd   r   r   r   	_fix_name2  s    
zImpLoader._fix_namec                 C   s   |  |}| jd tjkS Nrr   )rz   rg   rW   PKG_DIRECTORYry   r   r   r   
is_package:  s    
zImpLoader.is_packagec                 C   s   |  |}| jd u r| jd }|tjkrD| |}t|| jd| _nT|tjkr| 	  zt
| j| _W | j  q| j  0 n|tjkr|   | _| jS )Nrr   exec)rz   coderg   rW   ru   
get_sourcecompilerf   rv   rk   r   re   rm   r|   _get_delegateget_code)r`   rd   rx   sourcer   r   r   r   >  s    






zImpLoader.get_codec                 C   s   |  |}| jd u r| jd }|tjkrZ|   z| j | _W | j  q| j  0 nx|tj	krt
j| jd d rt| jd d d}| | _W d    q1 s0    Y  n|tjkr|   | _| jS )Nrr   ra   rs   )rz   r   rg   rW   ru   rk   re   r!   rm   rv   rH   r7   existsrf   rp   r|   r   r   )r`   rd   rx   fr   r   r   r   O  s    




*
zImpLoader.get_sourcec                 C   s   t | j}t|d}|jS )NrF   )r   rf   r   r   )r`   r   specr   r   r   r   a  s    

zImpLoader._get_delegatec                 C   sH   |  |}| jd }|tjkr*|   S |tjtjtjfv rD| j	S d S r{   )
rz   rg   rW   r|   r   get_filenameru   rv   rw   rf   )r`   rd   rx   r   r   r   r   f  s    


zImpLoader.get_filename)N)N)N)rh   ri   rj   r   r   rF   rl   r   rk   rz   r}   r   r   r   r   r   r   r   r   r     s   	

r   )zipimporterc           	      c   s   t tj| j }| j}t|}i }dd l}|D ]}||s>q.||d  t	j
}t|dkr|d dr|d |vrd||d < ||d  dfV  t|dkrq.||d }|dkrq.|r.d|vr.||vr.d||< || dfV  q.d S )	Nr   rr   r;   z__init__.pyTrF   r/   F)sorted	zipimport_zip_directory_cachearchiver8   lenrJ   
startswithrb   rH   seprN   )	rD   r8   dirlist_prefixZplenrA   rJ   rQ   rR   r   r   r   iter_zipimport_modulest  s*    
r   c                 C   st   t | } ztj|  }W nV tyn   tjD ]8}z || }tj| | W  qjW q, tyb   Y q,0 q,d }Y n0 |S r   )rH   fsdecoder5   path_importer_cacheKeyError
path_hooks
setdefaultr2   )Z	path_itemrD   	path_hookr   r   r   r     s    	



r   c                 c   s   |  drd| }t|d| v rV| dd }t|}t|dd }|d u rhd S ntjE d H  tj	}|D ]}t
|V  qld S )Nr/   'Relative module name {!r} not supportedr   r*   )r   formatr2   
rpartitionr   r\   r4   r5   	meta_pathr7   r   )rd   msgZpkg_namepkgr7   itemr   r   r   r     s    


r   c                 C   sn   | t jv r t j|  } | d u r d S t| trb| }t|dd }|d urF|S t|dd d u rZd S |j}n| }t|S )N
__loader____spec__)r5   r6   r<   r   r4   rh   r   )Zmodule_or_namemoduler   rd   r   r   r   r     s    


r   c                 C   s   |  drd| }t|ztj| }W nH ttttfyt } z(d}t|| t	|||W Y d }~n
d }~0 0 |d ur|j
S d S )Nr/   r   z,Error while finding loader for {!r} ({}: {}))r   r   r2   r   r   r   r   	TypeErrorr>   typer   )rd   r   r   exr   r   r   r     s    

.r   c                 C   s  t | ts| S |d }| d d  } |d\}}}|rdztj| j}W qj ttfy`   |  Y S 0 ntj}|D ]>}t |t	sqnt
|}|d urg }	t|dr||}
|
d ur|
jpg }	nt|dr||\}}	|	D ]}|| vr| | qtj||}tj|rnzt|}W n: tyP } z tjd||f  W Y d }~qnd }~0 0 |F |D ]0}|d}|r\|drq\| | q\W d    qn1 s0    Y  qn| S )Nz.pkgr/   r   r   zCan't open %s: %s

#)r<   listr   r5   r6   r*   r   r   r7   r=   r   rC   r   submodule_search_locationsr   appendrH   rO   isfilerp   rL   stderrwriterstripr   )r7   r   Z	sname_pkgZparent_package_Z
final_nameZsearch_pathdirr   portionsr   ZportionZpkgfiler   r   liner   r   r   r     sR    !






0r   c                 C   s   t j| }|d u rd S |j}|d u s0t|ds4d S tj| pJt j	|}|d u s^t|dsbd S |
d}|dtj|j tjj| }||S )Nr   __file__/r   )r   r   r   r   rC   r5   r6   get
_bootstrap_loadrb   insertrH   r7   dirnamer   rO   r   )packageresourcer   r   rn   partsZresource_namer   r   r   r   W  s    

r   z(?!\d)(\w+)(\.(?!\d)(\w+))*z	^(?P<pkg>z)(?P<cln>:(?P<obj>z)?)?$c           	      C   s   t | }|std| | }|drZt|d }|d}|rT|dng }nn| d}|d}t|}|r|d }| d| }zt|}|d |}W qx t	y   Y qY qx0 qx|}|D ]}t
||}q|S )Nzinvalid format: Zclnr   objr/   r   )_NAME_PATTERNmatchr>   	groupdictr   r   r\   rb   popr2   r4   )	r   r(   gdrn   r   rR   r'   sresultr   r   r   resolve_name  s0     







r   )Nr&   N)Nr&   )r&   )r&   )r&   )r&   )-collectionsr   	functoolsr   Zsimplegenericr   importlib.utilZimportlib.machineryrH   Zos.pathrer5   typesr   rX   __all__r   __doc__r   r   r	   r
   r@   rU   register	machinery
FileFinderr]   r   r   r   r   r   r2   r   r   r   r   r   r   Z_DOTTED_WORDSr   Ur   r   r   r   r   r   <module>   sV   

9

(Jc

^+