o
    
iw                     @   s   d Z ddlZddlZddlZddlZddlmZm	Z	m
Z
 ddlZeeZdZ	 dZdZdd Zd	d
 Zdd Zdd Zdd ZddddeddfddZdS )z Implements I/O streams over FTP.    N)FTPFTP_TLSerror_reply)ftpftps   )zftp://username@host/path/filez&ftp://username:password@host/path/filez+ftp://username:password@host:port/path/filezftps://username@host/path/filez'ftps://username:password@host/path/filez,ftps://username:password@host:port/path/filec                 C   s   | ot j| S N)urllibparseunquote)text r   B/home/ubuntu/.local/lib/python3.10/site-packages/smart_open/ftp.py_unquote$   s   r   c                 C   sN   t j| }|jtv sJ t|jt|jt|j|j	t
|jptt|jdS )N)schemeuri_pathuserhostportpassword)r	   r
   urlsplitr   SCHEMESdictr   pathusernamehostnameintr   DEFAULT_PORTr   )uri_as_string	split_urir   r   r   	parse_uri(   s   r    c                 C   sR   t jt| t| }|d}|d}|dkrdnd}t||f||d|S )Nr   r   r   TF)secure_connectiontransport_params)
smart_openutilscheck_kwargsopenr    pop)urimoder"   
parsed_urir   r   secure_connr   r   r   open_uri5   s   

r,   c                    sD   g d  fdd| D } fdd|   D }|r td| |S )N)timeoutsource_addressencodingc                    s   g | ]}| vr|qS r   r   ).0ksupported_keywordsr   r   
<listcomp>J   s    z4convert_transport_params_to_args.<locals>.<listcomp>c                    s   i | ]\}}| v r||qS r   r   )r0   r1   vr2   r   r   
<dictcomp>K   s    z4convert_transport_params_to_args.<locals>.<dictcomp>z.ignoring unsupported ftp keyword arguments: %r)itemsloggerwarning)r"   unsupported_keywordskwargsr   r2   r    convert_transport_params_to_argsD   s   r<   c           
   
   C   s   t |}|rtjtjjd}tdd|i|}ntdi |}z|| | W n ty: }	 zt	
d |	d }	~	ww z||| W n tyV }	 zt	
d |	d }	~	ww |r]|  |S )N)purposecontextz@Unable to connect to FTP server: try checking the host and port!zFUnable to login to FTP server: try checking the username and password!r   )r<   sslcreate_default_contextPurposeSERVER_AUTHr   r   connect	Exceptionr8   errorloginr   prot_p)
r   r   r   r   r!   r"   r;   ssl_contextr   er   r   r   _connectU   s0   
rJ   rbFc                 C   s   |st d|st d|si }t||||||}dddd}	z|	| \}
}W n ty5   t d|w |	| \}
}|d ||
 d	|  }||}d
d }|j|_||_||_	t
|||_|S )ai  Open a file for reading or writing via FTP/FTPS.

    Parameters
    ----------
    path: str
        The path on the remote server
    mode: str
        Must be "rb" or "wb"
    host: str
        The host to connect to
    user: str
        The username to use for the connection
    password: str
        The password for the specified username
    port: int
        The port to connect to
    secure_connection: bool
        True for FTPS, False for FTP
    transport_params: dict
        Additional parameters for the FTP connection.
        Currently supported parameters: timeout, source_address, encoding.
    z'you must specify the host to connect tozyou must specify the user)RETRrK   )STORwb)APPErN   )rK   rN   abzunsupported mode: zTYPE I c                 S   s    |    | j  | j  d S r   )
orig_closesocketcloseconn)selfr   r   r   
full_close   s   
zopen.<locals>.full_close)
ValueErrorrJ   KeyErrorvoidcmdtransfercmdmakefilerT   rR   rS   rU   types
MethodType)r   r)   r   r   r   r   r!   r"   rU   mode_to_ftp_cmdsftp_modefile_obj_moderS   fobjrW   r   r   r   r&   m   s4    

r&   )__doc__loggingr?   urllib.parser	   smart_open.utilsr#   ftplibr   r   r   r]   	getLogger__name__r8   r   r   URI_EXAMPLESr   r    r,   r<   rJ   r&   r   r   r   r   <module>   s2   

