B
    U[e                 @   s   d Z ddlm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 ddlmZ ddlmZmZ ddlZdd	lmZ dd
dZdddZdddZdddZdddZdddddgZdS )z1Connection file-related utilities for the kernel
    )absolute_importN)PopenPIPE)
ProfileDir)get_ipython_dir)filefind)str_to_bytesPY3)write_connection_filec             C   s>   | dkr,ddl m} | s$td| } t| jd| jgS )zReturn the path to the connection file of an app

    Parameters
    ----------
    app : IPKernelApp instance [optional]
        If unspecified, the currently running app will be used
    Nr   )IPKernelAppz.app not specified, and not in a running Kernel.)Zipykernel.kernelappr   initializedRuntimeErrorinstancer   connection_fileZconnection_dir)appr    r   0lib/python3.7/site-packages/ipykernel/connect.pyget_connection_file   s    r   kernel-*.jsonc             C   s   ddl }|jdtdd ddlm} yt| S  tk
r@   Y nX |dkrr| rb| }|j	}qt
t d}nt
t |}|j}tj| d|gd	S )
a  DEPRECATED: find a connection file, and return its absolute path.
    
    THIS FUNCTION IS DEPRECATED. Use juptyer_client.find_connection_file instead.

    Parameters
    ----------
    filename : str
        The connection file or fileglob to search for.
    profile : str [optional]
        The name of the profile to use when searching for the connection file,
        if different from the current IPython session or 'default'.

    Returns
    -------
    str : The absolute path of the connection file.
    r   NzUipykernel.find_connection_file is deprecated, use jupyter_client.find_connection_file   )
stacklevel)BaseIPythonApplicationdefaultr   )path)warningswarnDeprecationWarningZIPython.core.applicationr   r   IOErrorr   r   profile_dirr   Zfind_profile_dir_by_namer   security_dirjupyter_clientfind_connection_file)filenameprofiler   ZIPAppr   r   r    r   r   r   r"   (   s     
r"   c             C   s@   | dkrt  S |dk	r2tjdtdd t| |dS t| S dS )a  Return the absolute path for a connection file
    
    - If nothing specified, return current Kernel's connection file
    - If profile specified, show deprecation warning about finding connection files in profiles
    - Otherwise, call jupyter_client.find_connection_file
    Nz1Finding connection file by profile is deprecated.   )r   )r$   )r   r   r   r   r"   r!   )r   r$   r   r   r   _find_connection_fileT   s    
r&   Fc          	   C   sL   t | |}t|}| }W dQ R X |rHt|}t|dd|d< |S )a  Return the connection information for the current Kernel.

    Parameters
    ----------
    connection_file : str [optional]
        The connection file to be used. Can be given by absolute path, or
        IPython will search in the security directory of a given profile.
        If run from IPython,

        If unspecified, the connection file for the currently running
        IPython Kernel will be used, which is only allowed from inside a kernel.
    unpack : bool [default: False]
        if True, return the unpacked dict, otherwise just the string contents
        of the file.
    profile : DEPRECATED

    Returns
    -------
    The connection dictionary of the current kernel, as string or dict,
    depending on `unpack`.
    Nkey )r&   openreadjsonloadsr   get)r   Zunpackr$   cffinfor   r   r   get_connection_infoj   s    


r1   c             C   sf   |dkrg n|}t | |}dddg}i }tr8d|d< ttjd|d|g| ftttjd	kd
|S )a  Connect a qtconsole to the current kernel.

    This is useful for connecting a second qtconsole to a kernel, or to a
    local notebook.

    Parameters
    ----------
    connection_file : str [optional]
        The connection file to be used. Can be given by absolute path, or
        IPython will search in the security directory of a given profile.
        If run from IPython,

        If unspecified, the connection file for the currently running
        IPython Kernel will be used, which is only allowed from inside a kernel.
    argv : list [optional]
        Any extra args to be passed to the console.
    profile : DEPRECATED

    Returns
    -------
    :class:`subprocess.Popen` instance running the qtconsole frontend
    N;z+from IPython.qt.console import qtconsoleappzqtconsoleapp.main()TZstart_new_sessionz-cz
--existingZwin32)stdoutstderrZ	close_fds)r&   joinr	   r   sys
executabler   platform)r   argvr$   r.   cmdkwargsr   r   r   connect_qtconsole   s    
r<   r
   )N)r   N)N)NFN)NNN)__doc__Z
__future__r   r+   r6   
subprocessr   r   r   ZIPython.core.profiledirr   ZIPython.pathsr   Zipython_genutils.pathr   Zipython_genutils.py3compatr   r	   r!   r
   r   r"   r&   r1   r<   __all__r   r   r   r   <module>   s*   

,

"
-