U
    xÊ¹j)  ã                   @  s®   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 d¡ZG dd„ deƒZG dd„ deƒZdS )é    )ÚannotationsN)ÚdatetimeÚtimezone)Ú	ENOTEMPTY)ÚBytesIO)ÚPurePathÚPureWindowsPath)ÚAnyÚClassVar)ÚAbstractFileSystem)ÚLocalFileSystem)Ústringify_pathz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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'„ Zd0d(d)„ZdS )1ÚMemoryFileSystemz A 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  sz   t |tƒr&t |tƒrt |¡S t|ƒ}| d¡r@|tdƒd … }d|ksPd|krZ| d¡S | 	d¡ d¡}|rvd| S dS )Nz	memory://z::z://r   r   )
Ú
isinstancer   r   r   Ú_strip_protocolr   Ú
startswithÚlenÚrstripÚlstrip)ÚclsÚpath© r   ú\/var/www/html/bwave/python/venv/lib/python3.8/site-packages/fsspec/implementations/memory.pyr      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^|pèg }| 	|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�r¼g 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>j   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ÚoutZp2Zppathr   r   r   Úls,   sf    


üÿ

üÿ ýÿ

 
zMemoryFileSystem.lsc                 K  s¶   |   |¡}|| jks|| jkr&t|ƒ‚|  |¡ d¡rT|  |  |¡¡rTt|  |¡ƒ‚|r˜|  |¡ d¡r˜z| 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+   )r1   r   Úcreate_parentsr3   r   r   r   r<   l   s    
 zMemoryFileSystem.mkdirFc                 C  s2   z| j |dd� W n tk
r,   |s(‚ Y nX d S )NT)r=   )r<   r7   )r1   r   Úexist_okr   r   r   Úmakedirsz   s
    zMemoryFileSystem.makedirsÚ	overwritec                 K  s$   |dkrdnd}| j |||d� dS )zSSet the bytes of given file

        Avoids copies of the data if possible
        ÚcreateÚxbÚwb)ÚmodeÚdataN)Úopen)r1   r   ÚvaluerD   r3   r   r   r   Ú	pipe_file�   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.   r6   ÚremoveÚOSErrorr   r/   ©r1   r   r   r   r   Úrmdir‰   s    


zMemoryFileSystem.rmdirc                   s†   t  dˆ ¡ |  ˆ ¡‰ ˆ | jksBt‡ fdd„t| jƒ| j D ƒƒrNˆ dddœS ˆ | jkrz| jˆ  }ˆ |jdt|dd ƒd	œS t	ˆ ƒ‚d S )
Nzinfo: %sc                 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   )
ÚloggerÚdebugr   r.   ÚanyÚlistr   r   Úgetattrr/   )r1   r   r3   Zfileliker   rO   r   Úinfo–   s"    
ÿý


üzMemoryFileSystem.infoÚrbNc                 K  s  |   |¡}d|kr |  |¡r t‚|| jkr2t|ƒ‚|}t|ƒdkr`|  |¡}|  |¡r6t|ƒ‚q6|dkrª|| jkr | j| }|dkr’| 	dd¡ n
| 	d¡ |S t
|ƒ‚n^|dkrì|dkrÈ|  |¡rÈt‚t| || d	¡ƒ}	| jsè|	 ¡  |	S | jj}
td
|
› d|›�ƒ‚d S )NÚxr"   )rW   Úabzr+brY   r   é   >   rC   rB   rB   rE   zunsupported file mode for z: )r   Úexistsr7   r.   ÚIsADirectoryErrorr   r8   r:   r   Úseekr/   Ú
MemoryFileÚgetZ_intransÚcommitÚ	__class__Ú__name__Ú
ValueError)r1   r   rD   Ú
block_sizeZ
autocommitZcache_optionsr3   Úparentr&   Úmr   r   r   r   Ú_open¬   s6    	








zMemoryFileSystem._openc                 K  sj   |   |¡}|   |¡}|  |¡r<t| || j|  ¡ ƒ| j|< n*|  |¡r^|| jkrf| j |¡ nt|ƒ‚d S ©N)	r   r:   r^   r   ÚgetvalueÚisdirr.   r+   r/   )r1   Zpath1Zpath2r3   r   r   r   Úcp_fileÖ   s    


  ÿ

zMemoryFileSystem.cp_filec              
   K  sd   t  d|¡ |  |¡}zt| j|  ¡ ||… ƒW S  tk
r^ } zt|ƒ|‚W 5 d }~X Y nX d S )Nzcat: %s)rQ   rR   r   Úbytesr   Ú	getbufferÚKeyErrorr/   )r1   r   ÚstartÚendr3   Úer   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 rh   )r   r   rn   r/   ©r1   r   rq   r   r   r   Ú_rmë   s
    
zMemoryFileSystem._rmc              
   C  sJ   |   |¡}z| j| jW S  tk
rD } zt|ƒ|‚W 5 d }~X Y nX d S rh   )r   r   Úmodifiedrn   r/   rs   r   r   r   ru   ò   s
    
zMemoryFileSystem.modifiedc              
   C  sJ   |   |¡}z| j| jW S  tk
rD } zt|ƒ|‚W 5 d }~X Y nX d S rh   )r   r   r!   rn   r/   rs   r   r   r   r!   ù   s
    
zMemoryFileSystem.createdc                 C  s   |   |¡}|| jkS rh   )r   r   rK   r   r   r   r:      s    
zMemoryFileSystem.isfilec                   sx   t |tƒrˆ  |¡}n‡ fdd„|D ƒ}ˆ j|||d�}t|ƒD ]2}ˆ  |¡rZˆ  |¡ q@ˆ  |¡shq@q@ˆ  |¡ q@d S )Nc                   s   g | ]}ˆ   |¡‘qS r   )r   rM   ©r1   r   r   r'     s     z'MemoryFileSystem.rm.<locals>.<listcomp>)Ú	recursiveÚmaxdepth)	r   Ústrr   Zexpand_pathÚreversedr:   Zrm_filer[   rL   )r1   r   rw   rx   r4   rN   r   rv   r   Úrm  s    


zMemoryFileSystem.rm)T)T)F)r@   )rW   NTN)NN)FN)rb   Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r.   ÚprotocolZroot_markerÚclassmethodr   r6   r<   r?   rH   rL   rV   rg   rk   rr   rt   ru   r!   r:   r{   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 )r^   zÐA 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                   sV   t  d|¡ || _|| _tjtjd�| _tjtjd�| _	|rRt
ƒ  |¡ |  d¡ d S )Nzopen file %s©Útzr   )rQ   rR   Úfsr   r   Únowr   Úutcr!   ru   ÚsuperÚ__init__r]   )r1   r„   r   rE   ©ra   r   r   rˆ     s    zMemoryFile.__init__c                 C  s
   |   ¡ jS rh   )rm   Únbytesrv   r   r   r   r   )  s    zMemoryFile.sizec                 C  s   | S rh   r   rv   r   r   r   Ú	__enter__-  s    zMemoryFile.__enter__c                 C  s   d S rh   r   rv   r   r   r   Úclose0  s    zMemoryFile.closec                 C  s   d S rh   r   rv   r   r   r   Údiscard3  s    zMemoryFile.discardc                 C  s"   | | j j| j< tjtjd�| _d S )Nr‚   )r„   r   r   r   r…   r   r†   ru   rv   r   r   r   r`   6  s    zMemoryFile.commit)NNN)rb   r|   r}   r~   rˆ   Úpropertyr   r‹   rŒ   r�   r`   Ú__classcell__r   r   r‰   r   r^     s   

r^   )Ú
__future__r   Úloggingr   r   Úerrnor   Úior   Úpathlibr   r   Útypingr	   r
   Zfsspecr   Zfsspec.implementations.localr   Zfsspec.utilsr   Ú	getLoggerrQ   r   r^   r   r   r   r   Ú<module>   s   
  