o
    h                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlmZmZ d dlZddl	m
Z
 ddlmZ edZG dd de
Zd	d
 Zdd ZdS )    N)S_ISDIRS_ISLNK   )AbstractFileSysteminfer_storage_optionszfsspec.sftpc                       s   e Zd ZdZdZ fddZdd Zedd Ze	d	d
 Z
d&ddZd'ddZdd Zdd Ze	d(ddZd)ddZd(ddZdd Zd*d d!Zd"d# Zd$d% Z  ZS )+SFTPFileSystemzFiles over SFTP/SSH

    Peer-to-peer filesystem over SSH using paramiko.

    Note: if using this with the ``open`` or ``open_files``, with full URLs,
    there is no way to tell if a path is relative, so all paths are assumed
    to be absolute.
    )sftpsshc                    sB   | j rdS t jdi | |dd| _|| _|| _|   dS )a  

        Parameters
        ----------
        host: str
            Hostname or IP as a string
        temppath: str
            Location on the server to put files, when within a transaction
        ssh_kwargs: dict
            Parameters passed on to connection. See details in
            https://docs.paramiko.org/en/3.3/api/client.html#paramiko.client.SSHClient.connect
            May include port, username, password...
        Ntemppathz/tmp )_cachedsuper__init__popr   host
ssh_kwargs_connect)selfr   r   	__class__r   o/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/fsspec/implementations/sftp.pyr      s   zSFTPFileSystem.__init__c                 C   sP   t d| j t | _| jt  | jj| jfi | j	 | j
 | _d S )NzConnecting to SFTP server %s)loggerdebugr   paramiko	SSHClientclientset_missing_host_key_policyAutoAddPolicyconnectr   	open_sftpftpr   r   r   r   r   2   s
   
zSFTPFileSystem._connectc                 C   s   t |d S )Npathr   )clsr#   r   r   r   _strip_protocol9   s   zSFTPFileSystem._strip_protocolc                 C   s$   t | }|dd  |dd  |S )Nr#   protocol)r   r   )urlpathoutr   r   r   _get_kwargs_from_urls=   s   z$SFTPFileSystem._get_kwargs_from_urlsT  c                 C   sH   t d| | |rtd| |r| | d S | j|| d S )NzCreating folder %sFile exists: )r   r   existsFileExistsErrormakedirsr!   mkdir)r   r#   create_parentsmoder   r   r   r/   D   s   
zSFTPFileSystem.mkdirFc                 C   s~   |  |r|std| |d}|d d dkrdnd}|D ]}|r<|r.| d| n|}|  |s<| j|| q!d S )Nr+   /    )r,   r-   splitr!   r/   )r   r#   exist_okr1   partsnew_pathpartr   r   r   r.   N   s   

zSFTPFileSystem.makedirsc                 C   s   t d| | j| d S )NzRemoving folder %s)r   r   r!   rmdirr   r#   r   r   r   r:   [   s   zSFTPFileSystem.rmdirc                 C   s   |  | j|}||d< |S )Nname)_decode_statr!   stat)r   r#   r>   r   r   r   info_   s   zSFTPFileSystem.infoNc              
   C   s   t | jrd}n
t| jrd}nd}d| j|| j| jtjj| jtj	j
dtjj| jtj	j
dd}|r@d|d| jg|d< |S )	N	directorylinkfiler4   )tz)r<   sizetypeuidgidtimemtimer2   r<   )r   st_moder   st_sizest_uidst_giddatetimefromtimestampst_atimetimezoneutcst_mtimejoinrstripfilename)r>   parent_pathtr(   r   r   r   r=   d   s(   



zSFTPFileSystem._decode_statc                    sF   t d   fddj D }|r|S dd |D }t|S )NzListing folder %sc                    s   g | ]} | qS r   )r=   .0r>   r#   r   r   r   
<listcomp>   s    z%SFTPFileSystem.ls.<locals>.<listcomp>c                 S   s   g | ]}|d  qS )r<   r   rY   r   r   r   r\      s    )r   r   r!   listdir_itersorted)r   r#   detailstatspathsr   r[   r   ls}   s   zSFTPFileSystem.lsc                 K       t d|| | j|| d S )NzPut file %s into %s)r   r   r!   put)r   lpathrpathcallbackkwargsr   r   r   rd         zSFTPFileSystem.putc                 K   s4   |  |rtj|dd d S | j| || d S )NT)r6   )isdirosr.   r!   getr%   )r   rf   re   rh   r   r   r   get_file   s   
zSFTPFileSystem.get_filerbc                 K   s   t d| |dddu r@d| jtt g}| jj	|||r#|ndd}||_||_
| |_tt||_tt||_|S | jj	|||rI|ndd}|S )z
        block_size: int or None
            If 0, no buffering, if 1, line buffering, if >1, buffer that many
            bytes, if None use default from paramiko.
        zOpening file %s
autocommitTFr2   )bufsize)r   r   rl   rT   r   struuiduuid4r!   open
targetpathfstypes
MethodTypecommit_a_filecommitdiscard_a_filediscard)r   r#   r1   
block_sizerh   path2fr   r   r   _open   s   zSFTPFileSystem._openc                 C   s*   |  |r| j| d S | j| d S N)rj   r!   r:   remover;   r   r   r   _rm   s   
zSFTPFileSystem._rmc                 C   rc   )NzRenaming %s into %s)r   r   r!   posix_rename)r   oldnewr   r   r   mv   ri   zSFTPFileSystem.mv)Tr*   )Fr*   r   )F)rn   N)__name__
__module____qualname____doc__r&   r   r   classmethodr%   staticmethodr)   r/   r.   r:   r?   r=   rb   rd   rm   r   r   r   __classcell__r   r   r   r   r      s*    	






	
r   c                 C   s   | j | j| j d S r   )rw   r   r   rv   r"   r   r   r   rz      s   rz   c                 C   s   | j | j d S r   )rw   r   r   r"   r   r   r   r|      s   r|   )rN   loggingrk   rx   rs   r>   r   r   r   r4   r   utilsr   	getLoggerr   r   rz   r|   r   r   r   r   <module>   s    
  