U
    -e%                     @  s   d dl mZ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mZ d dlmZ edZG d	d
 d
eZG dd de
ZdS )    )absolute_importannotationsdivisionprint_functionN)datetime)	ENOTEMPTY)BytesIO)AnyClassVar)AbstractFileSystemzfsspec.memoryfsc                   @  s   e Zd ZU dZi Zded< dgZdZdZe	dd Z
d)d
dZd*ddZd+ddZdd Zdd Zdd Zdd Zd,ddZdd Zd-dd Zd!d" Zd#d$ Zd%d& Zd.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 Zmemory/c                 C  sT   | dr|tdd  }d|ks*d|kr4|dS |dd}|rPd| S dS )Nz	memory://z::z://r   r   )
startswithlenrstriplstrip)clspath r   ^/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/fsspec/implementations/memory.py_strip_protocol   s    

z MemoryFileSystem._strip_protocolTc           	      K  s  |  |}|| jkrB|s|gS || j| jd| j| j dgS t }|d }g }t| jD ]}||r^d|t|d  kr|	|| j| jd| j| j d q^t|t|kr^||t|d  
ddd  }||kr^|pg }|	|ddd || q^| jD ]}||rd|t|d  kr\||kr|	|ddd || nF||t|d  
ddd  }||kr|	|ddd || q|s|| jkrg S t||r|S tdd	 |D S )
Nfilenamesizetypecreatedr      r   	directoryr   r   r   c                 S  s   g | ]}|d  qS )r   r   ).0fr   r   r   
<listcomp>a   s     z'MemoryFileSystem.ls.<locals>.<listcomp>)r   r   r   r   	timestampsettupler   r   appendsplitaddpseudo_dirsFileNotFoundErrorsorted)	selfr   detailkwargspathsZstarteroutp2Zppathr   r   r   ls#   sf    




 

 
zMemoryFileSystem.lsc                 K  s   |  |}|| jks|| jkr&t|| |drT| | |rTt| ||r| |drz| j| ||f| W n tk
r   Y nX |r|| jkr| j	| d S )Nr   )
r   r   r+   FileExistsError_parentstripisfileNotADirectoryErrormkdirr(   )r.   r   create_parentsr0   r   r   r   r:   c   s    
 zMemoryFileSystem.mkdirFc                 C  s2   z| j |dd W n tk
r,   |s( Y nX d S )NT)r;   )r:   r5   )r.   r   exist_okr   r   r   makedirsq   s
    zMemoryFileSystem.makedirsc                 K  s   | j |d|d dS )zSSet the bytes of given file

        Avoids copies of the data if possible
        wb)dataN)open)r.   r   valuer0   r   r   r   	pipe_filex   s    zMemoryFileSystem.pipe_filec                 C  sR   |  |}|dkrd S || jkrF| |s8| j| qNttd|nt|d S )Nr   zDirectory not empty)r   r+   r4   removeOSErrorr   r,   r.   r   r   r   r   rmdir   s    


zMemoryFileSystem.rmdirc                 K  s   |  |}|| jkp|| jkS N)r   r   r+   )r.   r   r0   r   r   r   exists   s    
zMemoryFileSystem.existsc                   sz   |     | jks6t fddt| j| j D rB dddS  | jkrn| j  } |jdt|dd dS t d S )	Nc                 3  s   | ]}|  d  V  qdS )r   N)r   r"   pr   r   r   	<genexpr>   s    z(MemoryFileSystem.info.<locals>.<genexpr>r   r    r!   r   r   r   )r   r+   anylistr   r   getattrr,   )r.   r   r0   Zfileliker   rK   r   info   s     



zMemoryFileSystem.inforbNc           
      K  s   |  |}|| jkrt||}t|dkrJ| |}| |r t|q |dkr|| jkr| j| }|dkr||dd n
|d |S t	||dkrt
| ||d}	| js|	  |	S d S )Nr   )rQ   abzrb+rR   r      r>   r?   )r   r+   IsADirectoryErrorr   r6   r8   r5   r   seekr,   
MemoryFilegetZ_intranscommit)
r.   r   mode
block_sizeZ
autocommitZcache_optionsr0   parentr#   mr   r   r   _open   s*    	







zMemoryFileSystem._openc                 K  sj   |  |}|  |}| |r<t| || j|  | j|< n*| |r^|| jkrf| j| nt|d S rG   )	r   r8   rV   r   getvalueisdirr+   r(   r,   )r.   Zpath1Zpath2r0   r   r   r   cp_file   s    


  

zMemoryFileSystem.cp_filec                 K  sH   |  |}zt| j|  || W S  tk
rB   t|Y nX d S rG   )r   bytesr   	getbufferKeyErrorr,   )r.   r   startendr0   r   r   r   cat_file   s
    
zMemoryFileSystem.cat_filec              
   C  sH   |  |}z| j|= W n, tk
rB } zt||W 5 d }~X Y nX d S rG   )r   r   rc   r,   )r.   r   er   r   r   _rm   s
    
zMemoryFileSystem._rmc                 C  s:   |  |}z| j| jW S  tk
r4   t|Y nX d S rG   )r   r   modifiedrc   r,   rE   r   r   r   ri      s
    
zMemoryFileSystem.modifiedc                 C  s:   |  |}z| j| jW S  tk
r4   t|Y nX d S rG   )r   r   r   rc   r,   rE   r   r   r   r      s
    
zMemoryFileSystem.createdc                   sv   t |tr |}n fdd|D } j|||d}t|D ]0} |sPq@ |rf | q@ | q@d S )Nc                   s   g | ]}  |qS r   )r   rI   r.   r   r   r$      s     z'MemoryFileSystem.rm.<locals>.<listcomp>)	recursivemaxdepth)	
isinstancestrr   Zexpand_pathreversedrH   r8   Zrm_filerF   )r.   r   rk   rl   r1   rJ   r   rj   r   rm   s    


zMemoryFileSystem.rm)T)T)F)rQ   NTN)NN)FN)__name__
__module____qualname____doc__r   __annotations__r+   protocolZroot_markerclassmethodr   r4   r:   r=   rB   rF   rH   rP   r]   r`   rf   rh   ri   r   rp   r   r   r   r   r      s2   


@

    
#
r   c                      sN   e Zd ZdZd fdd	Zedd Zdd Zd	d
 Zdd Z	dd Z
  ZS )rV   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                   sJ   t d| || _|| _t | _t | _|rFt 	| | 
d d S )Nzopen file %sr   )loggerdebugfsr   r   utcnowr   ri   super__init__rU   )r.   rz   r   r?   	__class__r   r   r}     s    

zMemoryFile.__init__c                 C  s
   |   jS rG   )rb   nbytesrj   r   r   r   r     s    zMemoryFile.sizec                 C  s   | S rG   r   rj   r   r   r   	__enter__  s    zMemoryFile.__enter__c                 C  s   d S rG   r   rj   r   r   r   close  s    zMemoryFile.closec                 C  s   d S rG   r   rj   r   r   r   discard   s    zMemoryFile.discardc                 C  s   | | j j| j< t | _d S rG   )rz   r   r   r   r{   ri   rj   r   r   r   rX   #  s    zMemoryFile.commit)NNN)rq   rr   rs   rt   r}   propertyr   r   r   r   rX   __classcell__r   r   r~   r   rV     s   

rV   )
__future__r   r   r   r   loggingr   errnor   ior   typingr	   r
   Zfsspecr   Loggerrx   r   rV   r   r   r   r   <module>   s   
 w