o
    pfT                     @   s  d Z ddlmZmZ ddlZddlZddlZddlZddlZddl	Z	ddl
mZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lm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m Z  ej!rddlm"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- ddl.Z.ddl/Z/ddlm0Z0 ddlm1Z1 ddl2m3Z3 ddl4m5Z5 ej6dddZ7ej8G dd dej9Z:G dd de;Z<ej8G dd deZ=dS )z(Manage a volatile in-memory filesystem.
    )absolute_importunicode_literalsN)OrderedDict)RLock   )errors)overload)FS)copy_modified_time)ResourceTypeSeek)Info)Mode)iteratepathnormpathsplit)AnyBinaryIO
CollectionDictIterableIteratorListOptionalSupportsIntTextTupleUnion)_OpendirFactory)RawInfo)Permissions)SubFS_MMemoryFS)boundc                       s   e Zd Z fddZdd Zedd Zejdd Z	d	d
 Z
dd Zdd Zdd Zdd ZeZd/ddZ fddZd/ddZdd Zdd Zd0dd Zd!d" Zejfd#d$Zd%d& Zd/d'd(Zd)d* Zd+d, Zd-d. Z  ZS )1_MemoryFilec                    s   t t|   || _|| _t|| _|| _t	t
j|j| _t | _t | _d| _| jjrQ| jj | jd | j  W d    d S 1 sJw   Y  d S | jjrz| jj | jdtj | j | _W d    d S 1 ssw   Y  d S d S Nr   )superr%   __init___path
_memory_fsr   _mode
_dir_entrytypingcastioBytesIO
bytes_file	_bytes_iotimeaccessed_timemodified_timepostruncatelockseek	appendingosSEEK_ENDtell)selfpath	memory_fsmode	dir_entry	__class__ D/home/ertert/spirit/venv/lib/python3.10/site-packages/fs/memoryfs.pyr(   5   s(   



"
"z_MemoryFile.__init__c                 C   s   d}|j | j| jdS )Nz<memoryfile '{path}' '{mode}'>)r?   rA   )formatr)   r+   )r>   	_templaterE   rE   rF   __str__M   s   z_MemoryFile.__str__c                 C   s
   | j  S N)r+   to_platform_binr>   rE   rE   rF   rA   R   s   
z_MemoryFile.modec                 c   sP    | j j | j| j d V  | j | _W d    d S 1 s!w   Y  d S rJ   )r,   r8   r2   r9   r6   r=   rL   rE   rE   rF   
_seek_lockW   s   
"z_MemoryFile._seek_lockc                 C      t    | j_| _dS )z"Called when file data is modified.N)r3   r,   r5   rL   rE   rE   rF   	on_modify_      z_MemoryFile.on_modifyc                 C   rN   )zCalled when file is accessed.N)r3   r,   r4   rL   rE   rE   rF   	on_accessd   rP   z_MemoryFile.on_accessc                 C      d S rJ   rE   rL   rE   rE   rF   flushi      z_MemoryFile.flushc                 c   s&    | j | j | j D ]}|V  qd S rJ   )r2   r9   r6   )r>   linerE   rE   rF   __iter__m   s
   
z_MemoryFile.__iter__c                 C   s>   |    |   t| jW  d    S 1 sw   Y  d S rJ   )rM   rQ   nextr2   rL   rE   rE   rF   rW   s   s   
$z_MemoryFile.nextNc                 C   P   | j jstd|   |   | j|W  d    S 1 s!w   Y  d S NzFile not open for reading)r+   readingIOErrorrM   rQ   r2   readliner>   sizerE   rE   rF   r\   {      

$z_MemoryFile.readlinec                    sR   | j s'| jj | j|  tt|   W d    d S 1 s w   Y  d S d S rJ   )closedr,   r8   remove_open_filer'   r%   closerL   rC   rE   rF   rb      s   
"z_MemoryFile.closec                 C   rX   rY   )r+   rZ   r[   rM   rQ   r2   readr]   rE   rE   rF   rc      r_   z_MemoryFile.readc                 C      | j jS rJ   )r+   rZ   rL   rE   rE   rF   readable      z_MemoryFile.readablec                 C   rX   rY   )r+   rZ   r[   rM   rQ   r2   readinto)r>   bufferrE   rE   rF   rg      r_   z_MemoryFile.readintoc                 C   rX   rY   )r+   rZ   r[   rM   rQ   r2   	readlines)r>   hintrE   rE   rF   rj      r_   z_MemoryFile.readlinesc                 C      dS )NTrE   rL   rE   rE   rF   seekable   rT   z_MemoryFile.seekablec                 C   sF   |    |   | j|t|W  d    S 1 sw   Y  d S rJ   )rM   rQ   r2   r9   int)r>   r6   whencerE   rE   rF   r9      s   
$z_MemoryFile.seekc                 C      | j S rJ   )r6   rL   rE   rE   rF   r=      s   z_MemoryFile.tellc                 C   s   |   > |   | j|}|d ur7| j |k r7| jdtj}| jd||   | j| | tj |p:|W  d    S 1 sEw   Y  d S )Nr       )	rM   rO   r2   r7   r=   r9   r;   r<   write)r>   r^   new_size	file_sizerE   rE   rF   r7      s   
$z_MemoryFile.truncatec                 C   rd   rJ   )r+   writingrL   rE   rE   rF   writable   rf   z_MemoryFile.writablec                 C   rX   )NzFile not open for writing)r+   ru   r[   rM   rO   r2   rr   )r>   datarE   rE   rF   rr      r_   z_MemoryFile.writec                 C   sB   |    |   | j| W d    d S 1 sw   Y  d S rJ   )rM   rO   r2   
writelines)r>   sequencerE   rE   rF   rx      s   
"z_MemoryFile.writelinesrJ   )ri   ) __name__
__module____qualname__r(   rI   propertyrA   
contextlibcontextmanagerrM   rO   rQ   rS   rV   rW   __next__r\   rb   rc   re   rg   rj   rm   r   setr9   r=   r7   rv   rr   rx   __classcell__rE   rE   rC   rF   r%   3   s4    





r%   c                   @   s   e Zd Zdd Zedd Zedd Zedd Zed	d
 Z	edd
 Z	edd
 Z	d!dd
Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd!dd ZdS )"	_DirEntryc                 C   sZ   || _ || _t | _g | _d | _t | _t }|| _	|| _
|| _| js+t | _d S d S rJ   )resource_typenamer   _dir_open_files_bytes_filer   r8   r3   created_timer4   r5   is_dirr/   r0   )r>   r   r   current_timerE   rE   rF   r(      s   z_DirEntry.__init__c                 C   rp   rJ   )r   rL   rE   rE   rF   r1      s   z_DirEntry.bytes_filec                 C   s   | j tjkS rJ   )r   r   	directoryrL   rE   rE   rF   r      s   z_DirEntry.is_dirc                 C   sh   | j ' | jr	 W d    dS ttj| j}|dtj	 |
 W  d    S 1 s-w   Y  d S r&   )r8   r   r-   r.   r/   r0   r   r9   r;   r<   r=   )r>   r   rE   rE   rF   r^      s   $z_DirEntry.sizec                 C   rR   rJ   rE   r>   r   defaultrE   rE   rF   	get_entry      z_DirEntry.get_entryc                 C   rR   rJ   rE   r>   r   rE   rE   rF   r      r   c                 C   rR   rJ   rE   r   rE   rE   rF   r     r   Nc                 C   s   | j sJ d| j||S )Nzmust be a directory)r   r   getr   rE   rE   rF   r   	  s   c                 C   s   || j |< d S rJ   r   )r>   r   rB   rE   rE   rF   	set_entry     z_DirEntry.set_entryc                 C   s   | j |= d S rJ   r   r   rE   rE   rF   remove_entry  s   z_DirEntry.remove_entryc                 C   s   | j   d S rJ   )r   clearrL   rE   rE   rF   r     r   z_DirEntry.clearc                 C   s
   || j v S rJ   r   r   rE   rE   rF   __contains__     
z_DirEntry.__contains__c                 C   s
   t | jS rJ   )lenr   rL   rE   rE   rF   __len__  r   z_DirEntry.__len__c                 C   s   t | j S rJ   )listr   keysrL   rE   rE   rF   r   "  r   z_DirEntry.listc                 C      | j | d S rJ   )r   appendr>   memory_filerE   rE   rF   add_open_file&     z_DirEntry.add_open_filec                 C   r   rJ   )r   remover   rE   rE   rF   ra   *  r   z_DirEntry.remove_open_filec                 C   sR   |pd}d| j | jdi}d|v r%ddgt| j| j| j| j| jd|d< t|S )NrE   basic)r   r   detailsaccessedmodified)_writetyper^   r   r   created)	r   r   rn   r   r^   r4   r5   r   r   )r>   
namespacesinforE   rE   rF   to_info.  s   
z_DirEntry.to_inforJ   )rz   r{   r|   r(   r}   r1   r   r^   r   r   r   r   r   r   r   r   r   ra   r   rE   rE   rE   rF   r      s0    







r   c                       s   e Zd ZdZddddddddZ fddZdd	 Zd
d Zdd Zdd Z	 fddZ
d-ddZdd Zejr=d-ddZ		d.ddZd/ddZd/ddZd0d!d"Zd#d$ Zd%d& Zd'd( Z		d1d)d*Zd+d, Z  ZS )2r#   a  A filesystem that stored in memory.

    Memory filesystems are useful for caches, temporary data stores,
    unit testing, etc. Since all the data is in memory, they are very
    fast, but non-permanent. The `MemoryFS` constructor takes no
    arguments.

    Examples:
        Create with the constructor::

            >>> from fs.memoryfs import MemoryFS
            >>> mem_fs = MemoryFS()

        Or via an FS URL::

            >>> import fs
            >>> mem_fs = fs.open_fs('mem://')

    F T)case_insensitiveinvalid_path_charsnetwork	read_onlythread_safeunicode_pathsvirtualc                    s.   | j  | _ | tjd| _tt|   dS )zCreate an in-memory filesystem. N)	_metacopy_make_dir_entryr   r   rootr'   r#   r(   rL   rC   rE   rF   r(   ^  s   zMemoryFS.__init__c                 C   rl   )Nz
MemoryFS()rE   rL   rE   rE   rF   __repr__e  rT   zMemoryFS.__repr__c                 C   rl   )Nz<memfs>rE   rL   rE   rE   rF   rI   i  rT   zMemoryFS.__str__c                 C   s
   t ||S rJ   )r   )r>   r   r   rE   rE   rF   r   m  r   zMemoryFS._make_dir_entryc                 C   s   | j 6 t|}| j}t|D ] }|du r W d   dS |js* W d   dS ||}q|W  d   S 1 s<w   Y  dS )z6Get a directory entry, or `None` if one doesn't exist.N)_lockr   r   r   r   r   )r>   dir_pathcurrent_entrypath_componentrE   rE   rF   _get_dir_entryq  s   $zMemoryFS._get_dir_entryc                    s   | j s| `tt|  S rJ   )_closedr   r'   r#   rb   rL   rC   rE   rF   rb     s   zMemoryFS.closeNc                 C   s2   |  |}| |}|d u rt||j|dS N)r   )validatepathr   r   ResourceNotFoundr   )r>   r?   r   r)   rB   rE   rE   rF   getinfo  s
   


zMemoryFS.getinfoc                 C   sp   |    | |}| j" | |}|d u rt||js#t|| W  d    S 1 s1w   Y  d S rJ   )	checkr   r   r   r   r   r   DirectoryExpectedr   )r>   r?   r)   rB   rE   rE   rF   listdir  s   



$zMemoryFS.listdirc                 C   rR   rJ   rE   )r>   r?   factoryrE   rE   rF   opendir  rT   zMemoryFS.opendirc           
      C   s   |  |}| jY |dkr |r| |W  d    S t|t|\}}| |}|d u r4t|||}|d urD|sDt||d u rU| 	t
j|}	|||	 | |W  d    S 1 sdw   Y  d S N/)r   r   r   r   DirectoryExistsr   r   r   r   r   r   r   r   )
r>   r?   permissionsrecreater)   r   dir_name
parent_dirrB   new_dirrE   rE   rF   makedir  s&   





$zMemoryFS.makedirc                 C   s   t | |\}}t | |\}}| j` | |}	|	d u s#||	vr(t||	|}
|
jr5t|| |}|d u rCt||sN||v rNt	||
||
 |	| ||
_|rmt| || | W d    d S W d    d S 1 sxw   Y  d S rJ   )r   r   r   r   r   r   r   r   FileExpectedDestinationExistsr   r   r   r
   )r>   src_pathdst_path	overwritepreserve_timesrc_dirsrc_namedst_dirdst_namesrc_dir_entry	src_entrydst_dir_entryrE   rE   rF   move  s,   







"zMemoryFS.movec                 C   s   t | |\}}t | |\}}| j[ | |}	|	d u s#||	vr(t||	|}
|
js5t|| |}|d u sD|sI||vrIt||	||
 |	
| ||
_|rht| || | W d    d S W d    d S 1 ssw   Y  d S rJ   )r   r   r   r   r   r   r   r   r   r   r   r   r
   )r>   r   r   creater   r   r   r   r   r   r   r   rE   rE   rF   movedir  s(   






"zMemoryFS.movedirrri   c                 K   sJ  t |}|  | |}t|\}}|st|| j~ | |}	|	d u s*|	js/t	||j
ro||	vrD| tj|}
|	||
 n| |}
|jrQt||
jrYt|t|| ||
d}|
| |W  d    S ||	vrxt	||	|}
|
jrt|t|| ||
d}|
| |W  d    S 1 sw   Y  d S )N)r?   r@   rA   rB   )r   validate_binr   r   r   r   r   r   r   r   r   r   r   filer   	exclusive
FileExistsr%   r   r   )r>   r?   rA   	bufferingoptionsr+   r)   r   	file_nameparent_dir_entryfile_dir_entrymem_filerE   rE   rF   openbin  sH   











$zMemoryFS.openbinc                 C   s   |  |}| j7 t|\}}| |}|d u s||vr!t|tt| |}|j	r2t
||| W d    d S 1 sBw   Y  d S rJ   )r   r   r   r   r   r   r-   r.   r   r   r   r   )r>   r?   r)   r   r   r   r   rE   rE   rF   r   #  s   



"zMemoryFS.removec                 C   s<   |  |}|dkrt | |st|| | d S r   )r   r   RemoveRootErrorisemptyDirectoryNotEmpty
removetree)r>   r?   r)   rE   rE   rF   	removedir4  s   


zMemoryFS.removedirc                 C   s   |  |}| jI |dkr| j  	 W d    d S t|\}}| |}|d u s.||vr3t|t	t
| |}|jsDt||| W d    d S 1 sTw   Y  d S r   )r   r   r   r   r   r   r   r   r-   r.   r   r   r   r   )r>   r?   r)   r   r   r   dir_dir_entryrE   rE   rF   r   C  s   




"zMemoryFS.removetreec                 c   s    |    | |}| jF | |}|d u rt||js$t|| }|d ur6|\}}||| }|D ]}	t	
t||	}
|
j|dV  q8W d    d S 1 sVw   Y  d S r   )r   r   r   r   r   r   r   r   r   r-   r.   r   r   r   )r>   r?   r   pager)   rB   	filenamesstartendr   entryrE   rE   rF   scandirY  s$   



"zMemoryFS.scandirc           	      C   s   |  |}| jT t|\}}| |}|d u s||vr!t|tt|	|}d|v rL|d }d|v r;|d |_
d|v rT|d |_W d    d S W d    d S W d    d S 1 s_w   Y  d S )Nr   r   r   )r   r   r   r   r   r   r-   r.   r   r   r4   r5   )	r>   r?   r   r)   r   r   r   resource_entryr   rE   rE   rF   setinfos  s(   




"zMemoryFS.setinforJ   )NF)FF)r   ri   )NN)rz   r{   r|   __doc__r   r(   r   rI   r   r   rb   r   r   r-   TYPE_CHECKINGr   r   r   r   r   r   r   r   r   r   r   rE   rE   rC   rF   r#   >  sB    






/
)>r   
__future__r   r   r-   r~   r/   r;   sixr3   collectionsr   	threadingr   r   r   _typingr   baser	   r   r
   enumsr   r   r   r   rA   r   r?   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   arraymmapr   r   r   r    subfsr!   TypeVarr"   python_2_unicode_compatible	RawIOBaser%   objectr   r#   rE   rE   rE   rF   <module>   sB    8  k