B
    \A&                 @   s   d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	 ddl
mZmZmZmZmZmZ ddlmZ dd	lmZmZ G d
d deZdd ZG dd deZdS )z%A kernel manager for multiple kernels    )absolute_importN)LoggingConfigurable)import_item)InstanceDictListUnicodeAnyDottedObjectName)unicode_type   )NATIVE_KERNEL_NAMEKernelSpecManagerc               @   s   e Zd ZdS )DuplicateKernelErrorN)__name__
__module____qualname__ r   r   @lib/python3.7/site-packages/jupyter_client/multikernelmanager.pyr      s   r   c                s    fdd}|S )zDdecorator for proxying MKM.method(kernel_id) to individual KMs by IDc                s6   |  |}t| j}|||} | |f|| |S )N)
get_kernelgetattrr   )self	kernel_idargskwargskmmethodr)fr   r   wrapped   s
    

zkernel_method.<locals>.wrappedr   )r   r   r   )r   r   kernel_method   s    r    c               @   s~  e Zd ZdZeedddZeeddZ	e
ddddZdd	 Zed
dZdd ZedZdd ZedZe Zdd Zdd Zdd ZdDddZedEddZedFddZedGd!d"ZedHd#d$Zd%d& ZdId'd(Zed)d* Z ed+d, Z!edJd-d.Z"ed/d0 Z#d1d2 Z$d3d4 Z%edKd6d7Z&edLd8d9Z'ed:d; Z(edMd<d=Z)edNd>d?Z*edOd@dAZ+edPdBdCZ,dS )QMultiKernelManagerz&A class for managing multiple kernels.Tz'The name of the default kernel to start)Zconfighelp)Z
allow_nonez)jupyter_client.ioloop.IOLoopKernelManagerzThe kernel manager class.  This is configurable to allow
        subclassing of the KernelManager for customized behavior.
        c             C   s   t || _d S )N)r   kernel_manager_factory)r   nameoldnewr   r   r   _kernel_manager_class_changed9   s    z0MultiKernelManager._kernel_manager_class_changedz)this is kernel_manager_class after import)r"   c             C   s
   t | jS )N)r   kernel_manager_class)r   r   r   r   _kernel_manager_factory_default=   s    z2MultiKernelManager._kernel_manager_factory_defaultzzmq.Contextc             C   s
   t j S )N)zmqZContextinstance)r   r   r   r   _context_defaultA   s    z#MultiKernelManager._context_default c             C   s   t | j S )z6Return a list of the kernel ids of the active kernels.)list_kernelskeys)r   r   r   r   list_kernel_idsH   s    z"MultiKernelManager.list_kernel_idsc             C   s   t |  S )z%Return the number of running kernels.)lenr1   )r   r   r   r   __len__N   s    zMultiKernelManager.__len__c             C   s
   || j kS )N)r/   )r   r   r   r   r   __contains__R   s    zMultiKernelManager.__contains__Nc             K   s   | dtt }|| kr(td| |dkr6| j}i }| jrJ| j|d< | jf tj	
| jd| | | j|d|}|jf | || j|< |S )zStart a new kernel.

        The caller can pick a kernel_id by passing one in as a keyword arg,
        otherwise one will be picked using a uuid.

        The kernel ID for the newly started kernel is returned.
        r   zKernel already exists: %sNkernel_spec_managerzkernel-%s.json)connection_fileparentlogkernel_name)popr   uuidZuuid4r   default_kernel_namer5   r#   ospathjoinconnection_dirr8   start_kernelr/   )r   r9   r   r   Zconstructor_kwargsr   r   r   r   rA   U   s    

zMultiKernelManager.start_kernelFc             C   s   | j d|  | | dS )a3  Shutdown a kernel by its kernel uuid.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel to shutdown.
        now : bool
            Should the kernel be shutdown forcibly using a signal.
        restart : bool
            Will the kernel be restarted?
        zKernel shutdown: %sN)r8   inforemove_kernel)r   r   nowrestartr   r   r   shutdown_kernelr   s    z"MultiKernelManager.shutdown_kernelc             C   s   dS )z,Ask a kernel to shut down by its kernel uuidNr   )r   r   rE   r   r   r   request_shutdown   s    z#MultiKernelManager.request_shutdown皙?c             C   s   | j d|  dS )zMWait for a kernel to finish shutting down, and kill it if it doesn't
        zKernel shutdown: %sN)r8   rB   )r   r   ZwaittimeZpollintervalr   r   r   finish_shutdown   s    z"MultiKernelManager.finish_shutdownc             C   s   dS )zClean up a kernel's resourcesNr   )r   r   r6   r   r   r   cleanup   s    zMultiKernelManager.cleanupc             C   s   | j |S )zremove a kernel from our mapping.

        Mainly so that a kernel can be removed if it is already dead,
        without having to call shutdown_kernel.

        The kernel object is returned.
        )r/   r:   )r   r   r   r   r   rC      s    z MultiKernelManager.remove_kernelc             C   sP   |   }x|D ]}| | qW x*|D ]"}| | | | | | q&W dS )zShutdown all kernels.N)r1   rG   rI   rJ   rC   )r   rD   ZkidsZkidr   r   r   shutdown_all   s    



zMultiKernelManager.shutdown_allc             C   s   | j d|  dS )zInterrupt (SIGINT) the kernel by its uuid.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel to interrupt.
        zKernel interrupted: %sN)r8   rB   )r   r   r   r   r   interrupt_kernel   s    	z#MultiKernelManager.interrupt_kernelc             C   s   | j d||f  dS )a  Sends a signal to the kernel by its uuid.

        Note that since only SIGTERM is supported on Windows, this function
        is only useful on Unix systems.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel to signal.
        zSignaled Kernel %s with %sN)r8   rB   )r   r   Zsignumr   r   r   signal_kernel   s    z MultiKernelManager.signal_kernelc             C   s   | j d|  dS )zRestart a kernel by its uuid, keeping the same ports.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel to interrupt.
        zKernel restarted: %sN)r8   rB   )r   r   rD   r   r   r   restart_kernel   s    	z!MultiKernelManager.restart_kernelc             C   s   dS )zIs the kernel alive.

        This calls KernelManager.is_alive() which calls Popen.poll on the
        actual kernel subprocess.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel.
        Nr   )r   r   r   r   r   is_alive   s    zMultiKernelManager.is_alivec             C   s   || krt d| dS )zcheck that a kernel id is validzKernel with id not found: %sN)KeyError)r   r   r   r   r   _check_kernel_id   s    z#MultiKernelManager._check_kernel_idc             C   s   |  | | j| S )zGet the single KernelManager object for a kernel by its uuid.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel.
        )rQ   r/   )r   r   r   r   r   r      s    
zMultiKernelManager.get_kernelrE   c             C   s   dS )z&add a callback for the KernelRestarterNr   )r   r   callbackeventr   r   r   add_restart_callback   s    z'MultiKernelManager.add_restart_callbackc             C   s   dS )z)remove a callback for the KernelRestarterNr   )r   r   rR   rS   r   r   r   remove_restart_callback   s    z*MultiKernelManager.remove_restart_callbackc             C   s   dS )a  Return a dictionary of connection data for a kernel.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel.

        Returns
        =======
        connection_dict : dict
            A dict of the information needed to connect to a kernel.
            This includes the ip address and the integer port
            numbers of the different channels (stdin_port, iopub_port,
            shell_port, hb_port).
        Nr   )r   r   r   r   r   get_connection_info   s    z&MultiKernelManager.get_connection_infoc             C   s   dS )a6  Return a zmq Socket connected to the iopub channel.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel
        identity : bytes (optional)
            The zmq identity of the socket

        Returns
        =======
        stream : zmq Socket or ZMQStream
        Nr   )r   r   identityr   r   r   connect_iopub   s    z MultiKernelManager.connect_iopubc             C   s   dS )a6  Return a zmq Socket connected to the shell channel.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel
        identity : bytes (optional)
            The zmq identity of the socket

        Returns
        =======
        stream : zmq Socket or ZMQStream
        Nr   )r   r   rW   r   r   r   connect_shell  s    z MultiKernelManager.connect_shellc             C   s   dS )a6  Return a zmq Socket connected to the stdin channel.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel
        identity : bytes (optional)
            The zmq identity of the socket

        Returns
        =======
        stream : zmq Socket or ZMQStream
        Nr   )r   r   rW   r   r   r   connect_stdin  s    z MultiKernelManager.connect_stdinc             C   s   dS )a3  Return a zmq Socket connected to the hb channel.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel
        identity : bytes (optional)
            The zmq identity of the socket

        Returns
        =======
        stream : zmq Socket or ZMQStream
        Nr   )r   r   rW   r   r   r   
connect_hb/  s    zMultiKernelManager.connect_hb)N)FF)F)NrH   )T)F)F)rE   )rE   )N)N)N)N)-r   r   r   __doc__r   r   r<   r   r   r5   r
   r(   r'   r	   r#   r)   contextr,   r@   r   r/   r1   r3   r4   rA   r    rF   rG   rI   rJ   rC   rK   rL   rM   rN   rO   rQ   r   rT   rU   rV   rX   rY   rZ   r[   r   r   r   r   r!   *   s`   





r!   )r\   Z
__future__r   r=   r;   r*   Ztraitlets.config.configurabler   Zipython_genutils.importstringr   Z	traitletsr   r   r   r   r	   r
   Zipython_genutils.py3compatr   Z
kernelspecr   r   	Exceptionr   r    r!   r   r   r   r   <module>   s    