o
    3g)                     @  s  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mZ d dlmZ d dlmZmZ d dlmZ zd dlmZmZ d dlmZmZ W n eyk   d	d
 Zdd Zdd Zdd ZY nw edZe Z G dd deZ!G dd de!Z"dS )    )annotationsN)contextmanager)IS_WIN32)FSUserPath)
RemotePath)getgrgidgetgrnam)getpwnamgetpwuidc                 C     dS NN _xr   r   J/home/garg/my-data/venv/lib/python3.10/site-packages/plumbum/path/local.pyr         r   c                 C  r   r   r   r   r   r   r   r      r   r   c                 C     t d)Nz`getpwnam` not supportedOSErrorr   r   r   r   r
         r
   c                 C  r   )Nz`getgrnam` not supportedr   r   r   r   r   r	      r   r	   zplumbum.localc                      s  e Zd ZdZe Z fddZedd Zd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 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ed(d) ZdWd+d,Zd-d. Zd/d0 Zd1d2 ZdXd4d5ZdYd8d9Z dZd;d<Z!d[d=d>Z"d\d?d@Z#dAdB Z$d]dCdDZ%dEdF Z&d^dHdIZ'dJdK Z(dLdM Z)dNdO Z*d_dQdRZ+edSdT Z,edUdV Z-  Z.S )`	LocalPathz*The class implementing local-machine pathsc                   s   t |dkrt|d | rt|d ts|d S |stdtdd |D r.td|t | tj	tjj
dd |D  S )N   r   z/At least one path part is required (none given)c                 s  s    | ]}t |tV  qd S r   )
isinstancer   ).0pathr   r   r   	<genexpr>8       z$LocalPath.__new__.<locals>.<genexpr>z%LocalPath cannot be constructed from c                 s  s    | ]}t |V  qd S r   str)r   pr   r   r   r   ;       )lenr   LocalWorkdir	TypeErroranysuper__new__osr   normpathjoin)clsparts	__class__r   r   r(   /   s   zLocalPath.__new__c                 C  s   t | S r   r   selfr   r   r   _path>      zLocalPath._pathc                 C  s   | j S r   )r2   r0   r   r   r   	_get_infoB   s   zLocalPath._get_infoc                 G  s   t | S r   r   )r1   r-   r   r   r   _formE   r   zLocalPath._formc                 C     t jt| S r   )r)   r   basenamer    r0   r   r   r   nameH   s   zLocalPath.namec                 C  s   t tjt| S r   )r   r)   r   dirnamer    r0   r   r   r   r:   L      zLocalPath.dirnamec                 C     t jt| d S )Nr   )r)   r   splitextr    r0   r   r   r   suffixP   r;   zLocalPath.suffixc                 C  s<   g }t | }	 tj|\}}|r|| ntt|S qr   )r    r)   r   r=   appendlistreversed)r1   extsbaseextr   r   r   suffixesT   s   zLocalPath.suffixesc                 C      |   j}t|d }t||S Nr   )statst_uidr   r   )r1   uidr9   r   r   r   rJ   _      

zLocalPath.uidc                 C  rF   rG   )rH   st_gidr   r   )r1   gidr9   r   r   r   rM   e   rK   zLocalPath.gidc                 G  s   t | g|R  S r   r5   )r1   othersr   r   r   r+   k      zLocalPath.joinc                   s    fddt t D S )Nc                   s   g | ]} | qS r   r   r   fnr0   r   r   
<listcomp>o   s    z"LocalPath.list.<locals>.<listcomp>)r)   listdirr    r0   r   r0   r   r@   n   s   zLocalPath.listc                   sN   z fddt t D W S  ty&    fddt t D  Y S w )Nc                 3  s    | ]} |j  V  qd S r   )r9   rP   r0   r   r   r   s   r   z$LocalPath.iterdir.<locals>.<genexpr>c                 3  s    | ]} | V  qd S r   r   rP   r0   r   r   r   u   r"   )r)   scandirr    AttributeErrorrS   r0   r   r0   r   iterdirq   s
    zLocalPath.iterdirc                 C  r7   r   )r)   r   isdirr    r0   r   r   r   is_dirw   rO   zLocalPath.is_dirc                 C  r7   r   )r)   r   isfiler    r0   r   r   r   is_filez   rO   zLocalPath.is_filec                 C  r7   r   )r)   r   islinkr    r0   r   r   r   
is_symlink}   rO   zLocalPath.is_symlinkc                 C  r7   r   )r)   r   existsr    r0   r   r   r   r]      rO   zLocalPath.existsc                 C  s   t t| S r   )r)   rH   r    r0   r   r   r   rH         zLocalPath.statc                 C  s   t | j| S r   )r   r:   )r1   r9   r   r   r   	with_name   r^   zLocalPath.with_namec                 C  s   | j tjjd S rG   )r9   rsplitr)   r   extsepr0   r   r   r   stem   r;   zLocalPath.stemr   c                 C  s   |r	| tjjr|tjjkrtd|| j}|d u r"t| jnt|t| j}t	|D ]
}tj
|\}}q.t| j||  S )NzInvalid suffix )
startswithr)   r   ra   
ValueErrorr9   r#   rE   minranger=   r   r:   )r1   r>   depthr9   _r   r   r   with_suffix   s   "zLocalPath.with_suffixc                   s     | fddS )Nc              	     s(   dd t  tjt t | D S )Nc                 S  s   g | ]}t |qS r   r5   )r   mr   r   r   rR      s    z4LocalPath.glob.<locals>.<lambda>.<locals>.<listcomp>)globr)   r   r+   escaper    )patr0   r   r   <lambda>   s    z LocalPath.glob.<locals>.<lambda>)_glob)r1   patternr   r0   r   rk      s   
zLocalPath.globc              
   C  sp   |   sd S |  rtt|  d S z
tt|  W d S  ty7 } z|jtj	kr, W Y d }~d S d }~ww r   )
r]   rX   shutilrmtreer    r)   remover   errnoENOENTr1   exr   r   r   delete   s   zLocalPath.deletec                 C  s:   t |trtd|  d|tt| t| t|S )NzCannot move local path  to )r   r   r%   rq   mover    r   )r1   dstr   r   r   rz      s   
zLocalPath.moveNc                 C  s   t |trtd|  d|t|}|du r| rtd|r%|  |  r5tt	| t	| |S t|j
}| sB|  tt	| t	| |S )NzCannot copy local path ry   Fz*File exists and override was not specified)r   r   r%   r   r]   rx   rX   rq   copytreer    r:   mkdircopy2)r1   r{   overridedst_dirr   r   r   copy   s   

zLocalPath.copy  Tc              
   C  sz   |   r|s;z|rtt| | W d S tt| | W d S  ty: } z|jtjks.|s/ W Y d }~d S d }~ww d S r   )r]   r)   makedirsr    r}   r   rt   EEXIST)r1   modeparentsexist_okrw   r   r   r   r}      s   zLocalPath.mkdirrc                 C  s   t t| ||dS )Nencoding)openr    )r1   r   r   r   r   r   r      s
   zLocalPath.openc                 C  sj   |r
d|vr
|d }|  |}| }|r"||W  d    S |W  d    S 1 s.w   Y  d S )Nb)r   readdecode)r1   r   r   fdatar   r   r   r      s   $zLocalPath.readc                 C  sb   |r| |}|d u rt|trdnd}| |}|| W d    d S 1 s*w   Y  d S )Nwwb)encoder   r    r   write)r1   r   r   r   r   r   r   r   r      s   
"zLocalPath.writec                 C  sH   t t| ddd tt| d  W d    d S 1 sw   Y  d S )Nazutf-8r   )r   r    r)   utimer0   r   r   r   touch   s   "zLocalPath.touchc                 C  s   t tds	td|d u r| jnt|tr|nt|d }|d u r$| jnt|tr+|nt|d }t	t
| || |sD|d u rT|  rV|  D ]}t	t
||| qHd S d S d S )Nchownzos.chown() not supported   )hasattrr)   r   rJ   r   intr
   rM   r	   r   r    rX   walk)r1   ownergroup	recursiverJ   rM   subpathr   r   r   r      s    
zLocalPath.chownc                 C  s&   t tds	tdtt| | d S )Nchmodzos.chmod() not supported)r   r)   r   r   r    r1   r   r   r   r   r      s   
zLocalPath.chmodr   c                 C  s   t t| | |S r   )r)   accessr    _access_mode_to_flagsr   r   r   r   r     s   zLocalPath.accessc                 C  s   t |trtd|  d|ttdr tt| t| d S ddlm} | 	 r;|d ddd	d
t|t|  d S |d ddd
t|t|  d S )Nz)Cannot create a hardlink from local path ry   linkr   localcmd/Cmklink/Dz/H)
r   r   r%   r   r)   r   r    plumbum.machines.localr   rX   r1   r{   r   r   r   r   r     s   

" zLocalPath.linkc                 C  s   t |trtd|  d|ttdr tt| t| d S ddlm} | 	 r:|d ddd	t|t|  d S |d ddt|t|  d S )
Nz(Cannot create a symlink from local path ry   symlinkr   r   r   r   r   r   )
r   r   r%   r   r)   r   r    r   r   rX   r   r   r   r   r     s   

 zLocalPath.symlinkc              
   C  sp   zt tds
|  stt|  W d S tt|  W d S  ty7 } z|jtjkr, W Y d }~d S d }~ww )Nr   )	r   r)   rX   unlinkr    rmdirr   rt   ru   rv   r   r   r   r   %  s   zLocalPath.unlinkfilec                 C  s   t t|d tt| S )N:)urlparseurljoinr    urllibpathname2url)r1   schemer   r   r   as_uri1  s   zLocalPath.as_uric                 C  r<   rG   )r)   r   
splitdriver    r0   r   r   r   drive4  r;   zLocalPath.drivec                 C  s   t jjS r   )r)   r   sepr0   r   r   r   root8  r3   zLocalPath.root)r   r   )r   TT)r   N)Nr   )NN)NNN)r   )r   )/__name__
__module____qualname____doc__r   CASE_SENSITIVEr(   propertyr2   r4   r6   r9   r:   r>   rE   rJ   rM   r+   r@   rV   rX   rZ   r\   r]   rH   r_   rb   ri   rk   rx   rz   r   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r.   r   r   *   sh    









		




	



r   c                      sD   e Zd ZdZdd Z fddZdd Zdd	 Zed
d Z	  Z
S )r$   zWorking directory manipulatorc                 C  r   )Nzunhashable type)r%   r0   r   r   r   __hash__@  r   zLocalWorkdir.__hash__c                   s   t  | t S r   )r'   r(   r)   getcwd)r,   r.   r   r   r(   C  s   zLocalWorkdir.__new__c                 C  s:   t |trtd|td| tt| |  S )zChanges the current working directory to the given one

        :param newdir: The destination director (a string or a ``LocalPath``)
        znewdir cannot be zChdir to %s)	r   r   r%   loggerdebugr)   chdirr    r/   )r1   newdirr   r   r   r   F  s
   
zLocalWorkdir.chdirc                 C  s
   t | jS )z?Returns the current working directory as a ``LocalPath`` object)r   r2   r0   r   r   r   getpathQ  s   
zLocalWorkdir.getpathc              	   c  s6    | j }| |}z|V  W | | dS | | w )zA context manager used to ``chdir`` into a directory and then ``chdir`` back to
        the previous location; much like ``pushd``/``popd``.

        :param newdir: The destination directory (a string or a ``LocalPath``)
        N)r2   r   )r1   r   prevr   r   r   __call__U  s   
zLocalWorkdir.__call__)r   r   r   r   r   r(   r   r   r   r   r   r   r   r.   r   r$   =  s    r$   )#
__future__r   rt   rk   loggingr)   rq   urllib.parseparser   urllib.requestrequestr   
contextlibr   plumbum.libr   plumbum.path.baser   r   plumbum.path.remoter   grpr   r	   pwdr
   r   ImportError	getLoggerr   object_EMPTYr   r$   r   r   r   r   <module>   s6    
  