o
    5ti%                     @  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 edZG d	d
 d
eZG dd deZdS )    )annotationsN)datetimetimezone)	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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 memory/c                 C  sT   | dr|tdd  }d|v sd|v r|dS |dd}|r(d| S dS )Nz	memory://z::z://r   r   )
startswithlenrstriplstrip)clspath r   Q/home/ubuntu/.local/lib/python3.10/site-packages/fsspec/implementations/memory.py_strip_protocol   s   

z MemoryFileSystem._strip_protocolTc           	      K  s  |  |}|| jv r!|s|gS || j| jd| j| j dgS t }|d }g }t| jD ]T}||rd|t|d  vrU|	|| j| jd| j| j d q/t|t|kr||t|d  
ddd  }||vr|ptg }|	|ddd || q/| jD ]F}||rd|t|d  vr||vr|	|ddd || q||t|d  
ddd  }||vr|	|ddd || q|s|| jv 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pathsstarteroutp2ppathr   r   r   ls#   sl   




 



 

zMemoryFileSystem.lsc                 K  s   |  |}|| jv s|| jv rt|| |dr*| | |r*t| ||rM| |drMz| j| ||fi | W n	 tyL   Y nw |r\|| jvr^| j	| d S d S d S )Nr   )
r   r   r*   FileExistsError_parentstripisfileNotADirectoryErrormkdirr'   )r-   r   create_parentsr/   r   r   r   r;   c   s   
 zMemoryFileSystem.mkdirFc                 C  s0   z
| j |dd W d S  ty   |s Y d S w )NT)r<   )r;   r6   )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   valuer/   r   r   r   	pipe_filex   s   zMemoryFileSystem.pipe_filec                 C  sN   |  |}|dkrd S || jv r#| |s| j| d S ttd|t|)Nr   zDirectory not empty)r   r*   r5   removeOSErrorr   r+   r-   r   r   r   r   rmdir   s   


zMemoryFileSystem.rmdirc                   sv   |     | jv st fddt| j| j D r! dddS  | jv r7| j  } |jdt|dd dS t )	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   r/   fileliker   rJ   r   info   s    



zMemoryFileSystem.inforbNc                 K  s   |  |}|| jv rt||}t|dkr*| |}| |r$t|t|dks|dv rO|| jv rK| j| }|dkrD|dd |S |d |S t	||dkret
| ||d}	| jsc|	  |	S | jj}
td|
 d	|)
Nr   )rQ   abzr+brR   r      r?   r@   zunsupported file mode for z: )r   r*   IsADirectoryErrorr   r7   r9   r6   r   seekr+   
MemoryFileget_intranscommit	__class____name__
ValueError)r-   r   mode
block_size
autocommitcache_optionsr/   parentr"   mr   r   r   r   _open   s2   
	





zMemoryFileSystem._openc                 K  sn   |  |}|  |}| |rt| || j|  | j|< d S | |r3|| jvr1| j| d S d S t|N)	r   r9   rV   r   getvalueisdirr*   r'   r+   )r-   path1path2r/   r   r   r   cp_file   s   




zMemoryFileSystem.cp_filec                 K  s>   |  |}zt| j|  || W S  ty   t|w rd   )r   bytesr   	getbufferKeyErrorr+   )r-   r   startendr/   r   r   r   cat_file   s   
zMemoryFileSystem.cat_filec              
   C  s<   |  |}z| j|= W d S  ty } zt||d }~ww rd   )r   r   rl   r+   )r-   r   er   r   r   _rm   s   

zMemoryFileSystem._rmc                 C  0   |  |}z| j| jW S  ty   t|w rd   )r   r   modifiedrl   r+   rF   r   r   r   rs         
zMemoryFileSystem.modifiedc                 C  rr   rd   )r   r   r   rl   r+   rF   r   r   r   r      rt   zMemoryFileSystem.createdc                   sv   t |tr |}n	 fdd|D } j|||d}t|D ]} |s(q  |r3 | q  | q d S )Nc                   s   g | ]}  |qS r   )r   rH   r-   r   r   r#      s    z'MemoryFileSystem.rm.<locals>.<listcomp>)	recursivemaxdepth)	
isinstancestrr   expand_pathreversedexistsr9   rm_filerG   )r-   r   rv   rw   r0   rI   r   ru   r   rm   s   


zMemoryFileSystem.rm)T)F)rQ   NTN)NN)FN)r[   
__module____qualname____doc__r   __annotations__r*   protocolroot_markerclassmethodr   r5   r;   r>   rC   rG   rP   rc   ri   ro   rq   rs   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 )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                   sZ   t d| || _|| _tjtjd| _tjtjd| _	|r+t
 | | d d S d S )Nzopen file %stzr   )loggerdebugfsr   r   nowr   utcr   rs   super__init__rU   )r-   r   r   r@   rZ   r   r   r     s   zMemoryFile.__init__c                 C  s
   |   jS rd   )rk   nbytesru   r   r   r   r     s   
zMemoryFile.sizec                 C  s   | S rd   r   ru   r   r   r   	__enter__     zMemoryFile.__enter__c                 C     d S rd   r   ru   r   r   r   close  r   zMemoryFile.closec                 C  r   rd   r   ru   r   r   r   discard  r   zMemoryFile.discardc                 C  s"   | | j j| j< tjtjd| _d S )Nr   )r   r   r   r   r   r   r   rs   ru   r   r   r   rY   "  s   zMemoryFile.commit)NNN)r[   r   r   r   r   propertyr   r   r   r   rY   __classcell__r   r   r   r   rV     s    

rV   )
__future__r   loggingr   r   errnor   ior   typingr   r   fsspecr	   	getLoggerr   r
   rV   r   r   r   r   <module>   s    
 v