o
    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d/d(d)ZdS )0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 r%|dS |dd}|r3d| S dS )Nz	memory://z::z://r   r   )	
isinstancer   r   r   _strip_protocolr   removeprefixrstriplstrip)clspath r   c/home/www/facesmatcher.com/frenv_anti/lib/python3.10/site-packages/fsspec/implementations/memory.pyr      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>i   s    z'MemoryFileSystem.ls.<locals>.<listcomp>)r   r   r   r    	timestampsettuple
startswithlenappendsplitaddpseudo_dirsFileNotFoundErrorsorted)	selfr   detailkwargspathsZstarteroutZp2Z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,   )r2   r   create_parentsr4   r   r   r   r=   k   s   
 zMemoryFileSystem.mkdirFc                 C  s0   z
| j |dd W d S  ty   |s Y d S w )NT)r>   )r=   r8   )r2   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)r2   r   valuerE   r4   r   r   r   	pipe_file   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/   r7   removeOSErrorr   r0   r2   r   r   r   r   rmdir   s   


zMemoryFileSystem.rmdirc                   s   t d  |    | jv s!t fddt| j| j D r' dddS  | jv r=| j  } |jdt|dd d	S t	 )
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   rP   r   info   s"   



zMemoryFileSystem.inforbNc                 K  s  |  |}d|v r| |rt|| jv rt||}t|dkr5| |}| |r/t|t|dks!|dv rZ|| jv rV| j| }|dkrO|	dd |S |	d |S t
||dv r{|dkri| |ritt| ||d	}	| jsy|	  |	S | jj}
td
|
 d|)Nxr!   )rX   abzr+brZ   r      >   rC   rD   rC   rF   zunsupported file mode for z: )r   existsr8   r/   IsADirectoryErrorr+   r9   r;   r   seekr0   
MemoryFilegetZ_intranscommit	__class____name__
ValueError)r2   r   rE   
block_sizeZ
autocommitZcache_optionsr4   parentr%   mr   r   r   r   _open   s:   
	





zMemoryFileSystem._openc                 K  sn   |  |}|  |}| |rt| || j|  | j|< d S | |r3|| jvr1| j| d S d S t|N)	r   r;   r_   r   getvalueisdirr/   r,   r0   )r2   Zpath1Zpath2r4   r   r   r   cp_file   s   




zMemoryFileSystem.cp_filec              
   K  sV   t d| | |}zt| j|  || W S  ty* } zt||d }~ww )Nzcat: %s)rR   rS   r   bytesr   	getbufferKeyErrorr0   )r2   r   startendr4   er   r   r   cat_file   s   

zMemoryFileSystem.cat_filec              
   C  s<   |  |}z| j|= W d S  ty } zt||d }~ww ri   )r   r   ro   r0   r2   r   rr   r   r   r   _rm      

zMemoryFileSystem._rmc              
   C  <   |  |}z| j| jW S  ty } zt||d }~ww ri   )r   r   modifiedro   r0   rt   r   r   r   rx      rv   zMemoryFileSystem.modifiedc              
   C  rw   ri   )r   r   r    ro   r0   rt   r   r   r   r       rv   zMemoryFileSystem.createdc                 C  s   |  |}|| jv S ri   )r   r   rL   r   r   r   r;      s   

zMemoryFileSystem.isfilec                   sv   t |tr |}n	 fdd|D } j|||d}t|D ]} |r- | q  |s3q  | q d S )Nc                   s   g | ]}  |qS r   )r   rN   r2   r   r   r&     s    z'MemoryFileSystem.rm.<locals>.<listcomp>)	recursivemaxdepth)	r   strr   Zexpand_pathreversedr;   Zrm_filer\   rM   )r2   r   rz   r{   r5   rO   r   ry   r   rm  s   


zMemoryFileSystem.rm)T)F)rA   )rX   NTN)NN)FN)rc   
__module____qualname____doc__r   __annotations__r/   protocolZroot_markerclassmethodr   r7   r=   r@   rI   rM   rW   rh   rl   rs   ru   rx   r    r;   r~   r   r   r   r   r      s4   
 


@


*
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                   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   )rR   rS   fsr   r   nowr   utcr    rx   super__init__r^   )r2   r   r   rF   rb   r   r   r     s   zMemoryFile.__init__c                 C  s
   |   jS ri   )rn   nbytesry   r   r   r   r   (  s   
zMemoryFile.sizec                 C  s   | S ri   r   ry   r   r   r   	__enter__,     zMemoryFile.__enter__c                 C     d S ri   r   ry   r   r   r   close/  r   zMemoryFile.closec                 C  r   ri   r   ry   r   r   r   discard2  r   zMemoryFile.discardc                 C  s"   | | j j| j< tjtjd| _d S )Nr   )r   r   r   r   r   r   r   rx   ry   r   r   r   ra   5  s   zMemoryFile.commit)NNN)rc   r   r   r   r   propertyr   r   r   r   ra   __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   	getLoggerrR   r   r_   r   r   r   r   <module>   s    
  