
    #HJj                    B   d Z ddlm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mZmZmZmZmZ ddlZerddlmZ  ej,                  e      ZdZ	 dZd	Z G d
 de      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"y)z Implements I/O streams over FTP.    )annotationsN)FTPFTP_TLSerror_reply)IOTYPE_CHECKINGAny	TypedDictcast)TransportParams)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                  J    e Zd ZU ded<   ded<   ded<   ded<   ded<   ded	<   y
)_FTPUristrscheme
str | Noneuri_pathuserhostintportpasswordN)__name__
__module____qualname____annotations__     X/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/smart_open/ftp.pyr   r   *   s$    K


Ir    r   c                H    | xr t         j                  j                  |       S N)urllibparseunquote)texts    r!   _unquoter(   3   s    .FLL((..r    c                J   t         j                  j                  |       }|j                  t        v sJ |j                  t        |j                        t        |j                        |j                  t        |j                  xs t              t        |j                        dS )zBParse an ``ftp://`` or ``ftps://`` URI into connection components.)r   r   r   r   r   r   )r$   r%   urlsplitr   SCHEMESr(   pathusernamehostnamer   r   DEFAULT_PORTr   )uri_as_string	split_uris     r!   	parse_urir2   7   s    %%m4Iw&&&""Y^^,++,""INN2l3Y//0 r    c                    t         j                  j                  t        |       t	        t        |             }|j                  d      }|j                  d      }|dk(  }t        ||f||d|S )z?Open an FTP/FTPS URI using the given mode and transport params.r   r   r   )secure_connectiontransport_params)
smart_openutilscheck_kwargsopendictr2   pop)urimoder5   
parsed_urir   r   secure_conns          r!   open_urir@   E   sw    !!$(89!%in!5J~~j)H^^H%FF"K &)	
  r    c                    g d}| D cg c]	  }||vs| }}| j                         D ci c]  \  }}||v s|| }}}|rt        j                  d|       |S c c}w c c}}w )zDReturn the subset of `transport_params` that the FTP client accepts.)timeoutsource_addressencodingz.ignoring unsupported ftp keyword arguments: %r)itemsloggerwarning)r5   supported_keywordskunsupported_keywordsvkwargss         r!    convert_transport_params_to_argsrM   U   s{    
 (8W'7!1DV;VA'7W!1!7!7!9U!9v1QBT=Tad!9FUGI]^M XUs   	AAA A c                   t        |      }|r<t        j                  t        j                  j                        }t        dd|i|}nt        di |}	 |j                  | |       	 |j                  t        d|      t        d|             t        |t
              r|j                          |S # t        $ r t        j                  d        w xY w# t        $ r t        j                  d        w xY w)N)purposecontextz@Unable to connect to FTP server: try checking the host and port!r   zFUnable to login to FTP server: try checking the username and password!r   )rM   sslcreate_default_contextPurposeSERVER_AUTHr   r   connect	ExceptionrF   	exceptionloginr   r   
isinstanceprot_p)	r.   r-   r   r   r4   r5   rL   ssl_contextr   s	            r!   _connectr\   e   s     ..>?F009P9PQ4k4V4mFmHd#		$uh'eX)>? #w

J  [\
  abs   B1 )&C 1 C C4rbFc                   |sd}t        |      |sd}t        |      |si }t        ||||||      }	dddd}
	 |
|   \  }}|
|   \  }}|	j                  d	       |	j	                  | d
|        }|j                  t        d|            }dd}|j                  |_        ||_	        |	|_
        t        j                  ||      |_        |S # t        $ r}d|}t        |      |d}~ww xY w)a  Open a file for reading or writing via FTP/FTPS.

    Args:
        path: The path on the remote server.
        mode: Must be "rb" or "wb".
        host: The host to connect to.
        user: The username to use for the connection.
        password: The password for the specified username.
        port: The port to connect to.
        secure_connection: True for FTPS, False for FTP.
        transport_params: Additional parameters for the FTP connection.
            Currently supported parameters: timeout, source_address, encoding.

    Returns:
        A file-like object for the remote FTP/FTPS file.

    Raises:
        ValueError: If `host` or `user` is not specified, or if `mode` is unsupported.
    z'you must specify the host to connect tozyou must specify the user)RETRr]   )STORwb)APPEra   )r]   ra   abzunsupported mode: NzTYPE I r	   c                    | j                          | j                  j                          | j                  j                          y r#   )
orig_closesocketcloseconn)selfs    r!   
full_closezopen.<locals>.full_close   s*    		r    )rj   r	   returnNone)
ValueErrorr\   KeyErrorvoidcmdtransfercmdmakefiler   rh   rf   rg   ri   types
MethodType)r,   r=   r   r   r   r   r4   r5   msgri   mode_to_ftp_cmdsftp_modefile_obj_modeerrrg   fobjrk   s                    r!   r9   r9      s   : 7o)oD$h0ACSTD
'"24"8- /t4HmLL
!D623FUM :;D
 jjDODKDI!!*d3DJK#  '"4(+o3&'s   C 	C+C&&C+)r'   r   rl   r   )r0   r   rl   r   )r<   r   r=   r   r5   r   rl   IO[Any])r5   r   rl   zdict[str, Any])r.   r   r-   r   r   r   r   r   r4   boolr5   r   rl   zFTP | FTP_TLS)r,   r   r=   r   r   r   r   r   r   r   r   r   r4   r|   r5   zTransportParams | Nonerl   r{   )#__doc__
__future__r   loggingrQ   rs   urllib.parser$   ftplibr   r   r   typingr   r   r	   r
   r   smart_open.utilsr6   smart_open._typingr   	getLoggerr   rF   r+   r/   URI_EXAMPLESr   r(   r2   r@   rM   r\   r9   r   r    r!   <module>r      s.   ' "  
   , , : : 2			8	$
 i /    	
  & @ #/3>
>
> > 	>
 > > > -> >r    