[Python-checkins] CVS: python/dist/src/Lib rlcompleter.py,1.9,1.10
Neil Schemenauer
nascheme@users.sourceforge.net
Sat, 23 Mar 2002 15:44:53 -0800
Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv11979/Lib
Modified Files:
rlcompleter.py
Log Message:
Add namespace selection for rlcompleter. Closes SF patch 490026.
Index: rlcompleter.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/rlcompleter.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** rlcompleter.py 15 Feb 2001 22:15:13 -0000 1.9
--- rlcompleter.py 23 Mar 2002 23:44:51 -0000 1.10
***************
*** 1,8 ****
"""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.
It's very cool to do "import string" type "string.", hit the
--- 1,8 ----
"""Word completion for GNU readline 2.0.
! 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
***************
*** 47,50 ****
--- 47,76 ----
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):
***************
*** 55,58 ****
--- 81,87 ----
"""
+ if self.use_main_ns:
+ self.namespace = __main__.__dict__
+
if state == 0:
if "." in text:
***************
*** 68,73 ****
"""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.
"""
--- 97,102 ----
"""Compute matches when text is a simple name.
! Return a list of all keywords, built-in functions and names currently
! defined in self.namespace that match.
"""
***************
*** 77,81 ****
for list in [keyword.kwlist,
__builtin__.__dict__.keys(),
! __main__.__dict__.keys()]:
for word in list:
if word[:n] == text and word != "__builtins__":
--- 106,110 ----
for list in [keyword.kwlist,
__builtin__.__dict__.keys(),
! self.namespace.keys()]:
for word in list:
if word[:n] == text and word != "__builtins__":
***************
*** 87,94 ****
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.)
WARNING: this can still invoke arbitrary C code, if an object
--- 116,122 ----
Assuming the text is of the form NAME.NAME....[NAME], and is
! 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
***************
*** 101,105 ****
return
expr, attr = m.group(1, 3)
! object = eval(expr, __main__.__dict__)
words = dir(object)
if hasattr(object,'__class__'):
--- 129,133 ----
return
expr, attr = m.group(1, 3)
! object = eval(expr, self.namespace)
words = dir(object)
if hasattr(object,'__class__'):