##// END OF EJS Templates
implement complete_object(obj, prev_completions) generic function to provide custom completers for python object attributes (as opposed to string dispatching)
vivainio -
Show More
@@ -75,7 +75,7 b' import IPython.rlineimpl as readline'
75 import itertools
75 import itertools
76 from IPython.ipstruct import Struct
76 from IPython.ipstruct import Struct
77 from IPython import ipapi
77 from IPython import ipapi
78
78 from IPython import generics
79 import types
79 import types
80
80
81 # Python 2.4 offers sets as a builtin
81 # Python 2.4 offers sets as a builtin
@@ -200,10 +200,15 b' class Completer:'
200 return []
200 return []
201
201
202 words = dir2(obj)
202 words = dir2(obj)
203
203
204 try:
205 words = generics.complete_object(obj, words)
206 except ipapi.TryNext:
207 pass
204 # Build match list to return
208 # Build match list to return
205 n = len(attr)
209 n = len(attr)
206 return ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
210 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
211 return res
207
212
208 class IPCompleter(Completer):
213 class IPCompleter(Completer):
209 """Extension of the completer class with IPython-specific features"""
214 """Extension of the completer class with IPython-specific features"""
@@ -243,8 +248,7 b' class IPCompleter(Completer):'
243 self.readline.set_completer_delims(delims)
248 self.readline.set_completer_delims(delims)
244 self.get_line_buffer = self.readline.get_line_buffer
249 self.get_line_buffer = self.readline.get_line_buffer
245 self.omit__names = omit__names
250 self.omit__names = omit__names
246 self.merge_completions = shell.rc.readline_merge_completions
251 self.merge_completions = shell.rc.readline_merge_completions
247
248 if alias_table is None:
252 if alias_table is None:
249 alias_table = {}
253 alias_table = {}
250 self.alias_table = alias_table
254 self.alias_table = alias_table
@@ -614,14 +618,18 b' class IPCompleter(Completer):'
614 self.matches = matcher(text)
618 self.matches = matcher(text)
615 if self.matches:
619 if self.matches:
616 break
620 break
617
621 def uniq(alist):
622 set = {}
623 return [set.setdefault(e,e) for e in alist if e not in set]
624 self.matches = uniq(self.matches)
618 try:
625 try:
619 return self.matches[state].replace(magic_prefix,magic_escape)
626 ret = self.matches[state].replace(magic_prefix,magic_escape)
627 return ret
620 except IndexError:
628 except IndexError:
621 return None
629 return None
622 except:
630 except:
623 #from IPython.ultraTB import AutoFormattedTB; # dbg
631 #from IPython.ultraTB import AutoFormattedTB; # dbg
624 #tb=AutoFormattedTB('Verbose');tb() #dbg
632 #tb=AutoFormattedTB('Verbose');tb() #dbg
625
633
626 # If completion fails, don't annoy the user.
634 # If completion fails, don't annoy the user.
627 return None
635 return None
@@ -28,3 +28,28 b' result_display = generic(result_display)'
28 def inspect_object(obj):
28 def inspect_object(obj):
29 """ Called when you do obj? """
29 """ Called when you do obj? """
30 raise TryNext
30 raise TryNext
31 inspect_object = generic(inspect_object)
32
33 def complete_object(obj, prev_completions):
34 """ Custom completer dispatching for python objects
35
36 obj is the object itself.
37 prev_completions is the list of attributes discovered so far.
38
39 This should return the list of attributes in obj. If you only wish to
40 add to the attributes already discovered normally, return
41 own_attrs + prev_completions.
42 """
43
44 raise TryNext
45 complete_object = generic(complete_object)
46
47 #import os
48 #def my_demo_complete_object(obj, prev_completions):
49 # """ Demo completer that adds 'foobar' to the completions suggested
50 # for any object that has attribute (path), e.g. 'os'"""
51 # if hasattr(obj,'path'):
52 # return prev_completions + ['foobar']
53 # raise TryNext
54 #
55 #my_demo_complete_object = complete_object.when_type(type(os))(my_demo_complete_object)
@@ -1,3 +1,9 b''
1 2007-12-20 Ville Vainio <vivainio@gmail.com>
2
3 * completer.py, generics.py(complete_object): Allow
4 custom complers based on python objects via simplegeneric.
5 See generics.py / my_demo_complete_object
6
1 2007-12-13 Ville Vainio <vivainio@gmail.com>
7 2007-12-13 Ville Vainio <vivainio@gmail.com>
2
8
3 * iplib.py(raw_input): unix readline does not allow unicode in
9 * iplib.py(raw_input): unix readline does not allow unicode in
General Comments 0
You need to be logged in to leave comments. Login now