
    OOId                     j    d dl Z d dlZd dlZd dlmZ ddlmZ  G d de          Zd Z	 	 	 	 	 dd
Z	dS )    N)MutableMapping   )	url_to_fsc                   t    e Zd ZdZddZd ZddZd Zd	 Zd
 Z	d Z
ddZddZd Zd Zd Zd Zd Z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                 n   || _         |                    |                              d          | _        |                    t	          j        |d                    d d         | _        |t          t          t          f}|| _
        || _        || _        |r4| j                             |          s| j                             |           |rh| j                             |          st          d|z            | j                             |dz              | j                             |dz              d S d S )N/xz@Path %s does not exist. Create  with the ``create=True`` keywordz/a)fs_strip_protocolrstriproot	posixpathjoin_root_key_to_strFileNotFoundErrorIsADirectoryErrorNotADirectoryErrormissing_exceptionscheckcreateexistsmkdir
ValueErrortouchrm)selfr   r   r   r   r   s         .lib/python3.11/site-packages/fsspec/mapping.py__init__zFSMap.__init__%   s7   &&t,,33C88	 " 2 29>$3L3L M Mcrc R%!!""
 #5
 	$7>>$'' $d### 	$7>>$''  8:>?   GMM$+&&&GJJtd{#####	$ 	$    c                     	 | j                             | j        d           | j                             | j                   dS #  Y dS xY w)z0Remove all keys below root - empties out mappingTN)r   r   r   r   r   s    r   clearzFSMap.clear>   sK    	GJJty$'''GMM$)$$$$$	DDs   ?A Araisec                 `     fd|D             }dk    rnd}	  j                             ||          t          t                    r
|d         in#  j        $ r}t
          |d}~ww xY w fd                                D             f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                 :    g | ]}                     |          S  _key_to_str.0kr   s     r   
<listcomp>z"FSMap.getitems.<locals>.<listcomp>Z   s'    333!!!$$333r!   r%   return)on_errorr   Nc                 b    i | ]+\  }}|t          |j                  rt                      n|,S r(   )
isinstancer   KeyErrorr,   r-   vr   s      r   
<dictcomp>z"FSMap.getitems.<locals>.<dictcomp>b   sI     
 
 
1 jD,CDDK


!
 
 
r!   c                 j    i | ]/\  }}d k    st          |         t                    &||         0S )r/   )r2   BaseException)r,   keyk2r0   outs      r   r6   z"FSMap.getitems.<locals>.<dictcomp>f   sK     
 
 
R8##:c"g}+M+M# R###r!   )r   catr2   bytesr   r3   itemszip)r   keysr0   keys2oeer;   s   ` `   @r   getitemszFSMap.getitemsF   s   ( 4333d333!W,,XX(	"'++eb+11C#u%% &Qxo& 	" 	" 	"!	"
 
 
 
		
 
 

 
 
 
 
tU++
 
 
 	
s   ;A 
A0#A++A0c                 |      fd|                                 D             } j                            |           dS )zSet the values of multiple items in the store

        Parameters
        ----------
        values_dict: dict(str, bytes)
        c                 \    i | ](\  }}                     |          t          |          )S r(   )r*   maybe_convertr4   s      r   r6   z"FSMap.setitems.<locals>.<dictcomp>s   s5    XXXDAq$""1%%}Q'7'7XXXr!   N)r>   r   pipe)r   values_dictvaluess   `  r   setitemszFSMap.setitemsl   sE     YXXXKDUDUDWDWXXXVr!   c                 T      j                              fd|D                        dS )z#Remove multiple keys from the storec                 :    g | ]}                     |          S r(   r)   r+   s     r   r.   z"FSMap.delitems.<locals>.<listcomp>x   s'    666AD$$Q''666r!   N)r   r   )r   r@   s   ` r   delitemszFSMap.delitemsv   s2    

666666677777r!   c                     t          |t                    sMt          j        dt                     t          |t
                    rt          |          }t          |          }| j         | S )zGenerate full path for the keyzAfrom fsspec 2023.5 onward FSMap non-str keys will raise TypeError)r2   strwarningswarnDeprecationWarninglisttupler   r   r9   s     r   r*   zFSMap._key_to_strz   sl    #s## 	MS"   #t$$ !Cjjc((C'....r!   c                 `    |t          | j                  d                             d          S )zStrip path of to leave key nameNr	   )lenr   lstrip)r   ss     r   _str_to_keyzFSMap._str_to_key   s)    TY!!"))#...r!   c                     |                      |          }	 | j                            |          }n## | j        $ r ||cY S t	          |          w xY w|S )zRetrieve data)r*   r   r<   r   r3   )r   r9   defaultr-   results        r   __getitem__zFSMap.__getitem__   sm    S!!	 W[[^^FF& 	  	  	 "3--	  s   2 AAc                 \    |                      ||          }	 | |= n# t          $ r Y nw xY w|S )zPop data)r_   r3   )r   r9   r]   r^   s       r   popz	FSMap.pop   sJ    !!#w//	S		 	 	 	D	s    
))c                     |                      |          }| j                            | j                            |          d           | j                            |t          |                     dS )zStore value in keyT)exist_okN)r*   r   mkdirs_parent	pipe_filerG   )r   r9   values      r   __setitem__zFSMap.__setitem__   sa    s##tws++d;;;#}U3344444r!   c                 Z      fd j                              j                  D             S )Nc              3   B   K   | ]}                     |          V  d S N)r[   )r,   r
   r   s     r   	<genexpr>z!FSMap.__iter__.<locals>.<genexpr>   s1      EE  ##EEEEEEr!   )r   findr   r#   s   `r   __iter__zFSMap.__iter__   s-    EEEETW\\$)-D-DEEEEr!   c                 Z    t          | j                            | j                            S rk   )rX   r   rm   r   r#   s    r   __len__zFSMap.__len__   s     47<<	**+++r!   c                 z    	 | j                             |                     |                     dS #  t          xY w)z
Remove keyN)r   r   r*   r3   rV   s     r   __delitem__zFSMap.__delitem__   s=    	GJJt'',,-----	Ns   -1 	:c                     |                      |          }| j                            |          o| j                            |          S )zDoes key exist in mapping?)r*   r   r   isfile)r   r9   paths      r   __contains__zFSMap.__contains__   s<    $$w~~d##<t(<(<<r!   c                 <    t           | j        | j        dd| j        ffS )NF)r   r   r   r   r#   s    r   
__reduce__zFSMap.__reduce__   s    ty$'5%9PQQQr!   )FFN)r%   rk   )__name__
__module____qualname____doc__r    r$   rD   rK   rN   r*   r[   r_   ra   rh   rn   rp   rr   rv   rx   r(   r!   r   r   r   	   s        6$ $ $ $2  $
 $
 $
 $
L  8 8 8/ / // / /	 	 	 	   5 5 5F F F, , ,  = = =
R R R R Rr!   r   c                     t          | t          j                  st          | d          rOt          | d          r#| j        j        dv r|                     d          } t          t          |                     } | S )N	__array__dtypeMmint64)r2   arrayhasattrr   kindviewr=   
memoryview)rg   s    r   rG   rG      su    %%% ))D)D )5'"" 	(u{'74'?'? JJw''Ej''((Lr!    Fc                 V    t          | fi |\  }}||n|}t          |||||          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   alternate_rootkwargsr   urlpathr   s	            r   
get_mapperr      sF    N C**6**KB+7>>WDr5&=OPPPPr!   )r   FFNN)
r   r   rQ   collections.abcr   corer   r   rG   r   r(   r!   r   <module>r      s          * * * * * *      oR oR oR oR oRN oR oR oRd   	
)Q )Q )Q )Q )Q )Qr!   