
    OOIdX                         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j        d          Z G d de
          Zd	 Zd
 ZdS )    N)S_ISDIRS_ISLNK   )AbstractFileSysteminfer_storage_optionszfsspec.sftpc                        e Zd ZdZdZ fdZd Zed             Ze	d             Z
dd	Zdd
Zd Zd Ze	dd            ZddZddZd ZddZd Zd Z x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                     | j         rdS  t          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
            http://docs.paramiko.org/en/2.4/api/client.html#paramiko.client.SSHClient.connect
            May include port, username, password...
        Ntemppathz/tmp )	_cachedsuperr
   __init__popr   host
ssh_kwargs_connect)selfr   r   	__class__s      ;lib/python3.11/site-packages/fsspec/implementations/sftp.pyr   zSFTPFileSystem.__init__   sk     < 	F,nd##,::z:::"z6::	$    c                 F   t                               d| j        z             t          j                    | _        | j                            t          j                                | 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   s    r   r   zSFTPFileSystem._connect2   s    3di?@@@(**//0F0H0HIIIDI99999;((**r   c                 ,    t          |          d         S )Npathr   )clsr(   s     r   _strip_protocolzSFTPFileSystem._strip_protocol9   s    $T**622r   c                 |    t          |           }|                    dd            |                    dd            |S )Nr(   protocol)r   r   )urlpathouts     r   _get_kwargs_from_urlsz$SFTPFileSystem._get_kwargs_from_urls=   s<    #G,,
D!!!
r   F  c                    t                               d|z             |                     |          r"t          d                    |                    |r|                     |           d S | j                            ||           d S )NzCreating folder %sFile exists: {})r   r   existsFileExistsErrorformatmakedirsr%   mkdir)r   r(   create_parentsmodes       r   r7   zSFTPFileSystem.mkdirD   s    )D0111;;t 	B!"3":":4"@"@AAA 	'MM$HNN4&&&&&r   c                     |                      |          r$|s"t          d                    |                    |                    d          }d}|D ]:}|d|z   z  }|                      |          s| j                            ||           ;d S )Nr2   / )r3   r4   r5   splitr%   r7   )r   r(   exist_okr9   partsparts         r   r6   zSFTPFileSystem.makedirsN   s    ;;t 	BX 	B!"3":":4"@"@AAA

3 	+ 	+DC$JD;;t$$ +tT***	+ 	+r   c                 t    t                               d|z             | j                            |           d S )NzRemoving folder %s)r   r   r%   rmdirr   r(   s     r   rB   zSFTPFileSystem.rmdirZ   s4    )D0111tr   c                 j    |                      | j                            |                    }||d<   |S )Nname)_decode_statr%   stat)r   r(   rG   s      r   infozSFTPFileSystem.info^   s1      t!4!455Vr   Nc           	         t          | j                  rd}nt          | j                  rd}nd}d| j        || j        | j        t          j                            | j                  t          j                            | j	                  d}|r2d
                    |                    d          | j        g          |d<   |S )N	directorylinkfiler<   )rE   sizetypeuidgidtimemtimer;   rE   )r   st_moder   st_sizest_uidst_giddatetimeutcfromtimestampst_atimest_mtimejoinrstripfilename)rG   parent_pathtr.   s       r   rF   zSFTPFileSystem._decode_statc   s    4<   	AAT\"" 	AAAL;;%66t}EE&77FF
 
  	M((K$6$6s$;$;T]#KLLCK
r   c                      t                               dz              fd j                                      D             }|r|S d |D             }t	          |          S )NzListing folder %sc                 <    g | ]}                     |          S r   )rF   ).0rG   r(   r   s     r   
<listcomp>z%SFTPFileSystem.ls.<locals>.<listcomp>z   s)    WWW4""4..WWWr   c                     g | ]
}|d          S )rE   r   )rb   rG   s     r   rc   z%SFTPFileSystem.ls.<locals>.<listcomp>~   s    444dT&\444r   )r   r   r%   listdir_itersorted)r   r(   detailstatspathss   ``   r   lszSFTPFileSystem.lsx   su    (4/000WWWWW48;P;PQU;V;VWWW 	!L44e444E%== r   c                 |    t                               d|d|           | j                            ||           d S )Nz	Put file  into )r   r   r%   put)r   lpathrpathcallbackkwargss        r   rm   zSFTPFileSystem.put   s=    eeeUU;<<<UE"""""r   c                     |                      |          rt          j        |d           d S | j                            |                     |          |           d S )NT)r>   )isdirosr6   r%   getr*   )r   ro   rn   rq   s       r   get_filezSFTPFileSystem.get_file   s[    ::e 	=K------HLL--e44e<<<<<r   rbc                    t                               d|z             |                    dd          du rd                    | j        t          t          j                              g          }| j        	                    |||r|nd          }||_        ||_
        | |_        t          j        t          |          |_        t          j        t           |          |_        n!| j        	                    |||r|n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
autocommitTFr;   )bufsize)r   r   ru   r[   r   struuiduuid4r%   open
targetpathfstypes
MethodTypecommit_a_filecommitdiscard_a_filediscard)r   r(   r9   
block_sizerq   path2fs          r   _openzSFTPFileSystem._open   s     	&-...::lD))U22HHdmS->->?@@EeT3S::QSTTAAJALAD'q99AH(;;AIIdD
2R**PRSSAr   c                     |                      |          r| j                            |           d S | j                            |           d S N)rs   r%   rB   removerC   s     r   _rmzSFTPFileSystem._rm   sJ    ::d 	"HNN4     HOOD!!!!!r   c                 |    t                               d|d|           | j                            ||           d S )Nz	Renaming rl   )r   r   r%   posix_rename)r   oldnews      r   mvzSFTPFileSystem.mv   s?    ccc337888c3'''''r   )Fr0   r   )F)rw   N)__name__
__module____qualname____doc__r,   r   r   classmethodr*   staticmethodr/   r7   r6   rB   rH   rF   rj   rm   rv   r   r   r   __classcell__)r   s   @r   r
   r
      s]         H    ,+ + + 3 3 [3   \' ' ' '
+ 
+ 
+ 
+    
    \(! ! ! !# # # #= = =   (" " "( ( ( ( ( ( (r   r
   c                 P    | j                             | j        | j                   d S r   )r   r   r   r   r&   s    r   r   r      s"    GJJt}do.....r   c                 D    | j                             | j                   d S r   )r   r   r   r&   s    r   r   r      s    GKKr   )rW   loggingrt   r   r}   rG   r   r   r   r<   r   utilsr   	getLoggerr   r
   r   r   r   r   r   <module>r      s      				   ! ! ! ! ! ! ! !  ! ! ! ! ! ! ) ) ) ) ) )		=	)	)W( W( W( W( W(' W( W( W(t/ / /    r   