Add namespace selection for rlcompleter. Closes SF patch 490026.

This commit is contained in:
Neil Schemenauer 2002-03-23 23:44:51 +00:00
parent 62c06ba6a9
commit dbab3e3178

View File

@ -1,9 +1,9 @@
"""Word completion for GNU readline 2.0.
This requires the latest extension to the readline module (the
completes keywords, built-ins and globals in __main__; when completing
NAME.NAME..., it evaluates (!) the expression up to the last dot and
completes its attributes.
This requires the latest extension to the readline module. The completer
completes keywords, built-ins and globals in a selectable namespace (which
defaults to __main__); when completing NAME.NAME..., it evaluates (!) the
expression up to the last dot and completes its attributes.
It's very cool to do "import string" type "string.", hit the
completion key (twice), and see the list of names defined by the
@ -46,6 +46,32 @@ import __main__
__all__ = ["Completer"]
class Completer:
def __init__(self, namespace = None):
"""Create a new completer for the command line.
Completer([namespace]) -> completer instance.
If unspecified, the default namespace where completions are performed
is __main__ (technically, __main__.__dict__). Namespaces should be
given as dictionaries.
Completer instances should be used as the completion mechanism of
readline via the set_completer() call:
readline.set_completer(Completer(my_namespace).complete)
"""
if namespace and not isinstance(namespace, dict):
raise TypeError,'namespace must be a dictionary'
# Don't bind to namespace quite yet, but flag whether the user wants a
# specific namespace or to use __main__.__dict__. This will allow us
# to bind to __main__.__dict__ at completion time, not now.
if namespace is None:
self.use_main_ns = 1
else:
self.use_main_ns = 0
self.namespace = namespace
def complete(self, text, state):
"""Return the next possible completion for 'text'.
@ -54,6 +80,9 @@ class Completer:
returns None. The completion should begin with 'text'.
"""
if self.use_main_ns:
self.namespace = __main__.__dict__
if state == 0:
if "." in text:
self.matches = self.attr_matches(text)
@ -67,8 +96,8 @@ class Completer:
def global_matches(self, text):
"""Compute matches when text is a simple name.
Return a list of all keywords, built-in functions and names
currently defines in __main__ that match.
Return a list of all keywords, built-in functions and names currently
defined in self.namespace that match.
"""
import keyword
@ -76,7 +105,7 @@ class Completer:
n = len(text)
for list in [keyword.kwlist,
__builtin__.__dict__.keys(),
__main__.__dict__.keys()]:
self.namespace.keys()]:
for word in list:
if word[:n] == text and word != "__builtins__":
matches.append(word)
@ -86,10 +115,9 @@ class Completer:
"""Compute matches when text contains a dot.
Assuming the text is of the form NAME.NAME....[NAME], and is
evaluatable in the globals of __main__, it will be evaluated
and its attributes (as revealed by dir()) are used as possible
completions. (For class instances, class members are are also
considered.)
evaluatable in self.namespace, it will be evaluated and its attributes
(as revealed by dir()) are used as possible completions. (For class
instances, class members are are also considered.)
WARNING: this can still invoke arbitrary C code, if an object
with a __getattr__ hook is evaluated.
@ -100,7 +128,7 @@ class Completer:
if not m:
return
expr, attr = m.group(1, 3)
object = eval(expr, __main__.__dict__)
object = eval(expr, self.namespace)
words = dir(object)
if hasattr(object,'__class__'):
words.append('__class__')