
    "h%                        d dl mZ d dl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  ej                  d      Z G d	 d
e      Z G d de      Zy)    )annotationsN)datetimetimezone)	ENOTEMPTY)BytesIO)AnyClassVar)AbstractFileSystemzfsspec.memoryfsc                      e Zd ZU dZi Zded<   dgZdZdZe	d        Z
ddZdd	Zdd
Zd Zd Zd Z	 	 	 	 ddZd ZddZd Zd Zd ZddZy)MemoryFileSystemzA filesystem based on a dict of BytesIO objects

    This is a global filesystem so instances of this class all point to the same
    in memory filesystem.
    zClassVar[dict[str, Any]]store memory/c                    |j                  d      r|t        d      d  }d|v sd|v r|j                  d      S |j                  d      j                  d      }|rd|z   S dS )Nz	memory://z::z://r   r   )
startswithlenrstriplstrip)clspaths     Z/var/www/html/sandstorm/venv/lib/python3.12/site-packages/fsspec/implementations/memory.py_strip_protocolz MemoryFileSystem._strip_protocol   sf    ??;'K(*+D4<5D=;;s##{{3&&s+!sTz)r)    c                   | j                  |      }|| j                  v rI|s|gS || j                  |   j                  d| j                  |   j                  j	                         dgS t               }|dz   }g }t        | j                        D ]  }|j                  |      sd|t        |      d  vrS|j                  || j                  |   j                  d| j                  |   j                  j	                         d       xt        |      t        |      kD  s||t        |      d  j                  dd      d   z   }||vs|xs g }|j                  |ddd       |j                  |        | j                  D ]  }|j                  |      sd|t        |      d  vr,||vs*|j                  |ddd       |j                  |       Q||t        |      d  j                  dd      d   z   }||vsz|j                  |ddd       |j                  |        |s|| j                  v rg S t        |      |r|S t        |D 	cg c]  }	|	d   	 c}	      S c c}	w )	Nfilenamesizetypecreatedr      r   	directoryr   r   r    r   )r   r   r   r!   	timestampsettupler   r   appendsplitaddpseudo_dirsFileNotFoundErrorsorted)
selfr   detailkwargspathsstarteroutp2ppathfs
             r   lszMemoryFileSystem.ls#   sa   ##D)4::v ! JJt,11"#zz$/77AAC	  *

# 	)B}}W%bW00JJ$&$(JJrN$7$7$*'+zz"~'='='G'G'I	 Ws7|+#bW&8&>&>sA&Fq&IIEE)!iR

(-()(3 		%(1	)2 "" 	)B}}W%bW00

B;#OP		" $bW&8&>&>sA&Fq&IIEE)

E1k#RS		%(	) t'''	#D))J#.Qqy.//.s   I!c                ,   | j                  |      }|| j                  v s|| j                  v rt        |      | j	                  |      j                  d      r:| j                  | j	                  |            rt        | j	                  |            |rD| j	                  |      j                  d      r$	  | j                  | j	                  |      |fi | |r+|| j                  vr| j                  j                  |       y y y # t        $ r Y 9w xY w)Nr   )
r   r   r+   FileExistsError_parentstripisfileNotADirectoryErrormkdirr(   )r.   r   create_parentsr0   s       r   r>   zMemoryFileSystem.mkdirc   s    ##D)4::)9)9!9!$''<<##C(T[[d9K-L$T\\$%788dll4066s;

4<<-~HH D 0 00##D) 14 # s   6#D 	DDc                P    	 | j                  |d       y # t        $ r |s Y y w xY w)NT)r?   )r>   r9   )r.   r   exist_oks      r   makedirszMemoryFileSystem.makedirsq   s1    	JJtDJ1 	 	s    %%c                ,    | j                  |d|       y)zSSet the bytes of given file

        Avoids copies of the data if possible
        wb)dataN)open)r.   r   valuer0   s       r   	pipe_filezMemoryFileSystem.pipe_filex   s    
 			$5	)r   c                    | j                  |      }|dk(  ry || j                  v r>| j                  |      s| j                  j                  |       y t	        t
        d|      t        |      )Nr   zDirectory not empty)r   r+   r7   removeOSErrorr   r,   r.   r   s     r   rmdirzMemoryFileSystem.rmdir   sd    ##D)2:4###774=  ''-i)>EE#D))r   c                >   | j                        | j                  v s4t        fdt        | j                        | j                  z   D              rdddS | j                  v r+| j                     }|j
                  dt        |dd       dS t              )Nc              3  F   K   | ]  }|j                  d z           yw)r   N)r   ).0pr   s     r   	<genexpr>z(MemoryFileSystem.info.<locals>.<genexpr>   s#      +
)*ALL$+
s   !r   r#   r$   r   r!   r   )r   r+   anylistr   r   getattrr,   )r.   r   r0   filelikes    `  r   infozMemoryFileSystem.info   s    ##D)4###s +
.24::.>AQAQ.Q+
 (
 # 
 TZZzz$'H "8Y=	  $D))r   Nc                j   | j                  |      }|| j                  v rt        |      |}t        |      dkD  r<| j	                  |      }| j                  |      rt        |      t        |      dkD  r<|dv rT|| j                  v r;| j                  |   }|dk(  r|j                  dd       |S |j                  d       |S t        |      |dk(  r:t        | ||j                  d            }	| j                  s|	j                          |	S | j                  j                  }
t!        d|
 d	|      )
Nr"   )rbabzrb+rZ   r      rD   rE   zunsupported file mode for z: )r   r+   IsADirectoryErrorr   r:   r<   r9   r   seekr,   
MemoryFileget_intranscommit	__class____name__
ValueError)r.   r   mode
block_size
autocommitcache_optionsr0   parentr6   mr   s              r   _openzMemoryFileSystem._open   s'    ##D)4####D))&kAo\\&)F{{6"%f-- &kAo &&tzz!JJt$4<FF1aL  FF1I'--T\4vzz&'9:A==
H>>**D9$r$JKKr   c                b   | j                  |      }| j                  |      }| j                  |      r6t        | || j                  |   j	                               | j                  |<   y | j                  |      r+|| j                  vr| j                  j                  |       y y t        |      N)	r   r<   r^   r   getvalueisdirr+   r(   r,   )r.   path1path2r0   s       r   cp_filezMemoryFileSystem.cp_file   s    $$U+$$U+;;u *eTZZ.779!DJJu ZZD,,,  ''. - $E**r   c                    | j                  |      }	 t        | j                  |   j                         ||       S # t        $ r t        |      w xY wrm   )r   bytesr   	getbufferKeyErrorr,   )r.   r   startendr0   s        r   cat_filezMemoryFileSystem.cat_file   sU    ##D)	*D)335eC@AA 	*#D))	*s	   (< Ac                |    | j                  |      }	 | j                  |= y # t        $ r}t        |      |d }~ww xY wrm   )r   r   rv   r,   )r.   r   es      r   _rmzMemoryFileSystem._rm   s@    ##D)	1

4  	1#D)q0	1s   ! 	;6;c                    | j                  |      }	 | j                  |   j                  S # t        $ r t	        |      w xY wrm   )r   r   modifiedrv   r,   rL   s     r   r~   zMemoryFileSystem.modified   sF    ##D)	*::d#,,, 	*#D))	*	   , Ac                    | j                  |      }	 | j                  |   j                  S # t        $ r t	        |      w xY wrm   )r   r   r!   rv   r,   rL   s     r   r!   zMemoryFileSystem.created   sF    ##D)	*::d#+++ 	*#D))	*r   c                b   t        |t              r| j                  |      }n|D cg c]  }| j                  |       }}| j                  |||      }t	        |      D ]H  }| j                  |      s| j                  |      r| j                  |       8| j                  |       J y c c}w )N)	recursivemaxdepth)	
isinstancestrr   expand_pathreversedexistsr<   rm_filerM   )r.   r   r   r   rQ   r1   s         r   rmzMemoryFileSystem.rm   s    dC ''-D59:D((+:D:  X N% 
	A
 ;;q>{{1~Q

1
	 ;s   B,)T)F)rY   NTN)NN)FN)rc   
__module____qualname____doc__r   __annotations__r+   protocolroot_markerclassmethodr   r7   r>   rB   rH   rM   rW   rk   rr   ry   r|   r~   r!   r    r   r   r   r      s     ')E#($KHK* *>0@****0 $LL+*1**r   r   c                  L     e Zd ZdZd fd	Zed        Zd Zd Zd Z	d Z
 xZS )	r^   zA BytesIO which can't close and works as a context manager

    Can initialise with data. Each path should only be active once at any moment.

    No need to provide fs, path if auto-committing (default)
    c                8   t         j                  d|       || _        || _        t	        j
                  t        j                        | _        t	        j
                  t        j                        | _	        |r!t        | -  |       | j                  d       y y )Nzopen file %stzr   )loggerdebugfsr   r   nowr   utcr!   r~   super__init__r]   )r.   r   r   rE   rb   s       r   r   zMemoryFile.__init__  sg    ^T*	||x||4 5GT"IIaL r   c                6    | j                         j                  S rm   )ru   nbytesr.   s    r   r   zMemoryFile.size  s    ~~&&&r   c                    | S rm   r   r   s    r   	__enter__zMemoryFile.__enter__  s    r   c                     y rm   r   r   s    r   closezMemoryFile.close      r   c                     y rm   r   r   s    r   discardzMemoryFile.discard  r   r   c                    | | j                   j                  | j                  <   t        j                  t
        j                        | _        y )Nr   )r   r   r   r   r   r   r   r~   r   s    r   ra   zMemoryFile.commit"  s,    #'dii  5r   )NNN)rc   r   r   r   r   propertyr   r   r   r   ra   __classcell__)rb   s   @r   r^   r^     s5     ' '6r   r^   )
__future__r   loggingr   r   errnor   ior   typingr   r	   fsspecr
   Loggerr   r   r^   r   r   r   <module>r      sG    "  '     %	)	*r) rj!6 !6r   