a
    
d&                     @  s   d dl mZmZmZ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 )    )absolute_importannotationsdivisionprint_functionN)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 Zmemory/c                 C  sT   | dr|tdd  }d|v s*d|v r4|dS |dd}|rPd| S dS )Nz	memory://z::z://r   r   )
startswithlenrstriplstrip)clspath r   f/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/fsspec/implementations/memory.py_strip_protocol   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^|pg }|	|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 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       z'MemoryFileSystem.ls.<locals>.<listcomp>)r   r   r   r   	timestampsettupler   r   appendsplitaddpseudo_dirsFileNotFoundErrorsorted)	selfr   Z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rz| 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*   )r0   r   create_parentsr1   r   r   r   r;   c   s    
 zMemoryFileSystem.mkdirFc                 C  s0   z| j |dd W n ty*   |s& Y n0 d S )NT)r<   )r;   r6   )r0   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)r0   r   valuer1   r   r   r   	pipe_filex   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-   r5   removeOSErrorr   r.   r0   r   r   r   r   rmdir   s    


zMemoryFileSystem.rmdirc                   sz   |     | jv s6t fddt| j| j D rB dddS  | jv 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.   )r0   r   r1   Zfileliker   rJ   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rt
| ||d}	| js|	  |	S | jj}
td|
 d	|d S )
Nr    )rP   abzrb+rQ   r      r?   r@   zunsupported file mode for z: )r   r-   IsADirectoryErrorr   r7   r9   r6   r   seekr.   
MemoryFilegetZ_intranscommit	__class____name__
ValueError)r0   r   mode
block_sizeZ
autocommitZcache_optionsr1   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   r9   rU   r   getvalueisdirr-   r*   r.   )r0   path1path2r1   r   r   r   cp_file   s    




zMemoryFileSystem.cp_filec                 K  sF   |  |}zt| j|  || W S  ty@   t|Y n0 d S r`   )r   bytesr   	getbufferKeyErrorr.   )r0   r   startendr1   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 r`   )r   r   rh   r.   )r0   r   er   r   r   _rm   s
    
zMemoryFileSystem._rmc                 C  s8   |  |}z| j| jW S  ty2   t|Y n0 d S r`   )r   r   modifiedrh   r.   rF   r   r   r   rn      s
    
zMemoryFileSystem.modifiedc                 C  s8   |  |}z| j| jW S  ty2   t|Y n0 d S r`   )r   r   r   rh   r.   rF   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   rH   r0   r   r   r%      r&   z'MemoryFileSystem.rm.<locals>.<listcomp>)	recursivemaxdepth)	
isinstancestrr   Zexpand_pathreversedexistsr9   Zrm_filerG   )r0   r   rp   rq   r2   rI   r   ro   r   rm   s    


zMemoryFileSystem.rm)T)T)F)rP   NTN)NN)FN)rY   
__module____qualname____doc__r   __annotations__r-   protocolZroot_markerclassmethodr   r5   r;   r>   rC   rG   rO   r_   re   rk   rm   rn   r   rv   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 )rU   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                   sV   t d| || _|| _tjtjd| _tjtjd| _	|rRt
 | | d d S )Nzopen file %stzr   )loggerdebugfsr   r   nowr   utcr   rn   super__init__rT   )r0   r   r   r@   rX   r   r   r     s    zMemoryFile.__init__c                 C  s
   |   jS r`   )rg   nbytesro   r   r   r   r     s    zMemoryFile.sizec                 C  s   | S r`   r   ro   r   r   r   	__enter__  s    zMemoryFile.__enter__c                 C  s   d S r`   r   ro   r   r   r   close  s    zMemoryFile.closec                 C  s   d S r`   r   ro   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   rn   ro   r   r   r   rW   "  s    zMemoryFile.commit)NNN)rY   rw   rx   ry   r   propertyr   r   r   r   rW   __classcell__r   r   r   r   rU     s   

rU   )
__future__r   r   r   r   loggingr   r   errnor   ior	   typingr
   r   Zfsspecr   Loggerr   r   rU   r   r   r   r   <module>   s   
 v