U
    Zf                     @   sB   d Z ddlZddlZddlZddlZddlZg ZG dd dZdS )z3
ftputil.path - simulate `os.path` for FTP servers
    Nc                   @   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S )_Pathz
    Support class resembling `os.path`, accessible from the `FTPHost` object,
    e. g. as `FTPHost().path.abspath(path)`.

    Hint: substitute `os` with the `FTPHost` object.
    c                 C   sf   || _ |j| _t}|j| _|j| _|j| _|j| _|j| _|j| _|j	| _	|j
| _
|j| _|j| _d S )N)_host	_encoding	posixpathbasenamecommonprefixdirnameisabsjoinnormcasenormpathsplit
splitdrivesplitext)selfhostpp r   0/tmp/pip-unpacked-wheel-g7w5sq0v/ftputil/path.py__init__!   s    z_Path.__init__c                 C   sP   |}t jj|| jd}| |s2| | j |}t jt	
|| || jS )z*
        Return an absolute path.
        encoding)ftputiltoolas_str_pathr   r	   r
   r   getcwdZsame_string_type_asosfspathr   )r   pathZoriginal_pathr   r   r   abspath4   s    
  z_Path.abspathc                 C   sF   |dkrdS z| j j|dd}|dk	W S  tjjk
r@   Y dS X dS )z1
        Return true if the path exists.
             FZ_exception_for_missing_pathNT)r   lstatr   errorRootDirErrorr   r   Zlstat_resultr   r   r   existsB   s    
z_Path.existsc                 C   s   t j| | j|jS )z
        Return the timestamp for the last modification for `path` as a float.

        This will raise `PermanentError` if the path doesn't exist, but maybe
        other exceptions depending on the state of the server (e. g. timeout).
        )r   r   raise_for_empty_pathr   statst_mtimer   r   r   r   r   getmtimeN   s    z_Path.getmtimec                 C   s   t j| | j|jS )z
        Return the size of the `path` item as an integer.

        This will raise `PermanentError` if the path doesn't exist, but maybe
        raise other exceptions depending on the state of the server (e. g.
        timeout).
        )r   r   r)   r   r*   st_sizer,   r   r   r   getsizeX   s    z_Path.getsizec                 C   s   |dkst |dkr d}tj}n
d}tj}tjj|| jd}| || j	
 krT|S z| j	j|dd}W n4 tjjk
r   Y dS  tjjk
r   | Y S X |dkrdS ||jS dS )a  
        Return `True` if `path` represents the file system entity described by
        `dir_or_file` ("dir" or "file").

        Return `False` if `path` isn't a directory or file, respectively or if
        `path` leads to an infinite chain of links.
        )dirfiler0   TFr   r#   N)AssertionErrorr*   S_ISDIRS_ISREGr   r   r   r   r   r   r   r%   ZRecursiveLinksErrorr&   st_mode)r   r   Zdir_or_fileZshould_look_for_dirZstat_functionstat_resultr   r   r   _is_file_system_entitym   s$    
z_Path._is_file_system_entityc                 C   s   |dkrdS |  |dS )z
        Return true if the `path` exists and corresponds to a directory (no
        link).

        A non-existing path does _not_ cause a `PermanentError`, instead return
        `False`.
        r    Fr0   r7   r,   r   r   r   isdir   s    z_Path.isdirc                 C   s   |dkrdS |  |dS )z
        Return true if the `path` exists and corresponds to a regular file (no
        link).

        A non-existing path does _not_ cause a `PermanentError`, instead return
        `False`.
        r    Fr1   r8   r,   r   r   r   isfile   s    z_Path.isfilec                 C   sj   t jj|| jd}|dkrdS z| jj|dd}W n t jjk
rL   Y dS X |dkrZdS t	|j
S dS )z
        Return true if the `path` exists and is a link.

        A non-existing path does _not_ cause a `PermanentError`, instead return
        `False`.
        r   r!   Fr#   N)r   r   r   r   r   r$   r%   r&   r*   S_ISLNKr5   r'   r   r   r   islink   s    z_Path.islinkc              	   C   s   t jj|dd t jj|| jd}z| j|}W n tk
rH   Y dS X |||| |D ]X}| ||}z| j	|}W n tk
r   Y qZY nX t
|t
j rZ| ||| qZdS )a  
        Directory tree walk with callback function.

        For each directory in the directory tree rooted at top
        (including top itself, but excluding "." and ".."), call
        func(arg, dirname, fnames). dirname is the name of the
        directory, and fnames a list of the names of the files and
        subdirectories in dirname (excluding "." and "..").  func may
        modify the fnames list in-place (e.g. via del or slice
        assignment), and walk will only recurse into the
        subdirectories whose names remain in fnames; this can be used
        to implement a filter, or to impose a specific order of
        visiting.  No semantics are defined for, or required of, arg,
        beyond that arg is always passed to func.  It can be used,
        e.g., to pass a filename pattern, or a mutable object designed
        to accumulate statistics.  Passing None for arg is common.
        top)Zpath_argument_namer   N)r   r   r)   r   r   r   listdirOSErrorr
   r$   r*   r3   ST_MODEwalk)r   r=   funcargnamesnamer6   r   r   r   rA      s    
z
_Path.walkN)__name__
__module____qualname____doc__r   r   r(   r-   r/   r7   r9   r:   r<   rA   r   r   r   r   r      s   

#r   )	rI   r   r   r*   Zftputil.errorr   Zftputil.tool__all__r   r   r   r   r   <module>   s   