1 """Word completion for GNU readline 2.0.
3 This requires the latest extension to the readline module. The completer
4 completes keywords, built-ins and globals in a selectable namespace (which
5 defaults to __main__); when completing NAME.NAME..., it evaluates (!) the
6 expression up to the last dot and completes its attributes.
8 It's very cool to do "import sys" type "sys.", hit the
9 completion key (twice), and see the list of names defined by the
12 Tip: to use the tab key as the completion key, call
14 readline.parse_and_bind("tab: complete")
18 - Exceptions raised by the completer function are *ignored* (and
19 generally cause the completion to fail). This is a feature -- since
20 readline sets the tty device in raw (or cbreak) mode, printing a
21 traceback wouldn't work well without some complicated hoopla to save,
22 reset and restore the tty state.
24 - The evaluation of the NAME.NAME... form may cause arbitrary
25 application defined code to be executed if an object with a
26 __getattr__ hook is found. Since it is the responsibility of the
27 application (or the user) to enable this feature, I consider this an
28 acceptable risk. More complicated expressions (e.g. function calls or
29 indexing operations) are *not* evaluated.
31 - GNU readline is also used by the built-in functions input() and
32 raw_input(), and thus these also benefit/suffer from the completer
33 features. Clearly an interactive application can benefit by
34 specifying its own completer function and using raw_input() for all
37 - When the original stdin is not a tty device, GNU readline is never
38 used, and this module (and the readline module) are silently inactive.
45 __all__
= ["Completer"]
48 def __init__(self
, namespace
= None):
49 """Create a new completer for the command line.
51 Completer([namespace]) -> completer instance.
53 If unspecified, the default namespace where completions are performed
54 is __main__ (technically, __main__.__dict__). Namespaces should be
55 given as dictionaries.
57 Completer instances should be used as the completion mechanism of
58 readline via the set_completer() call:
60 readline.set_completer(Completer(my_namespace).complete)
63 if namespace
and not isinstance(namespace
, dict):
64 raise TypeError,'namespace must be a dictionary'
66 # Don't bind to namespace quite yet, but flag whether the user wants a
67 # specific namespace or to use __main__.__dict__. This will allow us
68 # to bind to __main__.__dict__ at completion time, not now.
73 self
.namespace
= namespace
75 def complete(self
, text
, state
):
76 """Return the next possible completion for 'text'.
78 This is called successively with state == 0, 1, 2, ... until it
79 returns None. The completion should begin with 'text'.
83 self
.namespace
= __main__
.__dict
__
87 self
.matches
= self
.attr_matches(text
)
89 self
.matches
= self
.global_matches(text
)
91 return self
.matches
[state
]
95 def _callable_postfix(self
, val
, word
):
96 if hasattr(val
, '__call__'):
100 def global_matches(self
, text
):
101 """Compute matches when text is a simple name.
103 Return a list of all keywords, built-in functions and names currently
104 defined in self.namespace that match.
110 for word
in keyword
.kwlist
:
113 for nspace
in [__builtin__
.__dict
__, self
.namespace
]:
114 for word
, val
in nspace
.items():
115 if word
[:n
] == text
and word
!= "__builtins__":
116 matches
.append(self
._callable
_postfix
(val
, word
))
119 def attr_matches(self
, text
):
120 """Compute matches when text contains a dot.
122 Assuming the text is of the form NAME.NAME....[NAME], and is
123 evaluatable in self.namespace, it will be evaluated and its attributes
124 (as revealed by dir()) are used as possible completions. (For class
125 instances, class members are also considered.)
127 WARNING: this can still invoke arbitrary C code, if an object
128 with a __getattr__ hook is evaluated.
132 m
= re
.match(r
"(\w+(\.\w+)*)\.(\w*)", text
)
135 expr
, attr
= m
.group(1, 3)
137 thisobject
= eval(expr
, self
.namespace
)
141 # get the content of the object, except __builtins__
142 words
= dir(thisobject
)
143 if "__builtins__" in words
:
144 words
.remove("__builtins__")
146 if hasattr(thisobject
, '__class__'):
147 words
.append('__class__')
148 words
.extend(get_class_members(thisobject
.__class
__))
152 if word
[:n
] == attr
and hasattr(thisobject
, word
):
153 val
= getattr(thisobject
, word
)
154 word
= self
._callable
_postfix
(val
, "%s.%s" % (expr
, word
))
158 def get_class_members(klass
):
160 if hasattr(klass
,'__bases__'):
161 for base
in klass
.__bases
__:
162 ret
= ret
+ get_class_members(base
)
170 readline
.set_completer(Completer().complete
)