o
    oĎi,                     @   s   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 Zdd Z	d	d
 Z
efddZG dd dZdaddZdd ZdbddZddddZddddZdcddZdddd Zd!d" Zddd#d$d%Zd&d' Zddd#d(d)Zddd*d+Zdbd,d-Zdd.d/d0d1Zddd2d3Zddd4d5Zd6d7 Zd8d9 Zd:d; Zd<d= Z d>d? Z!d@dA Z"dBdC Z#dDdE Z$dFdG Z%dHdI Z&dJdK Z'dLdM Z(G dNdO dOZ)dPdQ Z*dRdS Z+dddddTdUdVZ,dddddTdWdXZ-G dYdZ dZZ.d[d\ Z/G d]d^ d^Z0d_d` Z1dS )e    N)InvalidGlobArgumentsError   )	CloudPathc                  O   s   t | d tS Nr   
isinstancer   argskwargs r   P/home/jeff/fluffinator/venv/lib/python3.10/site-packages/cloudpathlib/patches.py_check_first_arg   s   r   c                  O   s   t | d d tS r   r   r   r   r   r   _check_first_arg_first_index   s   r   c                  O   s    t | d tpt |dd tS )Nr   root_dir)r   r   getr   r   r   r   _check_first_arg_or_root_dir   s    r   c                    s   |   fdd}|} |S )Nc                     s*   | i |r| i |S  | i |S Nr   r   	_original	cpl_checkcpl_versionr   r   _patched_version   s   z(_patch_factory.<locals>._patched_versionr   )original_versionr   r   r   r   r   r   _patch_factory   s   r   c                   @   s&   e Zd ZdddZdd Zdd ZdS )	
_OpenPatchNc                 C   s@   |d u rt j}|| _tj| _t|tj| _| jt _dd t_d S )Nc                 S      | S r   r   )xr   r   r   <lambda>2   s    z%_OpenPatch.__init__.<locals>.<lambda>)builtinsopen
_orig_openr   
__fspath___orig_fspathr   patched)selforiginal_openr   r   r   __init__%   s   z_OpenPatch.__init__c                 C   s   t jS r   )r   r   r$   r   r   r   	__enter__4      z_OpenPatch.__enter__c                 C   s   | j t_| jt_d S r   )r    r   r   r"   r   r!   r$   exc_type	exc_value	tracebackr   r   r   __exit__7      z_OpenPatch.__exit__r   __name__
__module____qualname__r&   r(   r.   r   r   r   r   r   $   s    
r   c                 C   s   t | S r   )r   )r%   r   r   r   
patch_open<      r4   c                 C   r   r   r   pathr   r   r   _cloudpath_fspath@      r8   .c                 C   s   t |  S r   )listiterdirr6   r   r   r   _cloudpath_os_listdirD      r=   )dir_fdc                C      |   S r   statr7   r?   r   r   r   _cloudpath_lstatH   r5   rD   c                C   r@   r   )mkdirrC   r   r   r   _cloudpath_mkdirL   r5   rF     Fc                 C   s   t j| d|dS )NT)parentsexist_ok)r   rE   )namemoderI   r   r   r   _cloudpath_os_makedirsP   s   rL   c                C   s   | j ddS )NF)
missing_okunlinkrC   r   r   r   _cloudpath_os_removeT   r>   rP   c                 C   s   | j D ]}|  qd S r   )rH   rmdir)rJ   dr   r   r   _cloudpath_os_removedirsX   s   

rS   )
src_dir_fd
dst_dir_fdc                C   
   |  |S r   renamesrcdstrT   rU   r   r   r   _cloudpath_os_rename]      
r\   c                 C   s   |  | t|  d S r   )rX   rS   )oldnewr   r   r   _cloudpath_os_renamesa   s   
r`   c                C   rV   r   rW   rY   r   r   r   _cloudpath_os_replacef   r]   ra   c                C   r@   r   )rQ   rC   r   r   r   _cloudpath_os_rmdirj   r5   rb   c                 C   r@   r   )r<   r6   r   r   r   _cloudpath_os_scandirn   r5   rc   T)r?   follow_symlinksc                C   r@   r   rA   )r7   r?   rd   r   r   r   _cloudpath_os_statr   r5   re   c                C   r@   r   rN   rC   r   r   r   _cloudpath_os_unlinkv   r5   rf   c                 c   s    | j |||dE d H  d S )N)top_downon_errorrd   )walk)toptopdownonerrorfollowlinksr   r   r   _cloudpath_os_walkz   s   rn   c                 C      | j S r   )rJ   r6   r   r   r   _cloudpath_os_path_basename   r)   rp   c                    st   d z&t  fdddd  D r' d7  t  fdddd  D sW n	 ty1   Y nw d d   S )Nr   c                 3   s$    | ]}|  d    kV  qdS )r   Nr   ).0itemipartsr   r   	<genexpr>   s   " z__common.<locals>.<genexpr>r   )all
IndexErrorru   r   rs   r   __common   s     rz   c                 C   s"   t dd | D }| d jj| S )Nc                 S   s   g | ]}|j qS r   ry   rq   pr   r   r   
<listcomp>   s    z1_cloudpath_os_path_commonpath.<locals>.<listcomp>r   )rz   clientr   )pathscommonr   r   r   _cloudpath_os_path_commonpath   s   r   c                 C   s   t dd | D }|S )Nc                 S   s   g | ]}t |qS r   )strr{   r   r   r   r}      s    z3_cloudpath_os_path_commonprefix.<locals>.<listcomp>)rz   )r;   r   r   r   r   _cloudpath_os_path_commonprefix   s   r   c                 C   ro   r   )parentr6   r   r   r   _cloudpath_os_path_dirname   r)   r   c                 C      |   jfS r   )rB   st_atimer6   r   r   r   _cloudpath_os_path_getatime   r>   r   c                 C   r   r   )rB   st_mtimer6   r   r   r   _cloudpath_os_path_getmtime   r>   r   c                 C   r   r   )rB   st_ctimer6   r   r   r   _cloudpath_os_path_getctime   r>   r   c                 C   r   r   )rB   st_sizer6   r   r   r   _cloudpath_os_path_getsize   r>   r   c                 G   s   |D ]}| | } q| S r   r   )r7   r   r|   r   r   r   _cloudpath_os_path_join   s   
r   c                 C   s   | j | jfS r   )r   rJ   r6   r   r   r   _cloudpath_os_path_split   r>   r   c                 C   s   t | d t| j  | jfS r   )r   lensuffixr6   r   r   r   _cloudpath_os_path_splitext   s   r   c                   @   $   e Zd Zdd Zdd Zdd ZdS )_OSPatchc                 C   s  dt jtfdt jtfdt jtfdt jtfdt j	t
fdt jtfdt jtfdt jtfd	t jtfd
t jtfdt jtfdt jtfdt jtfdt jtfdt jtfg}i | _|D ]\}}}|| j|< t ||}t!t || qRdt j"j#t$t%fdt j"j&t't(fdt j"j)t*t(fdt j"j+t,t%fdt j"j-t.j-t%fdt j"j/t0t%fdt j"j1t2t%fdt j"j3t4t%fdt j"j5t6t%fdt j"j7t.j8t%fdt j"j9t.j:t%fdt j"j;t<t%fdt j"j=t>t%fdt j"j?t@t%fg}i | _A|D ]\}}}}|| jA|< t |||d}t!t j"|| qd S )NfspathlistdirlstatrE   makedirsremove
removedirsrX   renamesreplacerQ   scandirrB   rO   ri   basename
commonpathcommonprefixdirnameexistsgetatimegetmtimegetctimegetsizeisfileisdirjoinsplitsplitextr   )Bosr   r8   r   r=   r   rD   rE   rF   r   rL   r   rP   r   rS   rX   r\   r   r`   r   ra   rQ   rb   r   rc   rB   re   rO   rf   ri   rn   os_originalsr   setattrr7   r   rp   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   is_filer   is_dirr   r   r   r   r   r   os_path_originals)r$   os_levelrJ   originalcloudr#   os_path_levelcheckr   r   r   r&      sd   

















z_OSPatch.__init__c                 C   s   d S r   r   r'   r   r   r   r(      r9   z_OSPatch.__enter__c                 C   sF   | j  D ]
\}}tt|| q| j D ]\}}ttj|| qd S r   )r   itemsr   r   r   r7   )r$   r+   r,   r-   rJ   r   r   r   r   r.      s
   z_OSPatch.__exit__Nr0   r   r   r   r   r      s    <r   c                   C      t  S r   )r   r   r   r   r   patch_os_functions  r)   r   c                 C   s   t | jD ] \}}d|v sd|v sd|v r%| jd | }| j|d  } nq| j}g }| j| }|s8d}||fS d|}||fS )N*?[/)	enumerateru   _new_cloudpathr   )pathnamert   part
root_partspattern_partsr   patternr   r   r   #_get_root_dir_pattern_from_pathname
  s   

r   r   r?   	recursiveinclude_hiddenc                C   s   t | tr t |tr | |std| d|  d| |}n t | tr4|d ur-tdt| \}}nt |tr<| }ntd||S )Nz"If both are CloudPaths, root_dir (z ) must be a parent of pathname (z).zFIf pathname is a CloudPath, root_dir must also be a CloudPath or None.z9At least one of pathname or root_dir must be a CloudPath.)r   r   is_relative_tor   relative_tor   glob)r   r   r?   r   r   r   r   r   r   _cloudpath_glob_iglob!  s$   



r   c                C   s   t t| ||||dS )Nr   )r;   r   )r   r   r?   r   r   r   r   r   _cloudpath_glob_globD  s   r   c                   @   r   )
_GlobPatchc                 C   s8   t j | _t j| _t| jttd| _t| jttd| _	d S )Nr   )
r   original_globigloboriginal_iglobr   r   r   patched_globr   patched_iglobr'   r   r   r   r&   S  s   z_GlobPatch.__init__c                 C      | j t_| jt_d S r   )r   r   r   r   r'   r   r   r   r(   c  s   z_GlobPatch.__enter__c                 C   r   r   )r   r   r   r   r*   r   r   r   r.   h  r/   z_GlobPatch.__exit__Nr0   r   r   r   r   r   R  s    r   c                   C   r   r   )r   r   r   r   r   
patch_globm  r)   r   c                   @   r   )_PatchAllBuiltinsc                 C   s   t  | _ t | _t | _d S r   )r4   r   r   r'   r   r   r   r&   r  s   z_PatchAllBuiltins.__init__c                 C   s"   | j   | j  | j  d S r   )r4   r(   r   r   r'   r   r   r   r(   w  s   


z_PatchAllBuiltins.__enter__c                 C   s4   | j ||| | j||| | j||| d S r   )r4   r.   r   r   r*   r   r   r   r.   }  s   z_PatchAllBuiltins.__exit__Nr0   r   r   r   r   r   q  s    r   c                   C   r   r   )r   r   r   r   r   patch_all_builtins  r)   r   r   )r:   )rG   F)TNF)2r   r   r   os.pathcloudpathlib.exceptionsr   	cloudpathr   r   r   r   r   r   r4   r8   r=   rD   rF   rL   rP   rS   r\   r`   ra   rb   rc   re   rf   rn   rp   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sd    




H$