completer.py
42 lines
| 1.6 KiB
| text/x-python
|
PythonLexer
Omar Andres Zapata Mesa
|
r5584 | # -*- coding: utf-8 -*- | ||
import readline | ||||
Thomas Kluyver
|
r5596 | from Queue import Empty | ||
Thomas Kluyver
|
r5595 | |||
Omar Andres Zapata Mesa
|
r5584 | class ClientCompleter2p(object): | ||
"""Client-side completion machinery. | ||||
How it works: self.complete will be called multiple times, with | ||||
state=0,1,2,... When state=0 it should compute ALL the completion matches, | ||||
and then return them for each value of state.""" | ||||
Omar Andres Zapata Mesa
|
r5585 | def __init__(self,client, km): | ||
Omar Andres Zapata Mesa
|
r5584 | self.km = km | ||
self.matches = [] | ||||
Omar Andres Zapata Mesa
|
r5585 | self.client = client | ||
Omar Andres Zapata Mesa
|
r5584 | |||
def complete_request(self,text): | ||||
Omar Andres Zapata Mesa
|
r5585 | line = readline.get_line_buffer() | ||
#msg_id = self.km.xreq_channel.complete(text=text,line=line)#this method is not working, the code not continue | ||||
Thomas Kluyver
|
r5591 | msg = self.km.session.send(self.km.xreq_channel.socket, | ||
Omar Andres Zapata Mesa
|
r5584 | 'complete_request', | ||
dict(text=text, line=line)) | ||||
Omar Andres Zapata Mesa
|
r5585 | # send completion request to kernel | ||
# Give the kernel up to 0.5s to respond | ||||
Thomas Kluyver
|
r5596 | msg_xreq = self.km.xreq_channel.get_msg(timeout=0.5) | ||
if msg["header"]['session'] == msg_xreq["parent_header"]['session'] and \ | ||||
msg_xreq["content"]["status"] == 'ok' and \ | ||||
msg_xreq["msg_type"] == "complete_reply" : | ||||
return msg_xreq["content"]["matches"] | ||||
return [] | ||||
Omar Andres Zapata Mesa
|
r5584 | |||
def complete(self, text, state): | ||||
Thomas Kluyver
|
r5595 | if state == 0: | ||
try: | ||||
self.matches = self.complete_request(text) | ||||
Thomas Kluyver
|
r5596 | except Empty: | ||
Thomas Kluyver
|
r5595 | print('WARNING: Kernel timeout on tab completion.') | ||
Thomas Kluyver
|
r5591 | try: | ||
return self.matches[state] | ||||
except IndexError: | ||||
return None | ||||