
    çg%(                        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dS )    )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 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                   t          |t                    r8t          |t                    rt          j        |          S t          |          }|                    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   )

isinstancer   r	   r   _strip_protocolr   
startswithlenrstriplstrip)clspaths     Y/var/www/html/ai-engine/env/lib/python3.11/site-packages/fsspec/implementations/memory.pyr   z MemoryFileSystem._strip_protocol   s    dH%% 	,$00 ,&6t<<<%d++??;'' 	,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>j   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   r5   FileExistsError_parentstripisfileNotADirectoryErrormkdirr2   )r8   r   create_parentsr:   s       r   rG   zMemoryFileSystem.mkdirl   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)rH   )rG   rB   )r8   r   exist_oks      r   makedirszMemoryFileSystem.makedirsz   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)r8   r   valuer:   s       r   	pipe_filezMemoryFileSystem.pipe_file   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   r5   r@   removeOSErrorr   r6   r8   r   s     r   rmdirzMemoryFileSystem.rmdir   s    ##D))2::F4###774== F ''-----i)>EEE#D)))r   c                l   t                               d           |                               | 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                    )	Nzinfo: %sc              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"   )
loggerdebugr   r5   anylistr   r$   getattrr6   )r8   r   r:   filelikes    `  r   infozMemoryFileSystem.info   s    Z&&&##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'   )rb   abzr+brd   r      rM   rN   zunsupported file mode for z: )r   r5   IsADirectoryErrorr   rC   rE   rB   r   seekr6   
MemoryFileget_intranscommit	__class____name__
ValueError)r8   r   mode
block_size
autocommitcache_optionsr:   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   rE   rh   r   getvalueisdirr5   r2   r6   )r8   path1path2r:   s       r   cp_filezMemoryFileSystem.cp_file   s    $$U++$$U++;;u 	+ *eTZ.7799! !DJu ZZ 	+D,,, ''..... -, $E***r   c                   t                               d|           |                     |          }	 t          | j        |                                         ||                   S # t          $ r}t          |          |d }~ww xY w)Nzcat: %s)r[   r\   r   bytesr   	getbufferKeyErrorr6   )r8   r   startendr:   es         r   cat_filezMemoryFileSystem.cat_file   s    Y%%%##D))	1D)3355eCi@AAA 	1 	1 	1#D))q0	1s   3A& &
B0B  Bc                    |                      |          }	 | j        |= d S # t          $ r}t          |          |d }~ww xY wrw   )r   r   r   r6   r8   r   r   s      r   _rmzMemoryFileSystem._rm   sY    ##D))	1
4    	1 	1 	1#D))q0	1s   ! 
A ;A c                    |                      |          }	 | j        |         j        S # t          $ r}t	          |          |d }~ww xY wrw   )r   r   modifiedr   r6   r   s      r   r   zMemoryFileSystem.modified   sY    ##D))	1:d#,, 	1 	1 	1#D))q0	1   ) 
AAAc                    |                      |          }	 | j        |         j        S # t          $ r}t	          |          |d }~ww xY wrw   )r   r   r&   r   r6   r   s      r   r&   zMemoryFileSystem.created   sY    ##D))	1:d#++ 	1 	1 	1#D))q0	1r   c                >    |                      |          }|| j        v S rw   )r   r   rU   s     r   rE   zMemoryFileSystem.isfile   s"    ##D))tz!!r   c                z    t          |t                    r                     |          }n fd|D             }                     |||          }t	          |          D ]X}                     |          r                     |           -                     |          sC                     |           Yd S )Nc                :    g | ]}                     |          S r+   )r   )r,   rY   r8   s     r   r.   z'MemoryFileSystem.rm.<locals>.<listcomp>  s'    :::D((++:::r   )	recursivemaxdepth)	r   strr   expand_pathreversedrE   rm_fileexistsrV   )r8   r   r   r   r;   rY   s   `     r   rmzMemoryFileSystem.rm   s    dC   	;''--DD::::T:::D  X NN% 
	 
	A{{1~~ 	Q
 [[^^ 

1
	 
	r   )T)F)rb   NTN)NN)FN)rm   
__module____qualname____doc__r   __annotations__r5   protocolroot_markerclassmethodr   r@   rG   rK   rQ   rV   ra   ru   r|   r   r   r   r&   rE   r   r+   r   r   r   r      sa          ')E(((($KHK* * [*>0 >0 >0 >0@* * * *   * * ** * ** * *2 $L $L $L $LL+ + +1 1 1 11 1 11 1 1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 )
rh   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   )r[   r\   fsr   r   nowr   utcr&   r   super__init__rg   )r8   r   r   rN   rl   s       r   r   zMemoryFile.__init__  s    ^T***	|x|444 555 	GGT"""IIaLLLLL	 	r   c                4    |                                  j        S rw   )r   nbytesr8   s    r   r$   zMemoryFile.size$  s    ~~&&r   c                    | S rw   r+   r   s    r   	__enter__zMemoryFile.__enter__(  s    r   c                    d S rw   r+   r   s    r   closezMemoryFile.close+      r   c                    d S rw   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   rk   zMemoryFile.commit1  s,    #'di  555r   )NNN)rm   r   r   r   r   propertyr$   r   r   r   rk   __classcell__)rl   s   @r   rh   rh     s               ' ' X'      6 6 6 6 6 6 6r   rh   )
__future__r   loggingr   r   errnor   ior   pathlibr   r	   typingr
   r   fsspecr   fsspec.implementations.localr   fsspec.utilsr   	getLoggerr[   r   rh   r+   r   r   <module>r      s=   " " " " " "  ' ' ' ' ' ' ' '             - - - - - - - -                 % % % % % % 8 8 8 8 8 8 ' ' ' ' ' '		,	-	-~ ~ ~ ~ ~) ~ ~ ~B!6 !6 !6 !6 !6 !6 !6 !6 !6 !6r   