
    i'                        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m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 G d de      Zy)    )annotationsN)datetimetimezone)	ENOTEMPTY)BytesIO)PurePathPureWindowsPath)AnyClassVar)AbstractFileSystem)LocalFileSystem)stringify_pathz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                D   t        |t              r0t        |t              rt        j                  |      S t        |      }|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   )

isinstancer   r	   r   _strip_protocolr   
startswithlenrstriplstrip)clspaths     LC:\Projects\mas-dev\.venv\Lib\site-packages\fsspec/implementations/memory.pyr   z MemoryFileSystem._strip_protocol   s    dH%$0&66t<<%d+??;'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,   s[   ##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   r0   FileExistsError_parentstripisfileNotADirectoryErrormkdirr-   )r3   r   create_parentsr5   s       r   rC   zMemoryFileSystem.mkdirl   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)rD   )rC   r>   )r3   r   exist_oks      r   makedirszMemoryFileSystem.makedirsz   s1    	JJtDJ1 	 	s    %%c                ,    | j                  |d|       y)zSSet the bytes of given file

        Avoids copies of the data if possible
        wb)dataN)open)r3   r   valuer5   s       r   	pipe_filezMemoryFileSystem.pipe_file   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   r0   r<   removeOSErrorr   r1   r3   r   s     r   rmdirzMemoryFileSystem.rmdir   sd    ##D)2:4###774=  ''-i)>EE#D))r   c                j   t         j                  d       | 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              )	Nzinfo: %sc              3  F   K   | ]  }|j                  d z           yw)r   N)r   ).0pr   s     r   	<genexpr>z(MemoryFileSystem.info.<locals>.<genexpr>   s#      +
.QALL$.Qs   !r   r(   r)   r!   r&   r"   )
loggerdebugr   r0   anylistr   r$   getattrr1   )r3   r   r5   filelikes    `  r   infozMemoryFileSystem.info   s    Z&##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r+bra   r      rI   rJ   zunsupported file mode for z: )r   r0   IsADirectoryErrorr   r?   rA   r>   r   seekr1   
MemoryFileget_intranscommit	__class____name__
ValueError)r3   r   mode
block_size
autocommitcache_optionsr5   parentr;   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   rA   re   r   getvalueisdirr0   r-   r1   )r3   path1path2r5   s       r   cp_filezMemoryFileSystem.cp_file   s    $$U+$$U+;;u *eTZZ.779!DJJu ZZD,,,  ''. - $E**r   c                    t         j                  d|       | j                  |      }	 t        | j                  |   j                         ||       S # t        $ r t        |      w xY w)Nzcat: %s)rX   rY   r   bytesr   	getbufferKeyErrorr1   )r3   r   startendr5   s        r   cat_filezMemoryFileSystem.cat_file   sc    Y%##D)	*D)335eC@AA 	*#D))	*s   (A A'c                |    | j                  |      }	 | j                  |= y # t        $ r}t        |      |d }~ww xY wrt   )r   r   r}   r1   )r3   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rt   )r   r   modifiedr}   r1   rQ   s     r   r   zMemoryFileSystem.modified   sF    ##D)	*::d#,,, 	*#D))	*	   , Ac                    | j                  |      }	 | j                  |   j                  S # t        $ r t	        |      w xY wrt   )r   r   r&   r}   r1   rQ   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)	r   strr   expand_pathreversedexistsrA   rm_filerR   )r3   r   r   r   rV   r6   s         r   rmzMemoryFileSystem.rm   s    dC ''-D59:TD((+TD:  X N%A
 ;;q>{{1~Q

1 ! ;s   B,)T)F)r`   NTN)NN)FN)rj   
__module____qualname____doc__r   __annotations__r0   protocolroot_markerclassmethodr   r<   rC   rG   rM   rR   r^   rr   ry   r   r   r   r&   r    r   r   r   r      s     ')E#($KHK* *>0@****2 $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 )	re   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   )rX   rY   fsr   r   nowr   utcr&   r   super__init__rd   )r3   r   r   rJ   ri   s       r   r   zMemoryFile.__init__  sg    ^T*	||x||4 5GT"IIaL r   c                6    | j                         j                  S rt   )r|   nbytesr3   s    r   r$   zMemoryFile.size   s    ~~&&&r   c                    | S rt   r   r   s    r   	__enter__zMemoryFile.__enter__$  s    r   c                     y rt   r   r   s    r   closezMemoryFile.close'      r   c                     y rt   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   rh   zMemoryFile.commit-  s,    #'dii  5r   )NNN)rj   r   r   r   r   propertyr$   r   r   r   rh   __classcell__)ri   s   @r   re   re     s5     ' '6r   re   )
__future__r   loggingr   r   errnor   ior   pathlibr   r	   typingr
   r   fsspecr   fsspec.implementations.localr   fsspec.utilsr   	getLoggerrX   r   re   r   r   r   <module>r      sR    "  '   -   % 8 '			,	-z) zz!6 !6r   