
    7Th8                       S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKJ	r	  S SK
JrJrJrJr  S SKJrJr  S SKJr  S SKJr  S SKJrJr  S S	KJr  S S
KJr  S SKJr  S SKJ r   S SK!J"r"  S SK#J$r$  \(       a  S SKJ%r%  \RL                  " S5      r' " S S\"5      r( " S S\5      r) " S S\)5      r* " S S\*5      r+ " S S5      r,g)    )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                      \ rS rSrSS jrSrg)WriteCachedTransaction   c                v   U R                    Vs/ s H  o"R                  PM     nnU R                    Vs/ s H  o"R                  PM     nnU(       a  U R                  R	                  XC5        U R                   R                  5         SU R                  l        S U R                  l        S U l        g s  snf s  snf NF)filespathfnfsputclear_intrans_transaction)selfcommitfrpathslpathss        U/var/www/auris/envauris/lib/python3.13/site-packages/fsspec/implementations/cached.pycompleteWriteCachedTransaction.complete   s    "&**-*Q&&*- $

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

 # .+s
   B1B6)r   NT)__name__
__module____qualname____firstlineno__r)   __static_attributes__     r(   r   r      s    r2   r   c                     ^  \ rS rSr% SrSrS\S'             S   SU 4S jjjr\S 5       r	S r
S	 rS
 rS rS rS rS rSS jrS r    SS jrS rSS jrS rSS jrU 4S jrS rS rSrU =r$ ) CachingFileSystem)   a  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                  >^  [         TT ]  " S0 UD6  Uc  Uc  [        S5      eUSL USL -  (       d  [        S5      eUS:X  a;  [        R                  " 5       nU/n[
        R                  " T T R                  U5        O[        U[        5      (       a  U/nOUn[        R                  " US   SS9  UT l        U=(       d    0 T l        UT l        UT l        UT l        U	T l        ST l        Ub  U
b  [        S5      eU
b  U
T l        O[)        Ub  UOS	5      T l        [        U[        5      (       a  UO9[        UR*                  [        5      (       a  UR*                  OUR*                  S
   T l        [/        T R                  5      T l        T R3                  5         Ub  UO[5        U40 T R                  D6T l        U 4S jnUT l        g)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   > TR                   R                  [        T5      R                  U 5      5      $ N)r   _strip_protocoltype)r   r#   s    r(   r@   3CachingFileSystem.__init__.<locals>._strip_protocol   s'    77**4:+E+Ed+KLLr2   r1   )super__init__
ValueErrortempfilemkdtempweakreffinalize_remove_tempdir
isinstancestrosmakedirsstoragekwargscache_checkcheck_filesexpirycompression_cache_size_mapperr   r8   target_protocolr   	_metadata
load_cacher
   r   r@   )r#   rW   cache_storagerQ   rR   expiry_timetarget_optionsr   
same_namesrT   cache_mapperrP   tempdirrO   r@   	__class__s   `              r(   rD   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r2   c                <     [        U 5        g ! [         a     g f = fr?   )r   	Exception)r_   s    r(   rJ   !CachingFileSystem._remove_tempdir   s     	7O 		s    
c                H    [         R                  " U R                  S   SS9  g )Nr;   Tr<   )rM   rN   rO   r#   s    r(   _mkcacheCachingFileSystem._mkcache   s    
DLL$t4r2   c                    U R                   c-  U R                  S   n[        S5      R                  USS9U l         U R                   $ )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.
r;   fileT)withdirs)rU   rO   r
   du)r#   	cache_dirs     r(   
cache_sizeCachingFileSystem.cache_size   sG     #R(I)&144Y4NDr2   c                    U R                   R                  5         U R                  5         [        R                  " 5       U l        g)z#Read set of stored blocks from fileN)rX   loadrg   time
last_cacherf   s    r(   rY   CachingFileSystem.load_cache   s(    ))+r2   c                    U R                  5         U R                  R                  5         [        R                  " 5       U l        SU l        g)z#Save set of stored blocks from fileN)rg   rX   saverr   rs   rU   rf   s    r(   
save_cacheCachingFileSystem.save_cache   s0    ))+r2   c                   U R                  5         U R                  (       d  g[        R                  " 5       U R                  -
  U R                  :  n[	        S U R
                   5       5      nU(       d  U(       d  U R                  5         gg)z0Reload caches if time elapsed or any disappearedNc              3  `   #    U  H$  n[         R                  R                  U5      v   M&     g 7fr?   )rM   r   exists).0rO   s     r(   	<genexpr>1CachingFileSystem._check_cache.<locals>.<genexpr>   s      L|Gw//|s   ,.)rg   rQ   rr   rs   allrO   rY   )r#   timecond	existconds      r(   _check_cacheCachingFileSystem._check_cache   s^    99;043C3CCLt||LL	9OO %r2   c                z    U R                  U5      nU R                  5         U R                  R                  X5      $ )z Is path in cache and still valid)r@   r   rX   
check_filer#   r   s     r(   _check_fileCachingFileSystem._check_file   s3    ##D)~~((44r2   c                b    [        U R                  S   5        U R                  5         SU l        g)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.
r;   N)r   rO   rY   rU   rf   s    r(   clear_cacheCachingFileSystem.clear_cache   s'     	t||B r2   c                z   U(       d  U R                   nU R                  5         U R                  R                  U5      u  p#U H?  n[        R
                  R                  U5      (       d  M)  [        R                  " U5        MA     U(       a(  [        U R                  S   5        U R                  5         SU l        g)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.
r;   N)rS   r   rX   clear_expiredrM   r   r{   remover   rO   rY   rU   )r#   r[   expired_fileswritable_cache_emptyr   s        r(   clear_expired_cache%CachingFileSystem.clear_expired_cache   s     ++K.2nn.J.J;.W+Bww~~b!!		"    4<<#$OOr2   c                    U R                  U5      nU R                  R                  U5      nUb  [        R                  " U5        SU l        g)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@   rX   pop_filerM   r   rU   )r#   r   r   s      r(   pop_from_cache CachingFileSystem.pop_from_cache  s@     ##D)^^$$T*>IIbMr2   c           
       ^ ^^^^ T R                  T5      mT R                  R                  T5      mSU;  a"  T R                  R                  " T4UUUUS.TD6$ T R                  T5      nU(       aJ  Uu  pxUS   US   pU
SL a!  [        R                  ST5        [        X5      $ [        R                  ST5        OT R                  T5      n	[        R                  R                  T R                  S   U	5      n[        5       n
TU	U
[        R                  " 5       T R                  R                  T5      S	.nT R                  R!                  TU5        [        R                  S
T5        UR#                  SS5      nT R%                  5         T R                  R                  " T4UUUUSUS.TD6mUc+  TR&                  US'   T R                  R!                  TU5        T R(                  (       a5  T R(                  S:X  a  [+        T5      OT R(                  n[,        U   " TSS9mSU;   a3  US   TR.                  :w  a  [1        SUS    STR.                   S35      eOTR.                  US'   UUU 4S jnT R(                  (       a  SOUn[3        TR.                  TR4                  TR&                  XUS9Tl        TR8                  mUUU 4S jTl        T R;                  5         T$ )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 %sr;   originalr   r   rr   uidz!Creating local sparse file for %ssizeNnone)r   r   r   r   
cache_typer   inferrbr   	blocksizezDCached file must be reopened with same block size as original (old: z, new )c           	        > TR                   R                  " T/[        U 5      -  U  Vs/ s H  oS   PM	     snU  Vs/ s H  oS   PM	     sn40 TD6$ s  snf s  snf )Nr      )r   
cat_rangeslen)rangesr   rP   r   r#   s     r(   _fetch_ranges.CachingFileSystem._open.<locals>._fetch_rangesz  s`    77%%V$%&v!1v&%&v!1v& 	 &&s
   A
A)multi_fetcherc                 (   > TR                  TT 5      $ r?   )close_and_update)closer%   r#   s   r(   <lambda>)CachingFileSystem._open.<locals>.<lambda>  s    $//59r2   )r@   r   _openr   loggerdebugopenrV   rM   r   joinrO   setrr   ukeyrX   update_filegetrg   r   rT   r   r   r   r   r   _fetch_rangecacher   rw   )r#   r   r   r   r   r   rP   detailr   hashr   r   compr   r   r   r%   s   ``    `        @@r(   r   CachingFileSystem._open  s   * ##D)ww&&t,d?77==%%+   !!$'JF!$<)9&~7>B~%LL>E<<%Ddll2.5BUF  		ww||D)F NN&&tV4LL<dC
 zz&$' 	GGMM	
!!'	
 	
 <VVF6NNN&&tV4 ##w. "$'%% 
 dAD)A& k"akk1,//5k/B.C DKK=+  2 #$++F;	 !% 0 0mKK=
 9r2   c                8    U R                   R                  U5      $ r?   )r   _parentr   s     r(   r   CachingFileSystem._parent  s    wwt$$r2   c                $    U R                  U5      $ r?   )rV   )r#   r   argss      r(   	hash_nameCachingFileSystem.hash_name  s     ||D!!r2   c                   UR                   (       a  gU R                  UR                  5      nU R                  R	                  X5         [
        R                  S5        U R                  5         [
        R                  S5        U" 5         SUl         g! [         a    [
        R                  S5         N0[         a    [
        R                  S5         NPf = f)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   rX   on_close_cached_filer   r   rw   OSError	NameError)r#   r%   r   r   s       r(   r   "CachingFileSystem.close_and_update  s    88##AFF+++A4	JLL)OOLL!
 	  	CLLAB 	JLLHI	Js   
:B C4CCc                8    U R                   R                  X5      $ r?   )r   ls)r#   r   r   s      r(   r   CachingFileSystem.ls  s    wwzz$''r2   c                  >^ ^ TS;   a  UU 4S j$ TS;   a  [         eTS;   a$  [        T 5      R                  R                  T 5      $ TS;   a  [	        [        T 5      T5      $ TS:X  a  [        T 5      $ [
        R                  T S5      nUR                  SS 5      nTU;   a  UT   $ Ub  TUR                  ;   a  UR                  T   $ [        U5      n[	        UT5      n[        R                  " U5      (       d  [        R                  " U5      (       a/  [        US	5      (       a  UR                  c  UR                  X45      $ U$ [        TT ]  T5      $ )
N>*   r   catr   headinfor   pipetailr   isdir__eq__r{   isfileto_dictto_json__hash__rD   rg   cat_filer   	open_many	pipe_file
__reduce__rn   r   
get_mapperrY   
local_file
read_blockrw   r   r   commit_manyr   r   end_transaction__getattribute___paths_from_pathr   start_transaction_make_local_detailsr   c                 X   > [        [        T5      T5      R                  T5      " U 0 UD6$ r?   )getattrrA   __get__)r   kwitemr#   s     r(   r   4CachingFileSystem.__getattribute__.<locals>.<lambda>  s+    wtDz4'@'H'H'N((r2   )__reduce_ex__)transaction)_cachetransaction_typer`   __dict__r   __self__)AttributeErrorrA   r   r   r   objectr   r   r   inspect
isfunctionisdatadescriptorhasattrr   rC   )r#   r   dr   clsmr`   s   ``    r(   r   "CachingFileSystem.__getattribute__  sA    +
 +
\  $$  ?":))11$77114:t,,;:##D*5UU419T7N^r{{"{{4((r(CT"A""1%%)A)A!)D)DAz**ajj.@ yy))H 7+D11r2   c                4   XL a  g[        U[        U 5      5      (       d  gU R                  UR                  :H  =(       a    U R                  UR                  :H  =(       a    U R                  UR                  :H  =(       a    U R
                  UR
                  :H  =(       ay    U R                  UR                  :H  =(       aY    U R                  UR                  :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ )zTest for equality.TF)
rK   rA   rO   rP   rQ   rR   rS   rT   rV   rW   )r#   others     r(   r   CachingFileSystem.__eq__  s    =%d,,LLEMM) >u||+>  E$5$55>   E$5$55> u||+	>
   E$5$55> -> $$(=(==		
r2   c                   [        [        U R                  5      5      [        [        U R                  5      5      -  [        U R
                  5      -  [        U R                  5      -  [        U R                  5      -  [        U R                  5      -  [        U R                  5      -  [        U R                  5      -  $ )zCalculate hash.)r   tuplerO   rL   rP   rQ   rR   rS   rT   rV   rW   rf   s    r(   r   CachingFileSystem.__hash__	  s     t||$%3t{{#$%4##$% 4##$% 4;;	 
 4##$% 4<< ! 4''()		
r2   )rU   rV   rX   r@   rQ   rR   rT   rS   r   rP   rs   rO   rW   )
Nr:   
   Fi:	 NNNNN)r]   zbool | Noner^   zAbstractCacheMapper | Noner?   )r   NTN)r   rL   r   r   returnrL   r+   )r,   r-   r.   r/   __doc__r8   __annotations__rD   staticmethodrJ   rg   rn   rY   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r0   __classcell__r`   s   @r(   r4   r4   )   s    & 1IH-H "&37o9  o9 1o9 o9b  5	 & 	5  <   qf%"
"(O2b
"
 
r2   r4   c                  L    \ rS rSrSrSrSrS rS rS r	SS	\
4S
 jrSS jrSrg)WholeFileCacheFileSystemi  a  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                b   U Vs/ s H  o3R                   PM     nnSUR                  ;   a  U R                  5         OoU Vs/ s Ha  n[        U R                  U4UR                  [
        R                   R                  U R                  S   U R                  U5      5      S.UD6PMc     sn$ U R                  (       a  [        eU Vs/ s H  o`R                  U5      PM     nn[        XG5       VV	s/ s H  u  pU	(       a  M  UPM     n
nn	[        XG5       VV	s/ s HA  u  p[
        R                   R                  U R                  S   U R                  U5      5      PMC     nnn	[        X5       VV	s/ s H  u  pU	(       a  M  UPM     nnn	U
(       a  U R                  R                  X5        U
 Vs/ s HF  nUU R                  U5      S[        R                  " 5       U R                  R                  U5      S.PMH     nn[        X5       H   u  p_U R                   R#                  X_5        M"     U R%                  5         S n[        X{5       VVs/ s H)  u  nn['        U(       a  U" U5      OUUR                  S9PM+     snn$ s  snf s  snf s  snf s  sn	nf s  sn	nf s  sn	nf s  snf s  snnf )Nr   r;   r   r   Tr   c                :    [        U [        5      (       a  U S   $ U $ )Nr   )rK   r
  r   s    r(   	firstpart5WholeFileCacheFileSystem.open_many.<locals>.firstpartT  s    &r5112a59r9r2   r   )r   r   rg   LocalTempFiler   rM   r   rO   rV   rT   NotImplementedErrorr   zipr   rr   r   rX   r   rw   r   )r#   
open_filesrP   ofpathsr   spdetailspr  downpathdownfn0r   downfn	newdetailr   r  fn0fn1s                      r(   r   "WholeFileCacheFileSystem.open_many(  sU   #-.:R:.*//!MMO "	 "D GG $ww||DLL$4dll46HI	
  "	 	 %%278%B##B'%8"%e"5?"5$!QA"5? E+
+ GGLLb)4<<?;+ 	 
 #&g"7A"7q""7AGGKK) %	 %D !%,,t," IIK77<<- %  	 !$H 8**48 !9OO	:  1
1S 33CjooF1
 	
_ /	 9?
 B	$
s>   JA(J
J1JJAJ2J J 2AJ&0J+c                   U R                   R                  U Vs/ s H  o"R                  PM     snU Vs/ s H  o"R                  PM     sn5        U Vs/ s H  o"R	                  5       PM       nU H$  n [
        R                  " UR                  5        M&     S U l	        g s  snf s  snf s  snf ! [         a     ML  f = fr?   )
r   r   r   r   r   rM   r   nameFileNotFoundErrorrU   )r#   r  r%   s      r(   r   $WholeFileCacheFileSystem.commit_many]  s    :.:aTT:.0LA0LM&'JqJ'A		!&&!    /0L'
 % s"   B"B'
B,6 B11
B?>B?c                L   U R                  U5      n[        R                  R                  U R                  S   U5      nUUS[
        R
                  " 5       U R                  R                  U5      S.nU R                  R                  X5        [        R                  SU5        U$ )Nr;   Tr   Copying %s to local cache)rV   rM   r   r   rO   rr   r   r   rX   r   r   r   )r#   r   r   r   r   s        r(   r   ,WholeFileCacheFileSystem._make_local_detailsh  s~    ||D!WW\\$,,r*D1IIK77<<%
 	""400$7	r2   Fraisec                   U R                  XUR                  S5      S9n/ n/ n/ n	0 n
UR                  5        H~  n U R                  U5      nU(       d4  U R	                  U5      nUR                  U5        UR                  U5        O[        U[        5      (       a  UOS U4u  pU	R                  U5        M     U(       a+  U R                  R                  Xx5        U R                  5         UR                  [        U5      5        [        Xi5       H=  u  p[        US5       nUR!                  5       X'   S S S 5        UR#                  S5        M?     [        U[$        5      (       a  [        U5      S:X  a  USL a  XS      n
U
$ ! [         a.  nUS:X  a  e US:X  a  XU'   UR                  U5         S nAGM  S nAff = f! , (       d  f       N= f)	Nmaxdepth)	recursiver5  r3  r  r   r   Fr   )expand_pathr   copyr   r   appendrK   r
  rc   r   r   rw   set_sizer   r  r   readrelative_updaterL   )r#   r   r6  on_errorcallbackrP   r!  getpaths
storepathsfnsoutr$  r   r   er%   s                   r(   r   WholeFileCacheFileSystem.catv  s      

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

2 " GGKK-OO#e*%_EAb$1  $$Q' % dC  SZ1_e9KAh-C
%   w&x'FQ   s$   A:F"F?
F<"F77F<?
G	c                &   U R                  U5      nSU;  ax  U R                  U5      n[        R                  R	                  U R
                  S   U5      nUR                  5        VVs0 s H  u  pgUS;  d  M  Xg_M     nnn[        X4X%S.UD6$ U R                  U5      n	U	(       aZ  U	u  pU	S   U	S   pUSL a9  [        R                  SU5        [        XR5      nU	R                  S	5      Ul        U$ [        S
U S35      eU R                  U5      nX#S'   U R!                  5         U R"                  (       a  U R$                  R&                  " U40 UD6 n[        US5       n[)        U[*        5      (       a0  [-        SUR.                  R0                  UR2                  5      Ul        U R"                  S:X  a  [5        U5      OU R"                  n[6        U   " USS9nSnU(       a8  [9        USS5      nUR;                  U5      nUR=                  U5        U(       a  M8  S S S 5        S S S 5        OU R$                  R?                  X5        U RA                  5         U R'                  X5      $ s  snnf ! , (       d  f       NY= f! , (       d  f       NF= f)Nr   r;   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@   rV   rM   r   r   rO   itemsr  r   r   r   r   r   r   rE   r   rg   rT   r   r   rK   r   r   r   fetcherr   r   r   r   r;  writeget_filerw   )r#   r   r   rP   r   r   kvuser_specified_kwargsr   _r   r%   f2r   datablocks                    r(   r   WholeFileCacheFileSystem._open  sF   ##D)d?<<%Ddll2.5B #LLN%*DAII * " % !W$WAVWW!!$'JFtfX&6v~7> N#ZZ
3
 <TF.0 
 ))$/Bv 	t.v.!T"d^ra!566'177??AFFCAG ''72 &d+)) 
 $K-#A{I>E66%=DHHTN d 6D..  GGT&zz$%%i%D 6D^..s1   )I+9I+J"B:I1J1
I?	;J
JrU   Nr   )r,   r-   r.   r/   r  r8   r   r   r   r   r   r   r   r0   r1   r2   r(   r  r    s9     HJ3
j	 " !+Z9&r2   r  c                     ^  \ rS rSrSrSrSr\rU 4S jr	S r
S rS rSU 4S	 jjrSS
 jrS rSS jr SU 4S jjrSS jrSrU =r$ )SimpleCacheFileSystemi  av  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                   > UR                  5       nS H  nSX#'   M	     [        TU ]  " S0 UD6  U R                   H>  n[        R
                  R                  U5      (       a  M)  [        R                  " USS9  M@     g )N)rQ   r[   rR   FTr<   r1   )r8  rC   rD   rO   rM   r   r{   rN   )r#   rP   r   keyrO   r`   s        r(   rD   SimpleCacheFileSystem.__init__  s_    [[]@CBG A2||G77>>'**Gd3 $r2   c                    U R                  5         U R                  U5      nU R                   HJ  n[        R                  R                  X25      n[        R                  R                  U5      (       d  MH  Us  $    g r?   )r   rV   rO   rM   r   r   r{   )r#   r   sharO   r   s        r(   r   !SimpleCacheFileSystem._check_file  sT    ll4 ||Gg+Bww~~b!!	 $r2   c                    g r?   r1   rf   s    r(   rw    SimpleCacheFileSystem.save_cache      r2   c                    g r?   r1   rf   s    r(   rY    SimpleCacheFileSystem.load_cache  rb  r2   c                   > U R                   (       a-  U R                  US5       nUR                  U5        S S S 5        g [        TU ]  X5        g ! , (       d  f       g = f)NrG  )r!   r   rK  rC   r   )r#   r   valuerP   r%   r`   s        r(   r   SimpleCacheFileSystem.pipe_file
  sE    ==4&! '& Gd* '&s   A
Ac                   U R                  U5      n/ n U R                  R                  " U4SS0UD6R                  5       nS nU R
                  (       Ga`  UR                  S5      S-   nU R                  R                   GH1  nUR                  U:X  a7  UR                  XR                  =(       d    UR                  5       SS.5        MK  UR                  R                  U5      (       d  Mm  UR                  R                  S5      UR                  S5      :X  aB  UR                  UR                  UR                  =(       d    UR                  5       SS.5        M  SR                  UR                  R!                  S5      S UR                  S5      S-    5      n	UR                  U	SSS.5        GM4     Ub	  U(       d  UeU(       a  U$ [#        S	 U 5       5      $ ! [         a  nUn S nAGNS nAff = f)
Nr   T/rj   r-  r   rA   r   r   	directoryc              3  *   #    U  H	  oS    v   M     g7f)r-  Nr1   )r|   rP  s     r(   r}   +SimpleCacheFileSystem.ls.<locals>.<genexpr>/  s     1Ais   )r@   r   r   r8  r.  r!   rstripr   r   r   r9  r   tell
startswithcountr   splitsorted)
r#   r   r   rP   r#  exrC  path1r%   dnames
             r(   r   SimpleCacheFileSystem.ls  s   ##D)	ggjj!%+df  B===KK$s*E%%++66T>NN!%vv/A6R VV&&u--vv||C(EKK,<<%&VVQVV5GqvvxQWX !$c):;QU[[=MPQ=Q)R Sq+'VW , >'HN1111/ ! 	B	s   -G 
G4'G//G4c                &  ^ U R                  T5      mU R                  (       a  U R                  R                   Vs/ s H  o3R                  T:X  d  M  UPM     nnU(       aY  US   R
                  (       a,  [        R                  R                  US   R                  5      OUS   R                  5       nTUSS.$ [        U4S jU R                  R                   5       5      nU(       a  TSSS.$ U R                  R                  " T40 UD6$ s  snf )Nr   rj   rj  c              3  ^   >#    U  H"  oR                   R                  TS -   5      v   M$     g7f)ri  N)r   rp  )r|   rP  r   s     r(   r}   -SimpleCacheFileSystem.info.<locals>.<genexpr>8  s'     R;QaFF%%dSj11;Qs   *-rk  )r@   r!   r   r   r   r   rM   getsizer   ro  anyr   r   )r#   r   rP   rP  r%   r   s    `    r(   r   SimpleCacheFileSystem.info1  s    ##D)== ,,22E2qffn2AE34Q4;;rwwqtww/AaDIIK $dFCCR4;K;K;Q;QRRA $aEEww||D+F++ Fs   DDc                .   [        U[        5      (       a$  U R                  " U R                  U5      U40 UD6  g [        U[        5      (       a=  UR                  5        H(  u  pEU R                  " U R                  U5      U40 UD6  M*     g [        S5      e)Nzpath must be str or dict)rK   rL   r   r@   dictrI  rE   )r#   r   rf  rP   rM  rN  s         r(   r   SimpleCacheFileSystem.pipe=  sx    dC  NN4//5uGGd##

t33A6DVD % 788r2   c                \  > U Vs/ s H  opR                  U5      PM     nn[        X5       V	Vs/ s H  u  pU	SL d  M  UPM     n
n	n[        X5       V	Vs/ s H  u  pU	SL d  M  U	PM     nn	nU R                  R                  X5        [        TU ]  " XU4XES.UD6$ s  snf s  snn	f s  snn	f )NF)max_gapr=  )r   r  r   r   rC   r   )r#   r!  startsendsr  r=  rP   r$  r'   lr&   r`   s              r(   r    SimpleCacheFileSystem.cat_rangesF  s     055u!""1%u5 #F 2A 2a5j! 2A #F 2A 2a5j! 2AF#w!4
)0
GM
 	
	 6AAs   BB"B"B(%B(c                   U R                  U5      nU R                  U5      nSU;  ay  [        R                  R	                  U R
                  S   U5      nUR                  5        VVs0 s H  u  pgUS;  d  M  Xg_M     nnn[        U U4UU R                  (       + US.UD6$ U R                  U5      nU(       a  [        XR5      $ [        R                  R	                  U R
                  S   U5      n[        R                  SU5        X#S'   U R                  5         S U l        U R                  (       a  U R                   R"                  " U40 UD6 n	[        US5       n
[%        U	[&        5      (       a0  [)        SU	R*                  R,                  U	R.                  5      U	l        U R                  S	:X  a  [1        U5      OU R                  n[2        U   " U	S
S9n	SnU(       a8  [5        U	SS5      nU	R7                  U5      nU
R9                  U5        U(       a  M8  S S S 5        S S S 5        OU R                   R;                  X5        U R#                  X5      $ s  snnf ! , (       d  f       NI= f! , (       d  f       N6= f)Nr   r;   rF  )r   r   r   r1  r   rG  r   r   r   r   Tr   rH  )r@   rV   rM   r   r   rO   rI  r  r!   r   r   r   r   rg   rU   rT   r   r   rK   r   r   r   rJ  r   r   r   r   r;  rK  rL  )r#   r   r   rP   r^  r   rM  rN  rO  r%   rQ  r   rR  rS  s                 r(   r   SimpleCacheFileSystem._openQ  s   ##D)ll4 d?dll2.4B #LLN%*DAII * " %
 ! #}}, (  d#>!WW\\$,,r*C00$7vt.v.!T"d^ra!566'177??AFFCAG ''72 &d+)) 
 $K-#A{I>E66%=DHHTN d 6D..  GGT&zz$%%S%0 6D^..s1   )I9II.B:II.
I+	'I..
I<rU  r?   r+   )Nr  rV  )r,   r-   r.   r/   r  r8   r   r   r   rD   r   rw   rY   r   r   r   r   r   r   r0   r  r  s   @r(   rX  rX    sX     HJ-4+2@
,9 ;C	
/& /&r2   rX  c                  f    \ rS rSrSrSS jrS rS rS rS r	S r
S	 r\S
 5       rSS jrS rSrg)r  i  z8A temporary local file, which will be uploaded on commitc                    X0l         [        X45      U l        X@l        U(       a  U R                  R	                  U5        X l        S U l        Xl        SU l        XPl	        Xpl
        g r   )r   r   fhr   seekr   r   r   r   r   rP   )r#   r   r   r   r   r   r  rP   s           r(   rD   LocalTempFile.__init__  sL    r.	GGLL		$r2   c                    [         U R                  U R                  U R                  SU R                  U R                  5       44$ )Nzr+b)r  r   r   r   r   ro  rf   s    r(   r   LocalTempFile.__reduce__  s7     WWdii%$))+N
 	
r2   c                    U R                   $ r?   )r  rf   s    r(   	__enter__LocalTempFile.__enter__  s    wwr2   c                $    U R                  5         g r?   )r   )r#   exc_typeexc_valexc_tbs       r(   __exit__LocalTempFile.__exit__  s    

r2   c                    U R                   (       a  g U R                  R                  5         SU l         U R                  (       a  U R	                  5         g g )NT)r   r  r   r   r$   rf   s    r(   r   LocalTempFile.close  s5    ;;??KKM r2   c                x    U R                   R                  5         [        R                  " U R                  5        g r?   )r  r   rM   r   r   rf   s    r(   discardLocalTempFile.discard  s    
		$''r2   c                |    U R                   R                  " U R                  U R                  40 U R                  D6  g r?   )r   r   r   r   rP   rf   s    r(   r$   LocalTempFile.commit  s%    DGGTYY6$++6r2   c                    U R                   $ r?   r  rf   s    r(   r-  LocalTempFile.name  s    wwr2   c                     SU R                    3$ )NzLocalTempFile: )r   rf   s    r(   __repr__LocalTempFile.__repr__  s     ,,r2   c                .    [        U R                  U5      $ r?   )r   r  )r#   r   s     r(   __getattr__LocalTempFile.__getattr__  s    tww%%r2   )	r   r   r  r   r   rP   r   r   r   N)rG  Tr   )r  rL   )r,   r-   r.   r/   r  rD   r   r  r  r   r  r$   propertyr-  r  r  r0   r1   r2   r(   r  r    sF    B
7  -&r2   r  )-
__future__r   r   loggingrM   rF   rr   rH   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   r4   r  rX  r  r1   r2   r(   <module>r     s    "   	     9 9 1 - $ , 4 C ? , * *G			?	+	[ 	k
* k
\E&0 E&Pa&4 a&H6& 6&r2   