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