U
    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                    s>   | j r
dS t jf | |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__ M/var/www/html/venv/lib/python3.8/site-packages/fsspec/implementations/sftp.pyr      s    zSFTPFileSystem.__init__c                 C   sL   t d| j t | _| jt  | jj| jf| j	 | j
 | _d S )NzConnecting to SFTP server %s)loggerdebugr   paramikoZ	SSHClientclientZset_missing_host_key_policyZAutoAddPolicyconnectr   Z	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   )Zurlpathoutr   r   r   _get_kwargs_from_urls=   s    z$SFTPFileSystem._get_kwargs_from_urlsT  c                 C   sF   t d| | |r$td| |r4| | n| j|| d S )NzCreating folder %sFile exists: )r   r   existsFileExistsErrormakedirsr   mkdir)r   r   Z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 ]6}|rB|r\| d| n|}|  |sB| j|| qBd S )Nr&   /    )r'   r(   splitr   r*   )r   r   exist_okr+   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   r4   [   s    zSFTPFileSystem.rmdirc                 C   s   |  | j|}||d< |S )Nname)_decode_statr   stat)r   r   r8   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}|rd|d| jg|d< |S )	N	directorylinkfiler.   )tz)r6   sizetypeuidgidtimemtimer,   r6   )r   st_moder   st_sizest_uidst_giddatetimefromtimestampst_atimetimezoneutcst_mtimejoinrstripfilename)r8   parent_pathtr#   r   r   r   r7   d   s,    

  zSFTPFileSystem._decode_statc                    sJ   t d   fddj D }|r0|S dd |D }t|S d S )NzListing folder %sc                    s   g | ]} | qS r   )r7   .0r8   r   r   r   r   
<listcomp>   s     z%SFTPFileSystem.ls.<locals>.<listcomp>c                 S   s   g | ]}|d  qS )r6   r   rS   r   r   r   rV      s     )r   r   r   Zlistdir_itersorted)r   r   detailstatspathsr   rU   r   ls}   s    zSFTPFileSystem.lsc                 K   s    t d|| | j|| d S )NzPut file %s into %s)r   r   r   put)r   lpathrpathcallbackkwargsr   r   r   r\      s    zSFTPFileSystem.putc                 K   s2   |  |rtj|dd n| j| || d S )NT)r0   )isdirosr)   r   getr!   )r   r^   r]   r`   r   r   r   get_file   s    
zSFTPFileSystem.get_filerbc                 K   s   t d| |dddkr~d| jtt g}| jj	|||rF|ndd}||_||_
| |_tt||_tt||_n| jj	|||r|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 %sZ
autocommitTFr,   )bufsize)r   r   rc   rN   r   struuiduuid4r   open
targetpathfstypes
MethodTypecommit_a_filecommitdiscard_a_filediscard)r   r   r+   
block_sizer`   Zpath2fr   r   r   _open   s    zSFTPFileSystem._openc                 C   s(   |  |r| j| n| j| d S N)ra   r   r4   remover5   r   r   r   _rm   s    
zSFTPFileSystem._rmc                 C   s    t d|| | j|| d S )NzRenaming %s into %s)r   r   r   Zposix_rename)r   oldnewr   r   r   mv   s    zSFTPFileSystem.mv)Tr%   )Fr%   )N)F)N)re   N)__name__
__module____qualname____doc__r"   r   r   classmethodr!   staticmethodr$   r*   r)   r4   r9   r7   r[   r\   rd   rv   ry   r|   __classcell__r   r   r   r   r      s(   	





	

r   c                 C   s   | j | j| j d S rw   )rm   r|   r   rl   r   r   r   r   rp      s    rp   c                 C   s   | j | j d S rw   )rm   ry   r   r   r   r   r   rr      s    rr   )rH   loggingrb   rn   ri   r8   r   r   r   r.   r   utilsr   	getLoggerr   r   rp   rr   r   r   r   r   <module>   s   
  