σ
~-¬[c           @@  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
  Z d d d  Z d d  Z d e d d  Z d d d d  Z d d d d d g Z d S(   s1   Connection file-related utilities for the kernel
i    (   t   absolute_importN(   t   Popent   PIPE(   t
   ProfileDir(   t   get_ipython_dir(   t   filefind(   t   str_to_bytest   PY3(   t   write_connection_filec         C@  s_   |  d k rF d d l m } | j   s7 t d   n  | j   }  n  t |  j d |  j g  S(   s½   Return the path to the connection file of an app

    Parameters
    ----------
    app : IPKernelApp instance [optional]
        If unspecified, the currently running app will be used
    i    (   t   IPKernelApps.   app not specified, and not in a running Kernelt   .N(	   t   Nonet   ipykernel.kernelappR	   t   initializedt   RuntimeErrort   instanceR   t   connection_filet   connection_dir(   t   appR	   (    (    s0   lib/python2.7/site-packages/ipykernel/connect.pyt   get_connection_file   s    s   kernel-*.jsonc         C@  sΣ   d d l  } | j d t d d d d l m } y t |   SWn t k
 rS n X| d k r | j   r | j	   } | j
 } q± t j t   d  } n t j t   |  } | j } t j |  d d	 | g S(
   s  DEPRECATED: find a connection file, and return its absolute path.
    
    THIS FUNCION 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.
    i    NsU   ipykernel.find_connection_file is deprecated, use jupyter_client.find_connection_filet
   stackleveli   (   t   BaseIPythonApplicationt   defaultt   pathR
   (   t   warningst   warnt   DeprecationWarningt   IPython.core.applicationR   R   t   IOErrorR   R   R   t   profile_dirR   t   find_profile_dir_by_nameR   t   security_dirt   jupyter_clientt   find_connection_file(   t   filenamet   profileR   t   IPAppR   R   R   (    (    s0   lib/python2.7/site-packages/ipykernel/connect.pyR!   (   s     		c         C@  sV   |  d k r t   S| d k	 rE t j d t d d t |  d | St j |   Sd S(   s  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
    s1   Finding connection file by profile is deprecated.R   i   R#   N(   R   R   R   R   R   R!   R    (   R   R#   (    (    s0   lib/python2.7/site-packages/ipykernel/connect.pyt   _find_connection_fileT   s    c         C@  sh   t  |  |  } t |   } | j   } Wd QX| rd t j |  } t | j d d   | d <n  | S(   sζ  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`.
    Nt   keyt    (   R%   t   opent   readt   jsont   loadsR   t   get(   R   t   unpackR#   t   cft   ft   info(    (    s0   lib/python2.7/site-packages/ipykernel/connect.pyt   get_connection_infoj   s    c      	   C@  s   | d k r g  n | } t |  |  } d j d d g  } i  } t rU t | d <n  t t j d | d | g | d t d t d	 t j	 d
 k | S(   sβ  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
    t   ;s+   from IPython.qt.console import qtconsoleapps   qtconsoleapp.main()t   start_new_sessions   -cs
   --existingt   stdoutt   stderrt	   close_fdst   win32N(
   R   R%   t   joinR   t   TrueR   t   syst
   executableR   t   platform(   R   t   argvR#   R.   t   cmdt   kwargs(    (    s0   lib/python2.7/site-packages/ipykernel/connect.pyt   connect_qtconsole   s    R   R   R!   R1   R@   (   t   __doc__t
   __future__R    R*   R:   t
   subprocessR   R   R   t   IPython.core.profiledirR   t   IPython.pathsR   t   ipython_genutils.pathR   t   ipython_genutils.py3compatR   R   R    R   R   R   R!   R%   t   FalseR1   R@   t   __all__(    (    (    s0   lib/python2.7/site-packages/ipykernel/connect.pyt   <module>   s*   ,"-