##// END OF EJS Templates
Fixing readline import in ipy_greedycompleter.py
Brian Granger -
Show More
@@ -1,75 +1,75
1 1 """ Greedy completer extension for IPython
2 2
3 3 Normal tab completer refuses to evaluate nonsafe stuff. This will evaluate
4 4 everything, so you need to consider the consequences of pressing tab
5 5 yourself!
6 6
7 7 Note that this extension simplifies readline interaction by setting
8 8 only whitespace as completer delimiter. If this works well, we will
9 9 do the same in default completer.
10 10
11 11 """
12 12 from IPython import generics,ipapi
13 13 from IPython.genutils import dir2
14 14
15 15 def attr_matches(self, text):
16 16 """Compute matches when text contains a dot.
17 17
18 18 MONKEYPATCHED VERSION (ipy_greedycompleter.py)
19 19
20 20 Assuming the text is of the form NAME.NAME....[NAME], and is
21 21 evaluatable in self.namespace or self.global_namespace, it will be
22 22 evaluated and its attributes (as revealed by dir()) are used as
23 23 possible completions. (For class instances, class members are are
24 24 also considered.)
25 25
26 26 WARNING: this can still invoke arbitrary C code, if an object
27 27 with a __getattr__ hook is evaluated.
28 28
29 29 """
30 30 import re
31 31
32 32 force_complete = 1
33 33 # Another option, seems to work great. Catches things like ''.<tab>
34 34 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
35 35
36 36 if m:
37 37 expr, attr = m.group(1, 3)
38 38 else:
39 39 # force match - eval anything that ends with colon
40 40 if not force_complete:
41 41 return []
42 42
43 43 m2 = re.match(r"(.+)\.(\w*)$", self.lbuf)
44 44 if not m2:
45 45 return []
46 46 expr, attr = m2.group(1,2)
47 47
48 48
49 49 try:
50 50 obj = eval(expr, self.namespace)
51 51 except:
52 52 try:
53 53 obj = eval(expr, self.global_namespace)
54 54 except:
55 55 return []
56 56
57 57 words = dir2(obj)
58 58
59 59 try:
60 60 words = generics.complete_object(obj, words)
61 61 except ipapi.TryNext:
62 62 pass
63 63 # Build match list to return
64 64 n = len(attr)
65 65 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
66 66 return res
67 67
68 68 def main():
69 import readline
69 import IPython.rlineimpl as readline
70 70 readline.set_completer_delims(" \n\t")
71 71 # monkeypatch - the code will be folded to normal completer later on
72 72 import IPython.completer
73 73 IPython.completer.Completer.attr_matches = attr_matches
74 74
75 75 main() No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now