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