Show More
@@ -1,109 +1,114 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 |
|
15 | |||
16 | def _get_kernel_manager(self): |
|
16 | def _get_kernel_manager(self): | |
17 | """ Returns the current kernel manager. |
|
17 | """ Returns the current kernel manager. | |
18 | """ |
|
18 | """ | |
19 | return self._kernel_manager |
|
19 | return self._kernel_manager | |
20 |
|
20 | |||
21 | def _set_kernel_manager(self, kernel_manager): |
|
21 | def _set_kernel_manager(self, kernel_manager): | |
22 | """ Disconnect from the current kernel manager (if any) and set a new |
|
22 | """ Disconnect from the current kernel manager (if any) and set a new | |
23 | kernel manager. |
|
23 | kernel manager. | |
24 | """ |
|
24 | """ | |
25 | # Disconnect the old kernel manager, if necessary. |
|
25 | # Disconnect the old kernel manager, if necessary. | |
26 | old_manager = self._kernel_manager |
|
26 | old_manager = self._kernel_manager | |
27 | if old_manager is not None: |
|
27 | if old_manager is not None: | |
28 | old_manager.started_channels.disconnect(self._started_channels) |
|
28 | old_manager.started_channels.disconnect(self._started_channels) | |
29 | old_manager.stopped_channels.disconnect(self._stopped_channels) |
|
29 | old_manager.stopped_channels.disconnect(self._stopped_channels) | |
30 |
|
30 | |||
31 | # Disconnect the old kernel manager's channels. |
|
31 | # Disconnect the old kernel manager's channels. | |
32 | old_manager.sub_channel.message_received.disconnect(self._dispatch) |
|
32 | old_manager.sub_channel.message_received.disconnect(self._dispatch) | |
33 | old_manager.shell_channel.message_received.disconnect(self._dispatch) |
|
33 | old_manager.shell_channel.message_received.disconnect(self._dispatch) | |
34 | old_manager.stdin_channel.message_received.disconnect(self._dispatch) |
|
34 | old_manager.stdin_channel.message_received.disconnect(self._dispatch) | |
35 | old_manager.hb_channel.kernel_died.disconnect( |
|
35 | old_manager.hb_channel.kernel_died.disconnect( | |
36 | self._handle_kernel_died) |
|
36 | self._handle_kernel_died) | |
37 |
|
37 | |||
38 | # Handle the case where the old kernel manager is still listening. |
|
38 | # Handle the case where the old kernel manager is still listening. | |
39 | if old_manager.channels_running: |
|
39 | if old_manager.channels_running: | |
40 | self._stopped_channels() |
|
40 | self._stopped_channels() | |
41 |
|
41 | |||
42 | # Set the new kernel manager. |
|
42 | # Set the new kernel manager. | |
43 | self._kernel_manager = kernel_manager |
|
43 | self._kernel_manager = kernel_manager | |
44 | if kernel_manager is None: |
|
44 | if kernel_manager is None: | |
45 | return |
|
45 | return | |
46 |
|
46 | |||
47 | # Connect the new kernel manager. |
|
47 | # Connect the new kernel manager. | |
48 | kernel_manager.started_channels.connect(self._started_channels) |
|
48 | kernel_manager.started_channels.connect(self._started_channels) | |
49 | kernel_manager.stopped_channels.connect(self._stopped_channels) |
|
49 | kernel_manager.stopped_channels.connect(self._stopped_channels) | |
50 |
|
50 | |||
51 | # Connect the new kernel manager's channels. |
|
51 | # Connect the new kernel manager's channels. | |
52 | kernel_manager.sub_channel.message_received.connect(self._dispatch) |
|
52 | kernel_manager.sub_channel.message_received.connect(self._dispatch) | |
53 | kernel_manager.shell_channel.message_received.connect(self._dispatch) |
|
53 | kernel_manager.shell_channel.message_received.connect(self._dispatch) | |
54 | kernel_manager.stdin_channel.message_received.connect(self._dispatch) |
|
54 | kernel_manager.stdin_channel.message_received.connect(self._dispatch) | |
55 | kernel_manager.hb_channel.kernel_died.connect(self._handle_kernel_died) |
|
55 | kernel_manager.hb_channel.kernel_died.connect(self._handle_kernel_died) | |
56 |
|
56 | |||
57 | # Handle the case where the kernel manager started channels before |
|
57 | # Handle the case where the kernel manager started channels before | |
58 | # we connected. |
|
58 | # we connected. | |
59 | if kernel_manager.channels_running: |
|
59 | if kernel_manager.channels_running: | |
60 | self._started_channels() |
|
60 | self._started_channels() | |
61 |
|
61 | |||
62 | kernel_manager = property(_get_kernel_manager, _set_kernel_manager) |
|
62 | kernel_manager = property(_get_kernel_manager, _set_kernel_manager) | |
63 |
|
63 | |||
64 | #--------------------------------------------------------------------------- |
|
64 | #--------------------------------------------------------------------------- | |
65 | # 'BaseFrontendMixin' abstract interface |
|
65 | # 'BaseFrontendMixin' abstract interface | |
66 | #--------------------------------------------------------------------------- |
|
66 | #--------------------------------------------------------------------------- | |
67 |
|
67 | |||
68 | def _handle_kernel_died(self, since_last_heartbeat): |
|
68 | def _handle_kernel_died(self, since_last_heartbeat): | |
69 | """ This is called when the ``kernel_died`` signal is emitted. |
|
69 | """ This is called when the ``kernel_died`` signal is emitted. | |
70 |
|
70 | |||
71 | This method is called when the kernel heartbeat has not been |
|
71 | 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 |
|
72 | active for a certain amount of time. The typical action will be to | |
73 | give the user the option of restarting the kernel. |
|
73 | give the user the option of restarting the kernel. | |
74 |
|
74 | |||
75 | Parameters |
|
75 | Parameters | |
76 | ---------- |
|
76 | ---------- | |
77 | since_last_heartbeat : float |
|
77 | since_last_heartbeat : float | |
78 | The time since the heartbeat was last received. |
|
78 | The time since the heartbeat was last received. | |
79 | """ |
|
79 | """ | |
80 |
|
80 | |||
81 | def _started_channels(self): |
|
81 | def _started_channels(self): | |
82 | """ Called when the KernelManager channels have started listening or |
|
82 | """ Called when the KernelManager channels have started listening or | |
83 | when the frontend is assigned an already listening KernelManager. |
|
83 | when the frontend is assigned an already listening KernelManager. | |
84 | """ |
|
84 | """ | |
85 |
|
85 | |||
86 | def _stopped_channels(self): |
|
86 | def _stopped_channels(self): | |
87 | """ Called when the KernelManager channels have stopped listening or |
|
87 | """ Called when the KernelManager channels have stopped listening or | |
88 | when a listening KernelManager is removed from the frontend. |
|
88 | when a listening KernelManager is removed from the frontend. | |
89 | """ |
|
89 | """ | |
90 |
|
90 | |||
91 | #--------------------------------------------------------------------------- |
|
91 | #--------------------------------------------------------------------------- | |
92 | # 'BaseFrontendMixin' protected interface |
|
92 | # 'BaseFrontendMixin' protected interface | |
93 | #--------------------------------------------------------------------------- |
|
93 | #--------------------------------------------------------------------------- | |
94 |
|
94 | |||
95 | def _dispatch(self, msg): |
|
95 | def _dispatch(self, msg): | |
96 | """ Calls the frontend handler associated with the message type of the |
|
96 | """ Calls the frontend handler associated with the message type of the | |
97 | given message. |
|
97 | given message. | |
98 | """ |
|
98 | """ | |
99 | msg_type = msg['header']['msg_type'] |
|
99 | msg_type = msg['header']['msg_type'] | |
100 | handler = getattr(self, '_handle_' + msg_type, None) |
|
100 | handler = getattr(self, '_handle_' + msg_type, None) | |
101 | if handler: |
|
101 | if handler: | |
102 | handler(msg) |
|
102 | handler(msg) | |
103 |
|
103 | |||
104 | def _is_from_this_session(self, msg): |
|
104 | def _is_from_this_session(self, msg): | |
105 | """ Returns whether a reply from the kernel originated from a request |
|
105 | """ Returns whether a reply from the kernel originated from a request | |
106 | from this frontend. |
|
106 | from this frontend. | |
107 | """ |
|
107 | """ | |
108 | session = self._kernel_manager.session.session |
|
108 | session = self._kernel_manager.session.session | |
109 |
re |
|
109 | parent = msg['parent_header'] | |
|
110 | if not parent: | |||
|
111 | # if the message has no parent, assume it is meant for all frontends | |||
|
112 | return True | |||
|
113 | else: | |||
|
114 | return parent.get('session') == session |
General Comments 0
You need to be logged in to leave comments.
Login now