U
    xÊ¹jÿ  ã                   @   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 ),ÚSFTPFileSystemzüFiles 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__© úZ/var/www/html/bwave/python/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œ}|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                    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   
  