o
    h.                     @   s   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mZ d dl	m
Z
 ddlmZmZ ddlmZmZ G dd deZG d	d
 d
eZG dd deZdddZdS )    N)FTPFTP_TLSError
error_perm)Any   )AbstractBufferedFileAbstractFileSystem)infer_storage_options
isfilelikec                	       s  e Zd ZdZdZdZdZ									d2 fd	d
	Zdd Ze	dd Z
edd Zd3ddZdd Zdd Zd4 fdd	Z				d5ddZdd Zd6dd Zd3d!ed"ed#ed$dfd%d&Zd7d!ed'ed$dfd(d)Zd*d+ Zd,d- Zd.d/ Zd8 fd0d1	Z  ZS )9FTPFileSystemzA filesystem over classic FTP/Fftp   N   utf-8c                    s   t  jdi | || _|| _|pd| _|pd|pd|pdf| _|| _|	| _|dur.|| _nd| _|
| _	| 
  | j	rB| j  dS dS )a  
        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
        encoding: str
            Encoding to use for directories and filenames in FTP connection
        tls: bool
            Use FTP-TLS, by default False
        z/tmp Ni    )super__init__hostporttempdircredtimeoutencoding	blocksizetls_connectr   prot_p)selfr   r   usernamepasswordacct
block_sizer   r   r   r   kwargs	__class__r   n/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/fsspec/implementations/ftp.pyr      s   ,
zFTPFileSystem.__init__c                 C   s   | j rt}nt}tjdkr|| j| jd| _n| jr't	d || jd| _n|| jd| _| j
| j| j | jj| j  d S )N)   	   )r   r   z1`encoding` not supported for python<3.9, ignoring)r   )r   r   r   sysversion_infor   r   r   warningswarnconnectr   r   loginr   )r    ftp_clsr   r   r(   r   O   s   

zFTPFileSystem._connectc                 C   s   dt |d dd S )Nr   path)r
   lstriprstrip)clsr2   r   r   r(   _strip_protocol^   s   zFTPFileSystem._strip_protocolc                 C   s$   t | }|dd  |dd  |S )Nr2   protocol)r
   pop)urlpathoutr   r   r(   _get_kwargs_from_urlsb   s   z#FTPFileSystem._get_kwargs_from_urlsTc           
      K   s^  |  |}g }|| jvrzZzdd | j|D }W n ty)   t| j|}Y nw |D ]3\}}d|dkr8dn||dg|d< |d dkrQt|d |d< nd	|d< |d d
kr_d|d< q,|| j|< W n/ t	y   z| 
|}|d dkr~||fg}W n t	tfy } zt||d }~ww Y nw | j||}	|stdd |	D S dd |	D S )Nc                 S   s,   g | ]\}}|d vr|d dvr||fqS )).z..type)pdircdirr   .0fndetailsr   r   r(   
<listcomp>o   s    z$FTPFileSystem.ls.<locals>.<listcomp>r   r   namer=   filesizer   dir	directoryc                 S   s   g | ]\}}|qS r   r   r@   r   r   r(   rD          c                 S   s   g | ]\}}|qS r   r   r@   r   r   r(   rD      rJ   )r6   dircacher   mlsdr   _mlsd2joinr3   intr   info
IndexErrorFileNotFoundErrorgetsorted)
r    r2   detailr%   r:   rB   rC   rP   excfilesr   r   r(   lsi   sL   






zFTPFileSystem.lsc              
      sv   |     dkrddddS | |  dd}zt fdd|D }W |S  ty: } zt |d }~ww )Nr   r   rI   )rE   rG   r=   Tc                 3   s     | ]}|d   kr|V  qdS )rE   Nr   )rA   fr2   r   r(   	<genexpr>   s    z%FTPFileSystem.info.<locals>.<genexpr>)r6   rX   _parentr3   nextStopIterationrR   )r    r2   r%   rW   r:   rV   r   rZ   r(   rP      s   

zFTPFileSystem.infoc                    sz   |  |rtj|st| d S t|r| nt|d  fdd}| jjd| | j	|d t|s; 
  d S d S )Nwbc                         |  d S N)writexoutfiler   r(   cb      z"FTPFileSystem.get_file.<locals>.cbRETR )r   callback)isdirosr2   existsmkdirr   openr   
retrbinaryr   close)r    rpathlpathr%   rg   r   re   r(   get_file   s    


zFTPFileSystem.get_filec              
      s   |d urt  j|||fi |S g   fdd}z| jjd| | j||d W n ttfy; } zt||d }~ww d S )Nc                    r`   ra   )appendrc   r:   r   r(   rg      rh   z"FTPFileSystem.cat_file.<locals>.cbri   r   restrj       )	r   cat_filer   rp   r   r   r   rR   rN   )r    r2   startendr%   rg   orig_excr&   rv   r(   rz      s    


zFTPFileSystem.cat_filerbc              	   K   s,   |  |}|p	| j}t| |||| j||dS )N)moder$   r   
autocommitcache_options)r6   r   FTPFiler   )r    r2   r   r$   r   r   r%   r   r   r(   _open   s   
	
zFTPFileSystem._openc                 C   *   |  |}| j| | | | d S ra   )r6   r   deleteinvalidate_cacher\   r    r2   r   r   r(   _rm      
zFTPFileSystem._rmc                 C   sB   | j |||d}t|D ]}| |r| | q| | qd S )N)	recursivemaxdepth)expand_pathreversedisfilerm_filermdir)r    r2   r   r   pathspr   r   r(   rm   s   
zFTPFileSystem.rmr2   create_parentsr%   returnc                 K   sZ   |  |}| |}|| jkr| |s|r| j||d | j| | | | d S )Nr   )r6   r\   root_markerrm   rn   r   mkdr   )r    r2   r   r%   parentr   r   r(   rn      s   

zFTPFileSystem.mkdirexist_okc                 C   s<   |  |}| |r|st| dd S | j|dd d S )Nz exists without `exist_ok`Tr   )r6   rm   FileExistsErrorrn   )r    r2   r   r   r   r(   makedirs   s   

zFTPFileSystem.makedirsc                 C   r   ra   )r6   r   rmdr   r\   r   r   r   r(   r      r   zFTPFileSystem.rmdirc                 K   sF   |  |}|  |}| j|| | | | | | | d S ra   )r6   r   renamer   r\   )r    path1path2r%   r   r   r(   mv   s
   

zFTPFileSystem.mvc                 C   s   | j   d S ra   )r   rq   r    r   r   r(   __del__  rh   zFTPFileSystem.__del__c                    s2   |d u r
| j   n| j |d  t | d S ra   )rK   clearr8   r   r   r   r&   r   r(   r     s   zFTPFileSystem.invalidate_cache)	r   NNNNNr   r   F)T)NN)r~   NNT)FNFra   )__name__
__module____qualname____doc__r   cachabler7   r   r   classmethodr6   staticmethodr;   rX   rP   rt   rz   r   r   r   strboolr   rn   r   r   r   r   r   __classcell__r   r   r&   r(   r      sJ    <


%

	
r   c                   @   s   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    r   c                       sN   e Zd ZdZ					d fdd	Zd	d
 Zdd Zdd ZdddZ  Z	S )r   z9Interact with a remote FTP file with read/write bufferingr~   defaultT	readaheadNc           	         sR   t  j||f|||||d| |s'| j| _d|d tt g| _d S d S )N)r   r$   r   
cache_typer   r   r   )r   r   r2   targetrN   r   uuiduuid4)	r    fsr2   r   r$   r   r   r   r%   r&   r   r(   r     s    
 zFTPFile.__init__c                 C   s   | j | j| j d S ra   )r   r   r2   r   r   r   r   r(   commit1  s   zFTPFile.commitc                 C   s   | j | j d S ra   )r   r   r2   r   r   r   r(   discard4  s   zFTPFile.discardc                    s   g dg fdd}zj jjdj j|d W n& tyG   zj j  j j  W n tyD   j 	  Y nw Y n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                    s   d  t | 7  < d   kr)| d   d     jk r(tn|  d   kr= jk r?td S d S )Nr   )lenru   rG   r   rc   r|   r:   r    r{   totalr   r(   rj   C  s   

z&FTPFile._fetch_range.<locals>.callbackri   rw   ry   )r   r   rp   r2   r   r   abortgetmultiliner   r   rN   )r    r{   r|   rj   r   r   r(   _fetch_range7  s(   	


zFTPFile._fetch_rangeFc                 C   s4   | j d | jjjd| j | j | j| jd dS )Nr   zSTOR )r   rx   T)bufferseekr   r   
storbinaryr2   r   offset)r    finalr   r   r(   _upload_chunk`  s
   zFTPFile._upload_chunk)r~   r   Tr   Nr   )
r   r   r   r   r   r   r   r   r   r   r   r   r&   r(   r     s    )r   r<   c                 C   s   g }g }|  ||j |D ]D}| }t|dk rq|d d|dd |d |d |d |d	 d
f}|d d d dkrFd|d d< nd|d d< || q|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   r)   r      )modifyz
unix.ownerz
unix.group	unix.moderG      r   drH   r=   rF   )rH   ru   splitr   rN   )r   r2   linesminfoline
split_linethisr   r   r(   rM   h  s(   
rM   )r<   )rl   r+   r   r-   ftplibr   r   r   r   typingr   specr   r	   utilsr
   r   r   	Exceptionr   r   rM   r   r   r   r(   <module>   s      S