a
    yn„f–'  ã                   @  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„ 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 )-Ú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|v sPd|v 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   ú`/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/fsspec/implementations/memory.pyr      s    




z MemoryFileSystem._strip_protocolTc           	      K  sà  |   |¡}|| jv rB|s|gS || j| jd| j| j ¡ dœgS tƒ }|d }g }t| jƒD ]¨}| |¡r^d|t|ƒd … vrª| 	|| j| jd| j| j ¡ dœ¡ q^t|ƒt|ƒkr^||t|ƒd …  
dd¡d  }||vr^|pèg }| 	|dddœ¡ | |¡ q^| jD ]–}| |¡�rd|t|ƒd … v�r\||v�r¢| 	|dddœ¡ | |¡ nF||t|ƒd …  
dd¡d  }||v�r| 	|dddœ¡ | |¡ �q|�sÄ|| jv �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   ó    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v s|| jv r&t|ƒ‚|  |¡ d¡rT|  |  |¡¡rTt|  |¡ƒ‚|rš|  |¡ d¡ršz| j|  |¡|fi |¤Ž W n ty˜   Y n0 |r´|| jvr´| j 	|¡ d S )Nr   )
r   r   r/   ÚFileExistsErrorÚ_parentÚstripÚisfileÚNotADirectoryErrorÚmkdirr,   )r2   r   Úcreate_parentsr4   r   r   r   r>   l   s    
 zMemoryFileSystem.mkdirFc                 C  s0   z| j |dd� W n ty*   |s&‚ Y n0 d S )NT)r?   )r>   r9   )r2   r   Úexist_okr   r   r   Úmakedirsz   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)r2   r   Úvaluer4   r   r   r   Ú	pipe_file�   s    zMemoryFileSystem.pipe_filec                 C  sR   |   |¡}|dkrd S || jv rF|  |¡s8| j |¡ qNttd|ƒ‚nt|ƒ‚d S )Nr   zDirectory not empty)r   r/   r8   ÚremoveÚOSErrorr   r0   ©r2   r   r   r   r   Úrmdirˆ   s    


zMemoryFileSystem.rmdirc                   s†   t  dˆ ¡ |  ˆ ¡‰ ˆ | jv sBt‡ fdd„t| jƒ| j D ƒƒrNˆ dddœS ˆ | jv 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   Úgetattrr0   )r2   r   r4   Zfileliker   rM   r   Úinfo•   s"    
ÿý


üzMemoryFileSystem.infoÚrbNc                 K  sà   |   |¡}|| jv rt|ƒ‚|}t|ƒdkrJ|  |¡}|  |¡r t|ƒ‚q |dv r”|| jv rŠ| j| }|dkr|| dd¡ n
| d¡ |S t	|ƒ‚nH|dkrÀt
| || d¡ƒ}	| js¼|	 ¡  |	S | jj}
td|
› d	|›�ƒ‚d S )
Nr"   )rU   Úabzr+brV   r   é   rB   rC   zunsupported file mode for z: )r   r/   ÚIsADirectoryErrorr   r:   r<   r9   r   Úseekr0   Ú
MemoryFileÚgetZ_intransÚcommitÚ	__class__Ú__name__Ú
ValueError)r2   r   ÚmodeÚ
block_sizeZ
autocommitZcache_optionsr4   Úparentr&   Úmr   r   r   r   Ú_open«   s.    	








zMemoryFileSystem._openc                 K  sj   |   |¡}|   |¡}|  |¡r<t| || j|  ¡ ƒ| j|< n*|  |¡r^|| jvrf| j |¡ nt|ƒ‚d S ©N)	r   r<   rZ   r   ÚgetvalueÚisdirr/   r,   r0   )r2   Zpath1Zpath2r4   r   r   r   Úcp_fileÑ   s    


ÿ

zMemoryFileSystem.cp_filec                 K  sR   t  d|¡ |  |¡}zt| j|  ¡ ||… ƒW S  tyL   t|ƒ‚Y n0 d S )Nzcat: %s)rO   rP   r   Úbytesr   Ú	getbufferÚKeyErrorr0   )r2   r   ÚstartÚendr4   r   r   r   Úcat_fileÞ   s    
zMemoryFileSystem.cat_filec              
   C  sJ   |   |¡}z| j|= W n. tyD } zt|ƒ|‚W Y d }~n
d }~0 0 d S re   )r   r   rk   r0   )r2   r   Úer   r   r   Ú_rmæ   s
    
zMemoryFileSystem._rmc                 C  s8   |   |¡}z| j| jW S  ty2   t|ƒ‚Y n0 d S re   )r   r   Úmodifiedrk   r0   rI   r   r   r   rq   í   s
    
zMemoryFileSystem.modifiedc                 C  s8   |   |¡}z| j| jW S  ty2   t|ƒ‚Y n0 d S re   )r   r   r!   rk   r0   rI   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   rK   ©r2   r   r   r'   ÿ   r(   z'MemoryFileSystem.rm.<locals>.<listcomp>)Ú	recursiveÚmaxdepth)	r   Ústrr   Zexpand_pathÚreversedÚexistsr<   Zrm_filerJ   )r2   r   rs   rt   r5   rL   r   rr   r   Úrmû   s    


zMemoryFileSystem.rm)T)T)F)rU   NTN)NN)FN)r^   Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r/   ÚprotocolZroot_markerÚclassmethodr   r8   r>   rA   rF   rJ   rT   rd   rh   rn   rp   rq   r!   rx   r   r   r   r   r      s0   


@

    ú
&
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 )rZ   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   )rO   rP   Úfsr   r   Únowr   Úutcr!   rq   ÚsuperÚ__init__rY   )r2   r�   r   rC   ©r]   r   r   r…     s    zMemoryFile.__init__c                 C  s
   |   ¡ jS re   )rj   Únbytesrr   r   r   r   r      s    zMemoryFile.sizec                 C  s   | S re   r   rr   r   r   r   Ú	__enter__$  s    zMemoryFile.__enter__c                 C  s   d S re   r   rr   r   r   r   Úclose'  s    zMemoryFile.closec                 C  s   d S re   r   rr   r   r   r   Údiscard*  s    zMemoryFile.discardc                 C  s"   | | j j| j< tjtjd�| _d S )Nr   )r�   r   r   r   r‚   r   rƒ   rq   rr   r   r   r   r\   -  s    zMemoryFile.commit)NNN)r^   ry   rz   r{   r…   Úpropertyr   rˆ   r‰   rŠ   r\   Ú__classcell__r   r   r†   r   rZ     s   

rZ   )Ú
__future__r   Úloggingr   r   Úerrnor   Úior   Úpathlibr   r   Útypingr	   r
   Zfsspecr   Zfsspec.implementations.localr   Zfsspec.utilsr   Ú	getLoggerrO   r   rZ   r   r   r   r   Ú<module>   s   
 ~