B
     S]2              	   @   s  d dl Z d dl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mZ d dlmZ d d	lmZ d d
lmZmZmZmZmZ d dlmZ d dlmZmZ d dlmZ dddddddddg	Zee ef Z!ee e j"f Z#edddZ$e dddZ%eeej& dddZ'dd  Z(e!e#ed!d"dZ)d1e!e#e e ed%d&dZ*e!e#e+d!d'dZ,d2e!e#e e e d%d(dZ-ee!e#ee d!d)dZ.e!e e/d*d+dZ0e!ee  dd,dZ1G d-d. d.ej&Z2d/d0 Z3dS )3    N   )abc)contextmanagersuppress)import_module)ResourceLoader)BytesIOTextIOWrapper)Path)
ModuleType)IterableIteratorOptionalSetUnion)cast)BinaryIOTextIO)ZipImportErrorPackageResourcecontentsis_resourceopen_binary	open_textpathread_binary	read_text)returnc             C   s\   t | dr0| jjdkr*td| jjqX| S n(t| }|jjdkrTtd| n|S dS )zTake a package name or module object and return the module.

    If a name, the module is imported.  If the passed or imported module
    object is not a package, raise an exception.
    __spec__Nz{!r} is not a package)hasattrr   submodule_search_locations	TypeErrorformatnamer   )packagemodule r'   $lib/python3.7/importlib/resources.py_get_package"   s    
r)   c             C   s,   t j| \}}|r$td| n|S dS )zNormalize a path by ensuring it is a string.

    If the resulting string contains path separators, an exception is raised.
    z{!r} must be only a file nameN)osr   split
ValueErrorr#   )r   parent	file_namer'   r'   r(   _normalize_path6   s    r/   )r%   r   c             C   s,   | j }t|jdr(ttj|j|jS d S )Nget_resource_reader)r   r    loaderr   resources_abcResourceReaderr0   r$   )r%   specr'   r'   r(   _get_resource_readerB   s
    r5   c             C   s&   | j jd ks| j js"td| d S )NzPackage has no location )r   originhas_locationFileNotFoundError)r%   r'   r'   r(   _check_locationP   s    r9   )r%   resourcer   c       
      C   s   t |}t| } t| }|dk	r*||S t|  tj| jj	}tj
|}tj||}yt|ddS  tk
r   tt| jj}d}t| jjdrtt ||}W dQ R X |dkr| jj}d||}	t|	nt|S Y nX dS )zDReturn a file-like object opened for binary reading of the resource.Nrb)modeget_dataz{!r} resource not found in {!r})r/   r)   r5   open_resourcer9   r*   r   abspathr   r6   dirnamejoinopenOSErrorr   r   r1   r    r   r=   r$   r#   r8   r   )
r%   r:   readerabsolute_package_pathpackage_path	full_pathr1   datapackage_namemessager'   r'   r(   r   U   s.    


utf-8strict)r%   r:   encodingerrorsr   c             C   s  t |}t| } t| }|dk	r2t||||S t|  tj| j	j
}tj|}tj||}yt|d||dS  tk
 r   tt| j	j}d}	t| j	jdrtt ||}	W dQ R X |	dkr| j	j}
d||
}t|ntt|	||S Y nX dS )zBReturn a file-like object opened for text reading of the resource.Nr)r<   rM   rN   r=   z{!r} resource not found in {!r})r/   r)   r5   r	   r>   r9   r*   r   r?   r   r6   r@   rA   rB   rC   r   r   r1   r    r   r=   r$   r#   r8   r   )r%   r:   rM   rN   rD   rE   rF   rG   r1   rH   rI   rJ   r'   r'   r(   r   t   s.    

c          	   C   s.   t |}t| } t| |
}| S Q R X dS )z+Return the binary contents of the resource.N)r/   r)   r   read)r%   r:   fpr'   r'   r(   r      s    c          	   C   s2   t |}t| } t| |||
}| S Q R X dS )zReturn the decoded string of the resource.

    The decoding-related arguments have the same semantics as those of
    bytes.decode().
    N)r/   r)   r   rP   )r%   r:   rM   rN   rQ   r'   r'   r(   r      s    	c       	      c   s   t |}t| } t| }|dk	rLyt||V  dS  tk
rH   Y qTX nt|  t| jjj	}|| }|
 rz|V  nvt| |}| }W dQ R X t \}}z$t|| t| t|V  W dyt| W n tk
r   Y nX X dS )ak  A context manager providing a file path object to the resource.

    If the resource does not already exist on its own on the file system,
    a temporary file will be created. If the file was created, the file
    will be deleted upon exiting the context manager (no exception is
    raised if the file was deleted prior to the context manager
    exiting).
    N)r/   r)   r5   r
   resource_pathr8   r9   r   r6   r-   existsr   rP   tempfileZmkstempr*   writecloseremove)	r%   r:   rD   package_directoryZ	file_pathrQ   rH   fdZraw_pathr'   r'   r(   r      s2    

)r%   r$   r   c          	   C   sz   t | } t| t| }|dk	r*||S ytt| }W n ttfk
rR   dS X ||kr`dS t| j	j
j| }| S )zYTrue if 'name' is a resource inside 'package'.

    Directories are *not* resources.
    NF)r)   r/   r5   r   setr   NotADirectoryErrorr8   r
   r   r6   r-   is_file)r%   r$   rD   Zpackage_contentsr   r'   r'   r(   r      s    
c             C   sT   t | } t| }|dk	r | S | jjdks4| jjs8dS t| jjj}t	|S dS )zReturn an iterable of entries in 'package'.

    Note that not all entries are resources.  Specifically, directories are
    not considered resources.  Use `is_resource()` on each entry returned here
    to check if it is a resource or not.
    Nr'   )
r)   r5   r   r   r6   r7   r
   r-   r*   listdir)r%   rD   rX   r'   r'   r(   r      s    c               @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )_ZipImportResourceReaderzPrivate class used to support ZipImport.get_resource_reader().

    This class is allowed to reference all the innards and private parts of
    the zipimporter.
    c             C   s   || _ || _d S )N)zipimporterfullname)selfr_   r`   r'   r'   r(   __init__  s    z!_ZipImportResourceReader.__init__c             C   sN   | j dd}| d| }yt| j|S  tk
rH   t|Y nX d S )N./)r`   replacer   r_   r=   rC   r8   )ra   r:   fullname_as_pathr   r'   r'   r(   r>     s    z&_ZipImportResourceReader.open_resourcec             C   s   t d S )N)r8   )ra   r:   r'   r'   r(   rR     s    z&_ZipImportResourceReader.resource_pathc             C   sF   | j dd}| d| }y| j| W n tk
r@   dS X dS )Nrc   rd   FT)r`   re   r_   r=   rC   )ra   r$   rf   r   r'   r'   r(   r   %  s    z$_ZipImportResourceReader.is_resourcec          	   c   s   t | j| j}|| jj}|jdks.t|j}t	 }xp| jj
D ]d}yt ||}W n tk
rp   wDY nX |jj}t|dkr|jV  qD||krD|| |V  qDW d S )Nz__init__.pyr   )r
   r_   get_filenamer`   Zrelative_toarchiver$   AssertionErrorr-   rZ   _filesr,   lenadd)ra   Zfullname_pathZrelative_pathrF   Zsubdirs_seenfilenameZrelativeparent_namer'   r'   r(   r   0  s     

z!_ZipImportResourceReader.contentsN)	__name__
__module____qualname____doc__rb   r>   rR   r   r   r'   r'   r'   r(   r^     s   r^   c             C   s2   y|  |sd S W n tk
r&   d S X t| |S )N)
is_packager   r^   )r_   r`   r'   r'   r(   _zipimport_get_resource_readerQ  s    
rt   )rK   rL   )rK   rL   )4r*   rT    r   r2   
contextlibr   r   	importlibr   Zimportlib.abcr   ior   r	   Zpathlibr
   typesr   typingr   r   r   r   r   r   Z	typing.ior   r   	zipimportr   __all__strr   PathLiker   r)   r/   r3   r5   r9   r   r   bytesr   r   r   boolr   r   r^   rt   r'   r'   r'   r(   <module>   sN   ! 
 ,E