
    ̒ge%                        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dS )    )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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.
    zClassVar[dict[str, Any]]store 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>a   s    ...Qqy...r   )r   r   r   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   r0   FileExistsError_parentstripisfileNotADirectoryErrormkdirr-   )r3   r   create_parentsr5   s       r   rB   zMemoryFileSystem.mkdirc   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)rC   )rB   r=   )r3   r   exist_oks      r   makedirszMemoryFileSystem.makedirsq   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)r3   r   valuer5   s       r   	pipe_filezMemoryFileSystem.pipe_filex   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   r0   r;   removeOSErrorr   r1   r3   r   s     r   rmdirzMemoryFileSystem.rmdir   s    ##D))2::F4###774== F ''-----i)>EEE#D)))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   r0   anylistr   r   getattrr1   )r3   r   r5   filelikes    `  r   infozMemoryFileSystem.info   s    ##D))4###s +
 +
 +
 +
.24:.>.>AQ.Q+
 +
 +
 (
 (
# #  
 TZz$'H "8Y==	   $D)))r   rbNc                   |                      |          }|| 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 | j        j        }
t!          d|
 d	|          )
Nr"   )r[   abzrb+r]   r      rH   rI   zunsupported file mode for z: )r   r0   IsADirectoryErrorr   r>   r@   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)))&kkAoo\\&))F{{6"" .%f--- &kkAoo &&&tz!!Jt$4<<FF1aLLLL FF1III'---T\\4vzz&'9'9::A= 


H>*DJ$JJ$JJKKKr   c                   |                      |          }|                      |          }|                     |          r8t          | || j        |                                                   | j        |<   d S |                     |          r'|| j        vr| j                            |           d S d S t          |          N)	r   r@   ra   r   getvalueisdirr0   r-   r1   )r3   path1path2r5   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rp   )r   bytesr   	getbufferKeyErrorr1   )r3   r   startendr5   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rp   )r   r   ry   r1   )r3   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rp   )r   r   modifiedry   r1   rP   s     r   r   zMemoryFileSystem.modified   sS    ##D))	*:d#,, 	* 	* 	*#D)))	*	   ) Ac                    |                      |          }	 | j        |         j        S # t          $ r t	          |          w xY wrp   )r   r   r!   ry   r1   rP   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'   rT   r3   s     r   r)   z'MemoryFileSystem.rm.<locals>.<listcomp>   s'    :::D((++:::r   )	recursivemaxdepth)	
isinstancestrr   expand_pathreversedexistsr@   rm_filerQ   )r3   r   r   r   r6   rT   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)rf   
__module____qualname____doc__r   __annotations__r0   protocolroot_markerclassmethodr   r;   rB   rF   rL   rQ   rZ   rn   ru   r|   r   r   r!   r   r&   r   r   r   r      sR          ')E(((($KHK* * [*>0 >0 >0 >0@* * * *   * * ** * ** * *0 $L $L $L $LL+ + +* * * *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 )
ra   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                  | _        t	          j        t          j                  | _	        |r8t                                          |           |                     d           d S d S )Nzopen file %stzr   )loggerdebugfsr   r   nowr   utcr!   r   super__init__r`   )r3   r   r   rI   re   s       r   r   zMemoryFile.__init__  s    ^T***	|x|444 555 	GGT"""IIaLLLLL	 	r   c                4    |                                  j        S rp   )rx   nbytesr3   s    r   r   zMemoryFile.size  s    ~~&&r   c                    | S rp   r&   r   s    r   	__enter__zMemoryFile.__enter__  s    r   c                    d S rp   r&   r   s    r   closezMemoryFile.close      r   c                    d S rp   r&   r   s    r   discardzMemoryFile.discard  r   r   c                v    | | j         j        | j        <   t          j        t
          j                  | _        d S )Nr   )r   r   r   r   r   r   r   r   r   s    r   rd   zMemoryFile.commit"  s,    #'di  555r   )NNN)rf   r   r   r   r   propertyr   r   r   r   rd   __classcell__)re   s   @r   ra   ra     s               ' ' X'      6 6 6 6 6 6 6r   ra   )
__future__r   loggingr   r   errnor   ior   typingr   r	   fsspecr
   Loggerr   r   ra   r&   r   r   <module>r      s    " " " " " "  ' ' ' ' ' ' ' '                             % % % % % %	)	*	*r r r r r) r r rj!6 !6 !6 !6 !6 !6 !6 !6 !6 !6r   