Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../usr/lib64/python3....
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
- When the original stdin is not a tty device, GNU readline is never
[26] Fix | Delete
used, and this module (and the readline module) are silently inactive.
[27] Fix | Delete
[28] Fix | Delete
"""
[29] Fix | Delete
[30] Fix | Delete
import atexit
[31] Fix | Delete
import builtins
[32] Fix | Delete
import __main__
[33] Fix | Delete
[34] Fix | Delete
__all__ = ["Completer"]
[35] Fix | Delete
[36] Fix | Delete
class Completer:
[37] Fix | Delete
def __init__(self, namespace = None):
[38] Fix | Delete
"""Create a new completer for the command line.
[39] Fix | Delete
[40] Fix | Delete
Completer([namespace]) -> completer instance.
[41] Fix | Delete
[42] Fix | Delete
If unspecified, the default namespace where completions are performed
[43] Fix | Delete
is __main__ (technically, __main__.__dict__). Namespaces should be
[44] Fix | Delete
given as dictionaries.
[45] Fix | Delete
[46] Fix | Delete
Completer instances should be used as the completion mechanism of
[47] Fix | Delete
readline via the set_completer() call:
[48] Fix | Delete
[49] Fix | Delete
readline.set_completer(Completer(my_namespace).complete)
[50] Fix | Delete
"""
[51] Fix | Delete
[52] Fix | Delete
if namespace and not isinstance(namespace, dict):
[53] Fix | Delete
raise TypeError('namespace must be a dictionary')
[54] Fix | Delete
[55] Fix | Delete
# Don't bind to namespace quite yet, but flag whether the user wants a
[56] Fix | Delete
# specific namespace or to use __main__.__dict__. This will allow us
[57] Fix | Delete
# to bind to __main__.__dict__ at completion time, not now.
[58] Fix | Delete
if namespace is None:
[59] Fix | Delete
self.use_main_ns = 1
[60] Fix | Delete
else:
[61] Fix | Delete
self.use_main_ns = 0
[62] Fix | Delete
self.namespace = namespace
[63] Fix | Delete
[64] Fix | Delete
def complete(self, text, state):
[65] Fix | Delete
"""Return the next possible completion for 'text'.
[66] Fix | Delete
[67] Fix | Delete
This is called successively with state == 0, 1, 2, ... until it
[68] Fix | Delete
returns None. The completion should begin with 'text'.
[69] Fix | Delete
[70] Fix | Delete
"""
[71] Fix | Delete
if self.use_main_ns:
[72] Fix | Delete
self.namespace = __main__.__dict__
[73] Fix | Delete
[74] Fix | Delete
if not text.strip():
[75] Fix | Delete
if state == 0:
[76] Fix | Delete
if _readline_available:
[77] Fix | Delete
readline.insert_text('\t')
[78] Fix | Delete
readline.redisplay()
[79] Fix | Delete
return ''
[80] Fix | Delete
else:
[81] Fix | Delete
return '\t'
[82] Fix | Delete
else:
[83] Fix | Delete
return None
[84] Fix | Delete
[85] Fix | Delete
if state == 0:
[86] Fix | Delete
if "." in text:
[87] Fix | Delete
self.matches = self.attr_matches(text)
[88] Fix | Delete
else:
[89] Fix | Delete
self.matches = self.global_matches(text)
[90] Fix | Delete
try:
[91] Fix | Delete
return self.matches[state]
[92] Fix | Delete
except IndexError:
[93] Fix | Delete
return None
[94] Fix | Delete
[95] Fix | Delete
def _callable_postfix(self, val, word):
[96] Fix | Delete
if callable(val):
[97] Fix | Delete
word = word + "("
[98] Fix | Delete
return word
[99] Fix | Delete
[100] Fix | Delete
def global_matches(self, text):
[101] Fix | Delete
"""Compute matches when text is a simple name.
[102] Fix | Delete
[103] Fix | Delete
Return a list of all keywords, built-in functions and names currently
[104] Fix | Delete
defined in self.namespace that match.
[105] Fix | Delete
[106] Fix | Delete
"""
[107] Fix | Delete
import keyword
[108] Fix | Delete
matches = []
[109] Fix | Delete
seen = {"__builtins__"}
[110] Fix | Delete
n = len(text)
[111] Fix | Delete
for word in keyword.kwlist:
[112] Fix | Delete
if word[:n] == text:
[113] Fix | Delete
seen.add(word)
[114] Fix | Delete
if word in {'finally', 'try'}:
[115] Fix | Delete
word = word + ':'
[116] Fix | Delete
elif word not in {'False', 'None', 'True',
[117] Fix | Delete
'break', 'continue', 'pass',
[118] Fix | Delete
'else'}:
[119] Fix | Delete
word = word + ' '
[120] Fix | Delete
matches.append(word)
[121] Fix | Delete
for nspace in [self.namespace, builtins.__dict__]:
[122] Fix | Delete
for word, val in nspace.items():
[123] Fix | Delete
if word[:n] == text and word not in seen:
[124] Fix | Delete
seen.add(word)
[125] Fix | Delete
matches.append(self._callable_postfix(val, word))
[126] Fix | Delete
return matches
[127] Fix | Delete
[128] Fix | Delete
def attr_matches(self, text):
[129] Fix | Delete
"""Compute matches when text contains a dot.
[130] Fix | Delete
[131] Fix | Delete
Assuming the text is of the form NAME.NAME....[NAME], and is
[132] Fix | Delete
evaluable in self.namespace, it will be evaluated and its attributes
[133] Fix | Delete
(as revealed by dir()) are used as possible completions. (For class
[134] Fix | Delete
instances, class members are also considered.)
[135] Fix | Delete
[136] Fix | Delete
WARNING: this can still invoke arbitrary C code, if an object
[137] Fix | Delete
with a __getattr__ hook is evaluated.
[138] Fix | Delete
[139] Fix | Delete
"""
[140] Fix | Delete
import re
[141] Fix | Delete
m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text)
[142] Fix | Delete
if not m:
[143] Fix | Delete
return []
[144] Fix | Delete
expr, attr = m.group(1, 3)
[145] Fix | Delete
try:
[146] Fix | Delete
thisobject = eval(expr, self.namespace)
[147] Fix | Delete
except Exception:
[148] Fix | Delete
return []
[149] Fix | Delete
[150] Fix | Delete
# get the content of the object, except __builtins__
[151] Fix | Delete
words = set(dir(thisobject))
[152] Fix | Delete
words.discard("__builtins__")
[153] Fix | Delete
[154] Fix | Delete
if hasattr(thisobject, '__class__'):
[155] Fix | Delete
words.add('__class__')
[156] Fix | Delete
words.update(get_class_members(thisobject.__class__))
[157] Fix | Delete
matches = []
[158] Fix | Delete
n = len(attr)
[159] Fix | Delete
if attr == '':
[160] Fix | Delete
noprefix = '_'
[161] Fix | Delete
elif attr == '_':
[162] Fix | Delete
noprefix = '__'
[163] Fix | Delete
else:
[164] Fix | Delete
noprefix = None
[165] Fix | Delete
while True:
[166] Fix | Delete
for word in words:
[167] Fix | Delete
if (word[:n] == attr and
[168] Fix | Delete
not (noprefix and word[:n+1] == noprefix)):
[169] Fix | Delete
match = "%s.%s" % (expr, word)
[170] Fix | Delete
try:
[171] Fix | Delete
val = getattr(thisobject, word)
[172] Fix | Delete
except Exception:
[173] Fix | Delete
pass # Include even if attribute not set
[174] Fix | Delete
else:
[175] Fix | Delete
match = self._callable_postfix(val, match)
[176] Fix | Delete
matches.append(match)
[177] Fix | Delete
if matches or not noprefix:
[178] Fix | Delete
break
[179] Fix | Delete
if noprefix == '_':
[180] Fix | Delete
noprefix = '__'
[181] Fix | Delete
else:
[182] Fix | Delete
noprefix = None
[183] Fix | Delete
matches.sort()
[184] Fix | Delete
return matches
[185] Fix | Delete
[186] Fix | Delete
def get_class_members(klass):
[187] Fix | Delete
ret = dir(klass)
[188] Fix | Delete
if hasattr(klass,'__bases__'):
[189] Fix | Delete
for base in klass.__bases__:
[190] Fix | Delete
ret = ret + get_class_members(base)
[191] Fix | Delete
return ret
[192] Fix | Delete
[193] Fix | Delete
try:
[194] Fix | Delete
import readline
[195] Fix | Delete
except ImportError:
[196] Fix | Delete
_readline_available = False
[197] Fix | Delete
else:
[198] Fix | Delete
readline.set_completer(Completer().complete)
[199] Fix | Delete
# Release references early at shutdown (the readline module's
[200] Fix | Delete
# contents are quasi-immortal, and the completer function holds a
[201] Fix | Delete
# reference to globals).
[202] Fix | Delete
atexit.register(lambda: readline.set_completer(None))
[203] Fix | Delete
_readline_available = True
[204] Fix | Delete
[205] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function