
    OOId%                         d dl mZmZmZ d dlZd dl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dS )    )absolute_importdivisionprint_functionN)datetime)	ENOTEMPTY)BytesIO)AbstractFileSystemzfsspec.memoryfsc                       e Zd ZdZi Z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 Z	 	 	 	 ddZd ZddZd Zd Zd ZddZdS )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.
     memory/c                     |                     d          r|t          d          d          }d|v sd|v r|                    d          S |                    d                              d          }|rd|z   ndS )Nz	memory://z::z://r   r   )
startswithlenrstriplstrip)clspaths     =lib/python3.11/site-packages/fsspec/implementations/memory.py_strip_protocolz MemoryFileSystem._strip_protocol   s    ??;'' 	,K((**+D4<<5D==;;s###{{3&&s++!)sTzzr)    Tc                 6   |                      |          }|| j        v r?|s|gS || j        |         j        d| j        |         j                                        dgS t                      }|dz   }g }t          | j                  D ]}|                    |          rd|t          |          d          vrM|	                    || j        |         j        d| j        |         j                                        d           ~t          |          t          |          k    rj||t          |          d          
                    dd          d         z   }||vr2|pg }|	                    |ddd           |                    |           
| j        D ]}|                    |          rd|t          |          d          vr3||vr.|	                    |ddd           |                    |           c||t          |          d          
                    dd          d         z   }||vr.|	                    |ddd           |                    |           |s|| j        v rg S t          |          |r|S t          d |D                       S )	Nfilenamesizetypecreatedr      r   	directoryr   r   r   c                     g | ]
}|d          S )r    ).0fs     r   
<listcomp>z'MemoryFileSystem.ls.<locals>.<listcomp>`   s    ...Qqy...r   )r   storer   r   	timestampsettupler   r   appendsplitaddpseudo_dirsFileNotFoundErrorsorted)	selfr   detailkwargspathsstarteroutp2ppaths	            r   lszMemoryFileSystem.ls"   s   ##D))4: v ! Jt,1"#z$/7AACC	   *
## 	) 	)B}}W%% )bW000JJ$&$(JrN$7$*'+z"~'='G'G'I'I	     WWs7||++#bW&8&>&>sA&F&Fq&IIEE))!iR

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

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

E1k#R#RSSS		%((( 	*t'''	#D))) 	J..#...///r   c                 d   |                      |          }|| j        v s	|| j        v rt          |          |                     |                              d          rJ|                     |                     |                    r"t          |                     |                    |r\|                     |                              d          r4	  | j        |                     |          |fi | n# t          $ r Y nw xY w|r%|| j        vr| j        	                    |           d S d S d S )Nr   )
r   r(   r/   FileExistsError_parentstripisfileNotADirectoryErrormkdirr,   )r2   r   create_parentsr4   s       r   rA   zMemoryFileSystem.mkdirb   sS   ##D))4:)9!9!9!$'''<<##C(( 	9T[[d9K9K-L-L 	9$T\\$%7%7888 	dll40066s;; 	
4<<--~HHHHHH"    	*D 000##D)))))	* 	*00s   "C7 7
DDFc                 ^    	 |                      |d           d S # t          $ r |s Y d S w xY w)NT)rB   )rA   r<   )r2   r   exist_oks      r   makedirszMemoryFileSystem.makedirsp   sX    	JJtDJ11111 	 	 	   	s    ,,c                 6    |                      |d|           dS )zSSet the bytes of given file

        Avoids copies of the data if possible
        wb)dataN)open)r2   r   valuer4   s       r   	pipe_filezMemoryFileSystem.pipe_filew   s"    
 			$5	)))))r   c                     |                      |          }|dk    rd S || j        v rG|                     |          s| j                            |           d S t	          t
          d|          t          |          )Nr   zDirectory not empty)r   r/   r:   removeOSErrorr   r0   r2   r   s     r   rmdirzMemoryFileSystem.rmdir~   s    ##D))2::F4###774== F ''-----i)>EEE#D)))r   c                 P    |                      |          }|| j        v p|| j        v S N)r   r(   r/   )r2   r   r4   s      r   existszMemoryFileSystem.exists   s/    ##D))tz!=TT-=%==r   c                 6   |                                | j        v s5t          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   H   K   | ]}|                     d z             V  dS )r   N)r   )r%   pr   s     r   	<genexpr>z(MemoryFileSystem.info.<locals>.<genexpr>   sD       +
 +
)*ALL$$+
 +
 +
 +
 +
 +
r   r   r!   r"   r   r   r   )r   r/   anylistr(   r   getattrr0   )r2   r   r4   filelikes    `  r   infozMemoryFileSystem.info   s    ##D))4###s +
 +
 +
 +
.24:.>.>AQ.Q+
 +
 +
 (
 (
# #  
 TZz$'H "8Y==	   $D)))r   rbNc                 j   |                      |          }|| j        v rt          |          |}t          |          dk    rL|                     |          }|                     |          rt          |          t          |          dk    L|dv rY|| j        v rA| j        |         }|dk    r|                    dd           n|                    d           |S t          |          |dk    rAt          | ||                    d                    }	| j        s|	                                 |	S d S )Nr    )r]   abzrb+r_   r      rG   rH   )r   r/   IsADirectoryErrorr   r=   r?   r<   r(   seekr0   
MemoryFileget_intranscommit)
r2   r   mode
block_size
autocommitcache_optionsr4   parentr&   ms
             r   _openzMemoryFileSystem._open   s;    ##D))4####D)))&kkAoo\\&))F{{6"" .%f--- &kkAoo &&&tz!!Jt$4<<FF1aLLLL FF1III'---4<<4vzz&'9'9::A= 


H	 <r   c                    |                      |          }|                      |          }|                     |          r8t          | || j        |                                                   | j        |<   d S |                     |          r'|| j        vr| j                            |           d S d S t          |          rR   )	r   r?   rc   r(   getvalueisdirr/   r,   r0   )r2   path1path2r4   s       r   cp_filezMemoryFileSystem.cp_file   s    $$U++$$U++;;u 	+ *eTZ.7799! !DJu ZZ 	+D,,, ''..... -, $E***r   c                     |                      |          }	 t          | j        |                                         ||                   S # t          $ r t          |          w xY wrR   )r   bytesr(   	getbufferKeyErrorr0   )r2   r   startendr4   s        r   cat_filezMemoryFileSystem.cat_file   sm    ##D))	*D)3355eCi@AAA 	* 	* 	*#D)))	*s   3A A%c                     |                      |          }	 | j        |= d S # t          $ r}t          |          |d }~ww xY wrR   )r   r(   rw   r0   )r2   r   es      r   _rmzMemoryFileSystem._rm   sY    ##D))	1
4    	1 	1 	1#D))q0	1s   ! 
A ;A c                     |                      |          }	 | j        |         j        S # t          $ r t	          |          w xY wrR   )r   r(   modifiedrw   r0   rO   s     r   r   zMemoryFileSystem.modified   sS    ##D))	*:d#,, 	* 	* 	*#D)))	*	   ) Ac                     |                      |          }	 | j        |         j        S # t          $ r t	          |          w xY wrR   )r   r(   r   rw   r0   rO   s     r   r   zMemoryFileSystem.created   sS    ##D))	*:d#++ 	* 	* 	*#D)))	*r   c                 z    t          |t                    r                     |          }n fd|D             }                     |||          }t	          |          D ]X}                     |          s                     |          r                     |           C                     |           Yd S )Nc                 :    g | ]}                     |          S r$   )r   )r%   rV   r2   s     r   r'   z'MemoryFileSystem.rm.<locals>.<listcomp>   s'    :::D((++:::r   )	recursivemaxdepth)	
isinstancestrr   expand_pathreversedrS   r?   rm_filerP   )r2   r   r   r   r5   rV   s   `     r   rmzMemoryFileSystem.rm   s    dC   	;''--DD::::T:::D  X NN% 
	 
	A
 ;;q>> {{1~~ Q

1
	 
	r   )T)F)r]   NTN)NN)FN)__name__
__module____qualname____doc__r(   r/   protocolroot_markerclassmethodr   r:   rA   rE   rK   rP   rS   r\   rm   rs   rz   r}   r   r   r   r$   r   r   r   r      sO         E$KHK* * [*>0 >0 >0 >0@* * * *   * * ** * *> > >* * *0 ! ! ! !F+ + +* * * *1 1 1* * ** * *     r   r   c                   R     e Zd ZdZd	 fd	Zed             Zd Zd Zd Z	d Z
 xZS )
rc   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)
    Nc                 .   t                               d|           || _        || _        t	          j                    | _        t	          j                    | _        |r8t                      	                    |           | 
                    d           d S d S )Nzopen file %sr   )loggerdebugfsr   r   utcnowr   r   super__init__rb   )r2   r   r   rH   	__class__s       r   r   zMemoryFile.__init__  s    ^T***	(( )) 	GGT"""IIaLLLLL	 	r   c                 4    |                                  j        S rR   )rv   nbytesr2   s    r   r   zMemoryFile.size  s    ~~&&r   c                     | S rR   r$   r   s    r   	__enter__zMemoryFile.__enter__  s    r   c                     d S rR   r$   r   s    r   closezMemoryFile.close      r   c                     d S rR   r$   r   s    r   discardzMemoryFile.discard  r   r   c                 ^    | | j         j        | j        <   t          j                    | _        d S rR   )r   r(   r   r   r   r   r   s    r   rf   zMemoryFile.commit"  s%    #'di  ))r   )NNN)r   r   r   r   r   propertyr   r   r   r   rf   __classcell__)r   s   @r   rc   rc     s               ' ' X'      * * * * * * *r   rc   )
__future__r   r   r   loggingr   errnor   ior   fsspecr	   Loggerr   r   rc   r$   r   r   <module>r      s    @ @ @ @ @ @ @ @ @ @                    % % % % % %	)	*	*s s s s s) s s sl!* !* !* !* !* !* !* !* !* !*r   