U
    çZßfE  ã                   @   sf   d Z ddlZddlZg ZdZG dd„ dƒZG dd„ dƒZdd	„ Zefd
d„Z	edfdd„Z
dd„ ZdS )z;
file_transfer.py - upload, download and generic file copy
é    Ni   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )Ú	LocalFilezf
    Represent a file on the local side which is to be transferred or is already
    transferred.
    c                 C   s   t j |¡| _|| _d S ©N)ÚosÚpathÚabspathÚnameÚmode)Úselfr   r   © r
   ú9/tmp/pip-unpacked-wheel-g7w5sq0v/ftputil/file_transfer.pyÚ__init__   s    zLocalFile.__init__c                 C   s   t j | j¡S ©zl
        Return `True` if the path representing this file exists. Otherwise
        return `False`.
        )r   r   Úexistsr   ©r	   r
   r
   r   r   "   s    zLocalFile.existsc                 C   s   t j | j¡S ©zL
        Return the timestamp for the last modification in seconds.
        )r   r   Úgetmtimer   r   r
   r
   r   Úmtime)   s    zLocalFile.mtimec                 C   s   dS )úP
        Return the precision of the last modification time in seconds.
        g      ð?r
   r   r
   r
   r   Úmtime_precision/   s    	zLocalFile.mtime_precisionc                 C   s   t | j| jƒS ©zL
        Return a file object for the name/path in the constructor.
        )Úopenr   r   r   r
   r
   r   Úfobj:   s    zLocalFile.fobjN©	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r
   r
   r
   r   r      s   r   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )Ú
RemoteFilezg
    Represent a file on the remote side which is to be transferred or is
    already transferred.
    c                 C   s&   || _ |j| _| j |¡| _|| _d S r   )Ú_hostr   Ú_pathr   r   r   )r	   Zftp_hostr   r   r
   r
   r   r   G   s    zRemoteFile.__init__c                 C   s   | j  | j¡S r   )r   r   r   r   r
   r
   r   r   M   s    zRemoteFile.existsc                 C   s   | j  | j¡S r   )r   r   r   r   r
   r
   r   r   T   s    zRemoteFile.mtimec                 C   s   | j  | j¡jS )r   )r   Ústatr   Z_st_mtime_precisionr   r
   r
   r   r   \   s    zRemoteFile.mtime_precisionc                 C   s   | j  | j| j¡S r   )r   r   r   r   r   r
   r
   r   r   c   s    zRemoteFile.fobjNr   r
   r
   r
   r   r   A   s   r   c                 C   s0   |   ¡ tjjkrdS |  ¡ |   ¡  | ¡ kS dS )aÄ  
    Return `True` if the source is newer than the target, else `False`.

    Both arguments are `LocalFile` or `RemoteFile` objects.

    It's assumed that the actual modification time is

      reported_mtime <= actual_mtime <= reported_mtime + mtime_precision

    i. e. that the reported mtime is the actual mtime or rounded down
    (truncated).

    For the purpose of this test the source is newer than the target if any of
    the possible actual source modification times is greater than the reported
    target modification time. In other words: If in doubt, the file should be
    transferred.

    This is the only situation where the source is _not_ considered newer than
    the target:

    |/////////////////////|              possible source mtime
                            |////////|   possible target mtime

    That is, the latest possible actual source modification time is before the
    first possible actual target modification time.
    TN)r   Úftputilr    ZUNKNOWN_PRECISIONr   )Úsource_fileÚtarget_filer
   r
   r   Úsource_is_newer_than_targetj   s    ÿr$   c                 c   s   |   |¡}|sq|V  q dS )a­  
    Return an iterator which yields the contents of the file object.

    For each iteration, at most `max_chunk_size` bytes are read from `fobj` and
    yielded as a byte string. If the file object is exhausted, then don't yield
    any more data but stop the iteration, so the client does _not_ get an empty
    byte string.

    Any exceptions resulting from reading the file object are passed through to
    the client.
    N)Úread)r   Úmax_chunk_sizeÚchunkr
   r
   r   Úchunks   s    
r(   c                 C   s.   t | |ƒD ]}| |¡ |dk	r
||ƒ q
dS )zL
    Copy data from file-like object source to file-like object target.
    N)r(   Úwrite)Úsource_fobjÚtarget_fobjr&   Úcallbackr'   r
   r
   r   Úcopyfileobj    s    
r-   c                 C   sb   |r |  ¡  pt| |ƒ}|s dS |  ¡ }z*| ¡ }zt|||d W 5 | ¡  X W 5 | ¡  X dS )aµ  
    Copy a file from `source_file` to `target_file`.

    These are `LocalFile` or `RemoteFile` objects. Which of them is a local or
    a remote file, respectively, is determined by the arguments. If
    `conditional` is true, the file is only copied if the target doesn't exist
    or is older than the source. If `conditional` is false, the file is copied
    unconditionally. Return `True` if the file was copied, else `False`.
    F)r,   T)r   r$   r   Úcloser-   )r"   r#   Zconditionalr,   Ztransfer_conditionr*   r+   r
   r
   r   Ú	copy_file®   s    
 ÿ
r/   )r   r   Zftputil.statr!   Ú__all__ZMAX_COPY_CHUNK_SIZEr   r   r$   r(   r-   r/   r
   r
   r
   r   Ú<module>   s   ))# ÿ
