
    g                          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j                  d      Z G d de      Zd Z	 	 	 	 	 d
d	Zy)    N)MutableMapping)cached_property)	url_to_fszfsspec.mappingc                       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y)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'
    Nc                 *   || _         |j                  |      | _        |j                  t        j                  |d            d d | _        |t        t        t        f}|| _	        || _
        || _        |r6| j                   j                  |      s| j                   j                  |       |rg| j                   j                  |      st        d| d      | j                   j                  |dz          | j                   j!                  |dz          y y )NxzPath z9 does not exist. Create  with the ``create=True`` keywordz/a)fs_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   s         C/var/www/openai/venv/lib/python3.12/site-packages/fsspec/mapping.py__init__zFSMap.__init__)   s    &&t,	 " 2 29>>$3L Mcr R%!!""
 #5
77>>$'d#77>>$' D6 "8 9  GGMM$+&GGJJtd{#     c                 J    ddl m}  || j                  | j                        S )z@dirfs instance that can be used with the same keys as the mapper   )DirFileSystem)pathr   )implementations.dirfsr"   r   r   )r   r"   s     r   dirfszFSMap.dirfsB   s     	9$"7"7DGGDDr   c                     t         j                  d| j                         	 | j                  j	                  | j                  d       | j                  j                  | j                         y#  Y yxY w)z0Remove all keys below root - empties out mappingzClear mapping at %sTN)loggerinfor   r   r   r   r   s    r   clearzFSMap.clearI   sN    )4995	GGJJtyy$'GGMM$))$	s   AA. .A2c                 T   |D cg c]  }| j                  |       }}|dk(  r|nd}	 | j                  j                  ||      }t        |t              r|d   |i}|j                         D ci c](  \  }}|t        || j
                        r
t               n|* }}}t        ||      D 	
ci c]C  \  }	}
|dk(  st        ||
   t              s&|	|dk(  r||
   n|j                  |
t        |
            E c}
}	S c c}w # | j
                  $ r}t        |d}~ww xY wc c}}w c c}
}	w )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)
        raisereturn)on_errorr   N)_key_to_strr   cat
isinstancebytesr   KeyErroritemszipBaseExceptionget)r   keysr.   kkeys2oeoutevkeyk2s              r   getitemszFSMap.getitemsR   s8   ( /33d!!!$d3!W,X(	"''++eb+1C#u%Qxo
 		
#1 jD,C,CD
!K# 	 
 tU+
+R8#:c"g}+M H/RSWWR"5NN+
 	
 4 && 	"!	"

s)   C;4D  0-D/AD$ DDDc                     |j                         D ci c]   \  }}| j                  |      t        |      " }}}| j                  j	                  |       yc c}}w )zSet the values of multiple items in the store

        Parameters
        ----------
        values_dict: dict(str, bytes)
        N)r4   r/   maybe_convertr   pipe)r   values_dictr9   r>   valuess        r   setitemszFSMap.setitemsx   sT     EPDUDUDWXDWDAq$""1%}Q'77DWXV Ys   %Ac                 ~    | j                   j                  |D cg c]  }| j                  |       c}       yc c}w )z#Remove multiple keys from the storeN)r   r   r/   )r   r8   r9   s      r   delitemszFSMap.delitems   s/    

6AD$$Q'676s   :c                     t        |t              s@t        j                  dt               t        |t
              rt        |      }t        |      }| j                   | j                  d      S )zGenerate full path for the keyzAfrom fsspec 2023.5 onward FSMap non-str keys will raise TypeError/)	r1   strwarningswarnDeprecationWarninglisttupler   rstrip)r   r?   s     r   r/   zFSMap._key_to_str   s]    #s#MMS" #t$Cjc(C''(.55c::r   c                 P    |t        | j                        d j                  d      S )zStrip path of to leave key nameNrK   )lenr   lstrip)r   ss     r   _str_to_keyzFSMap._str_to_key   s#    TYY!"))#..r   c                     | j                  |      }	 | j                  j                  |      }|S # | j                  $ r}||cY d}~S t	        |      |d}~ww xY w)zRetrieve dataN)r/   r   r0   r   r3   )r   r?   defaultr9   resultexcs         r   __getitem__zFSMap.__getitem__   s^    S!	)WW[[^F
 	 && 	)"3-S(	)s    0 AAAAAc                 R    | j                  ||      }	 | |= |S # t        $ r Y |S w xY w)zPop data)r\   r3   )r   r?   rY   rZ   s       r   popz	FSMap.pop   s@    !!#w/	S	   		s    	&&c                     | j                  |      }| j                  j                  | j                  j                  |      d       | j                  j	                  |t        |             y)zStore value in keyT)exist_okN)r/   r   mkdirs_parent	pipe_filerC   )r   r?   values      r   __setitem__zFSMap.__setitem__   sL    s#twws+d;#}U34r   c                 `      fd j                   j                   j                        D        S )Nc              3   @   K   | ]  }j                  |        y wN)rW   ).0r	   r   s     r   	<genexpr>z!FSMap.__iter__.<locals>.<genexpr>   s     E-D  #-Ds   )r   findr   r)   s   `r   __iter__zFSMap.__iter__   s    ETWW\\$))-DEEr   c                 ^    t        | j                  j                  | j                              S rh   )rT   r   rk   r   r)   s    r   __len__zFSMap.__len__   s    477<<		*++r   c                     	 | j                   j                  | j                  |             y# t        $ r}t        |d}~ww xY w)z
Remove keyN)r   r   r/   	Exceptionr3   )r   r?   r[   s      r   __delitem__zFSMap.__delitem__   s:    	$GGJJt'',- 	$#	$s   *- 	A=Ac                 Z    | j                  |      }| j                  j                  |      S )zDoes key exist in mapping?)r/   r   isfile)r   r?   r#   s      r   __contains__zFSMap.__contains__   s%    $ww~~d##r   c                 X    t         | j                  | j                  dd| j                  ffS )NF)r   r   r   r   r)   s    r   
__reduce__zFSMap.__reduce__   s&    tyy$''5%9P9PQQQr   )FFN)r,   rh   )__name__
__module____qualname____doc__r   r   r%   r*   rA   rG   rI   r/   rW   r\   r^   re   rl   rn   rq   rt   rv    r   r   r   r      sk    6$2 E E$
L8;/	5F,$$
Rr   r   c                     t        | t        j                        st        | d      rIt        | d      r)| j                  j                  dv r| j                  d      } t        t        |             } | S )N	__array__dtypeMmint64)r1   arrayhasattrr~   kindviewr2   
memoryview)rd   s    r   rC   rC      sX    %%)D5'"u{{'7'74'? JJw'Ej'(Lr   c                 L    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.
    )r   )r   r   )	urlr   r   r   alternate_rootkwargsr   urlpathr   s	            r   
get_mapperr      s8    N C*6*KB+7>WDr5&=OPPr   ) FFNN)r   loggingr   rM   collections.abcr   	functoolsr   fsspec.corer   	getLoggerr'   r   rC   r   r{   r   r   <module>r      sZ        * % !			+	,wRN wRt 	
)Qr   