U
    Zf                     @   sN   d Z ddlZg ZzddlZW n ek
r4   dZY nX ejZG dd dZdS )z=
ftputil.file - support for file-like objects on FTP servers
    Nc                   @   sd   e Zd ZdZdZdd Zddd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 )FTPFilez
    Represents a file-like object associated with an FTP host. File and socket
    are closed appropriately if the `close` method is called.
       c                 C   s$   || _ |j| _d| _d| _d| _dS )z!Construct the file(-like) object.TN)Z_host_sessionclosed_conn_fobj)selfhost r
   0/tmp/pip-unpacked-wheel-g7w5sq0v/ftputil/file.py__init__!   s
    zFTPFile.__init__N)restc             	   C   s
  |dkrt dd|kr$tjd|dkr>tjd|d|krVd|krVtd	d|k}d
|k}	|s~|dk	r~tjdd}
d|
}tjj | j	| W 5 Q R X |	rdnd}d||}tjj | j
||| _W 5 Q R X | jj|||||d| _d| _dS )z
        Open the remote file with given path name and mode.

        Contrary to the `open` builtin, this method returns `None`, instead
        this file object is modified in-place.
        Nz'open() argument 2 must be str, not Noneazappend mode not supported)rrbrtwwbwtzinvalid mode '{}'btz'can't have text and binary mode at oncer   z,`rest` argument can't be used for text filesIzTYPE {}ZRETRZSTORz{} {})	bufferingencodingerrorsnewlineF)	TypeErrorftputilerror
FTPIOErrorformat
ValueErrorZCommandNotImplementedErrorftplib_error_to_ftp_io_errorr   voidcmdZtransfercmdr   makefiler   r   )r   pathmoder   r   r   r   r   Zis_binary_modeZis_read_modeZtransfer_typecommandZcommand_typer
   r
   r   _open+   s<    


    zFTPFile._openc                 C   s   | S )z)
        Return a file iterator.
        r
   r   r
   r
   r   __iter__f   s    zFTPFile.__iter__c                 C   s   |   }|r|S tdS )zV
        Return the next line or raise `StopIteration`, if there are no more.
        N)readlineStopIteration)r   liner
   r
   r   __next__l   s    zFTPFile.__next__c                 C   s   | S )Nr
   r)   r
   r
   r   	__enter__z   s    zFTPFile.__enter__c                 C   s   |    dS )NF)close)r   exc_typeexc_valexc_tbr
   r
   r   __exit__   s    zFTPFile.__exit__c                 C   s*   |d  krt| j|S td|dS )z
        Handle requests for attributes unknown to `FTPFile` objects: delegate
        the requests to the contained file object.
        zgencoding flush isatty fileno read readline readlines seek tell truncate name softspace write writelinesz&'FTPFile' object has no attribute '{}'N)splitgetattrr   AttributeErrorr    )r   	attr_namer
   r
   r   __getattr__   s
    zFTPFile.__getattr__c              
   C   s
  | j r
dS | jj }z| j  d| _tjj	. t
dk	rPt| jt
rP| j  | j  W 5 Q R X | jj| j z"tjj	 | j  W 5 Q R X W nT tjjk
r } z2t|}|dd }| d dkr|dkrڂ W 5 d}~X Y nX W 5 | jj| d| _ X dS )z&
        Close the `FTPFile`.
        NT   r   z	timed out)Z150Z426Z450Z451)r   r   sock
gettimeout
settimeoutr   r0   r   r   r"   	SSLSocket
isinstancer   unwrap_close_timeoutZvoidrespr   str
splitlines)r   Zold_timeoutexcZ
error_coder
   r
   r   r0      s*    



zFTPFile.closec                 C   s   t dd S )Nzcannot serialize FTPFile object)r   r)   r
   r
   r   __getstate__   s    zFTPFile.__getstate__)NNNN)__name__
__module____qualname____doc__rA   r   r(   r*   r.   r/   r4   r9   r0   rE   r
   r
   r
   r   r      s"       	;
-r   )rI   Zftputil.errorr   __all__sslImportErrorr>   r   r
   r
   r
   r   <module>   s   
