
    wg4                       d dl mZ d dlZd dlZ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mZmZmZ d dlmZmZ d dlmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZ d dlmZ d dlm Z  d dl!m"Z" d dl#m$Z$ erd dlm%Z%  ejL                  d      Z' G d de"      Z( G d de      Z) G d de)      Z* G d de*      Z+ G d d      Z,y)    )annotationsN)rmtree)TYPE_CHECKINGAnyCallableClassVar)AbstractFileSystem
filesystem)DEFAULT_CALLBACK)compr)	BaseCache	MMapCache)BlocksizeMismatchError)create_cache_mapper)CacheMetadata)AbstractBufferedFile)Transaction)infer_compression)AbstractCacheMapperzfsspec.cachedc                      e Zd ZddZy)WriteCachedTransactionc                f   | j                   D cg c]  }|j                   }}| j                   D cg c]  }|j                   }}|r| j                  j	                  ||       | j                   j                          d| j                  _        d | j                  _        d | _        y c c}w c c}w NF)filespathfnfsputclear_intrans_transaction)selfcommitfrpathslpathss        b/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/fsspec/implementations/cached.pycompletezWriteCachedTransaction.complete   s    "&**-Q!&&-- $

+1!$$++GGKK'

 # .+s
   B)B.NT)__name__
__module____qualname__r(        r'   r   r      s    r.   r   c                       e Zd ZU dZdZded<   	 	 	 	 	 	 	 	 	 	 d	 	 	 d fdZed        Zd Z	d Z
d	 Zd
 Zd Zd Zd ZddZd Z	 	 	 	 ddZd ZddZd ZddZ fdZd Zd Z xZS )CachingFileSystemaS  Locally caching filesystem, layer over any other FS

    This class implements chunk-wise local storage of remote files, for quick
    access after the initial download. The files are stored in a given
    directory with hashes of URLs for the filenames. If no directory is given,
    a temporary one is used, which should be cleaned up by the OS after the
    process ends. The files themselves are sparse (as implemented in
    :class:`~fsspec.caching.MMapCache`), so only the data which is accessed
    takes up space.

    Restrictions:

    - the block-size must be the same for each access of a given file, unless
      all blocks of the file have already been read
    - caching can only be applied to file-systems which produce files
      derived from fsspec.spec.AbstractBufferedFile ; LocalFileSystem is also
      allowed, for testing
    )
blockcachecachedzClassVar[str | tuple[str, ...]]protocolc                N    t           di | ||t        d      |du |du z  st        d      |dk(  r9t        j                         }|g}t        j                    j                  |       nt        |t              r|g}n|}t        j                  |d   d       | _        |xs i  _        | _        | _        | _        |	 _        d _        ||
t        d      |
|
 _        nt)        ||nd	       _        t        |t              r|n4t        |j*                  t              r|j*                  n|j*                  d
    _        t/         j                         _         j3                          ||nt5        |fi  j                   _         fd}| _        y)a  

        Parameters
        ----------
        target_protocol: str (optional)
            Target filesystem protocol. Provide either this or ``fs``.
        cache_storage: str or list(str)
            Location to store files. If "TMP", this is a temporary directory,
            and will be cleaned up by the OS when this process ends (or later).
            If a list, each location will be tried in the order given, but
            only the last will be considered writable.
        cache_check: int
            Number of seconds between reload of cache metadata
        check_files: bool
            Whether to explicitly see if the UID of the remote file matches
            the stored one before using. Warning: some file systems such as
            HTTP cannot reliably give a unique hash of the contents of some
            path, so be sure to set this option to False.
        expiry_time: int
            The time in seconds after which a local copy is considered useless.
            Set to falsy to prevent expiry. The default is equivalent to one
            week.
        target_options: dict or None
            Passed to the instantiation of the FS, if fs is None.
        fs: filesystem instance
            The target filesystem to run against. Provide this or ``protocol``.
        same_names: bool (optional)
            By default, target URLs are hashed using a ``HashCacheMapper`` so
            that files from different backends with the same basename do not
            conflict. If this argument is ``true``, a ``BasenameCacheMapper``
            is used instead. Other cache mapper options are available by using
            the ``cache_mapper`` keyword argument. Only one of this and
            ``cache_mapper`` should be specified.
        compression: str (optional)
            To decompress on download. Can be 'infer' (guess from the URL name),
            one of the entries in ``fsspec.compression.compr``, or None for no
            decompression.
        cache_mapper: AbstractCacheMapper (optional)
            The object use to map from original filenames to cached filenames.
            Only one of this and ``same_names`` should be specified.
        Nz9Please provide filesystem instance(fs) or target_protocolz@Both filesystems (fs) and target_protocol may not be both given.TMPTexist_okzMCannot specify both same_names and cache_mapper in CachingFileSystem.__init__Fr   c                j    j                   j                  t              j                  |             S N)r   _strip_protocoltype)r   r"   s    r'   r;   z3CachingFileSystem.__init__.<locals>._strip_protocol   s'    77**4:+E+Ed+KLLr.   r-   )super__init__
ValueErrortempfilemkdtempweakreffinalize_remove_tempdir
isinstancestrosmakedirsstoragekwargscache_checkcheck_filesexpirycompression_cache_size_mapperr   r3   target_protocolr   	_metadata
load_cacher
   r   r;   )r"   rQ   cache_storagerK   rL   expiry_timetarget_optionsr   
same_namesrN   cache_mapperrJ   tempdirrI   r;   	__class__s   `              r'   r>   zCachingFileSystem.__init__?   s   n 	"6":/1K  d
$67R  E!&&(GiGT4#7#7A--(/'
GBK$/$*&&!&
  !l&>-  #'DL.(4
%DL /3/ !+BKK!="++2;;q> 	
 't||4"J,V$++,V	M *9r.   c                :    	 t        |        y # t        $ r Y y w xY wr:   )r   	Exception)rY   s    r'   rD   z!CachingFileSystem._remove_tempdir   s     	7O 		s    	c                L    t        j                  | j                  d   d       y )Nr6   Tr7   )rG   rH   rI   r"   s    r'   _mkcachezCachingFileSystem._mkcache   s    
DLL$t4r.   c                    | j                   0| j                  d   }t        d      j                  |d      | _         | j                   S )zReturn size of cache in bytes.

        If more than one cache directory is in use, only the size of the last
        one (the writable cache directory) is returned.
        r6   fileT)withdirs)rO   rI   r
   du)r"   	cache_dirs     r'   
cache_sizezCachingFileSystem.cache_size   sG     #R(I)&144Y4NDr.   c                    | j                   j                          | j                          t        j                         | _        y)z#Read set of stored blocks from fileN)rR   loadr_   time
last_cacher^   s    r'   rS   zCachingFileSystem.load_cache   s(    ))+r.   c                    | j                          | j                  j                          t        j                         | _        d| _        y)z#Save set of stored blocks from fileN)r_   rR   saverh   ri   rO   r^   s    r'   
save_cachezCachingFileSystem.save_cache   s0    ))+r.   c                    | j                          | j                  syt        j                         | j                  z
  | j                  kD  }t	        d | j
                  D              }|s|s| j                          yy)z0Reload caches if time elapsed or any disappearedNc              3  Z   K   | ]#  }t         j                  j                  |       % y wr:   )rG   r   exists).0rI   s     r'   	<genexpr>z1CachingFileSystem._check_cache.<locals>.<genexpr>   s     LGw/Ls   )+)r_   rK   rh   ri   allrI   rS   )r"   timecond	existconds      r'   _check_cachezCachingFileSystem._check_cache   s^    99;043C3CCLt||LL	9OO %r.   c                |    | j                  |      }| j                          | j                  j                  ||       S )z Is path in cache and still valid)r;   ru   rR   
check_filer"   r   s     r'   _check_filezCachingFileSystem._check_file   s5    ##D)~~((t44r.   c                b    t        | j                  d          | j                          d| _        y)zRemove all files and metadata from the cache

        In the case of multiple cache locations, this clears only the last one,
        which is assumed to be the read/write one.
        r6   N)r   rI   rS   rO   r^   s    r'   clear_cachezCachingFileSystem.clear_cache   s'     	t||B r.   c                V   |s| j                   }| j                          | j                  j                  |      \  }}|D ]7  }t        j
                  j                  |      s#t	        j                  |       9 |r(t        | j                  d          | j                          d| _        y)a  Remove all expired files and metadata from the cache

        In the case of multiple cache locations, this clears only the last one,
        which is assumed to be the read/write one.

        Parameters
        ----------
        expiry_time: int
            The time in seconds after which a local copy is considered useless.
            If not defined the default is equivalent to the attribute from the
            file caching instantiation.
        r6   N)rM   ru   rR   clear_expiredrG   r   ro   remover   rI   rS   rO   )r"   rU   expired_fileswritable_cache_emptyr   s        r'   clear_expired_cachez%CachingFileSystem.clear_expired_cache   s     ++K.2nn.J.J;.W++ 	Bww~~b!		"	  4<<#$OOr.   c                    | j                  |      }| j                  j                  |      }|t        j                  |       d| _        y)zRemove cached version of given file

        Deletes local copy of the given (remote) path. If it is found in a cache
        location which is not the last, it is assumed to be read-only, and
        raises PermissionError
        N)r;   rR   pop_filerG   r~   rO   )r"   r   r   s      r'   pop_from_cachez CachingFileSystem.pop_from_cache  s@     ##D)^^$$T*>IIbMr.   c           	          j                  |      } j                  j                  |      }d|vr"  j                  j                  |f||||d|S  j                  |      }|rL|\  }}|d   |d   }
}	|
du r"t        j                  d|       t        ||      S t        j                  d|       n j                  |      }	t        j                  j                   j                  d   |	      }t               }
||	|
t        j                          j                  j                  |      d	} j                  j!                  ||       t        j                  d
|        j#                            j                  j                  |f||||dd| j$                  r6 j$                  dk(  rt'        |      n j$                  }t)        |   d      d|v r1|d   j*                  k7  r.t-        d|d    dj*                   d      j*                  |d<   t/        j*                  j0                  j2                  ||
      _        j6                   fd_         j9                          S )a!  Wrap the target _open

        If the whole file exists in the cache, just open it locally and
        return that.

        Otherwise, open the file on the target FS, and make it have a mmap
        cache pointing to the location which we determine, in our cache.
        The ``blocks`` instance is shared, so as the mmap cache instance
        updates, so does the entry in our ``cached_files`` attribute.
        We monkey-patch this file, so that when it closes, we call
        ``close_and_update`` to save the state of the blocks.
        r)mode
block_size
autocommitcache_optionsr   blocksTOpening local copy of %sz#Opening partially cached copy of %sr6   originalr   r   rh   uidz!Creating local sparse file for %snone)r   r   r   r   
cache_typeinferrbr   	blocksizezDCached file must be reopened with same block size as original (old: z, new )c                 (    j                         S r:   )close_and_update)closer$   r"   s   r'   <lambda>z)CachingFileSystem._open.<locals>.<lambda>o  s    $//59 r.   )r;   r   _openry   loggerdebugopenrP   rG   r   joinrI   setrh   ukeyrR   update_filer_   rN   r   r   r   r   r   _fetch_rangesizecacher   rl   )r"   r   r   r   r   r   rJ   detailr   hashr   compr   r$   s   `           @@r'   r   zCachingFileSystem._open  sf   * ##D)ww&&t,d? 477==%%+   !!$'JFB!$<)9&D~7>B~%LL>E<<%Ddll2.5BUF  		ww||D)F NN&&tV4LL<dC 	DGGMM
!!'
 
  ##w. "$'%% 
 dAD)A& k"akk1,//5k/B.C DKK=+  #$++F;AKKVL9r.   c                8    | j                   j                  |      S r:   )r   _parentrx   s     r'   r   zCachingFileSystem._parents  s    wwt$$r.   c                $    | j                  |      S r:   )rP   )r"   r   argss      r'   	hash_namezCachingFileSystem.hash_namev  s     ||D!!r.   c                   |j                   ry| j                  |j                        }| j                  j	                  ||       	 t
        j                  d       | j                          t
        j                  d        |        d|_         y# t        $ r t
        j                  d       Y /t        $ r t
        j                  d       Y Nw xY w)z9Called when a file is closing, so store the set of blocksNzgoing to savesavedz&Cache saving failed while closing filez-Cache save failed due to interpreter shutdownT)
closedr;   r   rR   on_close_cached_filer   r   rl   OSError	NameError)r"   r$   r   r   s       r'   r   z"CachingFileSystem.close_and_update{  s    88##AFF+++At4	JLL)OOLL!
 	  	CLLAB 	JLLHI	Js   :B C/CCc                :    | j                   j                  ||      S r:   )r   ls)r"   r   r   s      r'   r   zCachingFileSystem.ls  s    wwzz$''r.   c                j    dv r fdS dv rt         dv r$t               j                  j                         S dv rt	        t                     S dk(  rt               S t
        j                   d      }|j                  dd       }|v r|   S ||j                  v r|j                     S t        |      }t	        |      }t        j                  |      st        j                  |      r*t        |d	      r|j                  |j                  ||      S |S t                 S )
N>*   r   catgetheadinfor   pipetailr   isdir__eq__ro   isfileto_dictto_json__hash__r>   r_   cat_filer   	open_many	pipe_file
__reduce__re   
cat_ranges
get_mapperrS   
local_file
read_blockrl   ry   r{   commit_manyru   r   end_transaction__getattribute___paths_from_pathr   start_transaction_make_local_detailsr   c                 X     t        t                    j                        | i |S r:   )getattrr<   __get__)r   kwitemr"   s     r'   r   z4CachingFileSystem.__getattribute__.<locals>.<lambda>  s0    'NwtDz4'@'H'H'N(( r.   )__reduce_ex__)transaction)_cachetransaction_typerZ   __dict__r   __self__)AttributeErrorr<   r   r   r   objectr   r   r   inspect
isfunctionisdatadescriptorhasattrr   r=   )r"   r   dr   clsmrZ   s   ``    r'   r   z"CachingFileSystem.__getattribute__  s:    +
 +
\  $$  ?":))11$77114:t,,;:##D*5UU419T7N^r{{"{{4((r(CT"A""1%)A)A!)DAz*ajj.@ yyS))H 7+D11r.   c                   | |u ryt        |t        |             sy| j                  |j                  k(  xr | j                  |j                  k(  xr | j                  |j                  k(  xr | j
                  |j
                  k(  xrj | j                  |j                  k(  xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S )zTest for equality.TF)
rE   r<   rI   rJ   rK   rL   rM   rN   rP   rQ   )r"   others     r'   r   zCachingFileSystem.__eq__  s    5=%d,LLEMM) >u||+>  E$5$55>   E$5$55> u||+	>
   E$5$55> -> $$(=(==		
r.   c                   t        t        | j                              t        t        | j                              z  t        | j
                        z  t        | j                        z  t        | j                        z  t        | j                        z  t        | j                        z  t        | j                        z  S )zCalculate hash.)r   tuplerI   rF   rJ   rK   rL   rM   rN   rP   rQ   r^   s    r'   r   zCachingFileSystem.__hash__  s     t||$%3t{{#$%4##$% 4##$% 4;;	 
 4##$% 4<< ! 4''()		
r.   )
Nr5   
   Fi:	 NNNNN)rW   zbool | NonerX   zAbstractCacheMapper | Noner:   )r   NTN)r   rF   r   r   returnrF   r)   )r*   r+   r,   __doc__r3   __annotations__r>   staticmethodrD   r_   re   rS   rl   ru   ry   r{   r   r   r   r   r   r   r   r   r   r   __classcell__rZ   s   @r'   r0   r0   )   s    & 1IH-H "&37o9  o9 1o9b  5	 & 	5  <   Yv%"
"(O2b
"
r.   r0   c                  @    e Zd ZdZdZdZd Zd Zd Zdde	fd	Z
dd
Zy)WholeFileCacheFileSystema  Caches whole remote files on first access

    This class is intended as a layer over any other file system, and
    will make a local copy of each file accessed, so that all subsequent
    reads are local. This is similar to ``CachingFileSystem``, but without
    the block-wise functionality and so can work even when sparse files
    are not allowed. See its docstring for definition of the init
    arguments.

    The class still needs access to the remote store for listing files,
    and may refresh cached files.
    	filecacheTc                   |D cg c]  }|j                    }}d|j                  v r| j                          nl|D cg c]`  }t        | j                  |f|j                  t
        j                   j                  | j                  d   | j                  |            d|b c}S | j                  rt        |D cg c]  }| j                  |       }}t        ||      D 	cg c]
  \  }}	|	r	| }
}}	t        ||      D 	cg c]A  \  }}	t
        j                   j                  | j                  d   | j                  |            C }}}	t        ||      D 	cg c]
  \  }}	|	r	| }}}	|
r| j                  j                  |
|       |
D cg c]D  }|| j                  |      dt        j                         | j                  j                  |      dF }}t        |
|      D ]!  \  }}| j                   j#                  ||       # | j%                          d }t        ||      D cg c]&  \  }}t'        |r ||      n||j                        ( c}}S c c}w c c}w c c}w c c}	}w c c}	}w c c}	}w c c}w c c}}w )Nr   r6   r   r   Tr   c                0    t        | t              r| d   S | S )N   )rE   r   r   s    r'   	firstpartz5WholeFileCacheFileSystem.open_many.<locals>.firstpart<  s    &r512a59r9r.   r   )r   r   r_   LocalTempFiler   rG   r   rI   rP   rN   NotImplementedErrorry   zipr   rh   r   rR   r   rl   r   )r"   
open_filesrJ   ofpathsr   spdetailspr   downpathdownfn0r   downfn	newdetailr   r   fn0fn1s                      r'   r   z"WholeFileCacheFileSystem.open_many  sX   #-.R..*//!MMO "	  GG $ww||DLL$4dll46HI	
 	 	 %%278B4##B'88"%eW"5?$!QQA?? E7+
1 GGLLb)4<<?;
 
 #&gw"7AQq"AAGGKK&) %	  !%,,t," IIK77<<-	I 	 !$Hi 8 9f**489OO	:  1
S 33CjooF
 	
_ /	 9?
 B	$
s=   IA%I";I'$
I,/I,AI2
I8(I8A	I>.+Jc                p   | j                   j                  |D cg c]  }|j                   c}|D cg c]  }|j                   c}       |D cg c]  }|j	                          c} |D ]"  }	 t        j                  |j                         $ d | _	        y c c}w c c}w c c}w # t        $ r Y Gw xY wr:   )
r   r   r   r   r   rG   r~   nameFileNotFoundErrorrO   )r"   r   r$   s      r'   r   z$WholeFileCacheFileSystem.commit_manyE  s    :.aQTT.0LA0LM&'q' 	A		!&&!	   /0L'
 % s"   BB
B$1B))	B54B5c                L   | j                  |      }t        j                  j                  | j                  d   |      }||dt        j
                         | j                  j                  |      d}| j                  j                  ||       t        j                  d|       |S )Nr6   Tr   Copying %s to local cache)rP   rG   r   r   rI   rh   r   r   rR   r   r   r   )r"   r   r   r   r   s        r'   r   z,WholeFileCacheFileSystem._make_local_detailsP  s    ||D!WW\\$,,r*D1IIK77<<%
 	""400$7	r.   Fraisec                p   | j                  |||j                  d            }g }g }g }	i }
|j                         D ]t  }	 | j                  |      }|s4| j	                  |      }|j                  |       |j                  |       nt        |t              r|nd |f\  }}|	j                  |       v |r,| j                  j                  ||       | j                          |j                  t        |             t        ||	      D ]>  \  }}t        |d      5 }|j!                         |
|<   d d d        |j#                  d       @ t        |t$              rt        |      dk(  r|du r|
|d      }
|
S # t        $ r,}|dk(  r |dk(  r||
|<   |j                  |       Y d }~hd }~ww xY w# 1 sw Y   xY w)	Nmaxdepth)	recursiver  r  r   r   r   Fr   )expand_pathr   copyry   r   appendrE   r   r\   r~   r   rl   set_sizelenr   r   readrelative_updaterF   )r"   r   r  on_errorcallbackrJ   r   getpaths
storepathsfnsoutr  r   r   er$   s                   r'   r   zWholeFileCacheFileSystem.cat^  s      I

:0F ! 
 
 	 A ))!,11!4BOOA&%%b)+5fe+D4QW.JFB

2	 " GGKK*-OO#e*%_ 	(EArb$ "1A"$$Q'	( dC SZ1_e9KeAh-C
%   w&x'CFQ " "s%    A1E4F,4	F)=!F$$F),F5	c                   | j                  |      }d|vru| j                  |      }t        j                  j	                  | j
                  d   |      }|j                         D ci c]  \  }}|dvr|| }}}t        | |f||d|S | j                  |      }	|	r\|	\  }	}|	d   |	d   }}
|du r:t        j                  d|       t        ||      }|	j                  d	      |_        |S t        d
| d      | j                  |      }||d<   | j!                          | j"                  r | j$                  j&                  |fi |5 }t        |d      5 }t)        |t*              r0t-        d|j.                  j0                  |j2                        |_        | j"                  dk(  rt5        |      n| j"                  }t7        |   |d      }d}|r2t9        |dd      }|j;                  |      }|j=                  |       |r2d d d        d d d        n| j$                  j?                  ||       | jA                          | j'                  ||      S c c}}w # 1 sw Y   VxY w# 1 sw Y   =xY w)Nr   r6   r   r   r   r   r   r   Tr   r   z&Attempt to open partially cached file z as a wholly cached filer   wbr   r   r   r   r     P )!r;   rP   rG   r   r   rI   itemsr   ry   r   r   r   r   r   r?   r   r_   rN   r   r   rE   r   r   r   fetcherr   r   r   r   r  writeget_filerl   )r"   r   r   rJ   r   r   kvuser_specified_kwargsr   _r   r$   f2r   datablocks                    r'   r   zWholeFileCacheFileSystem._open  sf   ##D)d?<<%Ddll2.5B #LLN%AqII 1%! % !tW$2WAVWW!!$'JFBtfX&6vA~7> TN#ZZ
3
 <TF.0 
 ))$/Bv 	t.v. #!T"d^ #ra!56'177??AFFCAG ''72 &d+)) 
 $K-#A{I>E66%=DHHTN # # #  GGT2&zz$%%i%D# # # #s+   'I	I$B,II$I!	I$$I-Nr   )r*   r+   r,   r   r3   r   r   r   r   r   r   r   r-   r.   r'   r   r     s9     HJ3
j	 " !+Z9&r.   r   c                  x     e Zd ZdZdZdZeZ fdZd Z	d Z
d Zd fd	Zdd	Zd
 ZddZ	 d fd	ZddZ xZS )SimpleCacheFileSystema  Caches whole remote files on first access

    This class is intended as a layer over any other file system, and
    will make a local copy of each file accessed, so that all subsequent
    reads are local. This implementation only copies whole files, and
    does not keep any metadata about the download time or file details.
    It is therefore safer to use in multi-threaded/concurrent situations.

    This is the only of the caching filesystems that supports write: you will
    be given a real local open file, and upon close and commit, it will be
    uploaded to the target filesystem; the writability or the target URL is
    not checked until that time.

    simplecacheTc                    |j                         }dD ]  }d||<   	 t        |   di | | j                  D ]9  }t        j
                  j                  |      r#t	        j                  |d       ; y )N)rK   rU   rL   FTr7   r-   )r  r=   r>   rI   rG   r   ro   rH   )r"   rJ   r   keyrI   rZ   s        r'   r>   zSimpleCacheFileSystem.__init__  sg    [[]@ 	CBsG	2|| 	4G77>>'*Gd3	4r.   c                    | j                          | j                  |      }| j                  D ]E  }t        j                  j                  ||      }t        j                  j                  |      sC|c S  y r:   )ru   rP   rI   rG   r   r   ro   )r"   r   sharI   r   s        r'   ry   z!SimpleCacheFileSystem._check_file  sX    ll4 || 	Ggs+Bww~~b!		r.   c                     y r:   r-   r^   s    r'   rl   z SimpleCacheFileSystem.save_cache      r.   c                     y r:   r-   r^   s    r'   rS   z SimpleCacheFileSystem.load_cache  r7  r.   c                    | j                   r-| j                  |d      5 }|j                  |       d d d        y t        |   ||       y # 1 sw Y   y xY w)Nr!  )r    r   r%  r=   r   )r"   r   valuerJ   r$   rZ   s        r'   r   zSimpleCacheFileSystem.pipe_file  sP    ==4& !  GdE* s   AAc                   | j                  |      }g }	  | j                  j                  |fddi|j                         }d }| j
                  rK|j                  d      dz   }| j                  j                  D ]  }|j                  |k(  r2|j                  ||j                  xs |j                         dd       E|j                  j                  |      sa|j                  j                  d      |j                  d      k(  r<|j                  |j                  |j                  xs |j                         dd       dj                  |j                  j!                  d      d |j                  d      dz          }	|j                  |	ddd         ||s||r|S t#        d	 |D              S # t        $ r}|}Y d }~d }~ww xY w)
Nr   T/ra   r	  r   r<   r   r   	directoryc              3  &   K   | ]	  }|d      yw)r	  Nr-   )rp   r*  s     r'   rq   z+SimpleCacheFileSystem.ls.<locals>.<genexpr>  s     1Aai1s   )r;   r   r   r  r
  r    rstripr   r   r   r  r   tell
startswithcountr   splitsorted)
r"   r   r   rJ   r   exr  path1r$   dnames
             r'   r   zSimpleCacheFileSystem.ls  s   ##D)	 dggjj!%+df  B==KK$s*E%%++ X66T>NN!%qvv/A6R VV&&u-vv||C(EKK,<<%&VVQVV5GqvvxQWX !$c):;QU[[=MPQ=Q)R Sq+'VWX >'HN1111/ ! 	B	s   -F8 8	GG		Gc                   | j                        | j                  r| j                  j                  D cg c]  }|j                  k(  s| }}|rT|d   j
                  r,t        j                  j                  |d   j                        n|d   j                         }|ddS t        fd| j                  j                  D              }|rdddS  | j                  j                  fi |S c c}w )Nr   ra   r=  c              3  Z   K   | ]"  }|j                   j                  d z          $ yw)r<  N)r   rB  )rp   r*  r   s     r'   rq   z-SimpleCacheFileSystem.info.<locals>.<genexpr>   s$     RaAFF%%dSj1Rs   (+r>  )r;   r    r   r   r   r   rG   getsizer   rA  anyr   r   )r"   r   rJ   r*  r$   r   s    `    r'   r   zSimpleCacheFileSystem.info  s    ##D)== ,,22EqaffnEAE34Q4;;rwwqtww/AaDIIK $dFCCR4;K;K;Q;QRRA $aEEtww||D+F++ Fs   C5C5c                   t        |t              r$ | j                  | j                  |      |fi | y t        |t              r<|j                         D ](  \  }} | j                  | j                  |      |fi | * y t        d      )Nzpath must be str or dict)rE   rF   r   r;   dictr#  r?   )r"   r   r:  rJ   r'  r(  s         r'   r   zSimpleCacheFileSystem.pipe%  s    dC DNN4//5uGGd#

 E1t33A6DVDE 788r.   c                P   |D cg c]  }| j                  |       }}t        ||      D 	cg c]  \  }	}|	du s| }
}	}t        ||      D 	cg c]  \  }	}|	du s|	 }}	}| j                  j                  |
|       t	        |   |||f||d|S c c}w c c}}	w c c}}	w )NF)max_gapr  )ry   r   r   r   r=   r   )r"   r   startsendsrP  r  rJ   r  r&   lr%   rZ   s              r'   r   z SimpleCacheFileSystem.cat_ranges.  s     055!$""1%55 #FE 2A1a5j!AA #FE 2A1a5j!AAFF#w!64
)08
GM
 	
	 6AAs   BBBB"B"c                   | j                  |      }| j                  |      }d|vrpt        j                  j	                  | j
                  d   |      }|j                         D ci c]  \  }}|dvr|| }}}t        | |f|| j                   |d|S | j                  |      }|rt        ||      S t        j                  j	                  | j
                  d   |      }t        j                  d|       ||d<   | j                          d | _        | j                  r | j                   j"                  |fi |5 }	t        |d      5 }
t%        |	t&              r0t)        d|	j*                  j,                  |	j.                        |	_        | j                  d	k(  rt1        |      n| j                  }t3        |   |	d
      }	d}|r2t5        |	dd      }|	j7                  |      }|
j9                  |       |r2d d d        d d d        n| j                   j;                  ||       | j#                  ||      S c c}}w # 1 sw Y   FxY w# 1 sw Y   -xY w)Nr   r6   r   )r   r   r   r  r   r!  r   r   r   r   Tr   r"  )r;   rP   rG   r   r   rI   r#  r   r    ry   r   r   r   r_   rO   rN   r   r   rE   r   r   r   r$  r   r   r   r   r  r%  r&  )r"   r   r   rJ   r5  r   r'  r(  r)  r$   r+  r   r,  r-  s                 r'   r   zSimpleCacheFileSystem._open9  s4   ##D)ll4 d?dll2.4B #LLN%AqII 1%! %
 ! #}}, (  d#D>!WW\\$,,r*C00$7vt.v. #!T"d^ #ra!56'177??AFFCAG ''72 &d+)) 
 $K-#A{I>E66%=DHHTN # # #  GGT2&zz$%%S%0# # # #s+   'H7>I	B,H=8I	=I	I		Ir:   r)   )Nr   r.  )r*   r+   r,   r   r3   r   r   r   r>   ry   rl   rS   r   r   r   r   r   r   r   r   s   @r'   r0  r0    sS     HJ-4+2@
,9 ;C	
/&r.   r0  c                  Z    e Zd ZdZddZd Zd Zd Zd Zd Z	d Z
ed	        Zdd
Zd Zy)r   z8A temporary local file, which will be uploaded on commitc                    || _         t        ||      | _        || _        |r| j                  j	                  |       || _        d | _        || _        d| _        || _	        || _
        y r   )r   r   fhr   seekr   r   r   r   r   rJ   )r"   r   r   r   r   r   rX  rJ   s           r'   r>   zLocalTempFile.__init__n  sZ    r4.	GGLL		$r.   c                    t         | j                  | j                  | j                  d| j                  | j                         ffS )Nzr+b)r   r   r   r   r   rA  r^   s    r'   r   zLocalTempFile.__reduce__{  s7     WWdii%$))+N
 	
r.   c                    | j                   S r:   )rW  r^   s    r'   	__enter__zLocalTempFile.__enter__  s    wwr.   c                $    | j                          y r:   )r   )r"   exc_typeexc_valexc_tbs       r'   __exit__zLocalTempFile.__exit__  s    

r.   c                    | j                   ry | j                  j                          d| _         | j                  r| j	                          y y )NT)r   rW  r   r   r#   r^   s    r'   r   zLocalTempFile.close  s5    ;;??KKM r.   c                v    | j                   j                          t        j                  | j                         y r:   )rW  r   rG   r~   r   r^   s    r'   discardzLocalTempFile.discard  s    
		$''r.   c                |     | j                   j                  | j                  | j                  fi | j                   y r:   )r   r   r   r   rJ   r^   s    r'   r#   zLocalTempFile.commit  s'    DGGTYY6$++6r.   c                    | j                   S r:   r   r^   s    r'   r	  zLocalTempFile.name  s    wwr.   c                     d| j                    S )NzLocalTempFile: )r   r^   s    r'   __repr__zLocalTempFile.__repr__  s     ,,r.   c                .    t        | j                  |      S r:   )r   rW  )r"   r   s     r'   __getattr__zLocalTempFile.__getattr__  s    tww%%r.   N)r!  Tr   )r   rF   )r*   r+   r,   r   r>   r   r[  r`  r   rc  r#   propertyr	  rg  ri  r-   r.   r'   r   r   k  sF    B
7  -&r.   r   )-
__future__r   r   loggingrG   r@   rh   rB   shutilr   typingr   r   r   r   fsspecr	   r
   fsspec.callbacksr   fsspec.compressionr   fsspec.corer   r   fsspec.exceptionsr   #fsspec.implementations.cache_mapperr   %fsspec.implementations.cache_metadatar   fsspec.specr   fsspec.transactionr   fsspec.utilsr   r   	getLoggerr   r   r0   r   r0  r   r-   r.   r'   <module>rz     s    "   	     9 9 1 - $ , 4 C ? , * *G			?	+	[ 	S
* S
lE&0 E&Pa&4 a&H6& 6&r.   