Show More
@@ -0,0 +1,75 b'' | |||||
|
1 | """ Greedy completer extension for IPython | |||
|
2 | ||||
|
3 | Normal tab completer refuses to evaluate nonsafe stuff. This will evaluate | |||
|
4 | everything, so you need to consider the consequences of pressing tab | |||
|
5 | yourself! | |||
|
6 | ||||
|
7 | Note that this extension simplifies readline interaction by setting | |||
|
8 | only whitespace as completer delimiter. If this works well, we will | |||
|
9 | do the same in default completer. | |||
|
10 | ||||
|
11 | """ | |||
|
12 | from IPython import generics,ipapi | |||
|
13 | from IPython.genutils import dir2 | |||
|
14 | ||||
|
15 | def attr_matches(self, text): | |||
|
16 | """Compute matches when text contains a dot. | |||
|
17 | ||||
|
18 | MONKEYPATCHED VERSION (ipy_greedycompleter.py) | |||
|
19 | ||||
|
20 | Assuming the text is of the form NAME.NAME....[NAME], and is | |||
|
21 | evaluatable in self.namespace or self.global_namespace, it will be | |||
|
22 | evaluated and its attributes (as revealed by dir()) are used as | |||
|
23 | possible completions. (For class instances, class members are are | |||
|
24 | also considered.) | |||
|
25 | ||||
|
26 | WARNING: this can still invoke arbitrary C code, if an object | |||
|
27 | with a __getattr__ hook is evaluated. | |||
|
28 | ||||
|
29 | """ | |||
|
30 | import re | |||
|
31 | ||||
|
32 | force_complete = 1 | |||
|
33 | # Another option, seems to work great. Catches things like ''.<tab> | |||
|
34 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) | |||
|
35 | ||||
|
36 | if m: | |||
|
37 | expr, attr = m.group(1, 3) | |||
|
38 | else: | |||
|
39 | # force match - eval anything that ends with colon | |||
|
40 | if not force_complete: | |||
|
41 | return [] | |||
|
42 | ||||
|
43 | m2 = re.match(r"(.+)\.(\w*)$", self.lbuf) | |||
|
44 | if not m2: | |||
|
45 | return [] | |||
|
46 | expr, attr = m2.group(1,2) | |||
|
47 | ||||
|
48 | ||||
|
49 | try: | |||
|
50 | obj = eval(expr, self.namespace) | |||
|
51 | except: | |||
|
52 | try: | |||
|
53 | obj = eval(expr, self.global_namespace) | |||
|
54 | except: | |||
|
55 | return [] | |||
|
56 | ||||
|
57 | words = dir2(obj) | |||
|
58 | ||||
|
59 | try: | |||
|
60 | words = generics.complete_object(obj, words) | |||
|
61 | except ipapi.TryNext: | |||
|
62 | pass | |||
|
63 | # Build match list to return | |||
|
64 | n = len(attr) | |||
|
65 | res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ] | |||
|
66 | return res | |||
|
67 | ||||
|
68 | def main(): | |||
|
69 | import readline | |||
|
70 | readline.set_completer_delims(" \n\t") | |||
|
71 | # monkeypatch - the code will be folded to normal completer later on | |||
|
72 | import IPython.completer | |||
|
73 | IPython.completer.Completer.attr_matches = attr_matches | |||
|
74 | ||||
|
75 | main() No newline at end of file |
@@ -87,6 +87,11 b' def main():' | |||||
87 | # For bzr completer, requires bzrlib (the python installation of bzr) |
|
87 | # For bzr completer, requires bzrlib (the python installation of bzr) | |
88 | #ip.load('ipy_bzr') |
|
88 | #ip.load('ipy_bzr') | |
89 |
|
89 | |||
|
90 | # Tab completer that is not quite so picky (i.e. | |||
|
91 | # "foo".<TAB> and str(2).<TAB> will work). Complete | |||
|
92 | # at your own risk! | |||
|
93 | #import ipy_greedycompleter | |||
|
94 | ||||
90 |
|
95 | |||
91 |
|
96 | |||
92 | # some config helper functions you can use |
|
97 | # some config helper functions you can use |
@@ -1,3 +1,10 b'' | |||||
|
1 | 2008-05-14 Ville Vainio <vivainio@gmail.com> | |||
|
2 | ||||
|
3 | * Extensions/ipy_greedycompleter.py: | |||
|
4 | New extension that enables a bit more "relaxed" tab | |||
|
5 | completer that evaluates code without safety checks | |||
|
6 | (i.e. there can be side effects like function calls) | |||
|
7 | ||||
1 | 2008-04-20 Ville Vainio <vivainio@gmail.com> |
|
8 | 2008-04-20 Ville Vainio <vivainio@gmail.com> | |
2 |
|
9 | |||
3 | * Extensions/ipy_lookfor.py: add %lookfor magic command |
|
10 | * Extensions/ipy_lookfor.py: add %lookfor magic command |
General Comments 0
You need to be logged in to leave comments.
Login now