
0c@_*                 @   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
 Z
 d d l m Z d d l m Z m Z m Z m Z e j e  Z d a Gd d   d e  Z Gd d	   d	 e  Z Gd
 d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z e d  e e
 j e i Z yR y d  d l  Z! Wn e" k
 rd  d l# Z! Yn Xe e e! j$ <e e e! j% <[! Wn e" e& f k
 rYn Xd 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path_to_cache_dirCachec                   s=   e  Z d  Z d   f d d  Z d d   Z d d   Z   S)ResourceCacheNc                sD   | 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-jynh7p1z/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 |  s t j	 |  t j j
 |  s d } n |  j | |  } | r t | d   } | j | j  Wd QRX| 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)__name__
__module____qualname__r   r   r&   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   I   s    	zResourceBase.__init__N)r'   r(   r)   r   r   r   r   r   r*   H   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_streamV   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!   f   s    zResource.bytesc             C   s   |  j  j |   S)N)r   get_size)r   r   r   r   sizej   s    zResource.sizeN)
r'   r(   r)   __doc__is_containerr.   r   r0   r!   r3   r   r   r   r   r,   N   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   	resourcesr   s    zResourceContainer.resourcesN)r'   r(   r)   r5   r   r8   r   r   r   r   r6   o   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   sC   | |  _  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   s_   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  } n9 |  j |  rH t |  |  } 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   SWd  QRXd  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 Sr   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 } x | r | j d  } | V| j r' | j } xb | j D]W } | sr | } n d j | | g  } |  j  |  } | j r | j |  q] | Vq] Wq' Wd  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   w   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   S)ZipResourceFinderz6
    Resource finder for resources in .zip files.
    c                s   t  t |   j |  |  j j } d t |  |  _ t |  j d  rY |  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 } np | rQ | d t j k rQ | t j } t j |  j |  } y |  j | j |  } Wn t k
 r d } Yn 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   s3   |  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 |  } xq | t |  j  k  r |  j | j |  s P|  j | | d   } | j	 | j
 t j d  d  | d 7} qi W| 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   s   | |  j  d   } | r9 | d t j k r9 | t j 7} t j |  j |  } y |  j | j |  } Wn t k
 r d } Yn 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   rh      s   rh   c             C   s   | t  t |   <d  S)N)_finder_registrytype)rC   finder_makerr   r   r   register_finder0  s    r   c             C   s   |  t  k r t  |  } n |  t j k r2 t |   t j |  } t | d d  } | d k ri t d   t | d d  } t j t |   } | d k r t 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   6  s    

r   	__dummy__c             C   sw   d } t  j |   t j j |   } t j t |   } | rs t } 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_pathR  s    	r   ).
__future__r   rs   ry   loggingr
   r   shutilrd   typesrn   r@   r   utilr   r   r   r   	getLoggerr'   ru   r/   r	   objectr*   r,   r6   r9   rh   r   zipimporterr   _frozen_importlib_externalZ_fiImportError_frozen_importlibSourceFileLoader
FileFinderAttributeErrorr   r   r   
ModuleTyper   r   r   r   r   r   r   <module>   sH   ",!ZN