
    #HJj+                    
   U d Z ddlmZ ddlZddlZddlZddlZddlm	Z	 ddl
mZmZmZ 	 ddlZddlZerddlmZ ddlmZ  ej.                  e      Zi Zd	ed
<   dZ	 dZdZ e e	d      jA                               gZ!d!dZ"d"dZ#de$fde#fde#fde#fde#fde#fdZ%ded<    G d de      Z&d#dZ'd$dZ(	 	 	 	 	 	 	 	 	 	 	 	 d%dZ)	 	 	 	 d&	 	 	 	 	 	 	 	 	 	 	 d'dZ*	 	 	 	 	 	 	 	 d(	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d)d Z+y# e$ r dZY w xY w)*a~  Implements I/O streams over SSH.

Example:
    >>> with open("/proc/version_signature", host="1.2.3.4") as conn:
    ...     print(conn.read())
    b'Ubuntu 4.4.0-1061.70-aws 4.4.131'

    Similarly, from a command line::

        $ python -c "from smart_open import ssh;print(ssh.open('/proc/version_signature', host='1.2.3.4').read())"
        b'Ubuntu 4.4.0-1061.70-aws 4.4.131'
    )annotationsN)Path)TYPE_CHECKINGAny	TypedDictT)Callable)TransportParamsz)dict[tuple[str, str], paramiko.SSHClient]_SSH)sshscpsftp   )zssh://username@host/path/filezssh://username@host//path/filezscp://username@host/path/filezsftp://username@host/path/filez~/.ssh/configc                H    | xr t         j                  j                  |       S N)urllibparseunquote)texts    X/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/smart_open/ssh.py_unquoter   C   s    .FLL((..    c                <    | dk(  ry| dk(  ryd|  d}t        |      )NnoFyesTzExpected 'yes' / 'no', got .)
ValueError)stringmsgs     r   	_str2boolr   G   s.    ~'xq
1C
S/r   connecttimeoutcompressiongssapiauthenticationgssapikeyexchangegssapidelegatecredentialsgssapitrustdns)timeoutcompressgss_authgss_kexgss_deleg_credsgss_trust_dnszdict[str, tuple[str, Callable]]_PARAMIKO_CONFIG_MAPc                  J    e Zd ZU ded<   ded<   ded<   ded<   ded<   ded	<   y
)_SSHUristrscheme
str | Noneuri_pathuserhost
int | NoneportpasswordN)__name__
__module____qualname____annotations__ r   r   r.   r.   d   s%    K


r   r.   c                V   t         j                  j                  |       }|j                  t        v sJ |j                  t        |j                        t        |j                        |j                  |j                  rt        |j                        ndt        |j                        dS )zJParse an ``ssh://``/``scp://``/``sftp://`` URI into connection components.N)r0   r2   r3   r4   r6   r7   )r   r   urlsplitr0   SCHEMESr   pathusernamehostnamer6   intr7   )uri_as_string	split_uris     r   	parse_urirF   m   s    %%m4Iw&&&""Y^^,++,""'0~~INN#4Y//0 r   c                    t         j                  j                  t        |      }t	        t        |             }|j                  d      }|j                  d       i ||}t        ||fi |S )zCOpen an SSH/SCP/SFTP URI using the given mode and transport params.r2   r0   )
smart_openutilscheck_kwargsopendictrF   pop)urimodetransport_paramskwargs
parsed_urir2   final_paramss          r   open_urirT   {   sd    **41ABF!%in!5J~~j)HNN8+j+F+L$/,//r   c                D   t        j                         }|j                          |j                  t        j                                |xs i j                         }d|vr|j                  d|       |j                  d|        |j                  | ||nt        fi | |S )Nkey_filenamer7   rA   )	paramiko	SSHClientload_system_host_keysset_missing_host_key_policyAutoAddPolicycopy
setdefaultconnectDEFAULT_PORT)rB   rA   r6   r7   connect_kwargsr   rQ   s          r   _connect_sshra      s     


C##H$:$:$<="((*FV#*h/
j(+CKK$"2$OOJr   c                   t        d | ||||fD              s| ||||fS | sd}t        |      |xs i j                         }t        D cg c]  }t	        |      j                         s|  }}d}	|D ]  }
	 t        j                  j                  |
      }| |j                         vr7|j                  |       }||j                  dd       }|	s|d   }	|5t        j                  t        t              5  t!        |d         }d d d        |j                  d      "|j                  dg       }t#        |      r||d<   t$        j'                         D ]-  \  }\  }}|j                  |      ||v s  |||         ||<   /  |t(        }|st+        j,                         }|	r|	} | ||||fS c c}w # t        $ r Y Nw xY w# 1 sw Y   xY w)	Nc              3  $   K   | ]  }|d u  
 y wr   r<   ).0args     r   	<genexpr>z&_maybe_fetch_config.<locals>.<genexpr>   s     W&Vssd{&Vs   z'you must specify the host to connect to r3   rB   r6   rV   identityfile)anyr   r\   _SSH_CONFIG_FILESr   existsrW   	SSHConfig	from_pathPermissionErrorget_hostnameslookupget
contextlibsuppressKeyErrorrC   lenr,   itemsr_   getpassgetuser)r4   rA   r7   r6   r`   r   connect_paramsfconfig_filesactual_hostnameconfig_filenamecfgrh   
param_namesshcfg_namefrom_strs                   r   _maybe_fetch_configr      s    WtXx~&VWWXx~==7o" %*002N0E0!DGNN4DA0LE
 O'	$$..?C s((**jjwwvt,H!*oO<$$Xz:3v;' ; n-577>26L< 1=~.3G3M3M3O/J/h!!*-5+:L-5c+6F-Gz* 4P? (P |??$8T>99q F  		 ;:s*   
F0)F08F56G5	GGG	c	           	        t        |||||      \  }}}}}|J |J | J ||f}	d}
d}t        |      D ]z  }	 t        |	   }|j                         }|J |j                  s(|j                          t        |||||      x}t        |	<   	 |j                         }|J |j                         }
 n |
J |
j                  | ||      }| |_        | |j                  di | |S # t        $ r t        |||||      x}t        |	<   Y w xY w# t        j                  $ r=}|j                  xr |j                  d   dk(  }||dz
  k(  s|s t        |	= Y d}~$d}~ww xY w)aw  Open a file on a remote machine over SSH.

    Expects authentication to be already set up via existing keys on the local machine.

    Args:
        path: The path to the file to open on the remote machine.
        mode: The mode to use for opening the file.
        host: The hostname of the remote machine. May not be None.
        user: The username to use to login to the remote machine.
            If None, defaults to the name of the current user.
        password: The password to use to login to the remote machine.
        port: The port to connect to.
        connect_kwargs: Any additional settings to be passed to paramiko.SSHClient.connect.
        prefetch_kwargs: Any additional settings to be passed to paramiko.SFTPFile.prefetch.
            The presence of this dict (even if empty) triggers prefetching.
        buffer_size: Passed to the bufsize argument of paramiko.SFTPClient.open.

    Returns:
        A file-like object.

    Raises:
        paramiko.SSHException: If the SSH session is no longer active and
            cannot be re-established within the retry limit.

    Note:
        If you specify a previously unseen host, then its host key will be added to
        the local ~/.ssh/known_hosts *automatically*.

        If ``username`` or ``password`` are specified in *both* the uri and
        ``transport_params``, ``transport_params`` will take precedence.
    N   r   zSSH session not active   )rO   bufsizer<   )r   ranger
   get_transportactiveclosera   rt   open_sftp_clientrW   SSHExceptionargsrK   nameprefetch)r@   rO   r4   r3   r7   r6   r`   prefetch_kwargsbuffer_sizekeysftp_clientattemptsattemptr   	transportexconnection_timed_outfobjs                     r   rK   rK      s   T 2EdHdN2.D$$ ,C.2KH?
	Ws)C ))+I(((##		".tT4>"ZZd3i	))+I(((#446K# #: """Dt[ADDI"((K1  	W*4tX~VVC$s)	W $$ 		#%77#Urwwqz=U/U (Q,&.B S			s*   AC'$D'!D
DE!2EE)r   r1   returnr1   )r   r/   r   bool)rD   r/   r   r.   )rN   r/   rO   r/   rP   r	   r   paramiko.SFTPFile)rB   r/   rA   r1   r6   r5   r7   r1   r`   dict[str, Any] | Noner   zparamiko.SSHClient)NNNN)r4   r1   rA   r1   r7   r1   r6   r5   r`   r   r   zLtuple[str | None, str | None, str | None, int | None, dict[str, Any] | None])rNNNNNN)r@   r1   rO   r/   r4   r1   r3   r1   r7   r1   r6   r5   r`   r   r   r   r   rC   r   r   ),__doc__
__future__r   rr   rw   loggingurllib.parser   pathlibr   typingr   r   r   rW   ImportErrorMISSING_DEPSsmart_open.utilsrH   collections.abcr   smart_open._typingr	   	getLoggerr8   loggerr
   r;   r?   r_   URI_EXAMPLESr/   
expanduserrj   r   r   floatr,   r.   rF   rT   ra   r   rK   r<   r   r   <module>r      s   #      0 0 (2			8	$
 35/ 4
   o.99;<= /( !%(	*'3#Y/3Y?&	29 5 i 0  	
 * (  ,0W:
W:W: W: 	W:
 *W: RW:x ,0-1W
W
W W 	W
 W W *W +W W Wc  Ls   C8 8DD