a
    If)                     @   s  d dl Z ddl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 d d
lmZ d dlmZ d dlmZmZmZmZ d dlmZ d dlmZmZ g dZeeef Zeee j f Z!eddd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(d4ee!eeed!d"d#Z)ee!e*dd$d%Z+d5ee!eeed!d&d'Z,eej-dd(d)Z.ee!d*dd+d,Z/ed-d. Z0eee1d/d0d1Z2eee dd2d3Z3dS )6    N   )abc)_common)as_file)contextmanagersuppress)import_module)ResourceLoader)BytesIOTextIOWrapper)Path)
ModuleType)ContextManagerIterableOptionalUnion)cast)BinaryIOTextIO)PackageResourcer   contentsfilesis_resourceopen_binary	open_textpathread_binary	read_text)returnc                 C   s   t | dr| S t| S )z)If name is a string, resolve to a module.__spec__)hasattrr   name r$   $lib/python3.9/importlib/resources.py_resolve$   s    
r&   c                 C   s&   t | }|jjdu r"td| |S )zTake a package name or module object and return the module.

    If a name, the module is imported.  If the resolved module
    object is not a package, raise an exception.
    Nz{!r} is not a package)r&   r    submodule_search_locations	TypeErrorformat)packagemoduler$   r$   r%   _get_package+   s    r,   c                 C   s&   t j| \}}|r"t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 name)osr   split
ValueErrorr)   )r   parent	file_namer$   r$   r%   _normalize_path7   s    r2   )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ResourceReaderr3   r#   )r*   specr$   r$   r%   _get_resource_readerB   s    r8   c                 C   s&   | j jd u 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    r=   )r*   resourcer   c           
      C   s   t |}t| } t| }|dur*||S tj| jjp:d}tj	|}tj
||}zt|ddW S  ty   tt| jj}d}t| jjdrtt ||}W d   n1 s0    Y  |du r| jj}d||}	t|	t| Y S 0 dS )zDReturn a file-like object opened for binary reading of the resource.Nznon-existent filerb)modeget_dataz{!r} resource not found in {!r})r2   r,   r8   open_resourcer-   r   abspathr    r9   dirnamejoinopenOSErrorr   r	   r4   r!   r   rA   r#   r)   r;   r
   )
r*   r>   readerZabsolute_package_pathpackage_path	full_pathr4   dataZpackage_namemessager$   r$   r%   r   U   s2    


(r   utf-8strict)r*   r>   encodingerrorsr   c                 C   s   t t| |||dS )zBReturn a file-like object opened for text reading of the resource.)rO   rP   )r   r   )r*   r>   rO   rP   r$   r$   r%   r   s   s    r   c                 C   s6   t | |}| W  d   S 1 s(0    Y  dS )z+Return the binary contents of the resource.N)r   read)r*   r>   fpr$   r$   r%   r   |   s    r   c                 C   s:   t | |||}| W  d   S 1 s,0    Y  dS )zReturn the decoded string of the resource.

    The decoding-related arguments have the same semantics as those of
    bytes.decode().
    N)r   rQ   )r*   r>   rO   rP   rR   r$   r$   r%   r      s    	r   c                 C   s   t t| S )z3
    Get a Traversable resource from a package
    )r   from_packager,   r<   r$   r$   r%   r      s    r   zContextManager[Path]c                 C   s2   t t| }|rt||S tt| t|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).
    )r8   r,   _path_from_readerr   r   r   joinpathr2   )r*   r>   rH   r$   r$   r%   r      s
    r   c                 c   s   t |}tt" t| |V  W d    d S 1 s80    Y  | |}tj|j|d}|V  W d    n1 sx0    Y  d S )N)suffix)	r2   r   r;   r   resource_pathrB   r   Z	_tempfilerQ   )rH   r>   Znorm_resourceZopener_readerresr$   r$   r%   rT      s    
 
rT   )r*   r#   r   c                 C   sT   t | } t| t| }|dur*||S tt| }||vrBdS t| |  S )zYTrue if 'name' is a resource inside 'package'.

    Directories are *not* resources.
    NF)	r,   r2   r8   r   setr   r   rS   is_file)r*   r#   rH   Zpackage_contentsr$   r$   r%   r      s    
r   c                 C   sd   t | } t| }|dur | S | jjdu p6| jjdk}|sD| jjsHdS tdd t| 	 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.
    N	namespacer$   c                 s   s   | ]}|j V  qd S )Nr"   ).0itemr$   r$   r%   	<genexpr>       zcontents.<locals>.<genexpr>)
r,   r8   r   r    r9   r:   listr   rS   iterdir)r*   rH   r[   r$   r$   r%   r      s    
r   )rM   rN   )rM   rN   )4r-    r   r5   r   r   
contextlibr   r   	importlibr   importlib.abcr	   ior
   r   pathlibr   typesr   typingr   r   r   r   r   Z	typing.ior   r   __all__strr   PathLiker   r&   r,   r2   r6   r8   r=   r   r   bytesr   r   Traversabler   r   rT   boolr   r   r$   r$   r$   r%   <module>   sZ   
   	  

