B
    Zeo[©  ã               @   s   d Z G dd„ deƒZdS )z@Defines a convenient mix-in class for implementing Qt frontends.c               @   sŒ   e Zd ZdZdZdZedd„ ƒZejdd„ ƒZedd„ ƒZ	e	jdd„ ƒZ	d	d
„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚBaseFrontendMixinzô A mix-in class for implementing Qt frontends.

    To handle messages of a particular type, frontends need only define an
    appropriate handler method. For example, to handle 'stream' messaged, define
    a '_handle_stream(msg)' method.
    Nc             C   s   | j S )z"Returns the current kernel client.)Ú_kernel_client)Úself© r   ú<lib/python3.7/site-packages/qtconsole/base_frontend_mixin.pyÚkernel_client   s    zBaseFrontendMixin.kernel_clientc             C   sø   | j }|dk	rx|j | j¡ |j | j¡ |jj | j¡ |j	j | j¡ |j
j | j¡ |jj | j¡ |jrx|  ¡  || _ |dkrŠdS |j | j¡ |j | j¡ |jj | j¡ |j	j | j¡ |j
j | j¡ |jj | j¡ |jrô|  ¡  dS )zdDisconnect from the current kernel client (if any) and set a new
            kernel client.
        N)r   Zstarted_channelsÚ
disconnectÚ_started_channelsZstopped_channelsÚ_stopped_channelsZiopub_channelZmessage_receivedÚ	_dispatchZshell_channelZstdin_channelZ
hb_channelZkernel_diedÚ_handle_kernel_diedZchannels_runningÚconnect)r   r   Z
old_clientr   r   r   r      s,    c             C   s   | j S )zThe kernel manager, if any)Ú_kernel_manager)r   r   r   r   Úkernel_managerB   s    z BaseFrontendMixin.kernel_managerc             C   s@   | j }|d k	r|j | j¡ || _ |d kr.d S |j | j¡ d S )N)r   Zkernel_restartedr   Ú_handle_kernel_restartedr   )r   r   Zold_manr   r   r   r   G   s    c             C   s   dS )a´   This is called when the ``kernel_died`` signal is emitted.

        This method is called when the kernel heartbeat has not been
        active for a certain amount of time.
        This is a strictly passive notification -
        the kernel is likely being restarted by its KernelManager.

        Parameters
        ----------
        since_last_heartbeat : float
            The time since the heartbeat was last received.
        Nr   )r   Zsince_last_heartbeatr   r   r   r   W   s    z%BaseFrontendMixin._handle_kernel_diedc             C   s   dS )a9   This is called when the ``kernel_restarted`` signal is emitted.

        This method is called when the kernel has been restarted by the
        autorestart mechanism.

        Parameters
        ----------
        since_last_heartbeat : float
            The time since the heartbeat was last received.
        Nr   )r   r   r   r   r   e   s    
z*BaseFrontendMixin._handle_kernel_restartedc             C   s   dS )zŒCalled when the KernelManager starts (or restarts) the kernel subprocess.
        Channels may or may not be running at this point.
        Nr   )r   r   r   r   Ú_started_kernelp   s    z!BaseFrontendMixin._started_kernelc             C   s   dS )z˜ Called when the KernelManager channels have started listening or
            when the frontend is assigned an already listening KernelManager.
        Nr   )r   r   r   r   r   u   s    z#BaseFrontendMixin._started_channelsc             C   s   dS )z“ Called when the KernelManager channels have stopped listening or
            when a listening KernelManager is removed from the frontend.
        Nr   )r   r   r   r   r	   z   s    z#BaseFrontendMixin._stopped_channelsc             C   s,   |d d }t | d| dƒ}|r(||ƒ dS )zg Calls the frontend handler associated with the message type of the
            given message.
        ÚheaderÚmsg_typeZ_handle_N)Úgetattr)r   Úmsgr   Zhandlerr   r   r   r
   ƒ   s    zBaseFrontendMixin._dispatchc             C   s   | j jj}|d  d|¡|kS )z-Return whether a message is from this sessionZparent_headerÚsession)r   r   Úget)r   r   Z
session_idr   r   r   Ú	from_hereŒ   s    
zBaseFrontendMixin.from_herec             C   s>   | j r
dS |  |¡}|d dkr,| jo*| S | jr6dS |S dS )z7Return whether we should include a given output messageFr   Zexecute_inputTN)Z_hiddenr   Zinclude_other_output)r   r   r   r   r   r   Úinclude_output‘   s    
z BaseFrontendMixin.include_output)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Úpropertyr   Úsetterr   r   r   r   r   r	   r
   r   r   r   r   r   r   r      s   +		r   N)r   Úobjectr   r   r   r   r   Ú<module>   s   