o
    NZh                     @   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   R/var/www/html/lang_env/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   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   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   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 ]}|r<|r.| d| n|}|  |s<| j|| q!d 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}|r@d|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                    sF   t d   fddj D }|r|S dd |D }t|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       t d|| | j|| d S )NzPut file %s into %s)r   r   r   put)r   lpathrpathcallbackkwargsr   r   r   r]         zSFTPFileSystem.putc                 K   s4   |  |rtj|dd d S | j| || d S )NT)r0   )isdirosr)   r   getr!   )r   r_   r^   ra   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 %sZ
autocommitTFr,   )bufsize)r   r   re   rN   r   struuiduuid4r   open
targetpathfstypes
MethodTypecommit_a_filecommitdiscard_a_filediscard)r   r   r+   
block_sizera   Zpath2fr   r   r   _open   s   zSFTPFileSystem._openc                 C   s*   |  |r| j| d S | j| d S N)rc   r   r4   remover5   r   r   r   _rm   s   
zSFTPFileSystem._rmc                 C   r\   )NzRenaming %s into %s)r   r   r   Zposix_rename)r   oldnewr   r   r   mv   rb   zSFTPFileSystem.mv)Tr%   )Fr%   ry   )F)rg   N)__name__
__module____qualname____doc__r"   r   r   classmethodr!   staticmethodr$   r*   r)   r4   r9   r7   r[   r]   rf   rx   r{   r~   __classcell__r   r   r   r   r      s*    	






	
r   c                 C   s   | j | j| j d S ry   )ro   r~   r   rn   r   r   r   r   rr      s   rr   c                 C   s   | j | j d S ry   )ro   r{   r   r   r   r   r   rt      s   rt   )rH   loggingrd   rp   rk   r8   r   r   r   r.   r   utilsr   	getLoggerr   r   rr   rt   r   r   r   r   <module>   s    
  