
    OOId*                         d dl Z d dlZd dlmZmZmZ d dlmZ ddlm	Z	m
Z
 ddlmZmZ  G d de
          Z G d	 d
e          Z G d de	          ZddZdS )    N)FTPError
error_perm)Any   )AbstractBufferedFileAbstractFileSystem)infer_storage_options
isfilelikec            	            e Zd ZdZdZdZdZ	 	 	 	 	 	 	 d  fd	Zd	 Ze	d
             Z
ed             Zd!dZd Zd Zd" fd	Z	 	 	 	 d#dZd Zd$dZd!dedededdfdZd%dededdfdZd Zd Zd Zd& fd	Z xZS )'FTPFileSystemzA filesystem over classic FTP/Fftp   N   c	                      t          t          |           j        di |	 || _        || _        |pd| _        |||f| _        || _        ||| _        nd| _        | 	                                 dS )ae  
        You can use _get_kwargs_from_urls to get some kwargs from
        a reasonable FTP url.

        Authentication will be anonymous if username/password are not
        given.

        Parameters
        ----------
        host: str
            The remote server name/ip to connect to
        port: int
            Port to connect with
        username: str or None
            If authenticating, the user's identifier
        password: str of None
            User's password on the server, if using
        acct: str or None
            Some servers also need an "account" string for auth
        block_size: int or None
            If given, the read-ahead or write buffer size.
        tempdir: str
            Directory on remote to put temporary files when in a transaction
        timeout: int
            Timeout of the ftp connection in seconds
        z/tmpNi    )
superr   __init__hostporttempdircredtimeout	blocksize_connect)selfr   r   usernamepasswordacct
block_sizer   r   kwargs	__class__s             :lib/python3.11/site-packages/fsspec/implementations/ftp.pyr   zFTPFileSystem.__init__   s    L 	,mT""+55f555		(&h,	!'DNN"DN    c                     t          | j                  | _        | j                            | j        | j                    | j        j        | j          d S )N)r   )r   r   r   connectr   r   loginr   r   s    r$   r   zFTPFileSystem._connectC   sK    t|,,,DI...	""""r%   c                 ~    dt          |          d                             d                              d          z   S )Nr   path)r
   lstriprstrip)clsr+   s     r$   _strip_protocolzFTPFileSystem._strip_protocolH   s7    *4008??DDKKCPPPPr%   c                 |    t          |           }|                    dd            |                    dd            |S )Nr+   protocol)r
   pop)urlpathouts     r$   _get_kwargs_from_urlsz#FTPFileSystem._get_kwargs_from_urlsL   s<    #G,,
D!!!
r%   Tc                    |                      |          }g }|| j        vr+	 	 d | j                            |          D             }n%# t          $ r t          | j        |          }Y nw xY w|D ]u\  }}|dk    rd}d                    ||                    d          g          |d<   |d         dk    rt          |d                   |d<   nd|d<   |d         d	k    rd
|d<   v|| j        |<   n\# t          $ rO 	 | 
                    |          }|d         dk    r||fg}n$# t          t          f$ r t          |          w xY wY nw xY w| j                            ||          }|st          d |D                       S d |D             S )Nc                 8    g | ]\  }}|d vr|d         dv||fS )).z..type)pdircdirr   .0fndetailss      r$   
<listcomp>z$FTPFileSystem.ls.<locals>.<listcomp>Y   sH       )R[00#FO3CCC W DCCr%   r    namer9   filesizer   dir	directoryc                     g | ]\  }}|S r   r   r<   s      r$   r@   z$FTPFileSystem.ls.<locals>.<listcomp>u   s    777+"g2777r%   c                     g | ]\  }}|S r   r   r<   s      r$   r@   z$FTPFileSystem.ls.<locals>.<listcomp>v   s    111KB111r%   )r/   dircacher   mlsdr   _mlsd2joinr,   intr   info
IndexErrorFileNotFoundErrorgetsorted)	r   r+   detailr"   r4   r>   r?   rN   filess	            r$   lszFTPFileSystem.lsS   s   ##D))t}$$21 -1X]]4-@-@  CC " 1 1 1 400CCC1#& 	6 	6KBs{{!&)hhbiinn/E&F&FGFOv&00*-gfo*>*>*+v%//*5&)d## 2 2 2299T??DF|v-- $d|nz* 2 2 2+D11122 !!$,, 	9777778881151111sH   $A	 C1 	A+(C1 *A++BC1 1
E
<&D#"E
#!EE
	E
c                 (   |                                dk    rddddS |                     |                                                   d          d          }	 fd|D             d         }n# t          $ r t                    w xY w|S )Nr   r   rF   )rB   rD   r9   Tc                 ,    g | ]}|d          k    |S )rB   r   )r=   fr+   s     r$   r@   z&FTPFileSystem.info.<locals>.<listcomp>   s'    999qyD'8'81'8'8'8r%   )r/   rU   _parentr,   rO   rP   )r   r+   r"   rT   r4   s    `   r$   rN   zFTPFileSystem.infox   s    ##D))3;;K@@@T**11#66==	*9999e999!<CC 	* 	* 	*#D)))	*
s    A5 5Bc                 ~   |                      |          r5t          j                            |          st          j        |           d S t          |          r|nt          |d          fd}| j                            d|z  | j	        |           t          |          s
                                 d S d S )Nwbc                 2                         |            d S N)write)xoutfiles    r$   cbz"FTPFileSystem.get_file.<locals>.cb   s    MM!r%   RETR %s)r   callback)isdirosr+   existsmkdirr   openr   
retrbinaryr   close)r   rpathlpathr"   ra   r`   s        @r$   get_filezFTPFileSystem.get_file   s    ::e 	7>>%((  Fe 	(GG5$''G	 	 	 	 	 	n 	 	
 	
 	

 %   	MMOOOOO	 	r%   c                     | t                      j        |||fi |S g fd}| j                            d|z  | j        ||           d                              S )Nc                 2                         |            d S r]   )append)r_   r4   s    r$   ra   z"FTPFileSystem.cat_file.<locals>.cb   s    JJqMMMMMr%   rb   r   restrc   r%   )r   cat_filer   ri   r   rL   )r   r+   startendr"   ra   r4   r#   s         @r$   rs   zFTPFileSystem.cat_file   s    ?#577#D%?????	 	 	 	 	 	n	 	 	
 	
 	
 xx}}r%   rbc           	      t    |                      |          }|p| j        }t          | |||| j        ||          S )N)moder!   r   
autocommitcache_options)r/   r   FTPFiler   )r   r+   rx   r!   rz   ry   r"   s          r$   _openzFTPFileSystem._open   sQ     ##D))14>
!L!'
 
 
 	
r%   c                     |                      |          }| j                            |           |                     |                     |                     d S r]   )r/   r   deleteinvalidate_cacherY   r   r+   s     r$   _rmzFTPFileSystem._rm   sO    ##D))dll40011111r%   c                     |                      |||          }t          |          D ]B}|                     |          r|                     |           -|                     |           Cd S )N)	recursivemaxdepth)expand_pathreversedisfilerm_filermdir)r   r+   r   r   pathsps         r$   rmzFTPFileSystem.rm   so      X NN% 	 	A{{1~~ Q

1		 	r%   r+   create_parentsr"   returnc                 P   |                      |          }|                     |          }|| j        k    r.|                     |          s|r|                     ||           | j                            |           |                     |                     |                     d S )Nr   )r/   rY   root_markerrf   rg   r   mkdr   )r   r+   r   r"   parents        r$   rg   zFTPFileSystem.mkdir   s    ##D))d##T%%%dkk&.A.A%n%JJvnJ===Tdll40011111r%   exist_okc                     |                      |          }|                     |          r|st          | d          d S |                     |d           d S )Nz exists without `exist_ok`Tr   )r/   rf   FileExistsErrorrg   )r   r+   r   s      r$   makedirszFTPFileSystem.makedirs   ss    ##D));;t 	2 K%&I&I&IJJJK K JJtDJ11111r%   c                     |                      |          }| j                            |           |                     |                     |                     d S r]   )r/   r   rmdr   rY   r   s     r$   r   zFTPFileSystem.rmdir   sO    ##D))Tdll40011111r%   c                 0   |                      |          }|                      |          }| j                            ||           |                     |                     |                     |                     |                     |                     d S r]   )r/   r   renamer   rY   )r   path1path2r"   s       r$   mvzFTPFileSystem.mv   s    $$U++$$U++u%%%dll511222dll51122222r%   c                 8    | j                                          d S r]   )r   rj   r)   s    r$   __del__zFTPFileSystem.__del__   s    r%   c                     || j                                          n| j                             |d            t          t          |                               |           d S r]   )rI   clearr2   r   r   r   )r   r+   r#   s     r$   r   zFTPFileSystem.invalidate_cache   s[    <M!!!!MdD)))mT""33D99999r%   )r   NNNNNr   )T)NN)rv   NNT)FNFr]   )__name__
__module____qualname____doc__r   cachabler1   r   r   classmethodr/   staticmethodr5   rU   rN   rm   rs   r|   r   r   strboolr   rg   r   r   r   r   r   __classcell__r#   s   @r$   r   r   
   s       ''KHH
 0 0 0 0 0 0d# # #
 Q Q [Q   \#2 #2 #2 #2J    *     & 
 
 
 
*2 2 2
   2 2# 2t 2c 2d 2 2 2 22 2S 2D 2T 2 2 2 22 2 2
3 3 3  : : : : : : : : : :r%   r   c                       e Zd ZdZdS )TransferDonez+Internal exception to break out of transferN)r   r   r   r   r   r%   r$   r   r      s        55Dr%   r   c                   H     e Zd ZdZ	 	 	 	 	 d fd	Zd Zd	 Zd
 ZddZ xZ	S )r{   z9Interact with a remote FTP file with read/write bufferingrv   defaultT	readaheadNc           	           t                      j        ||f|||||d| |sN| j        | _        d                    |d         t          t          j                              g          | _        d S d S )N)rx   r!   ry   
cache_typerz   r   r   )r   r   r+   targetrL   r   uuiduuid4)
r   fsr+   rx   r!   ry   r   rz   r"   r#   s
            r$   r   zFTPFile.__init__   s     			
 !!!'		
 		
 		
 		
 		
  	I)DK&"3S5F5F!GHHDIII	I 	Ir%   c                 P    | j                             | j        | j                   d S r]   )r   r   r+   r   r)   s    r$   commitzFTPFile.commit  s"    

49dk*****r%   c                 D    | j                             | j                   d S r]   )r   r   r+   r)   s    r$   discardzFTPFile.discard  s    

49r%   c                     g dg fd}	  j         j                            d j        z   j        |           nw# t
          $ rj 	  j         j                                          j         j                                         n)# t          $ r  j         	                                 Y nw xY wY nw xY wd
                              S )a  Get bytes between given byte limits

        Implemented by raising an exception in the fetch callback when the
        number of bytes received reaches the requested amount.

        Will fail if the server does not respect the REST command on
        retrieve requests.
        r   c                 H   dxx         t          |           z  cc<   d         z
  k    r<                    | d z
  d         z
                      j        k     rt          n                    |            d         z
  k    rj        k     r	t          d S d S )Nr   )lenrp   rD   r   )r_   ru   r4   r   rt   totals    r$   rc   z&FTPFile._fetch_range.<locals>.callback*  s    !HHHAHHHQx#+%%

17euQx778999??&& # 

1Qx3;&&3??"" '&??r%   rb   rq   r%   )r   r   ri   r+   r   r   abortgetmultiliner   r   rL   )r   rt   ru   rc   r4   r   s   ``` @@r$   _fetch_rangezFTPFile._fetch_range  s    
	# 
	# 
	# 
	# 
	# 
	# 
	# 
	# 
	#	#GK""DI%.!	 #      	# 	# 	##!!###((**** # # #  """""#	# xx}}s5   0A 
B:<BB:#B41B:3B44B:9B:Fc                     | j                             d           | j        j                            d| j        z   | j         | j        | j                   dS )Nr   zSTOR )r   rr   T)bufferseekr   r   
storbinaryr+   r   offset)r   finals     r$   _upload_chunkzFTPFile._upload_chunkG  sV    diT[ 	 	
 	
 	
 tr%   )rv   r   Tr   Nr   )
r   r   r   r   r   r   r   r   r   r   r   s   @r$   r{   r{      s        CC I I I I I I2+ + +  ' ' 'R       r%   r{   r8   c                    g }g }|                      ||j                   |D ]}|                                }|d         d                    |dd                   |d         |d         |d         |d         d	f}d
|d         d         d         k    rd|d         d<   nd|d         d<   |                    |           |S )a(  
    Fall back to using `dir` instead of `mlsd` if not supported.

    This parses a Linux style `ls -l` response to `dir`, but the response may
    be platform dependent.

    Parameters
    ----------
    ftp: ftplib.FTP
    path: str
        Expects to be given path, but defaults to ".".
           r      r      )modifyz
unix.ownerz
unix.group	unix.moderD   d   r   rE   r9   rC   )rE   rp   splitrL   )r   r+   linesminfolinethiss         r$   rK   rK   O  s     EEGGD%,  zz||H((4!9--"1g"1g!!WQ 	
 $q'+&q)))#DGFOO$DGFOTLr%   )r8   )re   r   ftplibr   r   r   typingr   specr   r	   utilsr
   r   r   	Exceptionr   r{   rK   r   r%   r$   <module>r      s'   				  ) ) ) ) ) ) ) ) ) )       ; ; ; ; ; ; ; ; 5 5 5 5 5 5 5 5i: i: i: i: i:& i: i: i:X	 	 	 	 	9 	 	 	P P P P P" P P Pf! ! ! ! ! !r%   