##// END OF EJS Templates
make sure _kernel_client is defined
MinRK -
Show More
@@ -1,119 +1,120 b''
1 """ Defines a convenient mix-in class for implementing Qt frontends.
1 """ Defines a convenient mix-in class for implementing Qt frontends.
2 """
2 """
3
3
4 class BaseFrontendMixin(object):
4 class BaseFrontendMixin(object):
5 """ A mix-in class for implementing Qt frontends.
5 """ A mix-in class for implementing Qt frontends.
6
6
7 To handle messages of a particular type, frontends need only define an
7 To handle messages of a particular type, frontends need only define an
8 appropriate handler method. For example, to handle 'stream' messaged, define
8 appropriate handler method. For example, to handle 'stream' messaged, define
9 a '_handle_stream(msg)' method.
9 a '_handle_stream(msg)' method.
10 """
10 """
11
11
12 #---------------------------------------------------------------------------
12 #---------------------------------------------------------------------------
13 # 'BaseFrontendMixin' concrete interface
13 # 'BaseFrontendMixin' concrete interface
14 #---------------------------------------------------------------------------
14 #---------------------------------------------------------------------------
15 _kernel_client = None
15
16
16 def _get_kernel_client(self):
17 def _get_kernel_client(self):
17 """Returns the current kernel client.
18 """Returns the current kernel client.
18 """
19 """
19 return self._kernel_client
20 return self._kernel_client
20
21
21 def _set_kernel_client(self, kernel_client):
22 def _set_kernel_client(self, kernel_client):
22 """Disconnect from the current kernel client (if any) and set a new
23 """Disconnect from the current kernel client (if any) and set a new
23 kernel client.
24 kernel client.
24 """
25 """
25 # Disconnect the old kernel client, if necessary.
26 # Disconnect the old kernel client, if necessary.
26 old_client = self._kernel_client
27 old_client = self._kernel_client
27 if old_client is not None:
28 if old_client is not None:
28 old_client.started_channels.disconnect(self._started_channels)
29 old_client.started_channels.disconnect(self._started_channels)
29 old_client.stopped_channels.disconnect(self._stopped_channels)
30 old_client.stopped_channels.disconnect(self._stopped_channels)
30
31
31 # Disconnect the old kernel client's channels.
32 # Disconnect the old kernel client's channels.
32 old_client.iopub_channel.message_received.disconnect(self._dispatch)
33 old_client.iopub_channel.message_received.disconnect(self._dispatch)
33 old_client.shell_channel.message_received.disconnect(self._dispatch)
34 old_client.shell_channel.message_received.disconnect(self._dispatch)
34 old_client.stdin_channel.message_received.disconnect(self._dispatch)
35 old_client.stdin_channel.message_received.disconnect(self._dispatch)
35 old_client.hb_channel.kernel_died.disconnect(
36 old_client.hb_channel.kernel_died.disconnect(
36 self._handle_kernel_died)
37 self._handle_kernel_died)
37
38
38 # Handle the case where the old kernel client is still listening.
39 # Handle the case where the old kernel client is still listening.
39 if old_client.channels_running:
40 if old_client.channels_running:
40 self._stopped_channels()
41 self._stopped_channels()
41
42
42 # Set the new kernel client.
43 # Set the new kernel client.
43 self._kernel_client = kernel_client
44 self._kernel_client = kernel_client
44 if kernel_client is None:
45 if kernel_client is None:
45 return
46 return
46
47
47 # Connect the new kernel client.
48 # Connect the new kernel client.
48 kernel_client.started_channels.connect(self._started_channels)
49 kernel_client.started_channels.connect(self._started_channels)
49 kernel_client.stopped_channels.connect(self._stopped_channels)
50 kernel_client.stopped_channels.connect(self._stopped_channels)
50
51
51 # Connect the new kernel client's channels.
52 # Connect the new kernel client's channels.
52 kernel_client.iopub_channel.message_received.connect(self._dispatch)
53 kernel_client.iopub_channel.message_received.connect(self._dispatch)
53 kernel_client.shell_channel.message_received.connect(self._dispatch)
54 kernel_client.shell_channel.message_received.connect(self._dispatch)
54 kernel_client.stdin_channel.message_received.connect(self._dispatch)
55 kernel_client.stdin_channel.message_received.connect(self._dispatch)
55 kernel_client.hb_channel.kernel_died.connect(self._handle_kernel_died)
56 kernel_client.hb_channel.kernel_died.connect(self._handle_kernel_died)
56
57
57 # Handle the case where the kernel client started channels before
58 # Handle the case where the kernel client started channels before
58 # we connected.
59 # we connected.
59 if kernel_client.channels_running:
60 if kernel_client.channels_running:
60 self._started_channels()
61 self._started_channels()
61
62
62 kernel_client = property(_get_kernel_client, _set_kernel_client)
63 kernel_client = property(_get_kernel_client, _set_kernel_client)
63
64
64 #---------------------------------------------------------------------------
65 #---------------------------------------------------------------------------
65 # 'BaseFrontendMixin' abstract interface
66 # 'BaseFrontendMixin' abstract interface
66 #---------------------------------------------------------------------------
67 #---------------------------------------------------------------------------
67
68
68 def _handle_kernel_died(self, since_last_heartbeat):
69 def _handle_kernel_died(self, since_last_heartbeat):
69 """ This is called when the ``kernel_died`` signal is emitted.
70 """ This is called when the ``kernel_died`` signal is emitted.
70
71
71 This method is called when the kernel heartbeat has not been
72 This method is called when the kernel heartbeat has not been
72 active for a certain amount of time. The typical action will be to
73 active for a certain amount of time. The typical action will be to
73 give the user the option of restarting the kernel.
74 give the user the option of restarting the kernel.
74
75
75 Parameters
76 Parameters
76 ----------
77 ----------
77 since_last_heartbeat : float
78 since_last_heartbeat : float
78 The time since the heartbeat was last received.
79 The time since the heartbeat was last received.
79 """
80 """
80
81
81 def _started_kernel(self):
82 def _started_kernel(self):
82 """Called when the KernelManager starts (or restarts) the kernel subprocess.
83 """Called when the KernelManager starts (or restarts) the kernel subprocess.
83 Channels may or may not be running at this point.
84 Channels may or may not be running at this point.
84 """
85 """
85
86
86 def _started_channels(self):
87 def _started_channels(self):
87 """ Called when the KernelManager channels have started listening or
88 """ Called when the KernelManager channels have started listening or
88 when the frontend is assigned an already listening KernelManager.
89 when the frontend is assigned an already listening KernelManager.
89 """
90 """
90
91
91 def _stopped_channels(self):
92 def _stopped_channels(self):
92 """ Called when the KernelManager channels have stopped listening or
93 """ Called when the KernelManager channels have stopped listening or
93 when a listening KernelManager is removed from the frontend.
94 when a listening KernelManager is removed from the frontend.
94 """
95 """
95
96
96 #---------------------------------------------------------------------------
97 #---------------------------------------------------------------------------
97 # 'BaseFrontendMixin' protected interface
98 # 'BaseFrontendMixin' protected interface
98 #---------------------------------------------------------------------------
99 #---------------------------------------------------------------------------
99
100
100 def _dispatch(self, msg):
101 def _dispatch(self, msg):
101 """ Calls the frontend handler associated with the message type of the
102 """ Calls the frontend handler associated with the message type of the
102 given message.
103 given message.
103 """
104 """
104 msg_type = msg['header']['msg_type']
105 msg_type = msg['header']['msg_type']
105 handler = getattr(self, '_handle_' + msg_type, None)
106 handler = getattr(self, '_handle_' + msg_type, None)
106 if handler:
107 if handler:
107 handler(msg)
108 handler(msg)
108
109
109 def _is_from_this_session(self, msg):
110 def _is_from_this_session(self, msg):
110 """ Returns whether a reply from the kernel originated from a request
111 """ Returns whether a reply from the kernel originated from a request
111 from this frontend.
112 from this frontend.
112 """
113 """
113 session = self._kernel_client.session.session
114 session = self._kernel_client.session.session
114 parent = msg['parent_header']
115 parent = msg['parent_header']
115 if not parent:
116 if not parent:
116 # if the message has no parent, assume it is meant for all frontends
117 # if the message has no parent, assume it is meant for all frontends
117 return True
118 return True
118 else:
119 else:
119 return parent.get('session') == session
120 return parent.get('session') == session
General Comments 0
You need to be logged in to leave comments. Login now