3
ûâdeD*  ã               @   s†  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	Z	ddl
mZ ddlmZmZmZ ejeƒZdaG dd„ deƒZG dd	„ d	eƒZG d
d„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZedƒee	jeiZyPyd dlZW n e k
�r   d dl!ZY nX eeej"< eeej#< eeej$< [W n e e%fk
�rV   Y nX dd„ Z&i Z'dd„ Z(ej)e*dƒƒZ+dd„ Z,dS )é    )Úunicode_literalsNé   )ÚDistlibException)Úcached_propertyÚget_cache_baseÚCachec                   s.   e Zd Zd‡ fdd„	Zdd„ Zdd„ Z‡  ZS )	ÚResourceCacheNc                s0   |d krt jjtƒ tdƒƒ}tt| ƒj|ƒ d S )Nzresource-cache)ÚosÚpathÚjoinr   ÚstrÚsuperr   Ú__init__)ÚselfÚbase)Ú	__class__© ú</tmp/pip-build-red6nood/pip/pip/_vendor/distlib/resources.pyr      s    zResourceCache.__init__c             C   s   dS )zã
        Is the cache stale for the given resource?

        :param resource: The :class:`Resource` being cached.
        :param path: The path of the resource in the cache.
        :return: True if the cache is stale.
        Tr   )r   Úresourcer
   r   r   r   Úis_stale"   s    	zResourceCache.is_stalec          	   C   s    |j j|ƒ\}}|dkr|}n~tjj| j| j|ƒ|ƒ}tjj|ƒ}tjj|ƒsXtj	|ƒ tjj
|ƒsjd}n| j||ƒ}|rœt|dƒ�}|j|jƒ W dQ R X |S )z¤
        Get a resource into the cache,

        :param resource: A :class:`Resource` instance.
        :return: The pathname of the resource in the cache.
        NTÚwb)ÚfinderÚget_cache_infor	   r
   r   r   Úprefix_to_dirÚdirnameÚisdirÚmakedirsÚexistsr   ÚopenÚwriteÚbytes)r   r   Úprefixr
   Úresultr   ÚstaleÚfr   r   r   Úget-   s    
zResourceCache.get)N)Ú__name__Ú
__module__Ú__qualname__r   r   r%   Ú__classcell__r   r   )r   r   r      s   r   c               @   s   e Zd Zdd„ ZdS )ÚResourceBasec             C   s   || _ || _d S )N)r   Úname)r   r   r+   r   r   r   r   H   s    zResourceBase.__init__N)r&   r'   r(   r   r   r   r   r   r*   G   s   r*   c               @   s@   e Zd ZdZdZdd„ Zedd„ ƒZedd„ ƒZed	d
„ ƒZ	dS )ÚResourcezÇ
    A class representing an in-package resource, such as a data file. This is
    not normally instantiated by user code, but rather by a
    :class:`ResourceFinder` which manages the resource.
    Fc             C   s   | j j| ƒS )z’
        Get the resource as a stream.

        This is not a property to make it obvious that it returns a new stream
        each time.
        )r   Ú
get_stream)r   r   r   r   Ú	as_streamU   s    zResource.as_streamc             C   s   t d krtƒ a t j| ƒS )N)Úcacher   r%   )r   r   r   r   Ú	file_path^   s    zResource.file_pathc             C   s   | j j| ƒS )N)r   Ú	get_bytes)r   r   r   r   r    e   s    zResource.bytesc             C   s   | j j| ƒS )N)r   Úget_size)r   r   r   r   Úsizei   s    zResource.sizeN)
r&   r'   r(   Ú__doc__Úis_containerr.   r   r0   r    r3   r   r   r   r   r,   M   s   	r,   c               @   s   e Zd ZdZedd„ ƒZdS )ÚResourceContainerTc             C   s   | j j| ƒS )N)r   Úget_resources)r   r   r   r   Ú	resourcesq   s    zResourceContainer.resourcesN)r&   r'   r(   r5   r   r8   r   r   r   r   r6   n   s   r6   c               @   s’   e Zd ZdZejjdƒrdZnd Zdd„ Zdd	„ Z	d
d„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZeejjƒZdd„ ZdS )!ÚResourceFinderz4
    Resource finder for file system resources.
    Újavaú.pycú.pyoú.classc             C   s.   || _ t|dd ƒ| _tjjt|ddƒƒ| _d S )NÚ
__loader__Ú__file__Ú )ÚmoduleÚgetattrÚloaderr	   r
   r   r   )r   rA   r   r   r   r   €   s    zResourceFinder.__init__c             C   s   t jj|ƒS )N)r	   r
   Úrealpath)r   r
   r   r   r   Ú_adjust_path…   s    zResourceFinder._adjust_pathc             C   sB   t |tƒrd}nd}|j|ƒ}|jd| jƒ tjj|Ž }| j|ƒS )Nó   /ú/r   )	Ú
isinstancer    ÚsplitÚinsertr   r	   r
   r   rE   )r   Úresource_nameÚsepÚpartsr"   r   r   r   Ú
_make_pathˆ   s    

zResourceFinder._make_pathc             C   s   t jj|ƒS )N)r	   r
   r   )r   r
   r   r   r   Ú_find”   s    zResourceFinder._findc             C   s
   d |j fS )N)r
   )r   r   r   r   r   r   —   s    zResourceFinder.get_cache_infoc             C   sD   | j |ƒ}| j|ƒsd }n&| j|ƒr0t| |ƒ}n
t| |ƒ}||_|S )N)rN   rO   Ú_is_directoryr6   r,   r
   )r   rK   r
   r"   r   r   r   Úfindš   s    



zResourceFinder.findc             C   s   t |jdƒS )NÚrb)r   r
   )r   r   r   r   r   r-   ¦   s    zResourceFinder.get_streamc          	   C   s    t |jdƒ�
}|jƒ S Q R X d S )NrR   )r   r
   Úread)r   r   r$   r   r   r   r1   ©   s    zResourceFinder.get_bytesc             C   s   t jj|jƒS )N)r	   r
   Úgetsize)r   r   r   r   r   r2   ­   s    zResourceFinder.get_sizec                s*   ‡fdd„‰ t ‡ fdd„tj|jƒD ƒƒS )Nc                s   | dko| j ˆ jƒ S )NÚ__pycache__)ÚendswithÚskipped_extensions)r$   )r   r   r   Úallowed±   s    z-ResourceFinder.get_resources.<locals>.allowedc                s   g | ]}ˆ |ƒr|‘qS r   r   )Ú.0r$   )rX   r   r   ú
<listcomp>´   s    z0ResourceFinder.get_resources.<locals>.<listcomp>)Úsetr	   Úlistdirr
   )r   r   r   )rX   r   r   r7   °   s    zResourceFinder.get_resourcesc             C   s   | j |jƒS )N)rP   r
   )r   r   r   r   r   r5   ¶   s    zResourceFinder.is_containerc             c   sŒ   | j |ƒ}|d k	rˆ|g}xn|r†|jdƒ}|V  |jr|j}xH|jD ]>}|sP|}ndj||gƒ}| j |ƒ}|jrz|j|ƒ qB|V  qBW qW d S )Nr   rG   )rQ   Úpopr5   r+   r8   r   Úappend)r   rK   r   ÚtodoÚrnamer+   Únew_nameÚchildr   r   r   Úiterator»   s     


zResourceFinder.iteratorN)r;   r<   r=   )r;   r<   )r&   r'   r(   r4   ÚsysÚplatformÚ
startswithrW   r   rE   rN   rO   r   rQ   r-   r1   r2   r7   r5   Ústaticmethodr	   r
   r   rP   rc   r   r   r   r   r9   v   s"   r9   c                   s`   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Z‡  ZS )ÚZipResourceFinderz6
    Resource finder for resources in .zip files.
    c                sZ   t t| ƒj|ƒ | jj}dt|ƒ | _t| jdƒr>| jj| _nt	j
| | _t| jƒ| _d S )Nr   Ú_files)r   rh   r   rC   ÚarchiveÚlenÚ
prefix_lenÚhasattrri   Ú	zipimportÚ_zip_directory_cacheÚsortedÚindex)r   rA   rj   )r   r   r   r   Ô   s    zZipResourceFinder.__init__c             C   s   |S )Nr   )r   r
   r   r   r   rE   ß   s    zZipResourceFinder._adjust_pathc             C   s¤   || j d … }|| jkrd}nX|r:|d tjkr:|tj }tj| j|ƒ}y| j| j|ƒ}W n tk
rt   d}Y nX |sŽtj	d|| j
jƒ ntj	d|| j
jƒ |S )NTr   Fz_find failed: %r %rz_find worked: %r %réÿÿÿÿ)rl   ri   r	   rL   Úbisectrq   rf   Ú
IndexErrorÚloggerÚdebugrC   r!   )r   r
   r"   Úir   r   r   rO   â   s    


zZipResourceFinder._findc             C   s&   | j j}|jdt|ƒ d … }||fS )Nr   )rC   rj   r
   rk   )r   r   r!   r
   r   r   r   r   ô   s    z ZipResourceFinder.get_cache_infoc             C   s   | j j|jƒS )N)rC   Úget_datar
   )r   r   r   r   r   r1   ù   s    zZipResourceFinder.get_bytesc             C   s   t j| j|ƒƒS )N)ÚioÚBytesIOr1   )r   r   r   r   r   r-   ü   s    zZipResourceFinder.get_streamc             C   s   |j | jd … }| j| d S )Né   )r
   rl   ri   )r   r   r
   r   r   r   r2   ÿ   s    zZipResourceFinder.get_sizec             C   s¤   |j | jd … }|r,|d tjkr,|tj7 }t|ƒ}tƒ }tj| j|ƒ}xV|t| jƒk rž| j| j|ƒsjP | j| |d … }|j	|j
tjdƒd ƒ |d7 }qJW |S )Nr   r   rr   )r
   rl   r	   rL   rk   r[   rs   rq   rf   ÚaddrI   )r   r   r
   Úplenr"   rw   Úsr   r   r   r7     s    
zZipResourceFinder.get_resourcesc             C   sj   || j d … }|r*|d tjkr*|tj7 }tj| j|ƒ}y| j| j|ƒ}W n tk
rd   d}Y nX |S )Nr   Frr   )rl   r	   rL   rs   rq   rf   rt   )r   r
   rw   r"   r   r   r   rP     s    

zZipResourceFinder._is_directory)r&   r'   r(   r4   r   rE   rO   r   r1   r-   r2   r7   rP   r)   r   r   )r   r   rh   Ð   s   rh   c             C   s   |t t| ƒ< d S )N)Ú_finder_registryÚtype)rC   Úfinder_makerr   r   r   Úregister_finder2  s    r‚   c             C   sŒ   | t krt |  }nv| tjkr$t| ƒ tj|  }t|ddƒ}|dkrJtdƒ‚t|ddƒ}tjt|ƒƒ}|dkrxtd|  ƒ‚||ƒ}|t | < |S )zŸ
    Return a resource finder for a package.
    :param package: The name of the package.
    :return: A :class:`ResourceFinder` instance for the package.
    Ú__path__Nz8You cannot get a finder for a module, only for a packager>   zUnable to locate finder for %r)	Ú_finder_cacherd   ÚmodulesÚ
__import__rB   r   r   r%   r€   )Úpackager"   rA   r
   rC   r�   r   r   r   r   9  s    


r   Ú	__dummy__c             C   sR   d}t j| ƒ tjj| ƒ}tjt|ƒƒ}|rNt}tj	j
| dƒ|_||_||ƒ}|S )z¬
    Return a resource finder for a path, which should represent a container.

    :param path: The path.
    :return: A :class:`ResourceFinder` instance for the path.
    Nr@   )ÚpkgutilÚget_importerrd   Úpath_importer_cacher%   r   r€   Ú_dummy_moduler	   r
   r   r?   r>   )r
   r"   rC   r   rA   r   r   r   Úfinder_for_pathU  s    
r�   )-Ú
__future__r   rs   ry   Úloggingr	   r‰   rd   Útypesrn   r@   r   Úutilr   r   r   Ú	getLoggerr&   ru   r/   r   Úobjectr*   r,   r6   r9   rh   r€   Úzipimporterr   Ú_frozen_importlib_externalZ_fiÚImportErrorÚ_frozen_importlibÚSourceFileLoaderÚ
FileFinderÚSourcelessFileLoaderÚAttributeErrorr‚   r„   r   Ú
ModuleTyper   rŒ   r�   r   r   r   r   Ú<module>   sH   
,!ZO



