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