
    obfe                        d Z ddlmZ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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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$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/ ej`                  r@ddlm1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; ddlm<Z< ddl&m=Z= ddl>m?Z?  ej                  dd      ZA e
j                  d      ZC ej                         dk(  ZEej                   G d de             ZGy# e$ r 	 ddlmZ n# e$ r dZY nw xY wY w xY w# e$ r 	 ddlmZ n# e$ r dZY nw xY wY w xY w)zManage the filesystem provided by your OS.

In essence, an `OSFS` is a thin layer over the `io` and `os` modules
of the Python standard library.
    )absolute_importprint_functionunicode_literalsN)scandir)sendfile   )errors)fsdecodefsencodefspath)	url_quote)FS)copy_modified_time)ResourceType)convert_os_errors)FileExpectedNoURL)Info)Modevalidate_open_mode)basenamedirname)Permissions)IOAnyBinaryIO
CollectionDictIteratorListOptionalSupportsIntTextTuple)_OpendirFactory)RawInfo)SubFS_OOSFS)boundzfs.osfsWindowsc                   p    e Zd ZdZ	 	 	 d# fd	Zd Zd Zd Zed        Z	ed        Z
ej                  ej                  ej                  ej                   ej"                  ej$                  ej&                  ej(                  ej*                  ej,                  ej.                  ej0                  ej2                  ej4                  iZed        Zd	 Zd
 Zd$dZd Z 	 	 d%dZ!d&dZ"d Z#d Z$e%jL                  rd$dZ'd'dZ(e)jT                  dd dk  roe+me,jZ                  e,j\                  e,j^                  e,j`                  e,jb                  e,jd                  hZ3 e4e,d      re3jk                  e,jl                         d(dZ7nd(dZ7e8rd$dZ9nd$dZ9	 	 d)dZ8d Z:d*dZ;d Z<d Z=	 	 	 	 	 	 d+d Z>d! Z? fd"Z@ xZAS ),r)   zCreate an OSFS.

    Examples:
        >>> current_directory_fs = OSFS('.')
        >>> home_fs = OSFS('~/')
        >>> windows_system32_fs = OSFS('c://system32')

    c                 :   t         t        |           t        |t              rt        |      }|| _        t        j                  j                  t        t        |                  \  }}|r	||xs dz   n|}t        j                  j                  |rt        j                  j                  |      n|      }t        j                  j                  t        j                  j                  |            }|| _        |rA	 t        j                  j!                  |      s t        j"                  |t%        |             nEt        j                  j!                  |      s&dj-                  |      }t)        j*                  |      ddddt        j                  j.                  ddx}	| _        	 t3        j4                  d	
      5 }
t        j                  j7                  |
j8                  j;                               |	d<   ddd       tD        r(djG                  d tI        d      D              dz   |	d<   yd|	d<   dt        jJ                  v r5	 t        jL                  tO        |      t        jJ                  d         |	d<   yy# t&        $ r+}t)        j*                  dj-                  ||      |      d}~ww xY w# 1 sw Y   xY w# t<        $ r@ t?        j@                         dk7  r%t        j                  jC                  d      dk(  |	d<   Y w xY w# t&        $ r Y yw xY w)a  Create an OSFS instance.

        Arguments:
            root_path (str or ~os.PathLike): An OS path or path-like object
                to the location on your HD you wish to manage.
            create (bool): Set to `True` to create the root directory if it
                does not already exist, otherwise the directory should exist
                prior to creating the ``OSFS`` instance (defaults to `False`).
            create_mode (int): The permissions that will be used to create
                the directory if ``create`` is `True` and the path doesn't
                exist, defaults to ``0o777``.
            expand_vars(bool): If `True` (the default) environment variables
                of the form ``~``, ``$name`` or ``${name}`` will be expanded.

        Raises:
            `fs.errors.CreateFailed`: If ``root_path`` does not
                exist, or could not be created.

        /modezunable to create {} ({})Nzroot path '{}' does not existFT)network	read_onlysupports_renamethread_safeunicode_pathsvirtualTmP)prefixcase_insensitiveDarwinAaaa c              3   F   K   | ]  }t        j                  |        y wN)sixunichr).0ns     )/usr/lib/python3/dist-packages/fs/osfs.py	<genexpr>z OSFS.__init__.<locals>.<genexpr>   s     9!

19s   !   z\:*?"<>|invalid_path_chars PC_PATH_MAXmax_sys_path_length)(superr)   __init__
isinstancebytesr
   	root_pathospath
splitdriver   
expanduser
expandvarsnormpathabspath
_root_pathisdirmakedirsintOSErrorr	   CreateFailedformatsupports_unicode_filenames_metatempfileNamedTemporaryFileexistsnamelower	Exceptionplatformsystemnormcase_WINDOWS_PLATFORMjoinrangepathconf_namespathconfr   )selfrO   createcreate_modeexpand_vars_driverW   errormessager_   	_tmp_file	__class__s              rD   rL   zOSFS.__init__[   s   6 	dD"$i' +I"WW//	9J0KL
5;Vz0S1
WW''.9BGGz*z

 WW%%bggooj&AB
$ww}}Z0KK
[1AB 77==,9@@L))'22 #WW??
 	

	K ,,E: Si,.GGNN9>>;O;O;Q,R()S 9uRy99KG &' +/E&' 1 1135;; ,b.?.?.N4E/0 2G  )).55iG ,S S 	K H,,.GG,<,<T,Bd,J()	K   sU   =?I? +K ;J6<K 
3L ?	J3&J..J36J?;K AL
L	LLc                 j    d}| j                   j                  }|j                  || j                        S )Nz{}({!r}))rv   __name__r]   rO   )rn   _fmt_class_names      rD   __repr__zOSFS.__repr__   s+    nn--{{;77    c                     d}| j                   j                  }|j                  |j                         | j                        S )Nz	<{} '{}'>)rv   rx   r]   rd   rO   )rn   fmtrz   s      rD   __str__zOSFS.__str__   s4    nn--zz+++-t~~>>r|   c           	          t        t        j                  j                  | j                  |j                  d      j                  dt        j                                    }|S )z&Convert a FS path to a path on the OS.r.   )r   rP   rQ   rj   rW   lstripreplaceseprn   rQ   sys_paths      rD   _to_sys_pathzOSFS._to_sys_path   sE     GGLL$++c*:*B*B3*OP
 r|   c           	          ddg|j                   |j                  |j                  t        | j	                  |            d}t        |dd      |d<   t        rdnd}|j                  ||<   |S )z;Make a *details* info dict from an `os.stat_result` object.accessedmodified)_writer   r   sizetypest_birthtimeNcreatedmetadata_changed)st_atimest_mtimest_sizerZ   _get_type_from_statgetattrri   st_ctime)clsstat_resultdetails	ctime_keys       rD   _make_details_from_statzOSFS._make_details_from_stat   su    
 ":.#,,#,,''//<=
 %[.$G	!2I8J	(11	r|   c                 v   i }t        |j                        j                         |d<   |j                  x|d<   }|j                  x|d<   }t
        sHddl}ddl}	 |j                  |      j                  |d<   	 |j                  |      j                  |d<   |S |S # t        $ r Y .w xY w# t        $ r Y |S w xY w)	z;Make an *access* info dict from an `os.stat_result` object.r/   permissionsgiduidr   Ngroupuser)r   st_modedumpst_gidst_uidri   grppwdgetgrgidgr_nameKeyErrorgetpwuidpw_name)r   r   accessr   r   r   r   s          rD   _make_access_from_statzOSFS._make_access_from_stat   s      +1D1D E J J L})000u)000u "%,,s"3";";w!$c!2!:!:v v  
  s$   B :B+ 	B('B(+	B87B8c                     |j                   }t        j                  |      }| j                  j	                  |t
        j                        S )z6Get the resource type from an `os.stat_result` object.)r   statS_IFMTSTAT_TO_RESOURCE_TYPEgetr   unknown)r   _statr   st_types       rD   r   zOSFS._get_type_from_stat   s:     --++g&((,,Wl6J6JKKr|   c                     t        t        d      rC	 t        rt        j                  |      S t	        t        j                  t        |                  S y # t        $ r Y y w xY w)Nreadlink)hasattrrP   ri   r   r
   r   r[   )rn   r   s     rD   
_gettargetzOSFS._gettarget  sX    2z"$;;x00#BKK0B$CDD   s   A &A 	A! A!c                 ,    | j                  |      }d|iS )Ntarget)r   )rn   r   _targets      rD   _make_link_infozOSFS._make_link_info  s    //(+'""r|   Nc                 B   | j                          |xs d}| j                  |      }| j                  |      }d }t        d|      5  t	        j
                  t        |            }d|v rt	        j                  t        |            }d d d        dt        |      t        j                  j                        di}d|v r| j                  |      |d<   d|v r9t        |      D ci c]!  }|j                  d      s|t        ||      # c}|d<   d|v r9t        |      D ci c]!  }|j                  d      s|t        ||      # c}|d<   d	|v r| j                  |      |d	<   d
|v r| j!                  |      |d
<   t#        |      S # 1 sw Y   xY wc c}w c c}w )N getinfolstatbasicrc   is_dirr   r   st_linkr   )checkvalidatepath
getsyspathr   rP   r   r   r   r   S_ISDIRr   r   dir
startswithr   r   r   r   )	rn   rQ   
namespaces_pathr   _lstatr   infoks	            rD   r   zOSFS.getinfo  s   

%2
!!$'??5)y$/ 	6GGHX./E*$(8"45	6 huoemm9TU
 
""::5ADOZ.1%j)*ALL<O75!$$DL j /26{*+all5>Q761%%DM Z//9DLz!!88?DNDz/	6 	6s%   AF
&F=F#F:F
Fc                 $   | j                          | j                  |      }| j                  |      }t        d|d      5  t	        j
                  t        |            }d d d        D cg c]  }t        |       c}S # 1 sw Y   "xY wc c}w )NlistdirT	directory)r   r   r   r   rP   r   r   r
   )rn   rQ   r   r   namesrc   s         rD   r   zOSFS.listdir7  sv    

!!$'$$U+y$$? 	3JJx12E	3+01411	3 	31s   B,BB
c                    | j                          t        j                  |      }| j                  |      }| j	                  |      }t        d|d      5  	 t        j                  ||       | j                  |      cd d d        S # t        $ r]}|j                  t        j                  k(  rt        j                  |      |j                  t        j                  k(  r|rn Y d }~{d }~ww xY w# 1 sw Y   y xY w)NmakedirTr   )r   r   get_moder   r   r   rP   mkdirr[   errnoENOENTr	   ResourceNotFoundEEXISTopendir)rn   rQ   r   recreater0   r   r   rs   s           rD   r   zOSFS.makedirA  s     	

##K0!!$'$$U+y$$? 
	'4( <<&
	' 
	'  ;;%,,. 11$77[[ELL0X 	
	' 
	's7   C1B.C1	C.AC)$C1)C..C11C:c                 *   t        |      }|j                          | j                          | j                  |      }|dk(  rt	        j
                  |      | j                  |      }t        d|      5  t        j                  rR|j                  rFt        j                  |t        j                  t        j                  z  t        j                  z        }t!        j                  |f|j#                         |d|}d d d        |S # 1 sw Y   S xY w)Nr.   openbin)r0   	buffering)r   validate_binr   r   r	   r   r   r   r@   PY2	exclusiverP   openO_RDWRO_CREATO_EXCLioto_platform_bin)	rn   rQ   r0   r   options_moder   r   binary_files	            rD   r   zOSFS.openbinX  s    T


!!$'C<%%d++$$U+y$/ 	ww5??778RYY-Cbii-OP''$446)OVK	 	 s   4B
DDc                    | j                          | j                  |      }| j                  |      }t        d|      5  	 t	        j
                  |       	 d d d        y # t        $ r}|j                  t        j                  k(  rGt        j                  dk(  r4t        j                  j                  |      rt        j                  |      |j                  t        j                  k(  rGt        j                  dk(  r4t        j                  j                  |      rt        j                  |       d }~ww xY w# 1 sw Y   y xY w)Nremovewin32darwin)r   r   r   r   rP   r   r[   r   EACCESsysrf   rQ   rX   r	   r   EPERM)rn   rQ   r   r   rs   s        rD   r   zOSFS.removei  s    

!!$'$$U+x. 			(#	 	  	;;%,,.3<<73Jww}}X.$11$77;;%++-#,,(2Jww}}X.$11$77		 	s*   D:A  	D7)C	D22D77D::Ec                 
   | j                          | j                  |      }|dk(  rt        j                         | j	                  |      }t        d|d      5  t        j                  |       d d d        y # 1 sw Y   y xY w)Nr.   	removedirTr   )r   r   r	   RemoveRootErrorr   r   rP   rmdirrn   rQ   r   r   s       rD   r   zOSFS.removedir|  sn    

!!$'C<((**$$T*{DDA 	HHX	 	 	s   A99Bc                      y r?   r   )rn   rQ   factorys      rD   r   zOSFS.opendir  s    r|   c                    | j                  |      }| j                  |      }| j                  |      t        j                  urt	        j
                  |      |s&| j                  |      rt	        j                  |      | j                  t        |            t        j                  urt	        j                  t        |            ||fS r?   )r   gettyper   filer	   r   rb   DestinationExistsr   r   DirectoryExpected)rn   src_pathdst_path	overwrite	_src_path	_dst_paths         rD   _check_copyzOSFS._check_copy  s    %%h/	%%h/	<<!):)::%%h//T[[3**844<<)*,2H2HH**78+<==)##r|      )      ENOTSUPc           	         | j                   5  | j                  |||      \  }}| j                  |      | j                  |      }}	 t        j                  |d      5 }	t        j                  |d      5 }
|	j                         |
j                         }}t        j                  |      j                  x}}d}|dkD  rt        ||||      }||z  }|dkD  rd d d        d d d        |rt        | || |       d d d        y # 1 sw Y   *xY w# 1 sw Y   .xY w# t        $ r9}|j                  | j                  vr t        j                  ||       Y d }~^d }~ww xY w# 1 sw Y   y xY w)Nrwr   )_lockr  r   r   r   filenorP   fstatr   r   r   r[   r   _sendfile_error_codesshutilcopy2)rn   r  r  r  preserve_timer  r  _src_sys_dst_syssrcdstfd_srcfd_dstsentmaxsizeoffsetes                    rD   copyz	OSFS.copy  sR    5'+'7'7(I'V$	9OOI.OOI. #
5 3/ /3WWXs3 /s-0ZZ\3::<FF-/XXf-=-E-EED7%&F"&('/'P &$ #'(	// %*44J)5 5/ // /  5wwd&@&@@LL8445+5 5s`   9EDD4A!C?DD?DDD	D	E /EEEEE%c                     | j                   5  | j                  |||      \  }}t        j                  | j	                  |      | j	                  |             d d d        y # 1 sw Y   y xY wr?   )r  r  r  r  r   )rn   r  r  r  r  r  r  s          rD   r!  z	OSFS.copy  s[     U'+'7'7(I'V$	9T__Y79STU U Us   AA!!A*c           
   #     K   | j                          |xs d}h dj                  |       }| j                  |      }t        rXt        j
                  j                  | j                  |j                  d      j                  dt        j                              }n| j                  |      }t        d|d      5  t        |      }	 |D ]D  }dt        |j                        |j!                         di}|r||j#                         }	d	|v r| j%                  |	      |d	<   d
|v r8t'        |	      D 
ci c]   }
|
j)                  d      r|
t+        |	|
      " c}
|d
<   d|v r| j-                  |	      |d<   d|v rJ|j#                  d      }t'        |      D 
ci c]   }
|
j)                  d      r|
t+        ||
      " c}
|d<   d|v r<| j/                  t        j
                  j                  ||j                              |d<   t1        |       G 	 t2        j4                  dk\  r|j7                          	 d d d        y c c}
w c c}
w # t2        j4                  dk\  r|j7                          w w xY w# 1 sw Y   y xY ww)Nr   >   r   r   r   r.   r   Tr   r   r   r   r   r   r   r   F)follow_symlinksr   )r
     )r   
isdisjointr   ri   rP   rQ   rj   rW   r   r   r   r   r   r   r
   rc   r   r   r   r   r   r   r   r   r   r   version_infoclose)rn   rQ   r   requires_statr   r   scandir_iter	dir_entryr   r   r   lstat_results               rD   _scandirzOSFS._scandir  sh    JJL#)rJ = H H TTM%%d+E 77<<OOT[[%5%=%=c266%J  ,,U3"9ddC )-&x0'-%1 #)	#(0(@*3*:*:*<&  )*3..*:K(J6262N2N$/3"Y  &3 .1-=0"()'(||E': %&w{A'>$>0"V
  (:5151L1L$/2"X #j0+4>>%>+PL *-\):-$%#$<<#6 !"7<#; ;-DM
 "Z/+/+?+? "Xy~~ F,DL #4j(G#)J ''61$**,S)- )-"0"- ''61$**, 2Q)- )-sW   B9I?;I3A)I
1%I AI
%I<AI
$I37	I? 
I

&I00I33I<8I?c           
   #     K   | j                          |xs d}| j                  |      }| j                  |      }t        d|d      5  t	        j
                  |      D ]p  }t        |      }t        j                  j                  ||      }t	        j                  t        |            }d|t        j                  |j                        di}	d|v r| j                  |      |	d<   d|v r8t        |      D 
ci c]   }
|
j                  d	      r|
t!        ||
      " c}
|	d<   d
|v rMt	        j"                  |      }t        |      D 
ci c]   }
|
j                  d	      r|
t!        ||
      " c}
|	d
<   d|v r2| j%                  t        j                  j                  ||            |	d<   d|v r| j'                  |      |	d<   t)        |	       s 	 d d d        y c c}
w c c}
w # 1 sw Y   y xY ww)Nr   r   Tr   r   r   r   r   r   r   r   r   )r   r   r   r   rP   r   r
   rQ   rj   r   r   r   r   r   r   r   r   r   r   r   r   )rn   rQ   r   r   r   
entry_name_entry_name
entry_pathr   r   r   r,  s               rD   r-  zOSFS._scandir  s    JJL#)rJ%%d+Eu-H"9ddC !%"$**X"6  %J"*:"6K!#h!DJ"$''(:*>"?K$/&*ll;3F3F&G"D !J.*.*F*F{*SY+ &)%5( ! ||E2 w{A66(V
 *,')xx
'; &)%6) ! ||E2 w|Q77)W
 +'+';';GGLL:>(V  :-)-)D)D[)QXt*$A %!% !%()+!% !%s>   AG3	B0G'9%G,G'
%G"/A$G'	G3
G''G0,G3c                 h    | j                  ||      }||\  }}t        j                  |||      }|S )N)r   )r-  	itertoolsislice)rn   rQ   r   page	iter_infostartends          rD   r   zOSFS.scandir>  s?     MM$:M>	JE3!((E3?Ir|   c                     t         j                  j                  | j                  |j	                  d      j                  dt         j                              }|S )Nr.   )rP   rQ   rj   rW   r   r   r   r   s      rD   r   zOSFS.getsyspathM  s9    77<<S1A1I1I#rvv1VWr|   c                 z    | j                  |      }|dk(  rd|z   S |dk(  rt        |      }d|z   S t        ||      )Ndownloadzfile://fszosfs://)r   r   r   )rn   rQ   purposer   url_paths        rD   geturlzOSFS.geturlR  sL    ??4(j x''_ *Hx''g&&r|   c                     | j                          | j                  |      }t        d|      5  t        j                  |      }d d d        | j                        }|S # 1 sw Y   xY w)Nr   )r   r   r   rP   r   r   )rn   rQ   r   r   resource_types        rD   r   zOSFS.gettype]  s^    

$$T*y$/ 	%778$D	%006	% 	%s   AA(c                 4   | j                          | j                  |      }| j                  |      }| j                  |      st	        j
                  |      t        d|      5  t        j                  j                  |      cd d d        S # 1 sw Y   y xY w)Nislink)
r   r   r   rb   r	   r   r   rP   rQ   rC  r   s       rD   rC  zOSFS.islinkf  sw    

!!$'$$U+{{4 ))$//x. 	,77>>(+	, 	, 	,s   %BBc           
      R   t        |      }	t        |       | j                          | j                  |      }
|
dk(  rt	        |      | j                  |
      }t        d|      5  t        j                  rR|	j                  rFt        j                  |t        j                  t        j                  z  t        j                  z        }|xs d}t        j                  |f|	j!                         ||	j"                  rd n|||	j"                  rd n|d|cd d d        S # 1 sw Y   y xY w)Nr.   r   zutf-8)r0   r   encodingr	   newline)r   r   r   r   r   r   r   r@   r   r   rP   r   r   r   r   r   to_platformbinary)rn   rQ   r0   r   rE  r	   rF  line_bufferingr   r   r   r   	_encodings                rD   r   z	OSFS.openp  s     T
4 

!!$'C<t$$$$U+vt, 	ww5??778RYY-Cbii-OP +GI77&&(#!&9 %' 		 	 	s   %B.DD&c                 L   | j                          | j                  |      }| j                  |      }t        j                  j                  |      st        j                  |      d|v r|d   }d|v sd|v rt        j                  t        |j                  d            }t        j                  t        |j                  d|            }t        ||n|      }t        |      }	||	.t        d|      5  t        j                  |||	f       d d d        y y y y # 1 sw Y   y xY w)Nr   r   r   setinfo)r   r   r   rP   rQ   rb   r	   r   typingcastfloatr   r   utime)
rn   rQ   r   r   r   r   	_accessed	_modifiedr   r   s
             rD   rL  zOSFS.setinfo  s   

!!$'$$U+ww~~h'))$//9oGW$
g(="KKw{{:/FG	"KKw{{:y/QR	 i.?YO +'8+?*9d; AHh+?@A A ,@ )> A As   5DD#c                     	 t        |       t
        t        |   |      S # t        $ r-}t        j                  |dj	                  ||            d}~ww xY w)z7Check path may be encoded, in addition to usual checks.zSpath '{path}' could not be encoded for the filesystem (check LANG env var); {error})rQ   rs   )msgN)r   UnicodeEncodeErrorr	   InvalidCharsInPathr]   rK   r)   r   )rn   rQ   rs   rv   s      rD   r   zOSFS.validatepath  s^    	TN T4-d33 " 	++%%+VUV%C 	s   ! 	A(AA)Fi  Tr?   )NF)r  )F)FF)NN)r;  )r  rW  NNr=   F)Brx   
__module____qualname____doc__rL   r{   r   r   classmethodr   r   r   S_IFDIRr   r   S_IFCHR	characterS_IFBLKblock_special_fileS_IFREGr   S_IFIFOfifoS_IFLNKsymlinkS_IFSOCKsocketr   r   r   r   r   r   r   r   r   r   rM  TYPE_CHECKINGr   r  r   r'  r   r   EIOEINVALENOSYSEBADFENOTSOCK
EOPNOTSUPPr  r   addr  r!  r   r-  r   r?  r   rC  r   rL  r   __classcell__)rv   s   @rD   r)   r)   P   s    Un8?  $  . 	l,,l,,l55l''l''l**|** L L
#
@2 	'."&  	$ f$)= IILLLLKKNN!
 5)$!%%emm4	5@	U 5	-r'	%X 	
	', BA$4 4r|   )HrZ  
__future__r   r   r   r   rM  r   r   r3  loggingrP   rf   r  r@   r   r`   r   ImportErrorr   r=   r	   	_fscompatr
   r   r   
_url_toolsr   baser   r!  r   enumsr   error_toolsr   r   r   r   r   r0   r   r   rQ   r   r   r   r   rh  r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   subfsr'   TypeVarr(   	getLoggerlogrg   ri   python_2_unicode_compatibler)   r   r|   rD   <module>r~     sR   I H 
   	   	   
    1 1 !  $  * '  * # $	    &	F	+B g	" $HOO%2    ]	42 ]	4 !]	4q  #   % sk   D* E *E0D76E7E>E EEEE)EE)E"E)!E""E)(E)