
    \j$                     ^   U d Z ddlZddlZddlZddlZddlmZmZm	Z	 	 ddl
Z
ddlZ ej                  e      Zi ZdZ	 dZdZej,                  j/                  d      gZd	 Zd
 ZdefdefdefdefdefdefdZeee	eef   f   ed<   d Zd Z d Z!ddZ"	 	 	 	 	 	 	 	 ddZ#y# e$ r dZY w xY w)aq  Implements I/O streams over SSH.

Examples
--------

>>> 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'

    N)DictCallableTupleT)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    8/root/env/lib/python3.12/site-packages/smart_open/ssh.py_unquoter   D   s    .FLL((..    c                 8    | dk(  ry| dk(  ryt        d|  d      )NnoFyesTzExpected 'yes' / 'no', got .)
ValueError)strings    r   	_str2boolr   H   s+    ~
26(!<
==r   connecttimeoutcompressiongssapiauthenticationgssapikeyexchangegssapidelegatecredentialsgssapitrustdns)timeoutcompressgss_authgss_kexgss_deleg_credsgss_trust_dns_PARAMIKO_CONFIG_MAPc           
      f   t         j                  j                  |       }|j                  t        v sJ t        |j                  t        |j                        t        |j                        |j                  |j                  rt        |j                        nd t        |j                              S )N)schemeuri_pathuserhostportpassword)r   r   urlsplitr(   SCHEMESdictr   pathusernamehostnamer,   intr-   )uri_as_string	split_uris     r   	parse_urir7   d   s    %%m4Iw&&&)..)i(()$-NNS ),,- r   c                     t         j                  j                  t        |      }t	        |       }|j                  d      }|j                  d       i ||}t        ||fi |S )Nr)   r(   )
smart_openutilscheck_kwargsopenr7   pop)urimodetransport_paramskwargs
parsed_urir)   final_paramss          r   open_urirD   q   s_    **41ABF3J~~j)HNN8+j+F+L$/,//r   c                 4   t        j                         }|j                          |j                  t        j                                |xs i j                         }d|vr|j                  d|       |j                  d|        |j                  | |fi | |S )Nkey_filenamer-   r2   )paramiko	SSHClientload_system_host_keysset_missing_host_key_policyAutoAddPolicycopy
setdefaultconnect)r3   r2   r,   r-   connect_kwargsr   rA   s          r   _connect_sshrP   z   s    



C##H$:$:$<="((*FV#*h/
j(+CKK$)&)Jr   c                 l   t        d | ||||fD              s| ||||fS | st        d      |xs i j                         }t        D cg c]$  }t        j
                  j                  |      s#|& }}d}|D ]  }		 t        j                  j                  |	      }
| |
j                         vr6|
j                  |       }
||
j                  dd       }|s|
d   }|	 t        |
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 &w xY w# t        t        f$ r Y w xY w)	Nc              3   $   K   | ]  }|d u  
 y wr    ).0args     r   	<genexpr>z&_maybe_fetch_config.<locals>.<genexpr>   s     Wssd{Ws   z'you must specify the host to connect to r*   r3   r,   rF   identityfile)anyr   rL   _SSH_CONFIG_FILESosr1   existsrG   	SSHConfig	from_pathPermissionErrorget_hostnameslookupgetr4   KeyErrorlenr&   itemsDEFAULT_PORTgetpassgetuser)r+   r2   r-   r,   rO   connect_paramsfconfig_filesactual_hostnameconfig_filenamecfgrX   
param_namesshcfg_namefrom_strs                  r   _maybe_fetch_configrr      s   WtXx~&VWWXx~==BCC" %*002N0F!BGGNN14EAFLF
 O' %H	$$..?C s((**jjwwvt,H!*oO<3v;' n-577>26L< 1=~.3G3M3M3O 	H/J/h!!*-5+:L-5c+6F-Gz*	HG%HX |??$8T>99y G  		  j)  	s0   $F-F;FF!	FF!F32F3c	           	         t        |||||      \  }}}}}||f}	d}
t        |
      D ]p  }	 t        |	   }|j                         j                  s(|j                          t        |||||      x}t        |	<   	 |j                         }|j                         } n j                  | ||      }| |_        | |j                  di | |S # t        $ r t        |||||      x}t        |	<   Y xw 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)a  Open a file on a remote machine over SSH.

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

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

    Returns
    -------
    A file-like object.

    Important
    ---------
    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
       r   zSSH session not active   N)r?   bufsizerS   )rr   range_SSHget_transportactivecloserP   rc   open_sftp_clientrG   SSHExceptionargsr<   nameprefetch)r1   r?   r+   r*   r-   r,   rO   prefetch_kwargsbuffer_sizekeyattemptsattemptr   	transportsftp_clientexconnection_timed_outfobjs                     r   r<   r<      sm   d 2EdHdN2.D$$ ,CH? 	Ws)C $$&--		".tT4>"ZZd3i	))+I#446K4 Dt[ADDI"((K-  	W*4tX~VVC$s)	W $$ 		#%77#Urwwqz=U/U (Q,&.B S			s*   AC7 C2!C/.C/2E2D==E)NNNN)rNNNNNN)$__doc__rg   r[   loggingurllib.parser   typingr   r   r   rG   ImportErrorMISSING_DEPSsmart_open.utilsr9   	getLogger__name__loggerrx   r/   rf   URI_EXAMPLESr1   
expanduserrZ   r   r   floatr&   str__annotations__r7   rD   rP   rr   r<   rS   r   r   <module>r      s!     	    			8	$
 

   WW''89 />& !%(	*'3#Y/3Y?&	29 d3c8m 445 
0	T:r 
			Wo  Ls   B" "B,+B,