Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions
rlcompleter.Completer Class Reference

List of all members.

Public Member Functions

def __init__
def complete
def global_matches
def attr_matches

Public Attributes

 use_main_ns
 namespace
 matches

Private Member Functions

def _callable_postfix

Detailed Description

Definition at line 41 of file rlcompleter.py.


Constructor & Destructor Documentation

def rlcompleter.Completer.__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)

Definition at line 42 of file rlcompleter.py.

00042 
00043     def __init__(self, namespace = None):
00044         """Create a new completer for the command line.
00045 
00046         Completer([namespace]) -> completer instance.
00047 
00048         If unspecified, the default namespace where completions are performed
00049         is __main__ (technically, __main__.__dict__). Namespaces should be
00050         given as dictionaries.
00051 
00052         Completer instances should be used as the completion mechanism of
00053         readline via the set_completer() call:
00054 
00055         readline.set_completer(Completer(my_namespace).complete)
00056         """
00057 
00058         if namespace and not isinstance(namespace, dict):
00059             raise TypeError('namespace must be a dictionary')
00060 
00061         # Don't bind to namespace quite yet, but flag whether the user wants a
00062         # specific namespace or to use __main__.__dict__. This will allow us
00063         # to bind to __main__.__dict__ at completion time, not now.
00064         if namespace is None:
00065             self.use_main_ns = 1
00066         else:
00067             self.use_main_ns = 0
00068             self.namespace = namespace

Here is the caller graph for this function:


Member Function Documentation

def rlcompleter.Completer._callable_postfix (   self,
  val,
  word 
) [private]

Definition at line 89 of file rlcompleter.py.

00089 
00090     def _callable_postfix(self, val, word):
00091         if hasattr(val, '__call__'):
00092             word = word + "("
00093         return word

Here is the caller graph for this function:

def rlcompleter.Completer.attr_matches (   self,
  text 
)
Compute matches when text contains a dot.

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 also considered.)

WARNING: this can still invoke arbitrary C code, if an object
with a __getattr__ hook is evaluated.

Definition at line 113 of file rlcompleter.py.

00113 
00114     def attr_matches(self, text):
00115         """Compute matches when text contains a dot.
00116 
00117         Assuming the text is of the form NAME.NAME....[NAME], and is
00118         evaluatable in self.namespace, it will be evaluated and its attributes
00119         (as revealed by dir()) are used as possible completions.  (For class
00120         instances, class members are also considered.)
00121 
00122         WARNING: this can still invoke arbitrary C code, if an object
00123         with a __getattr__ hook is evaluated.
00124 
00125         """
00126         import re
00127         m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text)
00128         if not m:
00129             return []
00130         expr, attr = m.group(1, 3)
00131         try:
00132             thisobject = eval(expr, self.namespace)
00133         except Exception:
00134             return []
00135 
00136         # get the content of the object, except __builtins__
00137         words = dir(thisobject)
00138         if "__builtins__" in words:
00139             words.remove("__builtins__")
00140 
00141         if hasattr(thisobject, '__class__'):
00142             words.append('__class__')
00143             words.extend(get_class_members(thisobject.__class__))
00144         matches = []
00145         n = len(attr)
00146         for word in words:
00147             if word[:n] == attr and hasattr(thisobject, word):
00148                 val = getattr(thisobject, word)
00149                 word = self._callable_postfix(val, "%s.%s" % (expr, word))
00150                 matches.append(word)
00151         return matches

Here is the call graph for this function:

def rlcompleter.Completer.complete (   self,
  text,
  state 
)
Return the next possible completion for 'text'.

This is called successively with state == 0, 1, 2, ... until it
returns None.  The completion should begin with 'text'.

Definition at line 69 of file rlcompleter.py.

00069 
00070     def complete(self, text, state):
00071         """Return the next possible completion for 'text'.
00072 
00073         This is called successively with state == 0, 1, 2, ... until it
00074         returns None.  The completion should begin with 'text'.
00075 
00076         """
00077         if self.use_main_ns:
00078             self.namespace = __main__.__dict__
00079 
00080         if state == 0:
00081             if "." in text:
00082                 self.matches = self.attr_matches(text)
00083             else:
00084                 self.matches = self.global_matches(text)
00085         try:
00086             return self.matches[state]
00087         except IndexError:
00088             return None

def rlcompleter.Completer.global_matches (   self,
  text 
)
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.

Definition at line 94 of file rlcompleter.py.

00094 
00095     def global_matches(self, text):
00096         """Compute matches when text is a simple name.
00097 
00098         Return a list of all keywords, built-in functions and names currently
00099         defined in self.namespace that match.
00100 
00101         """
00102         import keyword
00103         matches = []
00104         n = len(text)
00105         for word in keyword.kwlist:
00106             if word[:n] == text:
00107                 matches.append(word)
00108         for nspace in [builtins.__dict__, self.namespace]:
00109             for word, val in nspace.items():
00110                 if word[:n] == text and word != "__builtins__":
00111                     matches.append(self._callable_postfix(val, word))
00112         return matches

Here is the call graph for this function:


Member Data Documentation

Definition at line 81 of file rlcompleter.py.

Definition at line 67 of file rlcompleter.py.

Definition at line 64 of file rlcompleter.py.


The documentation for this class was generated from the following file: