|
rpm-build |
2bd099 |
"""Word completion for GNU readline.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
The completer completes keywords, built-ins and globals in a selectable
|
|
rpm-build |
2bd099 |
namespace (which defaults to __main__); when completing NAME.NAME..., it
|
|
rpm-build |
2bd099 |
evaluates (!) the expression up to the last dot and completes its attributes.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
It's very cool to do "import sys" type "sys.", hit the completion key (twice),
|
|
rpm-build |
2bd099 |
and see the list of names defined by the sys module!
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
Tip: to use the tab key as the completion key, call
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
readline.parse_and_bind("tab: complete")
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
Notes:
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
- Exceptions raised by the completer function are *ignored* (and generally cause
|
|
rpm-build |
2bd099 |
the completion to fail). This is a feature -- since readline sets the tty
|
|
rpm-build |
2bd099 |
device in raw (or cbreak) mode, printing a traceback wouldn't work well
|
|
rpm-build |
2bd099 |
without some complicated hoopla to save, reset and restore the tty state.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
- The evaluation of the NAME.NAME... form may cause arbitrary application
|
|
rpm-build |
2bd099 |
defined code to be executed if an object with a __getattr__ hook is found.
|
|
rpm-build |
2bd099 |
Since it is the responsibility of the application (or the user) to enable this
|
|
rpm-build |
2bd099 |
feature, I consider this an acceptable risk. More complicated expressions
|
|
rpm-build |
2bd099 |
(e.g. function calls or indexing operations) are *not* evaluated.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
- When the original stdin is not a tty device, GNU readline is never
|
|
rpm-build |
2bd099 |
used, and this module (and the readline module) are silently inactive.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
"""
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
import atexit
|
|
rpm-build |
2bd099 |
import builtins
|
|
rpm-build |
2bd099 |
import __main__
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
__all__ = ["Completer"]
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
class Completer:
|
|
rpm-build |
2bd099 |
def __init__(self, namespace = None):
|
|
rpm-build |
2bd099 |
"""Create a new completer for the command line.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
Completer([namespace]) -> completer instance.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
If unspecified, the default namespace where completions are performed
|
|
rpm-build |
2bd099 |
is __main__ (technically, __main__.__dict__). Namespaces should be
|
|
rpm-build |
2bd099 |
given as dictionaries.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
Completer instances should be used as the completion mechanism of
|
|
rpm-build |
2bd099 |
readline via the set_completer() call:
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
readline.set_completer(Completer(my_namespace).complete)
|
|
rpm-build |
2bd099 |
"""
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
if namespace and not isinstance(namespace, dict):
|
|
rpm-build |
2bd099 |
raise TypeError('namespace must be a dictionary')
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
# Don't bind to namespace quite yet, but flag whether the user wants a
|
|
rpm-build |
2bd099 |
# specific namespace or to use __main__.__dict__. This will allow us
|
|
rpm-build |
2bd099 |
# to bind to __main__.__dict__ at completion time, not now.
|
|
rpm-build |
2bd099 |
if namespace is None:
|
|
rpm-build |
2bd099 |
self.use_main_ns = 1
|
|
rpm-build |
2bd099 |
else:
|
|
rpm-build |
2bd099 |
self.use_main_ns = 0
|
|
rpm-build |
2bd099 |
self.namespace = namespace
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
def complete(self, text, state):
|
|
rpm-build |
2bd099 |
"""Return the next possible completion for 'text'.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
This is called successively with state == 0, 1, 2, ... until it
|
|
rpm-build |
2bd099 |
returns None. The completion should begin with 'text'.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
"""
|
|
rpm-build |
2bd099 |
if self.use_main_ns:
|
|
rpm-build |
2bd099 |
self.namespace = __main__.__dict__
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
if not text.strip():
|
|
rpm-build |
2bd099 |
if state == 0:
|
|
rpm-build |
2bd099 |
if _readline_available:
|
|
rpm-build |
2bd099 |
readline.insert_text('\t')
|
|
rpm-build |
2bd099 |
readline.redisplay()
|
|
rpm-build |
2bd099 |
return ''
|
|
rpm-build |
2bd099 |
else:
|
|
rpm-build |
2bd099 |
return '\t'
|
|
rpm-build |
2bd099 |
else:
|
|
rpm-build |
2bd099 |
return None
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
if state == 0:
|
|
rpm-build |
2bd099 |
if "." in text:
|
|
rpm-build |
2bd099 |
self.matches = self.attr_matches(text)
|
|
rpm-build |
2bd099 |
else:
|
|
rpm-build |
2bd099 |
self.matches = self.global_matches(text)
|
|
rpm-build |
2bd099 |
try:
|
|
rpm-build |
2bd099 |
return self.matches[state]
|
|
rpm-build |
2bd099 |
except IndexError:
|
|
rpm-build |
2bd099 |
return None
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
def _callable_postfix(self, val, word):
|
|
rpm-build |
2bd099 |
if callable(val):
|
|
rpm-build |
2bd099 |
word = word + "("
|
|
rpm-build |
2bd099 |
return word
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
def global_matches(self, text):
|
|
rpm-build |
2bd099 |
"""Compute matches when text is a simple name.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
Return a list of all keywords, built-in functions and names currently
|
|
rpm-build |
2bd099 |
defined in self.namespace that match.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
"""
|
|
rpm-build |
2bd099 |
import keyword
|
|
rpm-build |
2bd099 |
matches = []
|
|
rpm-build |
2bd099 |
seen = {"__builtins__"}
|
|
rpm-build |
2bd099 |
n = len(text)
|
|
rpm-build |
2bd099 |
for word in keyword.kwlist:
|
|
rpm-build |
2bd099 |
if word[:n] == text:
|
|
rpm-build |
2bd099 |
seen.add(word)
|
|
rpm-build |
2bd099 |
if word in {'finally', 'try'}:
|
|
rpm-build |
2bd099 |
word = word + ':'
|
|
rpm-build |
2bd099 |
elif word not in {'False', 'None', 'True',
|
|
rpm-build |
2bd099 |
'break', 'continue', 'pass',
|
|
rpm-build |
2bd099 |
'else'}:
|
|
rpm-build |
2bd099 |
word = word + ' '
|
|
rpm-build |
2bd099 |
matches.append(word)
|
|
rpm-build |
2bd099 |
for nspace in [self.namespace, builtins.__dict__]:
|
|
rpm-build |
2bd099 |
for word, val in nspace.items():
|
|
rpm-build |
2bd099 |
if word[:n] == text and word not in seen:
|
|
rpm-build |
2bd099 |
seen.add(word)
|
|
rpm-build |
2bd099 |
matches.append(self._callable_postfix(val, word))
|
|
rpm-build |
2bd099 |
return matches
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
def attr_matches(self, text):
|
|
rpm-build |
2bd099 |
"""Compute matches when text contains a dot.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
Assuming the text is of the form NAME.NAME....[NAME], and is
|
|
rpm-build |
2bd099 |
evaluable in self.namespace, it will be evaluated and its attributes
|
|
rpm-build |
2bd099 |
(as revealed by dir()) are used as possible completions. (For class
|
|
rpm-build |
2bd099 |
instances, class members are also considered.)
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
WARNING: this can still invoke arbitrary C code, if an object
|
|
rpm-build |
2bd099 |
with a __getattr__ hook is evaluated.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
"""
|
|
rpm-build |
2bd099 |
import re
|
|
rpm-build |
2bd099 |
m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text)
|
|
rpm-build |
2bd099 |
if not m:
|
|
rpm-build |
2bd099 |
return []
|
|
rpm-build |
2bd099 |
expr, attr = m.group(1, 3)
|
|
rpm-build |
2bd099 |
try:
|
|
rpm-build |
2bd099 |
thisobject = eval(expr, self.namespace)
|
|
rpm-build |
2bd099 |
except Exception:
|
|
rpm-build |
2bd099 |
return []
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
# get the content of the object, except __builtins__
|
|
rpm-build |
2bd099 |
words = set(dir(thisobject))
|
|
rpm-build |
2bd099 |
words.discard("__builtins__")
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
if hasattr(thisobject, '__class__'):
|
|
rpm-build |
2bd099 |
words.add('__class__')
|
|
rpm-build |
2bd099 |
words.update(get_class_members(thisobject.__class__))
|
|
rpm-build |
2bd099 |
matches = []
|
|
rpm-build |
2bd099 |
n = len(attr)
|
|
rpm-build |
2bd099 |
if attr == '':
|
|
rpm-build |
2bd099 |
noprefix = '_'
|
|
rpm-build |
2bd099 |
elif attr == '_':
|
|
rpm-build |
2bd099 |
noprefix = '__'
|
|
rpm-build |
2bd099 |
else:
|
|
rpm-build |
2bd099 |
noprefix = None
|
|
rpm-build |
2bd099 |
while True:
|
|
rpm-build |
2bd099 |
for word in words:
|
|
rpm-build |
2bd099 |
if (word[:n] == attr and
|
|
rpm-build |
2bd099 |
not (noprefix and word[:n+1] == noprefix)):
|
|
rpm-build |
2bd099 |
match = "%s.%s" % (expr, word)
|
|
rpm-build |
2bd099 |
try:
|
|
rpm-build |
2bd099 |
val = getattr(thisobject, word)
|
|
rpm-build |
2bd099 |
except Exception:
|
|
rpm-build |
2bd099 |
pass # Include even if attribute not set
|
|
rpm-build |
2bd099 |
else:
|
|
rpm-build |
2bd099 |
match = self._callable_postfix(val, match)
|
|
rpm-build |
2bd099 |
matches.append(match)
|
|
rpm-build |
2bd099 |
if matches or not noprefix:
|
|
rpm-build |
2bd099 |
break
|
|
rpm-build |
2bd099 |
if noprefix == '_':
|
|
rpm-build |
2bd099 |
noprefix = '__'
|
|
rpm-build |
2bd099 |
else:
|
|
rpm-build |
2bd099 |
noprefix = None
|
|
rpm-build |
2bd099 |
matches.sort()
|
|
rpm-build |
2bd099 |
return matches
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
def get_class_members(klass):
|
|
rpm-build |
2bd099 |
ret = dir(klass)
|
|
rpm-build |
2bd099 |
if hasattr(klass,'__bases__'):
|
|
rpm-build |
2bd099 |
for base in klass.__bases__:
|
|
rpm-build |
2bd099 |
ret = ret + get_class_members(base)
|
|
rpm-build |
2bd099 |
return ret
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
try:
|
|
rpm-build |
2bd099 |
import readline
|
|
rpm-build |
2bd099 |
except ImportError:
|
|
rpm-build |
2bd099 |
_readline_available = False
|
|
rpm-build |
2bd099 |
else:
|
|
rpm-build |
2bd099 |
readline.set_completer(Completer().complete)
|
|
rpm-build |
2bd099 |
# Release references early at shutdown (the readline module's
|
|
rpm-build |
2bd099 |
# contents are quasi-immortal, and the completer function holds a
|
|
rpm-build |
2bd099 |
# reference to globals).
|
|
rpm-build |
2bd099 |
atexit.register(lambda: readline.set_completer(None))
|
|
rpm-build |
2bd099 |
_readline_available = True
|