
    obT                        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jB                  rPd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jl                  dd      Z7ejp                   G d dejr                               Z: G d de;      Z<ejp                   G d de             Z=y)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                        e Zd Z fdZd Zed        Zej                  d        Z	d Z
d Zd Zd Zd	 ZeZdd
Z fdZddZd Zd ZddZd Zej0                  fdZd ZddZd Zd Zd Z xZS )_MemoryFilec                 P   t         t        |           || _        || _        t        |      | _        || _        t        j                  t        j                  |j                        | _        t        j                         | _        t        j                         | _        d| _        | j                  j$                  rU| j                  j&                  5  | j                  j)                  d       | j                  j%                          d d d        y | j                  j*                  ri| j                  j&                  5  | j                  j)                  dt,        j.                         | j                  j1                         | _        d d d        y y # 1 sw Y   y xY w# 1 sw Y   y xY w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__s        -/usr/lib/python3/dist-packages/fs/memoryfs.pyr+   z_MemoryFile.__init__5   s.   k4)+
#$Z
#  RZZ1E1EF!YY[!YY[::%% *##A&'')* * ZZ!!%% 1##Ar{{3>>..01 1 "* *1 1s   6F<A
FFF%c                 T    d}|j                  | j                  | j                        S )Nz<memoryfile '{path}' '{mode}'>)rB   rD   )formatr,   r.   )rA   	_templates     rG   __str__z_MemoryFile.__str__M   s%    4	TZZdjjAA    c                 6    | j                   j                         S N)r.   to_platform_binrA   s    rG   rD   z_MemoryFile.modeR   s     zz))++rL   c              #      K   | j                   j                  5  | j                  j                  | j                         d  | j                  j                         | _        d d d        y # 1 sw Y   y xY wwrN   )r/   r;   r5   r<   r9   r@   rP   s    rG   
_seek_lockz_MemoryFile._seek_lockW   sV      __!! 	-NN)~~**,DH	- 	- 	-s   A7A	A+"	A7+A40A7c                 X    t        j                          x| j                  _        | _        y)z"Called when file data is modified.N)r6   r/   r8   rP   s    rG   	on_modifyz_MemoryFile.on_modify_        >BYY[H%(:rL   c                 X    t        j                          x| j                  _        | _        y)zCalled when file is accessed.N)r6   r/   r7   rP   s    rG   	on_accessz_MemoryFile.on_accessd   rU   rL   c                      y rN    rP   s    rG   flushz_MemoryFile.flushi   s    rL   c              #      K   | j                   j                  | j                         | j                   D ]  }|  y wrN   )r5   r<   r9   )rA   lines     rG   __iter__z_MemoryFile.__iter__m   s4     DHH%NN 	DJ	s   <>c                     | j                         5  | j                          t        | j                        cd d d        S # 1 sw Y   y xY wrN   )rR   rW   nextr5   rP   s    rG   r_   z_MemoryFile.nexts   s7    __ 	(NN'	( 	( 	(s   %A  A	c                     | j                   j                  st        d      | j                         5  | j	                          | j
                  j                  |      cd d d        S # 1 sw Y   y xY wNzFile not open for reading)r.   readingIOErrorrR   rW   r5   readlinerA   sizes     rG   rd   z_MemoryFile.readline{   sV    zz!!566__ 	1NN>>**40	1 	1 	1   +A''A0c                     | j                   sM| j                  j                  5  | j                  j                  |        t        t
        |           d d d        y y # 1 sw Y   y xY wrN   )closedr/   r;   remove_open_filer*   r'   closerA   rF   s    rG   rk   z_MemoryFile.close   sU    {{%% 1006k4.01 1 1 1s   .AA%c                     | j                   j                  st        d      | j                         5  | j	                          | j
                  j                  |      cd d d        S # 1 sw Y   y xY wra   )r.   rb   rc   rR   rW   r5   readre   s     rG   rn   z_MemoryFile.read   sV    zz!!566__ 	-NN>>&&t,	- 	- 	-rg   c                 .    | j                   j                  S rN   )r.   rb   rP   s    rG   readablez_MemoryFile.readable       zz!!!rL   c                     | j                   j                  st        d      | j                         5  | j	                          | j
                  j                  |      cd d d        S # 1 sw Y   y xY wra   )r.   rb   rc   rR   rW   r5   readinto)rA   buffers     rG   rs   z_MemoryFile.readinto   sV    zz!!566__ 	3NN>>**62	3 	3 	3rg   c                     | j                   j                  st        d      | j                         5  | j	                          | j
                  j                  |      cd d d        S # 1 sw Y   y xY wra   )r.   rb   rc   rR   rW   r5   	readlines)rA   hints     rG   rv   z_MemoryFile.readlines   sV    zz!!566__ 	2NN>>++D1	2 	2 	2rg   c                      y)NTrY   rP   s    rG   seekablez_MemoryFile.seekable   s    rL   c                     | j                         5  | j                          | j                  j                  |t	        |            cd d d        S # 1 sw Y   y xY wrN   )rR   rW   r5   r<   int)rA   r9   whences      rG   r<   z_MemoryFile.seek   sE     __ 	9NN>>&&sCK8	9 	9 	9s   5AAc                     | j                   S rN   )r9   rP   s    rG   r@   z_MemoryFile.tell   s    xxrL   c                    | j                         5  | j                          | j                  j                  |      }|| j                  j	                         |k  ry| j                  j                  dt        j                        }| j                  j                  d||z
  z         | j                  j                  | |z   t        j                         |xs |cd d d        S # 1 sw Y   y xY w)Nr       )	rR   rT   r5   r:   r@   r<   r>   r?   write)rA   rf   new_size	file_sizes       rG   r:   z_MemoryFile.truncate   s    __ 	$NN~~..t4HDNN$7$7$9D$@ NN//2;;?	$$UdY.>%?@##TEI$5r{{C#8	$ 	$ 	$s   C	C$$C-c                 .    | j                   j                  S rN   )r.   writingrP   s    rG   writablez_MemoryFile.writable   rq   rL   c                     | j                   j                  st        d      | j                         5  | j	                          | j
                  j                  |      cd d d        S # 1 sw Y   y xY w)NzFile not open for writing)r.   r   rc   rR   rT   r5   r   )rA   datas     rG   r   z_MemoryFile.write   sV    zz!!566__ 	.NN>>''-	. 	. 	.rg   c                     | j                         5  | j                          | j                  j                  |       d d d        y # 1 sw Y   y xY wrN   )rR   rT   r5   
writelines)rA   sequences     rG   r   z_MemoryFile.writelines   s=    __ 	0NNNN%%h/	0 	0 	0s   ,AArN   )) __name__
__module____qualname__r+   rK   propertyrD   
contextlibcontextmanagerrR   rT   rW   rZ   r]   r_   __next__rd   rk   rn   rp   rs   rv   ry   r   setr<   r@   r:   r   r   r   __classcell__rF   s   @rG   r'   r'   3   s    10B
 , , - -I
I
( H11-"32  $xx 9	$".0rL   r'   c                       e Zd Zd Zed        Zed        Zed        Zed        Z	ed        Z	ed        Z	dd	Z	d
 Z
d Zd Zd Zd Zd Zd Zd ZddZy)	_DirEntryc                    || _         || _        t               | _        g | _        d | _        t               | _        t        j                         }|| _	        || _
        || _        | j                  st        j                         | _        y y rN   )resource_typenamer   _dir_open_files_bytes_filer   r;   r6   created_timer7   r8   is_dirr2   r3   )rA   r   r   current_times       rG   r+   z_DirEntry.__init__   sp    *	M	G	yy{()){{!zz|D rL   c                     | j                   S rN   )r   rP   s    rG   r4   z_DirEntry.bytes_file   s     rL   c                 <    | j                   t        j                  k(  S rN   )r   r   	directoryrP   s    rG   r   z_DirEntry.is_dir   s     !!\%;%;;;rL   c                 .   | j                   5  | j                  r
	 d d d        yt        j                  t        j
                  | j                        }|j                  dt        j                         |j                         cd d d        S # 1 sw Y   y xY wr)   )r;   r   r0   r1   r2   r3   r   r<   r>   r?   r@   )rA   r   s     rG   rf   z_DirEntry.size   sp     YY 	*{{	* 	* %kk"**d6F6FG  BKK0"'')	* 	* 	*s   BABBc                      y rN   rY   rA   r   defaults      rG   	get_entryz_DirEntry.get_entry        	rL   c                      y rN   rY   rA   r   s     rG   r   z_DirEntry.get_entry   r   rL   c                      y rN   rY   r   s      rG   r   z_DirEntry.get_entry  r   rL   Nc                 `    | j                   sJ d       | j                  j                  ||      S )Nzmust be a directory)r   r   getr   s      rG   r   z_DirEntry.get_entry	  s)    {{111{yy}}T7++rL   c                 "    || j                   |<   y rN   r   )rA   r   rE   s      rG   	set_entryz_DirEntry.set_entry  s    #		$rL   c                     | j                   |= y rN   r   r   s     rG   remove_entryz_DirEntry.remove_entry  s    IIdOrL   c                 8    | j                   j                          y rN   )r   clearrP   s    rG   r   z_DirEntry.clear  s    		rL   c                     || j                   v S rN   r   r   s     rG   __contains__z_DirEntry.__contains__  s    tyy  rL   c                 ,    t        | j                        S rN   )lenr   rP   s    rG   __len__z_DirEntry.__len__  s    499~rL   c                 H    t        | j                  j                               S rN   )listr   keysrP   s    rG   r   z_DirEntry.list"  s    DIINN$%%rL   c                 :    | j                   j                  |       y rN   )r   appendrA   memory_files     rG   add_open_filez_DirEntry.add_open_file&      ,rL   c                 :    | j                   j                  |       y rN   )r   remover   s     rG   rj   z_DirEntry.remove_open_file*  r   rL   c                     |xs d}d| j                   | j                  di}d|v rIddgt        | j                        | j                  | j
                  | j                  | j                  d|d<   t        |      S )NrY   basic)r   r   detailsaccessedmodified)_writetyperf   r   r   created)	r   r   r{   r   rf   r7   r8   r   r   )rA   
namespacesinfos      rG   to_infoz_DirEntry.to_info.  sz    %2
$))t{{CD
"%z2D../		 .. ..,,DO DzrL   rN   )r   r   r   r+   r   r4   r   rf   r	   r   r   r   r   r   r   r   r   rj   r   rY   rL   rG   r   r      s    ,"     < < * *      ,
$!&--rL   r   c                        e Zd ZdZddddddddZ fdZd Zd Zd	 Zd
 Z	 fdZ
ddZd Zej                  rddZ	 	 ddZddZddZddZd Zd Zd Z	 	 ddZd Z x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.

    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                     | j                   j                         | _         | j                  t        j                  d      | _        t        t        | #          y)zCreate an in-memory filesystem. N)	_metacopy_make_dir_entryr   r   rootr*   r$   r+   rl   s    rG   r+   zMemoryFS.__init__^  s>     ZZ__&
(()?)?D	h&(rL   c                      y)Nz
MemoryFS()rY   rP   s    rG   __repr__zMemoryFS.__repr__e  s    rL   c                      y)Nz<memfs>rY   rP   s    rG   rK   zMemoryFS.__str__i  s    rL   c                     t        ||      S rN   )r   )rA   r   r   s      rG   r   zMemoryFS._make_dir_entrym  s    --rL   c                     | j                   5  t        |      }| j                  }t        |      D ]5  }|
 ddd       y|j                  s
 ddd       y|j                  |      }7 |cddd       S # 1 sw Y   yxY w)z6Get a directory entry, or `None` if one doesn't exist.N)_lockr   r   r   r   r   )rA   dir_pathcurrent_entrypath_components       rG   _get_dir_entryzMemoryFS._get_dir_entryq  s     ZZ 		!)H IIM"-h"7 H (		! 		! %++		! 		! !. 7 7 GH !		! 		! 		!s   )A3A3A33A<c                 D    | j                   s| `t        t        |          S rN   )_closedr   r*   r$   rk   rl   s    rG   rk   zMemoryFS.close  s    ||	Xt*,,rL   c                     | j                  |      }| j                  |      }|t        j                  |      |j	                  |      S N)r   )validatepathr   r   ResourceNotFoundr   )rA   rB   r   r,   rE   s        rG   getinfozMemoryFS.getinfo  sM    !!$'''.	))$//  J 77rL   c                 :   | j                          | j                  |      }| j                  5  | j                  |      }|t	        j
                  |      |j                  st	        j                  |      |j                         cd d d        S # 1 sw Y   y xY wrN   )	checkr   r   r   r   r   r   DirectoryExpectedr   )rA   rB   r,   rE   s       rG   listdirzMemoryFS.listdir  s    

!!$'ZZ 	$++E2I --d33##..t44>>#	$ 	$ 	$s   ABBc                      y rN   rY   )rA   rB   factorys      rG   opendirzMemoryFS.opendir  s    rL   c                    | j                  |      }| j                  5  |dk(  r1|r| j                  |      cd d d        S t        j                  |      t        |      \  }}| j                  |      }|t        j                  |      |j                  |      }||st        j                  |      |2| j                  t        j                  |      }	|j                  ||	       | j                  |      cd d d        S # 1 sw Y   y xY wN/)r   r   r   r   DirectoryExistsr   r   r   r   r   r   r   r   )
rA   rB   permissionsrecreater,   r   dir_name
parent_dirrE   new_dirs
             rG   makedirzMemoryFS.makedir  s     !!$'ZZ 	&|<<-	& 	&
 !0066!&uHh,,X6J!--d33",,X6I$X,,T22 ..|/E/ExP$$Xw7<<%)	& 	& 	&s   D B9DDc                 p   t        | j                  |            \  }}t        | j                  |            \  }}| j                  5  | j                  |      }	|	||	vrt	        j
                  |      |	j                  |      }
|
j                  rt	        j                  |      | j                  |      }|t	        j
                  |      |s||v rt	        j                  |      |j                  ||
       |	j                  |       ||
_        |rt        | || |       d d d        y # 1 sw Y   y xY wrN   )r   r   r   r   r   r   r   r   FileExpectedDestinationExistsr   r   r   r   )rA   src_pathdst_path	overwritepreserve_timesrc_dirsrc_namedst_dirdst_namesrc_dir_entry	src_entrydst_dir_entrys               rG   movezMemoryFS.move  s(   !$"3"3H"=>!$"3"3H"=>ZZ 	C //8M$(E--h77%//9I))(33 //8M$--h778}#<..x88 ##Hi8&&x0%IN"44B+	C 	C 	Cs   CD,,D5c                 F   t        | j                  |            \  }}t        | j                  |            \  }}| j                  5  | j                  |      }	|	||	vrt	        j
                  |      |	j                  |      }
|
j                  st	        j                  |      | j                  |      }||s||vrt	        j
                  |      |j                  ||
       |	j                  |       ||
_        |rt        | || |       d d d        y # 1 sw Y   y xY wrN   )r   r   r   r   r   r   r   r   r   r   r   r   r   )rA   r  r  creater  r	  r
  r  r  r  r  r  s               rG   movedirzMemoryFS.movedir  s   !$"3"3H"=>!$"3"3H"=>ZZ 	C //8M$(E--h77%//9I##..x88 !//8M$V8U--h77 ##Hi8&&x0%IN"44B)	C 	C 	Cs   CDD c                    t        |      }|j                          | j                  |      }t        |      \  }}|st	        j
                  |      | j                  5  | j                  |      }	|	|	j                  st	        j                  |      |j                  r||	vr3| j                  t        j                  |      }
|	j                  ||
       n2| j                  |      }
|j                  rt	        j                   |      |
j                  rt	        j
                  |      t#        || ||
      }|
j%                  |       |cd d d        S ||	vrt	        j                  |      |	j'                  |      }
|
j                  rt	        j
                  |      t#        || ||
      }|
j%                  |       |cd d d        S # 1 sw Y   y xY w)N)rB   rC   rD   rE   )r   validate_binr   r   r   r  r   r   r   r   r  r   r   filer   	exclusive
FileExistsr'   r   r   )rA   rB   rD   	bufferingoptionsr.   r,   r   	file_nameparent_dir_entryfile_dir_entrymem_files               rG   openbinzMemoryFS.openbin  s   T
!!$'#El)%%d++ZZ #	#228<'/?/F/F--d33||$44%)%9%9,:K:KY%WN$..y.I%)%8%8%?N$//55!(( --d33&$T^ ,,X6-#	 #	0  00--d33-77	BN$$))$//"dH ((2G#	 #	 #	s   C,G
A,G

Gc                    | j                  |      }| j                  5  t        |      \  }}| j                  |      }|||vrt	        j
                  |      t        j                  t        | j                  |            }|j                  rt	        j                  |      |j                  |       d d d        y # 1 sw Y   y xY wrN   )r   r   r   r   r   r   r0   r1   r   r   r  r   )rA   rB   r,   r   r  r  r  s          rG   r   zMemoryFS.remove#  s    !!$'ZZ 	5"',Hi#228<'9<L+L--d33#[[D4G4G4NON$$))$//)))4	5 	5 	5s   BB==Cc                     | j                  |      }|dk(  rt        j                         | j                  |      st        j                  |      | j                  |       y r   )r   r   RemoveRootErrorisemptyDirectoryNotEmpty
removetree)rA   rB   r,   s      rG   	removedirzMemoryFS.removedir4  sU     !!$'C<((**||D!**400
 	rL   c                    | j                  |      }| j                  5  |dk(  r$| j                  j                          	 d d d        y t	        |      \  }}| j                  |      }|||vrt        j                  |      t        j                  t        | j                  |            }|j                  st        j                  |      |j                  |       d d d        y # 1 sw Y   y xY wr   )r   r   r   r   r   r   r   r   r0   r1   r   r   r   r   )rA   rB   r,   r   r  r  dir_dir_entrys          rG   r%  zMemoryFS.removetreeC  s    !!$'ZZ 	5|		!		5 	5 #(,Hi#228<'9<L+L--d33"KK	43F3Fu3MNM ''..t44)))4!	5 	5 	5s   !C&BC&&C/c              #     K   | j                          | j                  |      }| j                  5  | j                  |      }|t	        j
                  |      |j                  st	        j                  |      |j                         }|
|\  }}||| }|D ]?  }	t        j                  t        |j                  |	            }
|
j                  |       A 	 d d d        y # 1 sw Y   y xY wwr   )r   r   r   r   r   r   r   r   r   r0   r1   r   r   r   )rA   rB   r   pager,   rE   	filenamesstartendr   entrys              rG   scandirzMemoryFS.scandirY  s      	

!!$'ZZ 	;++E2I --d33##..t44!(I!
s%eC0	! ;Iy/B/B4/HImmzm::;	; 	; 	;s   .C0B*C$	C0$C-)C0c                 x   | j                  |      }| j                  5  t        |      \  }}| j                  |      }|||vrt	        j
                  |      t        j                  t        |j                  |            }d|v r!|d   }d|v r
|d   |_
        d|v r
|d   |_        d d d        y # 1 sw Y   y xY w)Nr   r   r   )r   r   r   r   r   r   r0   r1   r   r   r7   r8   )	rA   rB   r   r,   r   r  r  resource_entryr   s	            rG   setinfozMemoryFS.setinfos  s    !!$'ZZ 	G"',Hi#228<'9<L+L--d33#[[+55i@N D y/(3::3FN0(3::3FN0!	G 	G 	Gs   B	B00B9rN   )NF)FF)rr   )NN)r   r   r   __doc__r   r+   r   rK   r   r   rk   r   r   r0   TYPE_CHECKINGr   r  r  r  r  r   r&  r%  r/  r2  r   r   s   @rG   r$   r$   >  s    * ""E).!-8$ 	 	&<C6C4-^5"52 	;4GrL   )>r4  
__future__r   r   r0   r   r2   r>   sixr6   collectionsr   	threadingr   r   r   _typingr	   baser
   r   r   enumsr   r   r   r   rD   r   rB   r   r   r   r5  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$   rY   rL   rG   <module>rD     s    8   	 	 
  #     $ %   . .	    %(	J	/B   \0",, \0 !\0~h hV   GGr GG !GGrL   