o
    Q `	C                     @   s  d Z ddlmZ ddl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	m
Z
 ddlmZ ddl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 ddlmZ ddlmZ ddl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) ddlm*Z* ddlm+Z+ ddl,m-Z- ddl.m/Z/ ej0dddZ1ej2G dd dej3Z4G dd de5Z6ej2G dd deZ7dS )z(Manage a volatile in-memory filesystem.
    )absolute_import)unicode_literalsN)OrderedDict)RLock   )errors)FS)ResourceTypeSeek)Info)Mode)iteratepath)normpath)split)overload)
AnyBinaryIO
CollectionDictIteratorListOptionalSupportsIntUnionText)_OpendirFactory)RawInfo)Permissions)SubFS_MMemoryFS)Z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d/ddZd d! Zejfd"d#Zd$d% Zd0d'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___pathZ
_memory_fsr   _mode
_dir_entrytypingcastioBytesIO
bytes_file	_bytes_iotimeaccessed_timemodified_timepostruncatelockseekZ	appendingosSEEK_ENDtell)selfpath	memory_fsmode	dir_entry	__class__ -/usr/lib/python3/dist-packages/fs/memoryfs.pyr$   1   s(   



"
"z_MemoryFile.__init__c                 C   s   d}|j | j| jdS )Nz<memoryfile '{path}' '{mode}'>)r9   r;   )formatr%   r&   )r8   Z	_templater?   r?   r@   __str__I   s   z_MemoryFile.__str__c                 C   s
   | j  S N)r&   Zto_platform_binr8   r?   r?   r@   r;   N   s   
z_MemoryFile.modec                 c   sP    | j j | j| j d V  | j | _W d    d S 1 s!w   Y  d S rC   )r'   r3   r-   r4   r1   r7   rD   r?   r?   r@   
_seek_lockS   s   
"z_MemoryFile._seek_lockc                 C      t    | j_| _dS )z+Called when file data is modified.
        N)r.   r'   r0   rD   r?   r?   r@   	on_modify[      z_MemoryFile.on_modifyc                 C   rF   )z&Called when file is accessed.
        N)r.   r'   r/   rD   r?   r?   r@   	on_accessa   rH   z_MemoryFile.on_accessc                 C      d S rC   r?   rD   r?   r?   r@   flushg      z_MemoryFile.flushc                 c   s&    | j | j | j D ]}|V  qd S rC   )r-   r4   r1   )r8   liner?   r?   r@   __iter__k   s
   
z_MemoryFile.__iter__c                 C   s>   |    |   t| jW  d    S 1 sw   Y  d S rC   )rE   rI   nextr-   rD   r?   r?   r@   rO   q   s   
$z_MemoryFile.nextc                 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IOErrorrE   rI   r-   readliner8   sizer?   r?   r@   rU   y      

$z_MemoryFile.readlinec                    sR   | j s'| jj | j|  tt|   W d    d S 1 s w   Y  d S d S rC   )closedr'   r3   remove_open_filer#   r!   closerD   r=   r?   r@   r[      s   
"z_MemoryFile.closec                 C   rQ   rR   )r&   rS   rT   rE   rI   r-   readrV   r?   r?   r@   r\      rX   z_MemoryFile.readc                 C      | j jS rC   )r&   rS   rD   r?   r?   r@   readable      z_MemoryFile.readablec                 C   rQ   rR   )r&   rS   rT   rE   rI   r-   readinto)r8   bufferr?   r?   r@   r`      rX   z_MemoryFile.readintoc                 C   rQ   rR   )r&   rS   rT   rE   rI   r-   	readlines)r8   Zhintr?   r?   r@   rb      rX   z_MemoryFile.readlinesc                 C      dS )NTr?   rD   r?   r?   r@   seekable   rL   z_MemoryFile.seekablec                 C   sF   |    |   | j|t|W  d    S 1 sw   Y  d S rC   )rE   rI   r-   r4   int)r8   r1   whencer?   r?   r@   r4      s   
$z_MemoryFile.seekc                 C      | j S rC   )r1   rD   r?   r?   r@   r7      s   z_MemoryFile.tellNc                 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       )	rE   rG   r-   r2   r7   r4   r5   r6   write)r8   rW   Znew_size	file_sizer?   r?   r@   r2      s   
$z_MemoryFile.truncatec                 C   r]   rC   )r&   writingrD   r?   r?   r@   writable   r_   z_MemoryFile.writablec                 C   rQ   )NzFile not open for writing)r&   rk   rT   rE   rG   r-   ri   )r8   datar?   r?   r@   ri      rX   z_MemoryFile.writec                 C   sB   |    |   | j| W d    d S 1 sw   Y  d S rC   )rE   rG   r-   
writelines)r8   Zsequencer?   r?   r@   rn      s   
"z_MemoryFile.writelines)rP   rC   ) __name__
__module____qualname__r$   rB   propertyr;   
contextlibcontextmanagerrE   rG   rI   rK   rN   rO   __next__rU   r[   r\   r^   r`   rb   rd   r
   setr4   r7   r2   rl   ri   rn   __classcell__r?   r?   r=   r@   r!   /   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S )	_DirEntryc                 C   sZ   || _ || _t | _g | _d | _t | _t }|| _	|| _
|| _| js+t | _d S d S rC   )resource_typenamer   _dir_open_files_bytes_filer   r3   r.   created_timer/   r0   is_dirr*   r+   )r8   ry   rz   Zcurrent_timer?   r?   r@   r$      s   z_DirEntry.__init__c                 C   rg   rC   )r}   rD   r?   r?   r@   r,      s   z_DirEntry.bytes_filec                 C   s   | j tjkS rC   )ry   r	   	directoryrD   r?   r?   r@   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"   )r3   r   r(   r)   r*   r+   r}   r4   r5   r6   r7   )r8   r}   r?   r?   r@   rW      s   $z_DirEntry.sizec                 C   rJ   rC   r?   r8   rz   defaultr?   r?   r@   	get_entry      z_DirEntry.get_entryc                 C   rJ   rC   r?   r8   rz   r?   r?   r@   r      r   c                 C   rJ   rC   r?   r   r?   r?   r@   r     r   Nc                 C   s   | j sJ d| j||S )Nzmust be a directory)r   r{   getr   r?   r?   r@   r   	  s   c                 C   s   || j |< d S rC   r{   )r8   rz   r<   r?   r?   r@   	set_entry     z_DirEntry.set_entryc                 C   s   | j |= d S rC   r   r   r?   r?   r@   remove_entry  s   z_DirEntry.remove_entryc                 C   s
   || j v S rC   r   r   r?   r?   r@   __contains__     
z_DirEntry.__contains__c                 C   s
   t | jS rC   )lenr{   rD   r?   r?   r@   __len__  r   z_DirEntry.__len__c                 C   s   t | j S rC   )listr{   keysrD   r?   r?   r@   r     r   z_DirEntry.listc                 C      | j | d S rC   )r|   appendr8   Zmemory_filer?   r?   r@   add_open_file"     z_DirEntry.add_open_filec                 C   r   rC   )r|   remover   r?   r?   r@   rZ   &  r   z_DirEntry.remove_open_filerC   )ro   rp   rq   r$   rr   r,   r   rW   r   r   r   r   r   r   r   r   rZ   r?   r?   r?   r@   rx      s,    







rx   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  Zd!d" Zd#d$ Z  ZS )(r    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.

    Example:
        >>> mem_fs = MemoryFS()

    Or via an FS URL:
        >>> import fs
        >>> mem_fs = fs.open_fs('mem://')

    F T)Zcase_insensitiveZinvalid_path_charsZnetworkZ	read_onlyZthread_safeZunicode_pathsZ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$   rD   r=   r?   r@   r$   G  s   zMemoryFS.__init__c                 C   rc   )Nz
MemoryFS()r?   rD   r?   r?   r@   __repr__O  rL   zMemoryFS.__repr__c                 C   rc   )Nz<memfs>r?   rD   r?   r?   r@   rB   S  rL   zMemoryFS.__str__c                 C   s
   t ||S rC   )rx   )r8   ry   rz   r?   r?   r@   r   W  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 )z?Get a directory entry, or `None` if one doesn't exist.
        N)_lockr   r   r   r   r   )r8   dir_pathZcurrent_entryZpath_componentr?   r?   r@   _get_dir_entry[  s   $zMemoryFS._get_dir_entryc                    s   | j s| `tt|  S rC   )Z_closedr   r#   r    r[   rD   r=   r?   r@   r[   j  s   zMemoryFS.closeNc                 C   sx   |pd}|  |}| |}|d u rt|d|j|jdi}d|v r8ddgt|j|j|j	|j
|jd|d< t|S )Nr?   Zbasic)rz   r   detailsaccessedmodified)Z_writetyperW   r   r   Zcreated)validatepathr   r   ResourceNotFoundrz   r   re   ry   rW   r/   r0   r~   r   )r8   r9   Z
namespacesr%   r<   infor?   r?   r@   getinfop  s   



zMemoryFS.getinfoc                 C   sp   |    | |}| j" | |}|d u rt||js#t|| W  d    S 1 s1w   Y  d S rC   )	checkr   r   r   r   r   r   DirectoryExpectedr   )r8   r9   r%   r<   r?   r?   r@   listdir  s   



$zMemoryFS.listdirc                 C   rJ   rC   r?   )r8   r9   factoryr?   r?   r@   opendir  rL   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   ZDirectoryExistsr   r   r   r   r   r	   r   r   )
r8   r9   permissionsZrecreater%   r   Zdir_nameZ
parent_dirr<   Znew_dirr?   r?   r@   makedir  s&   





$zMemoryFS.makedirrrP   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)r9   r:   r;   r<   )r   Zvalidate_binr   r   r   FileExpectedr   r   r   r   Zcreater   r	   filer   Z	exclusiveZ
FileExistsr!   r   r   )r8   r9   r;   	bufferingoptionsr&   r%   r   	file_nameparent_dir_entryfile_dir_entryZmem_filer?   r?   r@   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 rC   )r   r   r   r   r   r   r(   r)   rx   r   r   r   )r8   r9   r%   r   r   r   r   r?   r?   r@   r     s   



"zMemoryFS.removec                 C   s   |  |}|dkrt | j@ t|\}}| |}|d u s$||vr)t|tt	| |}|j
s:t|t|rCt||| W d    d S 1 sSw   Y  d S r   )r   r   ZRemoveRootErrorr   r   r   r   r(   r)   rx   r   r   r   ZDirectoryNotEmptyr   )r8   r9   r%   r   r   r   Zdir_dir_entryr?   r?   r@   	removedir  s   




"zMemoryFS.removedirc           	      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)   rx   r   r/   r0   )	r8   r9   r   r%   r   r   r   Zresource_entryr   r?   r?   r@   setinfo
  s(   




"zMemoryFS.setinforC   )NF)r   rP   )ro   rp   rq   __doc__r   r$   r   rB   r   r   r[   r   r   r(   TYPE_CHECKINGr   r   r   r   r   r   rw   r?   r?   r=   r@   r    +  s6    




/)8r   Z
__future__r   r   rs   r*   r5   r.   r(   collectionsr   Z	threadingr   Zsixr   r   baser   Zenumsr	   r
   r   r   r;   r   r9   r   r   r   Z_typingr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zsubfsr   TypeVarr   Zpython_2_unicode_compatible	RawIOBaser!   objectrx   r    r?   r?   r?   r@   <module>   sB    0 $X