U
    be                     @   s   d Z ddlZddlZddlZzddlZW n ek
r@   dZY nX ddlZ	e
eZi ZdZdZdZdd Zd	d
 Zdd Zdd ZddddedfddZdS )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'

    NT)sshZscpsftp   )zssh://username@host/path/filezssh://username@host//path/filezscp://username@host/path/filezsftp://username@host/path/filec                 C   s   | ot j| S )N)urllibparseunquote)text r	   P/var/www/html/services/stratfitenv/lib/python3.8/site-packages/smart_open/ssh.py_unquote8   s    r   c                 C   sN   t j| }|jtkstt|jt|jt|j	|j
t|jp>tt|jdS )N)schemeuri_pathuserhostportpassword)r   r   urlsplitr   SCHEMESAssertionErrordictr   pathusernamehostnameintr   DEFAULT_PORTr   )Zuri_as_stringZ	split_urir	   r	   r
   	parse_uri<   s    r   c                 C   s@   t jt| t| }|d}|d t||fd|i|S )Nr   r   transport_params)
smart_openutilsZcheck_kwargsopenr   pop)urimoder   Z
parsed_urir   r	   r	   r
   open_uriI   s
    

r#   c                 C   sb   t  }|  |t   |di  }d|krB|d| |d| |j| |f| |S )NZconnect_kwargsZkey_filenamer   r   )	paramikoZ	SSHClientZload_system_host_keysZset_missing_host_key_policyZAutoAddPolicygetcopy
setdefaultconnect)r   r   r   r   r   r   kwargsr	   r	   r
   _connect_sshQ   s    r*   rc                 C   s   |st d|st }|s i }||f}d}t|D ]}	zt| }
W n, tk
rp   t||||| }
t|< Y nX z|
 }| }W  qW q4 t	j
k
r } z0|jo|jd dk}|	|d ks|sƂ t|= W 5 d}~X Y q4X q4|| |}| |_|S )ax  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.
    transport_params: dict, optional
        Any additional settings to be passed to paramiko.SSHClient.connect

    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
    z'you must specify the host to connect to   r   zSSH session not active   N)
ValueErrorgetpassgetuserrange_SSHKeyErrorr*   Zget_transportZopen_sftp_clientr$   ZSSHExceptionargsr   name)r   r"   r   r   r   r   r   keyZattemptsattemptr   	transportZsftp_clientexZconnection_timed_outZfobjr	   r	   r
   r   ]   s2    #
r   )__doc__r/   loggingurllib.parser   r$   ImportErrorZMISSING_DEPSZsmart_open.utilsr   	getLogger__name__loggerr2   r   r   ZURI_EXAMPLESr   r   r#   r*   r   r	   r	   r	   r
   <module>	   s$   

