a
    h(                     @  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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  sj   t |tr&t |trt|S t|}|d}d|v s@d|v rJ|dS |dd}|rfd| S dS )Nz	memory://z::z://r   r   )	
isinstancer   r   r   _strip_protocolr   removeprefixrstriplstrip)clspath r   K/var/www/auris/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^|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>i       z'MemoryFileSystem.ls.<locals>.<listcomp>)r   r   r   r    	timestampsettuple
startswithlen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   r0   FileExistsError_parentstripisfileNotADirectoryErrormkdirr-   )r3   r   create_parentsr4   r   r   r   r>   k   s    
 zMemoryFileSystem.mkdirFc                 C  s0   z| j |dd W n ty*   |s& Y n0 d S )NT)r?   )r>   r9   )r3   r   exist_okr   r   r   makedirsy   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)r3   r   valuerF   r4   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   r0   r8   removeOSErrorr   r1   r3   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   r0   anylistr   r   getattrr1   )r3   r   r4   Zfileliker   rQ   r   info   s"    



zMemoryFileSystem.inforbNc                 K  s  |  |}d|v r | |r t|| jv r2t||}t|dkr`| |}| |r6t|q6|dv r|| jv r| j| }|dkr|	dd n
|	d |S t
|n^|dv r|dkr| |rtt| ||d	}	| js|	  |	S | jj}
td
|
 d|d S )Nxr!   )rY   abzr+br[   r      >   rD   rE   rD   rG   zunsupported file mode for z: )r   existsr9   r0   IsADirectoryErrorr,   r:   r<   r   seekr1   
MemoryFilegetZ_intranscommit	__class____name__
ValueError)r3   r   rF   
block_sizeZ
autocommitZcache_optionsr4   parentr%   mr   r   r   r   _open   s6    	








zMemoryFileSystem._openc                 K  sj   |  |}|  |}| |r<t| || j|  | j|< n*| |r^|| jvrf| j| nt|d S N)	r   r<   r`   r   getvalueisdirr0   r-   r1   )r3   path1path2r4   r   r   r   cp_file   s    




zMemoryFileSystem.cp_filec              
   K  sf   t d| | |}zt| j|  || W S  ty` } zt||W Y d }~n
d }~0 0 d S )Nzcat: %s)rS   rT   r   bytesr   	getbufferKeyErrorr1   )r3   r   startendr4   er   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 rj   )r   r   rr   r1   r3   r   ru   r   r   r   _rm   s
    
zMemoryFileSystem._rmc              
   C  sL   |  |}z| j| jW S  tyF } zt||W Y d }~n
d }~0 0 d S rj   )r   r   modifiedrr   r1   rw   r   r   r   ry      s
    
zMemoryFileSystem.modifiedc              
   C  sL   |  |}z| j| jW S  tyF } zt||W Y d }~n
d }~0 0 d S rj   )r   r   r    rr   r1   rw   r   r   r   r       s
    
zMemoryFileSystem.createdc                 C  s   |  |}|| jv S rj   )r   r   rM   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   rO   r3   r   r   r&     r'   z'MemoryFileSystem.rm.<locals>.<listcomp>)	recursivemaxdepth)	r   strr   Zexpand_pathreversedr<   Zrm_filer]   rN   )r3   r   r{   r|   r5   rP   r   rz   r   rm  s    


zMemoryFileSystem.rm)T)T)F)rB   )rY   NTN)NN)FN)rd   
__module____qualname____doc__r   __annotations__r0   protocolZroot_markerclassmethodr   r8   r>   rA   rJ   rN   rX   ri   ro   rv   rx   ry   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`   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   )rS   rT   fsr   r   nowr   utcr    ry   super__init__r_   )r3   r   r   rG   rc   r   r   r     s    zMemoryFile.__init__c                 C  s
   |   jS rj   )rq   nbytesrz   r   r   r   r   (  s    zMemoryFile.sizec                 C  s   | S rj   r   rz   r   r   r   	__enter__,  s    zMemoryFile.__enter__c                 C  s   d S rj   r   rz   r   r   r   close/  s    zMemoryFile.closec                 C  s   d S rj   r   rz   r   r   r   discard2  s    zMemoryFile.discardc                 C  s"   | | j j| j< tjtjd| _d S )Nr   )r   r   r   r   r   r   r   ry   rz   r   r   r   rb   5  s    zMemoryFile.commit)NNN)rd   r   r   r   r   propertyr   r   r   r   rb   __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   	getLoggerrS   r   r`   r   r   r   r   <module>   s   
  