o
    3gC                     @  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Zd dlZd dl	m
Z
mZ d dlmZ ejejejejdZG dd deZejdd	d
ZG dd	 d	ee
ZG dd dZdS )    )annotationsN)ABCabstractmethod)reduce)fwrxc                   @  s   e Zd ZdZdddZdS )FSUserzA special object that represents a file-system user. It derives from ``int``, so it behaves
    just like a number (``uid``/``gid``), but also have a ``.name`` attribute that holds the
    string-name of the user, if given (otherwise ``None``)
    Nc                 C  s   t | |}||_|S N)int__new__name)clsvalr   self r   I/home/garg/my-data/venv/lib/python3.10/site-packages/plumbum/path/base.pyr      s   zFSUser.__new__r   )__name__
__module____qualname____doc__r   r   r   r   r   r
      s    r
   	_PathImplPath)boundc                   @  st  e Zd ZdZdZdd Zdd
dZdd Zdd Zdd Z	dd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dd)d*Zdd,d-Zd.d/ d0d/ fd1d2Zeedd4d5Zed6d7 Zeedd8d9Zeedd:d;Zeedd<d=Zeedd>d?Zeedd@dAZeeddCdDZ eeddFdGZ!eeddHdIZ"edddMdNZ#eddOdPZ$eddQdRZ%eddTdUZ&eddWdXZ'eddYdZZ(d[d\ Z)edd]d^Z*dd_d`Z+dadb Z,eddcddZ-eddedfZ.eddhdiZ/eddkdlZ0edddpdqZ1drds Z2eddvdwZ3edxdy Z4edzd{ Z5d|d} Z6edd~dZ7edddZ8eddJddddZ9eddddZ:eddddZ;edd Z<edddZ=edd Z>e?dddZ@eddddZAedd ZBedd ZCedd ZDdd ZEedd ZFdd ZGdd ZHe?dd ZIdddZJedd ZKedd ZLdJS )r   zAn abstraction over file system paths. This class is abstract, and the two implementations
    are :class:`LocalPath <plumbum.machines.local.LocalPath>` and
    :class:`RemotePath <plumbum.path.remote.RemotePath>`.
    Tc                 C  s   d| j j d|  dS )N< >)	__class__r   r   r   r   r   __repr__'   s   zPath.__repr__r   r   other
typing.Anyreturnc                 C  
   |  |S )zJoins two pathsjoinr   r!   r   r   r   __truediv__*      
zPath.__truediv__c                 C  s*   t |tkst|tr| | S t| | S r   )typestr
isinstancer   )r   keyr   r   r   __getitem__.   s   zPath.__getitem__c                 C  r$   )VReturns a (possibly empty) list of paths that matched the glob-pattern under this path)glob)r   exprr   r   r   __floordiv__3   r)   zPath.__floordiv__c                 C  s   t |  S )z(Iterate over the files in this directory)iterlistr   r   r   r   __iter__7   s   zPath.__iter__objectboolc                 C  sN   t |tr|  | kS t |tr%| jrt| |kS t|  | kS tS r   )r,   r   	_get_infor+   CASE_SENSITIVElowerNotImplementedr'   r   r   r   __eq__;   s   

zPath.__eq__c                 C  
   | |k S r   r   r'   r   r   r   __ne__F      
zPath.__ne__c                 C     t | t |kS r   r+   r'   r   r   r   __gt__I      zPath.__gt__c                 C     t | t |kS r   rA   r'   r   r   r   __ge__L   rC   zPath.__ge__c                 C     t | t |k S r   rA   r'   r   r   r   __lt__O   rC   zPath.__lt__c                 C     t | t |kS r   rA   r'   r   r   r   __le__R   rC   zPath.__le__c                 C  s"   | j r	tt| S tt|  S r   )r9   hashr+   r:   r   r   r   r   __hash__U   s   "zPath.__hash__c                 C     t t| S r   r7   r+   r   r   r   r   __bool__X      zPath.__bool__c                 C  s   t | S )zAdded for Python 3.6 supportrA   r   r   r   r   
__fspath__[   s   zPath.__fspath__c                 C  s0   z| |j   W S  ty   | |   Y S w )zEPaths should support checking to see if an file or folder is in them.)r   existsAttributeError)r   itemr   r   r   __contains___   s
   zPath.__contains__partsc                 G     d S r   r   r   rU   r   r   r   _formf      z
Path._form   c                 C  s   |  d| S )z1Go up in ``count`` directories (the default is 1)z../r%   r   countr   r   r   upj   s   zPath.upc                 C     dS NTr   _r   r   r   <lambda>p       zPath.<lambda>c                 C  r^   r_   r   r`   r   r   r   rb   q   rc   c                 c  sD    |   D ]}||r|V  | r||r|||E dH  qdS )an  traverse all (recursive) sub-elements under this directory, that match the given filter.
        By default, the filter accepts everything; you can provide a custom filter function that
        takes a path as an argument and returns a boolean

        :param filter: the filter (predicate function) for matching results. Only paths matching
                       this predicate are returned. Defaults to everything.
        :param dir_filter: the filter (predicate function) for matching directories. Only directories
                           matching this predicate are recursed into. Defaults to everything.
        N)r4   is_dirwalk)r   filter
dir_filterpr   r   r   re   n   s   z	Path.walkr+   c                 C  r^   )z#The basename component of this pathNr   r   r   r   r   r      rc   z	Path.namec                 C  s   t jdtdd | jS )2Included for compatibility with older Plumbum codezUse .name instead   
stacklevel)warningswarnFutureWarningr   r   r   r   r   basename   s   zPath.basenamec                 C  r^   )z@The name without an extension, or the last component of the pathNr   r   r   r   r   stem   rc   z	Path.stemc                 C  r^   )z"The dirname component of this pathNr   r   r   r   r   dirname   rc   zPath.dirnamec                 C  r^   )z'The root of the file tree (`/` on Unix)Nr   r   r   r   r   root   rc   z	Path.rootc                 C  r^   )zThe drive letter (on Windows)Nr   r   r   r   r   drive   rc   z
Path.drivec                 C  r^   )zThe suffix of this fileNr   r   r   r   r   suffix   rc   zPath.suffix	list[str]c                 C  r^   )zThis is a list of all suffixesNr   r   r   r   r   suffixes   rc   zPath.suffixesr
   c                 C  r^   )zThe user that owns this path. The returned value is a :class:`FSUser <plumbum.path.FSUser>`
        object which behaves like an ``int`` (as expected from ``uid``), but it also has a ``.name``
        attribute that holds the string-name of the userNr   r   r   r   r   uid   rc   zPath.uidc                 C  r^   )zThe group that owns this path. The returned value is a :class:`FSUser <plumbum.path.FSUser>`
        object which behaves like an ``int`` (as expected from ``gid``), but it also has a ``.name``
        attribute that holds the string-name of the groupNr   r   r   r   r   gid   rc   zPath.gidNscheme
str | Nonec                 C  r^   )zJReturns a universal resource identifier. Use ``scheme`` to force a scheme.Nr   )r   rz   r   r   r   as_uri   rc   zPath.as_uric                 C  rV   r   r   r   r   r   r   r8      rY   zPath._get_infoc                 G  r^   )z(Joins this path with any number of pathsNr   rW   r   r   r   r&      rc   z	Path.joinbuiltins.list[_PathImpl]c                 C  r^   )z#Returns the files in this directoryNr   r   r   r   r   r4      rc   z	Path.listtyping.Iterable[_PathImpl]c                 C  r^   )z[Returns an iterator over the directory. Might be slightly faster on Python 3.5 than .list()Nr   r   r   r   r   iterdir   rc   zPath.iterdirc                 C  r^   )zAReturns ``True`` if this path is a directory, ``False`` otherwiseNr   r   r   r   r   rd      rc   zPath.is_dirc                 C     t jdtdd |  S )ri   zUse .is_dir() insteadrj   rk   )rm   rn   ro   rd   r   r   r   r   isdir      z
Path.isdirc                 C  r^   )zDReturns ``True`` if this path is a regular file, ``False`` otherwiseNr   r   r   r   r   is_file   rc   zPath.is_filec                 C  r   )ri   zUse .is_file() insteadrj   rk   )rm   rn   ro   r   r   r   r   r   isfile   r   zPath.isfilec                 C  r   )ri   zUse is_symlink insteadrj   rk   )rm   rn   ro   
is_symlinkr   r   r   r   islink   r   zPath.islinkc                 C  r^   )zEReturns ``True`` if this path is a symbolic link, ``False`` otherwiseNr   r   r   r   r   r      rc   zPath.is_symlinkc                 C  r^   )z9Returns ``True`` if this path exists, ``False`` otherwiseNr   r   r   r   r   rQ      rc   zPath.existsos.stat_resultc                 C  r^   )zReturns the os.stats for a fileNr   r   r   r   r   stat   rc   z	Path.statr   c                 C  r^   )z%Returns a path with the name replacedNr   )r   r   r   r   r   	with_name   rc   zPath.with_nameru   depth
int | Nonec                 C  r^   )a#  Returns a path with the suffix replaced. Up to last ``depth`` suffixes will be
        replaced. None will replace all suffixes. If there are less than ``depth`` suffixes,
        this will replace all suffixes. ``.tar.gz`` is an example where ``depth=2`` or
        ``depth=None`` is usefulNr   )r   ru   r   r   r   r   with_suffix   rc   zPath.with_suffixc                 C  s   t | jdkr	| S | |S )z|Adds a suffix if one does not currently exist (otherwise, no change). Useful
        for loading files with a default suffixr   )lenrw   r   )r   ru   r   r   r   preferred_suffix   s   zPath.preferred_suffixpatternstr | typing.Iterable[str]c                 C  r^   )r/   Nr   )r   r   r   r   r   r0      rc   z	Path.globc                 C  r^   )z/Deletes this path (recursively, if a directory)Nr   r   r   r   r   delete  rc   zPath.deletec                 C  r^   )z'Moves this path to a different locationNr   r   dstr   r   r   move
  rc   z	Path.movec                 C  s   |  |  | S )zDRenames this path to the ``new name`` (only the basename is changed))r   r]   )r   newnamer   r   r   rename  s   zPath.renamec                 C  r^   )zCopies this path (recursively, if a directory) to the destination path "dst".
        Raises TypeError if dst exists and override is False.
        Will overwrite if override is True.
        Will silently fail to copy if override is None (the default).Nr   )r   r   overrider   r   r   copy  rc   z	Path.copy  c                 C  r^   )aA  
        Creates a directory at this path.

        :param mode: **Currently only implemented for local paths!** Numeric mode to use for directory
                     creation, which may be ignored on some systems. The current implementation
                     reproduces the behavior of ``os.mkdir`` (i.e., the current umask is first masked
                     out), but this may change for remote paths. As with ``os.mkdir``, it is recommended
                     to call :func:`chmod` explicitly if you need to be sure.
        :param parents: If this is true (the default), the directory's parents will also be created if
                        necessary.
        :param exist_ok: If this is true (the default), no exception will be raised if the directory
                         already exists (otherwise ``OSError``).

        Note that the defaults for ``parents`` and ``exist_ok`` are the opposite of what they are in
        Python's own ``pathlib`` - this is to maintain backwards-compatibility with Plumbum's behaviour
        from before they were implemented.
        Nr   )r   modeparentsexist_okr   r   r   mkdir  rc   z
Path.mkdirr   )encodingr   r   	io.IOBasec                C  r^   )zopens this path as a fileNr   )r   r   r   r   r   r   open-  rc   z	Path.openc                 C  r^   )zreturns the contents of this file as a ``str``. By default the data is read
        as text, but you can specify the encoding, e.g., ``'latin1'`` or ``'utf8'``Nr   )r   r   r   r   r   read1  rc   z	Path.readdatatyping.AnyStrNonec                 C  r^   )zwrites the given data to this file. By default the data is written as-is
        (either text or binary), but you can specify the encoding, e.g., ``'latin1'``
        or ``'utf8'``Nr   )r   r   r   r   r   r   write6  rc   z
Path.writec                 C  r^   )z=Update the access time. Creates an empty file if none exists.Nr   r   r   r   r   touch<  rc   z
Path.touchc                 C  r^   )a  Change ownership of this path.

        :param owner: The owner to set (either ``uid`` or ``username``), optional
        :param group: The group to set (either ``gid`` or ``groupname``), optional
        :param recursive: whether to change ownership of all contained files and subdirectories.
                          Only meaningful when ``self`` is a directory. If ``None``, the value
                          will default to ``True`` if ``self`` is a directory, ``False`` otherwise.
        Nr   )r   ownergroup	recursiver   r   r   chown@  rc   z
Path.chownc                 C  r^   )zeChange the mode of path to the numeric mode.

        :param mode: file mode as for os.chmod
        Nr   r   r   r   r   r   chmodK  rc   z
Path.chmodc                   s:    d u rt  t| trttj fdd|  D dS | S )Nc                   s   g | ]} | qS r   r   ).0mflagsr   r   
<listcomp>X  s    z.Path._access_mode_to_flags.<locals>.<listcomp>r   )FLAGSr,   r+   r   operatoror_r:   )r   r   r   r   r   _access_mode_to_flagsR  s
   
 zPath._access_mode_to_flagsr   	int | strc                 C  r^   )a
  Test file existence or permission bits

        :param mode: a bitwise-or of access bits, or a string-representation thereof:
                     ``'f'``, ``'x'``, ``'r'``, ``'w'`` for ``os.F_OK``, ``os.X_OK``,
                     ``os.R_OK``, ``os.W_OK``
        Nr   r   r   r   r   access\  rc   zPath.accessc                 C  r^   )z_Creates a hard link from ``self`` to ``dst``

        :param dst: the destination path
        Nr   r   r   r   r   linke  rc   z	Path.linkc                 C  r^   )zcCreates a symbolic link from ``self`` to ``dst``

        :param dst: the destination path
        Nr   r   r   r   r   symlinkl  rc   zPath.symlinkc                 C  r^   )zDeletes a symbolic linkNr   r   r   r   r   unlinks  rc   zPath.unlinkc                 O  s<   g }| }||j kr||j |j }||j ks	|ddd S )zSplits the path on directory separators, yielding a list of directories, e.g,
        ``"/var/log/messages"`` will yield ``['var', 'log', 'messages']``.
        N)rr   appendr   )r   _args_kargsrU   pathr   r   r   splitw  s   

z
Path.splitc                 C  s   | j | j g|  R S )zNSplits the directory into parts, including the base directory, returns a tuple)rt   rs   r   r   r   r   r   rU     s   z
Path.partsc                 C  sd   t |tr
| |}|  }| }tttdd t||}t	dgt||  ||d  S )ak  Computes the "relative path" require to get from ``source`` to ``self``. They satisfy the invariant
        ``source_path + (target_path - source_path) == target_path``. For example::

            /var/log/messages - /var/log/messages = []
            /var/log/messages - /var              = [log, messages]
            /var/log/messages - /                 = [var, log, messages]
            /var/log/messages - /var/tmp          = [.., log, messages]
            /var/log/messages - /opt              = [.., var, log, messages]
            /var/log/messages - /opt/lib          = [.., .., var, log, messages]
        c                 S  s   | d | d kS )Nr   rZ   r   )rh   r   r   r   rb     s    z"Path.relative_to.<locals>.<lambda>z..N)
r,   r+   rX   r   r   r4   	itertools	takewhilezipRelativePath)r   sourcerU   	baseparts	ancestorsr   r   r   relative_to  s   

"zPath.relative_toc                 C  r$   )z#Same as ``self.relative_to(other)``)r   r'   r   r   r   __sub__  r)   zPath.__sub__c                   s,   t | tr	 | S  fdd| D }t|S )zNApplies a glob string or list/tuple/iterable to the current path, using ``fn``c                   s   h | ]} |D ]}|qqS r   r   )r   single_patternvaluefnr   r   	<setcomp>  s    zPath._glob.<locals>.<setcomp>)r,   r+   sorted)r   r   resultsr   r   r   _glob  s   
z
Path._globFc                 C  s   | S )zAdded to allow pathlib like syntax. Does nothing since
        Plumbum paths are always absolute. Does not (currently) resolve
        symlinks.r   )r   strictr   r   r   resolve  s   zPath.resolvec                   s,    fddt t jd ddD }t|S )z"Pathlib like sequence of ancestorsc                 3  s4    | ]}t  fd d jd|  jd V  qdS )c                   s     | | S r   )rX   )r	   yr   r   r   rb     s    z(Path.parents.<locals>.<genexpr>.<lambda>Nr   )r   rU   )r   ir   r   r   	<genexpr>  s
    "
zPath.parents.<locals>.<genexpr>rZ   r   r   )ranger   rU   tuple)r   as_listr   r   r   r     s   
zPath.parentsc                 C  s
   | j d S )z Pathlib like parent of the path.r   )r   r   r   r   r   parent  s   
zPath.parent)r   r   r!   r"   r#   r   )r!   r6   r#   r7   )r   r   rU   r"   r#   r   rZ   )r#   r+   )r   r   r#   r   )r#   rv   )r#   r
   r   )rz   r{   r#   r+   )r#   r"   )r   r   r#   r}   )r   r   r#   r~   )r#   r7   )r#   r   )r   r   r   r"   r#   r   )r   r   ru   r+   r   r   r#   r   )r   r   r   r   r#   r}   )r   TT)r   )r   r+   r   r{   r#   r   )r   r{   r#   r+   )r   r   r   r{   r#   r   )NNN)r   )r   r   r#   r7   )F)Mr   r   r   r   r9   r    r(   r.   r2   r5   r<   r>   rB   rE   rG   rI   rK   rN   rP   rT   r   rX   r]   re   propertyr   rp   rq   rr   rs   rt   ru   rw   rx   ry   r|   r8   r&   r4   r   rd   r   r   r   r   r   rQ   r   r   r   r   r0   r   r   r   r   r   r   r   r   r   r   r   staticmethodr   r   r   r   r   r   rU   r   r   r   r   r   r   r   r   r   r   r      s    










	






c                   @  s   e Zd Z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dd Zdd Zdd Zdd Zdd Zd$dd Zd!d" Zd#S )%r   a  
    Relative paths are the "delta" required to get from one path to another.
    Note that relative path do not point at anything, and thus are not paths.
    Therefore they are system agnostic (but closed under addition)
    Paths are always absolute and point at "something", whether existent or not.

    Relative paths are created by subtracting paths (``Path.relative_to``)
    c                 C  s
   || _ d S r   rU   rW   r   r   r   __init__  r?   zRelativePath.__init__c                 C  s   d | jS )N/r&   rU   r   r   r   r   __str__  rO   zRelativePath.__str__c                 C  
   t | jS r   )r3   rU   r   r   r   r   r5     r?   zRelativePath.__iter__c                 C  r   r   )r   rU   r   r   r   r   __len__  r?   zRelativePath.__len__c                 C  s
   | j | S r   r   )r   indexr   r   r   r.     r?   zRelativePath.__getitem__c                 C  s   d| j dS )NzRelativePath()r   r   r   r   r   r      s   zRelativePath.__repr__c                 C  s   t | t |kS r   rA   r'   r   r   r   r<     rC   zRelativePath.__eq__c                 C  r=   r   r   r'   r   r   r   r>     r?   zRelativePath.__ne__c                 C  r@   r   rA   r'   r   r   r   rB     rC   zRelativePath.__gt__c                 C  rD   r   rA   r'   r   r   r   rE     rC   zRelativePath.__ge__c                 C  rF   r   rA   r'   r   r   r   rG     rC   zRelativePath.__lt__c                 C  rH   r   rA   r'   r   r   r   rI     rC   zRelativePath.__le__c                 C  rL   r   )rJ   r+   r   r   r   r   rK     rO   zRelativePath.__hash__c                 C  rL   r   rM   r   r   r   r   rN     rO   zRelativePath.__bool__rZ   c                 C  s   t | jd |  S r   )r   rU   r[   r   r   r   r]     s   zRelativePath.upc                 C  s   |j | j S r   r   )r   r   r   r   r   __radd__  rO   zRelativePath.__radd__Nr   )r   r   r   r   r   r   r5   r   r.   r    r<   r>   rB   rE   rG   rI   rK   rN   r]   r   r   r   r   r   r     s$    	
r   )
__future__r   builtinsior   r   ostypingrm   abcr   r   	functoolsr   F_OKW_OKR_OKX_OKr   r   r
   TypeVarr   r+   r   r   r   r   r   r   <module>   s$       "