Show More
@@ -1,58 +1,58 b'' | |||||
1 | """Adapt readline completer interface to make ZMQ request. |
|
1 | """Adapt readline completer interface to make ZMQ request. | |
2 | """ |
|
2 | """ | |
3 | # -*- coding: utf-8 -*- |
|
3 | # -*- coding: utf-8 -*- | |
4 | import readline |
|
|||
5 | try: |
|
4 | try: | |
6 | from queue import Empty # Py 3 |
|
5 | from queue import Empty # Py 3 | |
7 | except ImportError: |
|
6 | except ImportError: | |
8 | from Queue import Empty # Py 2 |
|
7 | from Queue import Empty # Py 2 | |
9 |
|
8 | |||
10 | from IPython.config import Configurable |
|
9 | from IPython.config import Configurable | |
11 | from IPython.core.completer import IPCompleter |
|
10 | from IPython.core.completer import IPCompleter | |
12 | from IPython.utils.traitlets import Float |
|
11 | from IPython.utils.traitlets import Float | |
|
12 | import IPython.utils.rlineimpl as readline | |||
13 |
|
13 | |||
14 | class ZMQCompleter(IPCompleter): |
|
14 | class ZMQCompleter(IPCompleter): | |
15 | """Client-side completion machinery. |
|
15 | """Client-side completion machinery. | |
16 |
|
16 | |||
17 | How it works: self.complete will be called multiple times, with |
|
17 | How it works: self.complete will be called multiple times, with | |
18 | state=0,1,2,... When state=0 it should compute ALL the completion matches, |
|
18 | state=0,1,2,... When state=0 it should compute ALL the completion matches, | |
19 | and then return them for each value of state.""" |
|
19 | and then return them for each value of state.""" | |
20 |
|
20 | |||
21 | timeout = Float(5.0, config=True, help='timeout before completion abort') |
|
21 | timeout = Float(5.0, config=True, help='timeout before completion abort') | |
22 |
|
22 | |||
23 | def __init__(self, shell, client, config=None): |
|
23 | def __init__(self, shell, client, config=None): | |
24 | super(ZMQCompleter,self).__init__(config=config) |
|
24 | super(ZMQCompleter,self).__init__(config=config) | |
25 |
|
25 | |||
26 | self.shell = shell |
|
26 | self.shell = shell | |
27 | self.client = client |
|
27 | self.client = client | |
28 | self.matches = [] |
|
28 | self.matches = [] | |
29 |
|
29 | |||
30 | def complete_request(self,text): |
|
30 | def complete_request(self,text): | |
31 | line = readline.get_line_buffer() |
|
31 | line = readline.get_line_buffer() | |
32 | cursor_pos = readline.get_endidx() |
|
32 | cursor_pos = readline.get_endidx() | |
33 |
|
33 | |||
34 | # send completion request to kernel |
|
34 | # send completion request to kernel | |
35 | # Give the kernel up to 0.5s to respond |
|
35 | # Give the kernel up to 0.5s to respond | |
36 | msg_id = self.client.shell_channel.complete(text=text, line=line, |
|
36 | msg_id = self.client.shell_channel.complete(text=text, line=line, | |
37 | cursor_pos=cursor_pos) |
|
37 | cursor_pos=cursor_pos) | |
38 |
|
38 | |||
39 | msg = self.client.shell_channel.get_msg(timeout=self.timeout) |
|
39 | msg = self.client.shell_channel.get_msg(timeout=self.timeout) | |
40 | if msg['parent_header']['msg_id'] == msg_id: |
|
40 | if msg['parent_header']['msg_id'] == msg_id: | |
41 | return msg["content"]["matches"] |
|
41 | return msg["content"]["matches"] | |
42 | return [] |
|
42 | return [] | |
43 |
|
43 | |||
44 | def rlcomplete(self, text, state): |
|
44 | def rlcomplete(self, text, state): | |
45 | if state == 0: |
|
45 | if state == 0: | |
46 | try: |
|
46 | try: | |
47 | self.matches = self.complete_request(text) |
|
47 | self.matches = self.complete_request(text) | |
48 | except Empty: |
|
48 | except Empty: | |
49 | #print('WARNING: Kernel timeout on tab completion.') |
|
49 | #print('WARNING: Kernel timeout on tab completion.') | |
50 | pass |
|
50 | pass | |
51 |
|
51 | |||
52 | try: |
|
52 | try: | |
53 | return self.matches[state] |
|
53 | return self.matches[state] | |
54 | except IndexError: |
|
54 | except IndexError: | |
55 | return None |
|
55 | return None | |
56 |
|
56 | |||
57 | def complete(self, text, line, cursor_pos=None): |
|
57 | def complete(self, text, line, cursor_pos=None): | |
58 | return self.rlcomplete(text, 0) |
|
58 | return self.rlcomplete(text, 0) |
General Comments 0
You need to be logged in to leave comments.
Login now