Georg Brandl | c56e667 | 2012-03-27 07:46:46 +0200 | [diff] [blame] | 1 | """Word completion for GNU readline. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 2 | |
Georg Brandl | c56e667 | 2012-03-27 07:46:46 +0200 | [diff] [blame] | 3 | The completer completes keywords, built-ins and globals in a selectable |
| 4 | namespace (which defaults to __main__); when completing NAME.NAME..., it |
| 5 | evaluates (!) the expression up to the last dot and completes its attributes. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 6 | |
Georg Brandl | c56e667 | 2012-03-27 07:46:46 +0200 | [diff] [blame] | 7 | It's very cool to do "import sys" type "sys.", hit the completion key (twice), |
| 8 | and see the list of names defined by the sys module! |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 9 | |
| 10 | Tip: to use the tab key as the completion key, call |
| 11 | |
| 12 | readline.parse_and_bind("tab: complete") |
| 13 | |
| 14 | Notes: |
| 15 | |
Georg Brandl | c56e667 | 2012-03-27 07:46:46 +0200 | [diff] [blame] | 16 | - Exceptions raised by the completer function are *ignored* (and generally cause |
| 17 | the completion to fail). This is a feature -- since readline sets the tty |
| 18 | device in raw (or cbreak) mode, printing a traceback wouldn't work well |
| 19 | without some complicated hoopla to save, reset and restore the tty state. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 20 | |
Georg Brandl | c56e667 | 2012-03-27 07:46:46 +0200 | [diff] [blame] | 21 | - The evaluation of the NAME.NAME... form may cause arbitrary application |
| 22 | defined code to be executed if an object with a __getattr__ hook is found. |
| 23 | Since it is the responsibility of the application (or the user) to enable this |
| 24 | feature, I consider this an acceptable risk. More complicated expressions |
| 25 | (e.g. function calls or indexing operations) are *not* evaluated. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 26 | |
| 27 | - GNU readline is also used by the built-in functions input() and |
| 28 | raw_input(), and thus these also benefit/suffer from the completer |
| 29 | features. Clearly an interactive application can benefit by |
| 30 | specifying its own completer function and using raw_input() for all |
| 31 | its input. |
| 32 | |
| 33 | - When the original stdin is not a tty device, GNU readline is never |
Georg Brandl | c56e667 | 2012-03-27 07:46:46 +0200 | [diff] [blame] | 34 | used, and this module (and the readline module) are silently inactive. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 35 | |
| 36 | """ |
| 37 | |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 38 | import __builtin__ |
| 39 | import __main__ |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 40 | |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 41 | __all__ = ["Completer"] |
| 42 | |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 43 | class Completer: |
Neil Schemenauer | dbab3e3 | 2002-03-23 23:44:51 +0000 | [diff] [blame] | 44 | def __init__(self, namespace = None): |
| 45 | """Create a new completer for the command line. |
| 46 | |
| 47 | Completer([namespace]) -> completer instance. |
| 48 | |
| 49 | If unspecified, the default namespace where completions are performed |
| 50 | is __main__ (technically, __main__.__dict__). Namespaces should be |
| 51 | given as dictionaries. |
| 52 | |
| 53 | Completer instances should be used as the completion mechanism of |
| 54 | readline via the set_completer() call: |
| 55 | |
| 56 | readline.set_completer(Completer(my_namespace).complete) |
| 57 | """ |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 58 | |
Neil Schemenauer | dbab3e3 | 2002-03-23 23:44:51 +0000 | [diff] [blame] | 59 | if namespace and not isinstance(namespace, dict): |
| 60 | raise TypeError,'namespace must be a dictionary' |
| 61 | |
| 62 | # Don't bind to namespace quite yet, but flag whether the user wants a |
| 63 | # specific namespace or to use __main__.__dict__. This will allow us |
| 64 | # to bind to __main__.__dict__ at completion time, not now. |
| 65 | if namespace is None: |
| 66 | self.use_main_ns = 1 |
| 67 | else: |
| 68 | self.use_main_ns = 0 |
| 69 | self.namespace = namespace |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 70 | |
| 71 | def complete(self, text, state): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 72 | """Return the next possible completion for 'text'. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 74 | This is called successively with state == 0, 1, 2, ... until it |
| 75 | returns None. The completion should begin with 'text'. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 77 | """ |
Neil Schemenauer | dbab3e3 | 2002-03-23 23:44:51 +0000 | [diff] [blame] | 78 | if self.use_main_ns: |
| 79 | self.namespace = __main__.__dict__ |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 81 | if state == 0: |
| 82 | if "." in text: |
| 83 | self.matches = self.attr_matches(text) |
| 84 | else: |
| 85 | self.matches = self.global_matches(text) |
Guido van Rossum | d458faa | 1998-06-12 19:42:14 +0000 | [diff] [blame] | 86 | try: |
| 87 | return self.matches[state] |
| 88 | except IndexError: |
| 89 | return None |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 90 | |
Facundo Batista | 66c5277 | 2008-07-02 16:52:55 +0000 | [diff] [blame] | 91 | def _callable_postfix(self, val, word): |
Brett Cannon | 52597be | 2008-08-01 01:45:49 +0000 | [diff] [blame] | 92 | if hasattr(val, '__call__'): |
Facundo Batista | 66c5277 | 2008-07-02 16:52:55 +0000 | [diff] [blame] | 93 | word = word + "(" |
| 94 | return word |
| 95 | |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 96 | def global_matches(self, text): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 97 | """Compute matches when text is a simple name. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 98 | |
Neil Schemenauer | dbab3e3 | 2002-03-23 23:44:51 +0000 | [diff] [blame] | 99 | Return a list of all keywords, built-in functions and names currently |
| 100 | defined in self.namespace that match. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 101 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 102 | """ |
| 103 | import keyword |
| 104 | matches = [] |
| 105 | n = len(text) |
Facundo Batista | 66c5277 | 2008-07-02 16:52:55 +0000 | [diff] [blame] | 106 | for word in keyword.kwlist: |
| 107 | if word[:n] == text: |
| 108 | matches.append(word) |
| 109 | for nspace in [__builtin__.__dict__, self.namespace]: |
| 110 | for word, val in nspace.items(): |
Fred Drake | 46bd9a6 | 2000-05-31 14:31:00 +0000 | [diff] [blame] | 111 | if word[:n] == text and word != "__builtins__": |
Facundo Batista | 66c5277 | 2008-07-02 16:52:55 +0000 | [diff] [blame] | 112 | matches.append(self._callable_postfix(val, word)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 113 | return matches |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 114 | |
| 115 | def attr_matches(self, text): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 116 | """Compute matches when text contains a dot. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 117 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 118 | Assuming the text is of the form NAME.NAME....[NAME], and is |
Ezio Melotti | f5469cf | 2013-08-17 15:43:51 +0300 | [diff] [blame] | 119 | evaluable in self.namespace, it will be evaluated and its attributes |
Neil Schemenauer | dbab3e3 | 2002-03-23 23:44:51 +0000 | [diff] [blame] | 120 | (as revealed by dir()) are used as possible completions. (For class |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 121 | instances, class members are also considered.) |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 122 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 123 | WARNING: this can still invoke arbitrary C code, if an object |
| 124 | with a __getattr__ hook is evaluated. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 126 | """ |
| 127 | import re |
| 128 | m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text) |
| 129 | if not m: |
Georg Brandl | 4286138 | 2008-03-06 07:43:02 +0000 | [diff] [blame] | 130 | return [] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 131 | expr, attr = m.group(1, 3) |
Georg Brandl | 627a666 | 2008-05-11 21:03:42 +0000 | [diff] [blame] | 132 | try: |
Facundo Batista | f3f6759 | 2008-07-21 14:28:17 +0000 | [diff] [blame] | 133 | thisobject = eval(expr, self.namespace) |
Georg Brandl | 627a666 | 2008-05-11 21:03:42 +0000 | [diff] [blame] | 134 | except Exception: |
| 135 | return [] |
Facundo Batista | f3f6759 | 2008-07-21 14:28:17 +0000 | [diff] [blame] | 136 | |
| 137 | # get the content of the object, except __builtins__ |
| 138 | words = dir(thisobject) |
| 139 | if "__builtins__" in words: |
| 140 | words.remove("__builtins__") |
| 141 | |
| 142 | if hasattr(thisobject, '__class__'): |
Guido van Rossum | 4e20de5 | 1999-10-26 13:09:08 +0000 | [diff] [blame] | 143 | words.append('__class__') |
Facundo Batista | f3f6759 | 2008-07-21 14:28:17 +0000 | [diff] [blame] | 144 | words.extend(get_class_members(thisobject.__class__)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 145 | matches = [] |
| 146 | n = len(attr) |
| 147 | for word in words: |
Facundo Batista | f3f6759 | 2008-07-21 14:28:17 +0000 | [diff] [blame] | 148 | if word[:n] == attr and hasattr(thisobject, word): |
| 149 | val = getattr(thisobject, word) |
Facundo Batista | 66c5277 | 2008-07-02 16:52:55 +0000 | [diff] [blame] | 150 | word = self._callable_postfix(val, "%s.%s" % (expr, word)) |
| 151 | matches.append(word) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 152 | return matches |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 153 | |
Guido van Rossum | 4e20de5 | 1999-10-26 13:09:08 +0000 | [diff] [blame] | 154 | def get_class_members(klass): |
| 155 | ret = dir(klass) |
| 156 | if hasattr(klass,'__bases__'): |
| 157 | for base in klass.__bases__: |
| 158 | ret = ret + get_class_members(base) |
| 159 | return ret |
| 160 | |
Georg Brandl | 3583cff | 2006-04-30 18:14:54 +0000 | [diff] [blame] | 161 | try: |
| 162 | import readline |
| 163 | except ImportError: |
| 164 | pass |
| 165 | else: |
| 166 | readline.set_completer(Completer().complete) |