a
    h                      @   st   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	 e
dZG dd deZdd	 ZdddZdS )    N)MutableMapping)cached_property)	url_to_fszfsspec.mappingc                   @   s   e Zd ZdZd%ddZedd Zdd	 Zd&ddZdd Z	dd Z
dd Zdd Zd'ddZd(ddZdd Zdd Zdd Zdd  Zd!d" Zd#d$ ZdS ))FSMapa  Wrap a FileSystem instance as a mutable wrapping.

    The keys of the mapping become files under the given root, and the
    values (which must be bytes) the contents of those files.

    Parameters
    ----------
    root: string
        prefix for all the files
    fs: FileSystem instance
    check: bool (=True)
        performs a touch at the location, to check for write access.

    Examples
    --------
    >>> fs = FileSystem(**parameters)  # doctest: +SKIP
    >>> d = FSMap('my-data/path/', fs)  # doctest: +SKIP
    or, more likely
    >>> d = fs.get_mapper('my-data/path/')

    >>> d['loc1'] = b'Hello World'  # doctest: +SKIP
    >>> list(d.keys())  # doctest: +SKIP
    ['loc1']
    >>> d['loc1']  # doctest: +SKIP
    b'Hello World'
    FNc                 C   s   || _ ||| _|t|dd d | _|d u r@tttf}|| _	|| _
|| _|rn| j |sn| j | |r| j |std| d| j |d  | j |d  d S )NxzPath z9 does not exist. Create  with the ``create=True`` keywordz/a)fsZ_strip_protocolroot	posixpathjoin_root_key_to_strFileNotFoundErrorIsADirectoryErrorNotADirectoryErrormissing_exceptionscheckcreateexistsmkdir
ValueErrortouchrm)selfr	   r   r   r   r    r   </var/www/auris/lib/python3.9/site-packages/fsspec/mapping.py__init__)   s*    
zFSMap.__init__c                 C   s   ddl m} || j| jdS )z@dirfs instance that can be used with the same keys as the mapper   )DirFileSystem)pathr   )Zimplementations.dirfsr   r   r   )r   r   r   r   r   dirfsB   s    zFSMap.dirfsc                 C   sB   t d| j z"| j| jd | j| j W n   Y n0 dS )z0Remove all keys below root - empties out mappingzClear mapping at %sTN)loggerinfor	   r   r   r   r   r   r   r   clearI   s    zFSMap.clearraisec              
      s   fdd|D } dkr nd}z*j j||dttrJ|d iW n, jyx } zt|W Y d}~n
d}~0 0 fdd	 D  fd
d	t||D S )a  Fetch multiple items from the store

        If the backend is async-able, this might proceed concurrently

        Parameters
        ----------
        keys: list(str)
            They keys to be fetched
        on_error : "raise", "omit", "return"
            If raise, an underlying exception will be raised (converted to KeyError
            if the type is in self.missing_exceptions); if omit, keys with exception
            will simply not be included in the output; if "return", all keys are
            included in the output, but the value will be bytes or an exception
            instance.

        Returns
        -------
        dict(key, bytes|exception)
        c                    s   g | ]}  |qS r   _key_to_str.0kr"   r   r   
<listcomp>f       z"FSMap.getitems.<locals>.<listcomp>r$   return)on_errorr   Nc                    s(   i | ] \}}|t | jr t n|qS r   )
isinstancer   KeyErrorr(   r)   vr"   r   r   
<dictcomp>n   s   z"FSMap.getitems.<locals>.<dictcomp>c                    sH   i | ]@\}} d ks"t | ts| dkr4| n|t|qS )r,   r$   )r.   BaseExceptiongetr/   )r(   keyZk2)r-   outr   r   r2   r   s   )r   catr.   bytesr   r/   itemszip)r   keysr-   keys2Zoeer   )r-   r6   r   r   getitemsR   s    

zFSMap.getitemsc                    s&    fdd|  D } j| dS )zSet the values of multiple items in the store

        Parameters
        ----------
        values_dict: dict(str, bytes)
        c                    s    i | ]\}}  |t|qS r   )r&   maybe_convertr0   r"   r   r   r2      r+   z"FSMap.setitems.<locals>.<dictcomp>N)r9   r   pipe)r   Zvalues_dictvaluesr   r"   r   setitemsx   s    zFSMap.setitemsc                    s    j  fdd|D  dS )z#Remove multiple keys from the storec                    s   g | ]}  |qS r   r%   r'   r"   r   r   r*      r+   z"FSMap.delitems.<locals>.<listcomp>N)r   r   )r   r;   r   r"   r   delitems   s    zFSMap.delitemsc                 C   sD   t |ts0tdt t |tr(t|}t|}| j | dS )zGenerate full path for the keyzAfrom fsspec 2023.5 onward FSMap non-str keys will raise TypeError/)	r.   strwarningswarnDeprecationWarninglisttupler   rstrip)r   r5   r   r   r   r&      s    

zFSMap._key_to_strc                 C   s   |t | jd dS )zStrip path of to leave key nameNrD   )lenr	   lstrip)r   sr   r   r   _str_to_key   s    zFSMap._str_to_keyc              
   C   sh   |  |}z| j|}W nH | jyb } z.|durD|W  Y d}~S t||W Y d}~n
d}~0 0 |S )zRetrieve dataN)r&   r   r7   r   r/   )r   r5   defaultr)   resultexcr   r   r   __getitem__   s    
 zFSMap.__getitem__c                 C   s.   |  ||}z
| |= W n ty(   Y n0 |S )zPop data)rS   r/   )r   r5   rP   rQ   r   r   r   pop   s    
z	FSMap.popc                 C   s8   |  |}| jj| j|dd | j|t| dS )zStore value in keyT)exist_okN)r&   r   Zmkdirs_parentZ	pipe_filer?   )r   r5   valuer   r   r   __setitem__   s    
zFSMap.__setitem__c                    s    fdd j  jD S )Nc                 3   s   | ]}  |V  qd S N)rO   )r(   r   r"   r   r   	<genexpr>   r+   z!FSMap.__iter__.<locals>.<genexpr>)r   findr	   r"   r   r"   r   __iter__   s    zFSMap.__iter__c                 C   s   t | j| jS rY   )rL   r   r[   r	   r"   r   r   r   __len__   s    zFSMap.__len__c              
   C   sF   z| j | | W n* ty@ } zt|W Y d}~n
d}~0 0 dS )z
Remove keyN)r   r   r&   	Exceptionr/   )r   r5   rR   r   r   r   __delitem__   s    zFSMap.__delitem__c                 C   s   |  |}| j|S )zDoes key exist in mapping?)r&   r   isfile)r   r5   r   r   r   r   __contains__   s    
zFSMap.__contains__c                 C   s   t | j| jdd| jffS )NF)r   r	   r   r   r"   r   r   r   
__reduce__   s    zFSMap.__reduce__)FFN)r$   )N)N)__name__
__module____qualname____doc__r   r   r   r#   r>   rB   rC   r&   rO   rS   rT   rX   r\   r]   r_   ra   rb   r   r   r   r   r      s$   

	
&


	r   c                 C   sF   t | tjst| drBt| dr6| jjdv r6| d} tt| } | S )NZ	__array__dtypeZMmint64)r.   arrayhasattrrg   kindviewr8   
memoryview)rW   r   r   r   r?      s
    
r?    Fc           	      K   s6   t | fi |\}}|dur |n|}t|||||dS )a  Create key-value interface for given URL and options

    The URL will be of the form "protocol://location" and point to the root
    of the mapper required. All keys will be file-names below this location,
    and their values the contents of each key.

    Also accepts compound URLs like zip::s3://bucket/file.zip , see ``fsspec.open``.

    Parameters
    ----------
    url: str
        Root URL of mapping
    check: bool
        Whether to attempt to read from the location before instantiation, to
        check that the mapping does exist
    create: bool
        Whether to make the directory corresponding to the root before
        instantiating
    missing_exceptions: None or tuple
        If given, these exception types will be regarded as missing keys and
        return KeyError when trying to read data. By default, you get
        (FileNotFoundError, IsADirectoryError, NotADirectoryError)
    alternate_root: None or str
        In cases of complex URLs, the parser may fail to pick the correct part
        for the mapper root, so this arg can override

    Returns
    -------
    ``FSMap`` instance, the dict-like key-value store.
    N)r   )r   r   )	urlr   r   r   Zalternate_rootkwargsr   Zurlpathr	   r   r   r   
get_mapper   s    'rq   )rn   FFNN)ri   loggingr
   rF   collections.abcr   	functoolsr   Zfsspec.corer   	getLoggerr    r   r?   rq   r   r   r   r   <module>   s    
 ;     