Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 1 | """Word completion for GNU readline 2.0. |
| 2 | |
Neil Schemenauer | dbab3e3 | 2002-03-23 23:44:51 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 7 | |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 8 | It's very cool to do "import sys" type "sys.", hit the |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 9 | completion key (twice), and see the list of names defined by the |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 10 | sys module! |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 11 | |
| 12 | Tip: to use the tab key as the completion key, call |
| 13 | |
| 14 | readline.parse_and_bind("tab: complete") |
| 15 | |
| 16 | Notes: |
| 17 | |
| 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. |
| 23 | |
| 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. |
| 30 | |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 31 | - When the original stdin is not a tty device, GNU readline is never |
| 32 | used, and this module (and the readline module) are silently inactive. |
| 33 | |
| 34 | """ |
| 35 | |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 36 | import builtins |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 37 | import __main__ |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 38 | |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 39 | __all__ = ["Completer"] |
| 40 | |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 41 | class Completer: |
Neil Schemenauer | dbab3e3 | 2002-03-23 23:44:51 +0000 | [diff] [blame] | 42 | def __init__(self, namespace = None): |
| 43 | """Create a new completer for the command line. |
| 44 | |
| 45 | Completer([namespace]) -> completer instance. |
| 46 | |
| 47 | If unspecified, the default namespace where completions are performed |
| 48 | is __main__ (technically, __main__.__dict__). Namespaces should be |
| 49 | given as dictionaries. |
| 50 | |
| 51 | Completer instances should be used as the completion mechanism of |
| 52 | readline via the set_completer() call: |
| 53 | |
| 54 | readline.set_completer(Completer(my_namespace).complete) |
| 55 | """ |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 56 | |
Neil Schemenauer | dbab3e3 | 2002-03-23 23:44:51 +0000 | [diff] [blame] | 57 | if namespace and not isinstance(namespace, dict): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 58 | raise TypeError('namespace must be a dictionary') |
Neil Schemenauer | dbab3e3 | 2002-03-23 23:44:51 +0000 | [diff] [blame] | 59 | |
| 60 | # Don't bind to namespace quite yet, but flag whether the user wants a |
| 61 | # specific namespace or to use __main__.__dict__. This will allow us |
| 62 | # to bind to __main__.__dict__ at completion time, not now. |
| 63 | if namespace is None: |
| 64 | self.use_main_ns = 1 |
| 65 | else: |
| 66 | self.use_main_ns = 0 |
| 67 | self.namespace = namespace |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 68 | |
| 69 | def complete(self, text, state): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 70 | """Return the next possible completion for 'text'. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 72 | This is called successively with state == 0, 1, 2, ... until it |
| 73 | returns None. The completion should begin with 'text'. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 75 | """ |
Neil Schemenauer | dbab3e3 | 2002-03-23 23:44:51 +0000 | [diff] [blame] | 76 | if self.use_main_ns: |
| 77 | self.namespace = __main__.__dict__ |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 79 | if state == 0: |
| 80 | if "." in text: |
| 81 | self.matches = self.attr_matches(text) |
| 82 | else: |
| 83 | self.matches = self.global_matches(text) |
Guido van Rossum | d458faa | 1998-06-12 19:42:14 +0000 | [diff] [blame] | 84 | try: |
| 85 | return self.matches[state] |
| 86 | except IndexError: |
| 87 | return None |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 88 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 89 | def _callable_postfix(self, val, word): |
Benjamin Peterson | bebfbe2 | 2008-07-05 21:20:33 +0000 | [diff] [blame] | 90 | if hasattr(val, '__call__'): |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 91 | word = word + "(" |
| 92 | return word |
| 93 | |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 94 | def global_matches(self, text): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 95 | """Compute matches when text is a simple name. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 96 | |
Neil Schemenauer | dbab3e3 | 2002-03-23 23:44:51 +0000 | [diff] [blame] | 97 | Return a list of all keywords, built-in functions and names currently |
| 98 | defined in self.namespace that match. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 99 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 100 | """ |
| 101 | import keyword |
| 102 | matches = [] |
| 103 | n = len(text) |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 104 | for word in keyword.kwlist: |
| 105 | if word[:n] == text: |
| 106 | matches.append(word) |
| 107 | for nspace in [builtins.__dict__, self.namespace]: |
| 108 | for word, val in nspace.items(): |
Benjamin Peterson | f385485 | 2008-07-05 20:59:18 +0000 | [diff] [blame] | 109 | if word[:n] == text and word != "__builtins__": |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 110 | matches.append(self._callable_postfix(val, word)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 111 | return matches |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 112 | |
| 113 | def attr_matches(self, text): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 114 | """Compute matches when text contains a dot. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 116 | Assuming the text is of the form NAME.NAME....[NAME], and is |
Neil Schemenauer | dbab3e3 | 2002-03-23 23:44:51 +0000 | [diff] [blame] | 117 | evaluatable in self.namespace, it will be evaluated and its attributes |
| 118 | (as revealed by dir()) are used as possible completions. (For class |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 119 | instances, class members are also considered.) |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 121 | WARNING: this can still invoke arbitrary C code, if an object |
| 122 | with a __getattr__ hook is evaluated. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 123 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 124 | """ |
| 125 | import re |
| 126 | m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text) |
| 127 | if not m: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 128 | return [] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 129 | expr, attr = m.group(1, 3) |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 130 | try: |
Benjamin Peterson | 095c119 | 2008-07-21 16:32:10 +0000 | [diff] [blame] | 131 | thisobject = eval(expr, self.namespace) |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 132 | except Exception: |
| 133 | return [] |
Benjamin Peterson | 095c119 | 2008-07-21 16:32:10 +0000 | [diff] [blame] | 134 | |
| 135 | # get the content of the object, except __builtins__ |
| 136 | words = dir(thisobject) |
| 137 | if "__builtins__" in words: |
| 138 | words.remove("__builtins__") |
| 139 | |
| 140 | if hasattr(thisobject, '__class__'): |
Guido van Rossum | 4e20de5 | 1999-10-26 13:09:08 +0000 | [diff] [blame] | 141 | words.append('__class__') |
Benjamin Peterson | 095c119 | 2008-07-21 16:32:10 +0000 | [diff] [blame] | 142 | words.extend(get_class_members(thisobject.__class__)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 143 | matches = [] |
| 144 | n = len(attr) |
| 145 | for word in words: |
Benjamin Peterson | 095c119 | 2008-07-21 16:32:10 +0000 | [diff] [blame] | 146 | if word[:n] == attr and hasattr(thisobject, word): |
| 147 | val = getattr(thisobject, word) |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 148 | word = self._callable_postfix(val, "%s.%s" % (expr, word)) |
| 149 | matches.append(word) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 150 | return matches |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 151 | |
Guido van Rossum | 4e20de5 | 1999-10-26 13:09:08 +0000 | [diff] [blame] | 152 | def get_class_members(klass): |
| 153 | ret = dir(klass) |
| 154 | if hasattr(klass,'__bases__'): |
| 155 | for base in klass.__bases__: |
| 156 | ret = ret + get_class_members(base) |
| 157 | return ret |
| 158 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 159 | try: |
| 160 | import readline |
| 161 | except ImportError: |
| 162 | pass |
| 163 | else: |
| 164 | readline.set_completer(Completer().complete) |