U
    i2e                     @   sd   d dl mZ d dlmZ ddlmZ G dd dZG dd dZdd
dZG dd dZ	dd Z
dS )    )suppress)TextIOWrapper   )abcc                   @   s(   e Zd ZdZdd fddZdd ZdS )	SpecLoaderAdapterz>
    Adapt a package spec to adapt the underlying loader.
    c                 C   s   | j S N)loaderspec r   A/tmp/pip-unpacked-wheel-bn52x7jx/importlib_resources/_adapters.py<lambda>       zSpecLoaderAdapter.<lambda>c                 C   s   || _ ||| _d S r   )r
   r   )selfr
   adapterr   r   r   __init__   s    zSpecLoaderAdapter.__init__c                 C   s   t | j|S r   )getattrr
   r   namer   r   r   __getattr__   s    zSpecLoaderAdapter.__getattr__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )TraversableResourcesLoaderz9
    Adapt a loader to provide TraversableResources.
    c                 C   s
   || _ d S r   r	   r   r
   r   r   r   r      s    z#TraversableResourcesLoader.__init__c                 C   s   t | j S r   )CompatibilityFilesr
   _nativer   r   r   r   get_resource_reader   s    z.TraversableResourcesLoader.get_resource_readerN)r   r   r   r   r   r   r   r   r   r   r      s   r   rc                 O   s8   |dkrt | f||S |dkr$| S td| dd S )Nr   rbzInvalid mode value 'z"', only 'r' and 'rb' are supported)r   
ValueError)filemodeargskwargsr   r   r   _io_wrapper    s
    r&   c                   @   sr   e Zd Zd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	e
d
d Zdd Zdd Zdd ZdS )r   zj
    Adapter for an existing or non-existent resource reader
    to provide a compatibility .files().
    c                   @   sJ   e Zd ZdZdd Zdd Zdd ZeZdd	 Ze	d
d Z
dddZdS )zCompatibilityFiles.SpecPathzk
        Path tied to a module spec.
        Can be read and exposes the resource reader children.
        c                 C   s   || _ || _d S r   )_spec_reader)r   r
   readerr   r   r   r   4   s    z$CompatibilityFiles.SpecPath.__init__c                    s*    j stdS t fdd j  D S )Nr   c                 3   s   | ]}t  j|V  qd S r   )r   	ChildPathr(   ).0pathr   r   r   	<genexpr>;   s   z6CompatibilityFiles.SpecPath.iterdir.<locals>.<genexpr>)r(   itercontentsr-   r   r-   r   iterdir8   s
    z#CompatibilityFiles.SpecPath.iterdirc                 C   s   dS NFr   r-   r   r   r   is_file@   s    z#CompatibilityFiles.SpecPath.is_filec                 C   s   | j st|S t| j |S r   )r(   r   
OrphanPathr*   r   otherr   r   r   joinpathE   s    
z$CompatibilityFiles.SpecPath.joinpathc                 C   s   | j jS r   )r'   r   r-   r   r   r   r   J   s    z CompatibilityFiles.SpecPath.namer   c                 O   s   t | jd |f||S r   )r&   r(   open_resourcer   r#   r$   r%   r   r   r   openN   s    z CompatibilityFiles.SpecPath.openN)r   r   r   r   r   r   r1   r3   is_dirr7   propertyr   r:   r   r   r   r   SpecPath.   s   
r>   c                   @   sN   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	dd Z
dddZdS )zCompatibilityFiles.ChildPathzw
        Path tied to a resource reader child.
        Can be read but doesn't expose any meaningful children.
        c                 C   s   || _ || _d S r   )r(   _name)r   r)   r   r   r   r   r   W   s    z%CompatibilityFiles.ChildPath.__init__c                 C   s   t dS Nr   r/   r-   r   r   r   r1   [   s    z$CompatibilityFiles.ChildPath.iterdirc                 C   s   | j | jS r   )r(   is_resourcer   r-   r   r   r   r3   ^   s    z$CompatibilityFiles.ChildPath.is_filec                 C   s
   |    S r   )r3   r-   r   r   r   r<   a   s    z#CompatibilityFiles.ChildPath.is_dirc                 C   s   t | j|S r   )r   r4   r   r5   r   r   r   r7   d   s    z%CompatibilityFiles.ChildPath.joinpathc                 C   s   | j S r   )r?   r-   r   r   r   r   g   s    z!CompatibilityFiles.ChildPath.namer   c                 O   s   t | j| j|f||S r   )r&   r(   r8   r   r9   r   r   r   r:   k   s     z!CompatibilityFiles.ChildPath.openN)r   r;   r   r   r   r   r*   Q   s   
r*   c                   @   sJ   e Zd ZdZdd Zdd Zdd ZeZdd	 Ze	d
d Z
dddZdS )zCompatibilityFiles.OrphanPathz
        Orphan path, not tied to a module spec or resource reader.
        Can't be read and doesn't expose any meaningful children.
        c                 G   s   t |dk rtd|| _d S )Nr   z/Need at least one path part to construct a path)lenr!   _path)r   
path_partsr   r   r   r   v   s    z&CompatibilityFiles.OrphanPath.__init__c                 C   s   t dS r@   rA   r-   r   r   r   r1   {   s    z%CompatibilityFiles.OrphanPath.iterdirc                 C   s   dS r2   r   r-   r   r   r   r3   ~   s    z%CompatibilityFiles.OrphanPath.is_filec                 C   s   t j| j|f S r   )r   r4   rD   r5   r   r   r   r7      s    z&CompatibilityFiles.OrphanPath.joinpathc                 C   s
   | j d S )N)rD   r-   r   r   r   r      s    z"CompatibilityFiles.OrphanPath.namer   c                 O   s   t dd S )NzCan't open orphan path)FileNotFoundErrorr9   r   r   r   r:      s    z"CompatibilityFiles.OrphanPath.openN)r   r;   r   r   r   r   r4   p   s   
r4   c                 C   s
   || _ d S r   r	   r   r   r   r   r      s    zCompatibilityFiles.__init__c              
   C   s2   t t  | jj| jjW  5 Q R  S Q R X d S r   )r   AttributeErrorr
   r   r   r   r-   r   r   r   r(      s    
zCompatibilityFiles._readerc                 C   s   | j }t|dr|S | S )zB
        Return the native reader if it supports files().
        files)r(   hasattr)r   r)   r   r   r   r      s    zCompatibilityFiles._nativec                 C   s   t | j|S r   )r   r(   )r   attrr   r   r   r      s    zCompatibilityFiles.__getattr__c                 C   s   t | j| jS r   )r   r>   r
   r(   r-   r   r   r   rI      s    zCompatibilityFiles.filesN)r   r   r   r   r   ZTraversabler>   r*   r4   r   r=   r(   r   r   rI   r   r   r   r   r   (   s   #
r   c                 C   s   t | jtS )z`
    Construct a package spec with traversable compatibility
    on the spec/loader/reader.
    )r   __spec__r   )packager   r   r   	wrap_spec   s    rN   N)r   )
contextlibr   ior    r   r   r   r&   r   rN   r   r   r   r   <module>   s   
{