mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Add namespace selection for rlcompleter. Closes SF patch 490026.
This commit is contained in:
parent
62c06ba6a9
commit
dbab3e3178
1 changed files with 40 additions and 12 deletions
|
@ -1,9 +1,9 @@
|
||||||
"""Word completion for GNU readline 2.0.
|
"""Word completion for GNU readline 2.0.
|
||||||
|
|
||||||
This requires the latest extension to the readline module (the
|
This requires the latest extension to the readline module. The completer
|
||||||
completes keywords, built-ins and globals in __main__; when completing
|
completes keywords, built-ins and globals in a selectable namespace (which
|
||||||
NAME.NAME..., it evaluates (!) the expression up to the last dot and
|
defaults to __main__); when completing NAME.NAME..., it evaluates (!) the
|
||||||
completes its attributes.
|
expression up to the last dot and completes its attributes.
|
||||||
|
|
||||||
It's very cool to do "import string" type "string.", hit the
|
It's very cool to do "import string" type "string.", hit the
|
||||||
completion key (twice), and see the list of names defined by the
|
completion key (twice), and see the list of names defined by the
|
||||||
|
@ -46,6 +46,32 @@ import __main__
|
||||||
__all__ = ["Completer"]
|
__all__ = ["Completer"]
|
||||||
|
|
||||||
class 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):
|
def complete(self, text, state):
|
||||||
"""Return the next possible completion for 'text'.
|
"""Return the next possible completion for 'text'.
|
||||||
|
@ -54,6 +80,9 @@ class Completer:
|
||||||
returns None. The completion should begin with 'text'.
|
returns None. The completion should begin with 'text'.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
if self.use_main_ns:
|
||||||
|
self.namespace = __main__.__dict__
|
||||||
|
|
||||||
if state == 0:
|
if state == 0:
|
||||||
if "." in text:
|
if "." in text:
|
||||||
self.matches = self.attr_matches(text)
|
self.matches = self.attr_matches(text)
|
||||||
|
@ -67,8 +96,8 @@ class Completer:
|
||||||
def global_matches(self, text):
|
def global_matches(self, text):
|
||||||
"""Compute matches when text is a simple name.
|
"""Compute matches when text is a simple name.
|
||||||
|
|
||||||
Return a list of all keywords, built-in functions and names
|
Return a list of all keywords, built-in functions and names currently
|
||||||
currently defines in __main__ that match.
|
defined in self.namespace that match.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
import keyword
|
import keyword
|
||||||
|
@ -76,7 +105,7 @@ class Completer:
|
||||||
n = len(text)
|
n = len(text)
|
||||||
for list in [keyword.kwlist,
|
for list in [keyword.kwlist,
|
||||||
__builtin__.__dict__.keys(),
|
__builtin__.__dict__.keys(),
|
||||||
__main__.__dict__.keys()]:
|
self.namespace.keys()]:
|
||||||
for word in list:
|
for word in list:
|
||||||
if word[:n] == text and word != "__builtins__":
|
if word[:n] == text and word != "__builtins__":
|
||||||
matches.append(word)
|
matches.append(word)
|
||||||
|
@ -86,10 +115,9 @@ class Completer:
|
||||||
"""Compute matches when text contains a dot.
|
"""Compute matches when text contains a dot.
|
||||||
|
|
||||||
Assuming the text is of the form NAME.NAME....[NAME], and is
|
Assuming the text is of the form NAME.NAME....[NAME], and is
|
||||||
evaluatable in the globals of __main__, it will be evaluated
|
evaluatable in self.namespace, it will be evaluated and its attributes
|
||||||
and its attributes (as revealed by dir()) are used as possible
|
(as revealed by dir()) are used as possible completions. (For class
|
||||||
completions. (For class instances, class members are are also
|
instances, class members are are also considered.)
|
||||||
considered.)
|
|
||||||
|
|
||||||
WARNING: this can still invoke arbitrary C code, if an object
|
WARNING: this can still invoke arbitrary C code, if an object
|
||||||
with a __getattr__ hook is evaluated.
|
with a __getattr__ hook is evaluated.
|
||||||
|
@ -100,7 +128,7 @@ class Completer:
|
||||||
if not m:
|
if not m:
|
||||||
return
|
return
|
||||||
expr, attr = m.group(1, 3)
|
expr, attr = m.group(1, 3)
|
||||||
object = eval(expr, __main__.__dict__)
|
object = eval(expr, self.namespace)
|
||||||
words = dir(object)
|
words = dir(object)
|
||||||
if hasattr(object,'__class__'):
|
if hasattr(object,'__class__'):
|
||||||
words.append('__class__')
|
words.append('__class__')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue