o
    Rhj                     @  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	m
Z
 ddlmZ ddlmZ ddlmZ dd	 ZG d
d dejZG dd dejZG dd dejZG dd dejZdd ZdS )    )annotationsN)Iterator   )abc)only)ZipPathc                 C  s   t tj| S N)itercollectionsOrderedDictfromkeys)items r   ]/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/importlib_resources/readers.pyremove_duplicates   s   r   c                   @  s$   e Zd Zdd Zdd Zdd ZdS )
FileReaderc                 C  s   t |jj| _d S r   )pathlibPathpathparent)selfloaderr   r   r   __init__   s   zFileReader.__init__c                 C     t | j|S z{
        Return the file system path to prevent
        `resources.path()` from creating a temporary
        copy.
        strr   joinpathr   resourcer   r   r   resource_path      zFileReader.resource_pathc                 C     | j S r   r   r   r   r   r   files!      zFileReader.filesN)__name__
__module____qualname__r   r    r%   r   r   r   r   r      s    r   c                      s4   e Zd Zdd Z fddZdd Zdd Z  ZS )		ZipReaderc                 C  sH   |j dd| _ ||r|d\}}}|  j |d 7  _ |j| _d S )N\/.)prefixreplace
is_package
rpartitionarchive)r   r   module_namer   r   r   r   &   s
   
zZipReader.__init__c              
     s6   zt  |W S  ty } zt|jd d }~ww Nr   )superopen_resourceKeyErrorFileNotFoundErrorargs)r   r   exc	__class__r   r   r8   -   s   zZipReader.open_resourcec                 C  s   |   |}| o| S )zf
        Workaround for `zipfile.Path.is_file` returning true
        for non-existent paths.
        )r%   r   is_fileexists)r   r   targetr   r   r   is_resource3   s   zZipReader.is_resourcec                 C  s   t | j| jS r   )r   r2   r.   r$   r   r   r   r%   ;      zZipReader.files)r'   r(   r)   r   r8   rB   r%   __classcell__r   r   r=   r   r*   %   s
    r*   c                      sx   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	 fddZ
edd Zdd Zedd Zdd Z  ZS )MultiplexedPathz
    Given a series of Traversable objects, implement a merged
    version of the interface across all objects. Useful for
    namespace packages which may be multihomed at a single
    name.
    c                 G  sF   t ttt|| _| jsd}t|tdd | jD s!tdd S )Nz.MultiplexedPath must contain at least one pathc                 s  s    | ]}|  V  qd S r   is_dir.0r   r   r   r   	<genexpr>L       z+MultiplexedPath.__init__.<locals>.<genexpr>z)MultiplexedPath only supports directories)listmap_ensure_traversabler   _pathsr:   allNotADirectoryError)r   pathsmessager   r   r   r   G   s   zMultiplexedPath.__init__c                 C  sF   dd | j D }td}tjt||d|d}t| jdd |D S )Nc                 s  s"    | ]}|  D ]}|V  qqd S r   )iterdir)rI   r   childr   r   r   rJ   P   s     z*MultiplexedPath.iterdir.<locals>.<genexpr>r5   )keyc                 s  s    | ]\}}|V  qd S r   r   )rI   r5   locsr   r   r   rJ   S   rK   )rO   operator
attrgetter	itertoolsgroupbysortedrM   _follow)r   childrenby_namegroupsr   r   r   rT   O   s   
zMultiplexedPath.iterdirc                 C     t |  dNz is not a filer:   r$   r   r   r   
read_bytesU   rC   zMultiplexedPath.read_bytesc                 O  ra   rb   rc   r   r;   kwargsr   r   r   	read_textX   rC   zMultiplexedPath.read_textc                 C     dS )NTr   r$   r   r   r   rG   [      zMultiplexedPath.is_dirc                 C  rh   )NFr   r$   r   r   r   r?   ^   ri   zMultiplexedPath.is_filec                   s4   zt  j| W S  tjy   | jd j|  Y S w r6   )r7   r   r   TraversalErrorrO   )r   descendantsr=   r   r   r   a   s
   zMultiplexedPath.joinpathc                 C  sZ   t |d\}}}zt|W S  ty,   z| | W  Y S  ty+   t| Y  Y S w w )z
        Construct a MultiplexedPath if needed.

        If children contains a sole element, return it.
        Otherwise, return a MultiplexedPath of the items.
        Unless one of the items is not a Directory, then return the first.
           )rZ   teer   
ValueErrorrQ   next)clsr^   subdirsone_dirone_filer   r   r   r]   i   s   	
zMultiplexedPath._followc                 O  ra   rb   rc   re   r   r   r   open|   rC   zMultiplexedPath.openc                 C  s   | j d jS r6   )rO   r5   r$   r   r   r   r5      s   zMultiplexedPath.namec                 C  s"   d dd | jD }d| dS )Nz, c                 s  s    | ]	}d | d V  qdS )'Nr   rH   r   r   r   rJ          z+MultiplexedPath.__repr__.<locals>.<genexpr>zMultiplexedPath())joinrO   )r   rR   r   r   r   __repr__   s   zMultiplexedPath.__repr__)r'   r(   r)   __doc__r   rT   rd   rg   rG   r?   r   classmethodr]   rt   propertyr5   ry   rD   r   r   r=   r   rE   ?   s    

rE   c                   @  sN   e Zd Zdd ZedddZedd
dZedddZdd Z	dd Z
dS )NamespaceReaderc                 C  s0   dt |vr
tdtttt| j| | _d S )NNamespacePathzInvalid path)r   rn   rE   filterboolrM   _resolver   )r   namespace_pathr   r   r   r      s   zNamespaceReader.__init__returnabc.Traversable | Nonec                 C  s   dd |  |D }t|dS )a  
        Given an item from a namespace path, resolve it to a Traversable.

        path_str might be a directory on the filesystem or a path to a
        zipfile plus the path within the zipfile, e.g. ``/foo/bar`` or
        ``/foo/baz.zip/inner_dir`` or ``foo\baz.zip\inner_dir\sub``.

        path_str might also be a sentinel used by editable packages to
        trigger other behaviors (see python/importlib_resources#311).
        In that case, return None.
        c                 s  s    | ]	}|  r|V  qd S r   rF   )rI   candr   r   r   rJ      rv   z+NamespaceReader._resolve.<locals>.<genexpr>N)_candidate_pathsro   )rp   path_strdirsr   r   r   r      s   
zNamespaceReader._resolver   r   Iterator[abc.Traversable]c                 c  s"    t |V  | |E d H  d S r   )r   r   _resolve_zip_path)rp   r   r   r   r   r      s   z NamespaceReader._candidate_pathsc              	   c  s    t ttd| D ]7}ttttt	% | |
 d  ddd }t| d |  |dV  W d    n1 s=w   Y  qd S )Nz[\\/]r+   r,   )reversedrL   refinditer
contextlibsuppressr:   IsADirectoryErrorrQ   PermissionErrorendr/   r   startlstrip)r   matchinnerr   r   r   r      s    z!NamespaceReader._resolve_zip_pathc                 C  r   r   r   r   r   r   r   r       r!   zNamespaceReader.resource_pathc                 C  r"   r   r#   r$   r   r   r   r%      r&   zNamespaceReader.filesN)r   r   )r   r   r   r   )r   r   )r'   r(   r)   r   r{   r   r   staticmethodr   r    r%   r   r   r   r   r}      s    r}   c                 C  s(   t | ts| S tjdtdd t| S )zk
    Convert deprecated string arguments to traversables (pathlib.Path).

    Remove with Python 3.15.
    z<String arguments are deprecated. Pass a Traversable instead.rl   )
stacklevel)
isinstancer   warningswarnDeprecationWarningr   r   r#   r   r   r   rN      s   

rN   )
__future__r   r
   r   rZ   rX   r   r   r   collections.abcr    r   
_itertoolsr   compat.py39r   r   TraversableResourcesr   r*   TraversablerE   r}   rN   r   r   r   r   <module>   s$    I3