##// END OF EJS Templates
Simplify handling of messaging in zmqterminal.
Thomas Kluyver -
Show More
@@ -16,16 +16,15 b' class ClientCompleter2p(object):'
16 16
17 17 def complete_request(self,text):
18 18 line = readline.get_line_buffer()
19 #msg_id = self.km.xreq_channel.complete(text=text,line=line)#this method is not working, the code not continue
20 msg = self.km.session.send(self.km.xreq_channel.socket,
21 'complete_request',
22 dict(text=text, line=line))
19 cursor_pos = readline.get_endidx()
20
23 21 # send completion request to kernel
24 22 # Give the kernel up to 0.5s to respond
23 msg_id = self.km.xreq_channel.complete(text=text, line=line,
24 cursor_pos=cursor_pos)
25
25 26 msg_xreq = self.km.xreq_channel.get_msg(timeout=0.5)
26 if msg["header"]['session'] == msg_xreq["parent_header"]['session'] and \
27 msg_xreq["content"]["status"] == 'ok' and \
28 msg_xreq["msg_type"] == "complete_reply" :
27 if msg_xreq['parent_header']['msg_id'] == msg_id:
29 28 return msg_xreq["content"]["matches"]
30 29 return []
31 30
@@ -50,11 +50,7 b' class Frontend(object):'
50 50
51 51 def __init__(self, kernelmanager):
52 52 self.km = kernelmanager
53 self.session = kernelmanager.session
54 self.request_socket = self.km.xreq_channel.socket
55 self.sub_socket = self.km.sub_channel.socket
56 self.reply_socket = self.km.rep_channel.socket
57 self.msg_header = self.km.session.msg_header()
53 self.session_id = self.km.session.session
58 54 self.completer = ClientCompleter2p(self, self.km)
59 55 readline.parse_and_bind("tab: complete")
60 56 readline.parse_and_bind('set show-all-if-ambiguous on')
@@ -119,31 +115,30 b' class Frontend(object):'
119 115
120 116 See parent class :meth:`execute` docstring for full details.
121 117 """
122 self.km.xreq_channel.execute(source, hidden)
118 msg_id = self.km.xreq_channel.execute(source, hidden)
123 119 while not self.km.xreq_channel.msg_ready():
124 120 try:
125 121 self.handle_rep_channel(timeout=0.1)
126 122 except Empty:
127 123 pass
128 self.handle_xreq_channel()
124 self.handle_execute_reply(msg_id)
129 125
130 def handle_xreq_channel(self):
131 self.msg_xreq = self.km.xreq_channel.get_msg()
132 if self.msg_header["session"] == self.msg_xreq["parent_header"]["session"]:
133 if self.msg_xreq["content"]["status"] == 'ok' :
134 if self.msg_xreq["msg_type"] == "execute_reply" :
135 self.handle_sub_channel()
136 self.prompt_count = self.msg_xreq["content"]["execution_count"]+1
126 def handle_execute_reply(self, msg_id):
127 msg_xreq = self.km.xreq_channel.get_msg()
128 if msg_xreq["parent_header"]["msg_id"] == msg_id:
129 if msg_xreq["content"]["status"] == 'ok' :
130 self.handle_sub_channel()
131 self.prompt_count = msg_xreq["content"]["execution_count"] + 1
137 132
138 133 else:
139 etb = self.msg_xreq["content"]["traceback"]
134 etb = msg_xreq["content"]["traceback"]
140 135 print >> sys.stderr, etb[0]
141 136 try: # These bits aren't there for a SyntaxError
142 137 print >> sys.stderr, etb[1]
143 138 print >> sys.stderr, etb[2]
144 139 except IndexError:
145 140 pass
146 self.prompt_count = self.msg_xreq["content"]["execution_count"]+1
141 self.prompt_count = msg_xreq["content"]["execution_count"] + 1
147 142
148 143
149 144 def handle_sub_channel(self):
@@ -158,30 +153,29 b' class Frontend(object):'
158 153 """
159 154 while self.km.sub_channel.msg_ready():
160 155 sub_msg = self.km.sub_channel.get_msg()
161 if self.msg_header["username"] == sub_msg['parent_header']['username'] and \
162 self.km.session.session == sub_msg['parent_header']['session']:
156 if self.session_id == sub_msg['parent_header']['session']:
163 157 if sub_msg['msg_type'] == 'status' :
164 158 if sub_msg["content"]["execution_state"] == "busy" :
165 159 pass
166 160
167 if sub_msg['msg_type'] == 'stream' :
161 elif sub_msg['msg_type'] == 'stream' :
168 162 if sub_msg["content"]["name"] == "stdout":
169 print >> sys.stdout,sub_msg["content"]["data"]
163 print >> sys.stdout, sub_msg["content"]["data"]
170 164 sys.stdout.flush()
171 if sub_msg["content"]["name"] == "stderr" :
172 print >> sys.stderr,sub_msg["content"]["data"]
165 elif sub_msg["content"]["name"] == "stderr" :
166 print >> sys.stderr, sub_msg["content"]["data"]
173 167 sys.stderr.flush()
174 168
175 if sub_msg['msg_type'] == 'pyout' :
169 elif sub_msg['msg_type'] == 'pyout' :
176 170 print >> sys.stdout,"Out[%i]:"%sub_msg["content"]["execution_count"], sub_msg["content"]["data"]["text/plain"]
177 171 sys.stdout.flush()
178 172
179 173 def handle_rep_channel(self, timeout=0.1):
180 174 """ Method to capture raw_input
181 175 """
182 self.msg_rep = self.km.rep_channel.get_msg(timeout=timeout)
183 if self.msg_header["session"] == self.msg_rep["parent_header"]["session"] :
184 raw_data = raw_input(self.msg_rep["content"]["prompt"])
176 msg_rep = self.km.rep_channel.get_msg(timeout=timeout)
177 if self.session_id == msg_rep["parent_header"]["session"] :
178 raw_data = raw_input(msg_rep["content"]["prompt"])
185 179 self.km.rep_channel.input(raw_data)
186 180
187 181
General Comments 0
You need to be logged in to leave comments. Login now