U
    9%e                     @   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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superr   __init__popr   host
ssh_kwargs_connect)selfr   r   	__class__ Z/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/fsspec/implementations/sftp.pyr      s    zSFTPFileSystem.__init__c                 C   sN   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_urlsF  c                 C   sH   t d|  | |r&td||r6| | n| j|| d S )NzCreating folder %sFile exists: {})r   r   existsFileExistsErrorformatmakedirsr   mkdir)r   r   Zcreate_parentsmoder   r   r   r+   D   s    
zSFTPFileSystem.mkdirc                 C   s\   |  |r|std||d}d}|D ](}|d| 7 }|  |s.| j|| q.d S )Nr&   / )r'   r(   r)   splitr   r+   )r   r   exist_okr,   parts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   r3   Z   s    zSFTPFileSystem.rmdirc                 C   s   |  | j|}||d< |S )Nname)_decode_statr   stat)r   r   r7   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)r5   sizetypeuidgidtimemtimer-   r5   )r   st_moder   st_sizest_uidst_giddatetimefromtimestampst_atimetimezoneutcst_mtimejoinrstripfilename)r7   parent_pathtr#   r   r   r   r6   c   s,    

  zSFTPFileSystem._decode_statc                    sL   t d    fddj D }|r2|S dd |D }t|S d S )NzListing folder %sc                    s   g | ]} | qS r   )r6   .0r7   r   r   r   r   
<listcomp>~   s     z%SFTPFileSystem.ls.<locals>.<listcomp>c                 S   s   g | ]}|d  qS )r5   r   rR   r   r   r   rU      s     )r   r   r   Zlistdir_itersorted)r   r   detailstatspathsr   rT   r   ls|   s    zSFTPFileSystem.lsc                 K   s$   t d||f  | 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rd| jtt g}| jj	|||rH|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   rb   rM   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)r`   r   r3   remover4   r   r   r   _rm   s    
zSFTPFileSystem._rmc                 C   s$   t d||f  | j|| d S )NzRenaming %s into %s)r   r   r   Zposix_rename)r   oldnewr   r   r   mv   s    zSFTPFileSystem.mv)Fr%   )Fr%   )N)F)N)rd   N)__name__
__module____qualname____doc__r"   r   r   classmethodr!   staticmethodr$   r+   r*   r3   r8   r6   rZ   r[   rc   ru   rx   r{   __classcell__r   r   r   r   r      s(   	





	

r   c                 C   s   | j | j| j d S rv   )rl   r{   r   rk   r   r   r   r   ro      s    ro   c                 C   s   | j | j d S rv   )rl   rx   r   r   r   r   r   rq      s    rq   )rG   loggingra   rm   rh   r7   r   r   r   r.   r   utilsr   	getLoggerr   r   ro   rq   r   r   r   r   <module>   s   
 