a
    {Œ@h)  ã                   @   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 )z±Take 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 )z€Normalize 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ƒrÆttƒ� | |¡}W d  ƒ n1 s¼0    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 )z‹Return 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 )zõReturn 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   
ÿ   ýý	  ýýþ

