
    ̒ge                     v    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  G d de          Z	d Z
	 	 	 	 	 ddZdS )    N)MutableMapping)cached_property   )	url_to_fsc                       e Zd ZdZddZe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                 p   || _         |                    |                              d          | _        |                    t	          j        |d                    d d         | _        |t          t          t          f}|| _
        || _        || _        |r4| j                             |          s| j                             |           |ri| j                             |          st          d| d          | j                             |dz              | j                             |dz              d S d S )N/xzPath z9 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__&   s@   &&t,,33C88	 " 2 29>$3L3L M Mcrc R%!!""
 #5
 	$7>>$'' $d### 	$7>>$''  9D 9 9 9   GMM$+&&&GJJtd{#####	$ 	$    c                 <    ddl m}  || j        | j                  S )z@dirfs instance that can be used with the same keys as the mapperr   )DirFileSystem)pathr   )implementations.dirfsr$   r   r   )r   r$   s     r    dirfszFSMap.dirfs?   s0     	988888}$"7DGDDDDr"   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.clearF   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>b   s'    333!!!$$333r"   r+   return)on_errorr   Nc                 b    i | ]+\  }}|t          |j                  rt                      n|,S r.   )
isinstancer   KeyErrorr2   r3   vr   s      r    
<dictcomp>z"FSMap.getitems.<locals>.<dictcomp>j   sI     
 
 
1 jD,CDDK


!
 
 
r"   c                 j    i | ]/\  }}d k    st          |         t                    &||         0S )r5   )r8   BaseException)r2   keyk2r6   outs      r    r<   z"FSMap.getitems.<locals>.<dictcomp>n   sK     
 
 
R8##:c"g}+M+M# R###r"   )r   catr8   bytesr   r9   itemszip)r   keysr6   keys2oeerA   s   ` `   @r    getitemszFSMap.getitemsN   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.   )r0   maybe_convertr:   s      r    r<   z"FSMap.setitems.<locals>.<dictcomp>{   s5    XXXDAq$""1%%}Q'7'7XXXr"   N)rD   r   pipe)r   values_dictvaluess   `  r    setitemszFSMap.setitemst   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/   r1   s     r    r4   z"FSMap.delitems.<locals>.<listcomp>   s'    666AD$$Q''666r"   N)r   r   )r   rF   s   ` r    delitemszFSMap.delitems~   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)r8   strwarningswarnDeprecationWarninglisttupler   r   r?   s     r    r0   zFSMap._key_to_str   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)r0   r   rB   r   r9   )r   r?   defaultr3   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)re   r9   )r   r?   rc   rd   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)r0   r   mkdirs_parent	pipe_filerM   )r   r?   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)ra   )r2   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 rq   )r^   r   rs   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   r0   r9   r\   s     r    __delitem__zFSMap.__delitem__   s=    	GJJt'',,-----	Ns   -1 	:c                     |                      |          }| j                            |          o| j                            |          S )zDoes key exist in mapping?)r0   r   r   isfile)r   r?   r%   s      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+   rq   )__name__
__module____qualname____doc__r!   r   r'   r*   rJ   rQ   rT   r0   ra   re   rg   rn   rt   rv   rx   r{   r}   r.   r"   r    r   r   
   s:        6$ $ $ $2 E E _E  $
 $
 $
 $
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)r8   arrayhasattrr   kindviewrC   
memoryview)rm   s    r    rM   rM      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   rW   collections.abcr   	functoolsr   corer   r   rM   r   r.   r"   r    <module>r      s          * * * * * * % % % % % %      vR vR vR vR vRN vR vR vRr   	
)Q )Q )Q )Q )Q )Qr"   