def complete(self, text, state):
"""Return the next possible completion for 'text'.
If a command has not been entered, then complete against command list.
Otherwise try to call complete_<command> to get list of completions.
"""
if state == 0:
import readline
origline = readline.get_line_buffer()
line = origline.lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
if begidx>0:
cmd, args, foo = self.parseline(line)
if cmd == '':
compfunc = self.completedefault
else:
try:
compfunc = getattr(self, 'complete_' + cmd)
except AttributeError:
compfunc = self.completedefault
else:
compfunc = self.completenames
self.completion_matches = compfunc(text, line, begidx, endidx)
try:
return self.completion_matches[state]
except IndexError:
return None
python类get_line_buffer()的实例源码
def prompt_redraw (self):
self.postcmd(False, "")
sys.stdout.write("\n" + self.prompt + readline.get_line_buffer())
sys.stdout.flush()
def complete(text, state):
buffer = readline.get_line_buffer()
return (_complete_path(buffer)+[None])[state]
def _GetNextCompletion(self, state):
if state == 0:
# TODO: Tokenize it according to our language. If this is $PS2, we also
# need previous lines! Could make a VirtualLineReader instead of
# StringLineReader?
buf = readline.get_line_buffer()
# Begin: the index of the first char of the 'word' in the line. Words
# are parsed according to readline delims (which we won't use).
begin = readline.get_begidx()
# The current position of the cursor. The thing being completed.
end = readline.get_endidx()
if self.debug:
self.status_out.Write(0,
'line: %r / begin - end: %d - %d, part: %r', buf, begin, end,
buf[begin:end])
self.comp_iter = self.root_comp.Matches(buf, self.status_out)
if self.comp_iter is None:
self.status_out.Write(0, "ASSERT comp_iter shouldn't be None")
try:
next_completion = self.comp_iter.next()
except StopIteration:
next_completion = None # sentinel?
return next_completion
def __call__(self, unused_word, state):
"""Return a single match."""
# NOTE: The readline library tokenizes words. We bypass that and use
# get_line_buffer(). So we get 'for x in l' instead of just 'l'.
#self.status_out.Write(0, 'word %r state %s', unused_word, state)
try:
return self._GetNextCompletion(state)
except Exception as e:
traceback.print_exc()
self.status_out.Write(0, 'Unhandled exception while completing: %s', e)
def complete(self, text, state):
"""Return the next possible completion for 'text'.
If a command has not been entered, then complete against command list.
Otherwise try to call complete_<command> to get list of completions.
"""
if state == 0:
import readline
origline = readline.get_line_buffer()
line = origline.lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
if begidx>0:
cmd, args, foo = self.parseline(line)
if cmd == '':
compfunc = self.completedefault
else:
try:
compfunc = getattr(self, 'complete_' + cmd)
except AttributeError:
compfunc = self.completedefault
else:
compfunc = self.completenames
self.completion_matches = compfunc(text, line, begidx, endidx)
try:
return self.completion_matches[state]
except IndexError:
return None
def complete(self, text, state):
"""Return the next possible completion for 'text'.
If a command has not been entered, then complete against command list.
Otherwise try to call complete_<command> to get list of completions.
"""
if state == 0:
import readline
origline = readline.get_line_buffer()
line = origline.lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
if begidx>0:
cmd, args, foo = self.parseline(line)
if cmd == '':
compfunc = self.completedefault
else:
try:
compfunc = getattr(self, 'complete_' + cmd)
except AttributeError:
compfunc = self.completedefault
else:
compfunc = self.completenames
self.completion_matches = compfunc(text, line, begidx, endidx)
try:
return self.completion_matches[state]
except IndexError:
return None
def complete(self, text, state):
"""Return the next possible completion for 'text'.
If a command has not been entered, then complete against command list.
Otherwise try to call complete_<command> to get list of completions.
"""
if state == 0:
import readline
origline = readline.get_line_buffer()
line = origline.lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
if begidx>0:
cmd, args, foo = self.parseline(line)
if cmd == '':
compfunc = self.completedefault
else:
try:
compfunc = getattr(self, 'complete_' + cmd)
except AttributeError:
compfunc = self.completedefault
else:
compfunc = self.completenames
self.completion_matches = compfunc(text, line, begidx, endidx)
try:
return self.completion_matches[state]
except IndexError:
return None
def print_ahead(expr, prompt = '', *args, **kwargs):
'''Prints expr above the current line'''
current_input = readline.get_line_buffer()
v.erase_line()
stdout.write('\r')
print_out(expr, *args, **kwargs)
print_out(prompt + current_input, ending = '')
def complete(self, text, state):
if state == 0:
self._build_match_list(
readline.get_line_buffer(),
readline.get_begidx(), readline.get_endidx()
)
try:
return self.matches[state]
except IndexError:
pass
#except Exception as e:
# print 'Error: {0}'.format(repr(e))
return None
# vim: :ai:sw=4:ts=4:sts=4:et:ft=python:fo=corqj2:sm:tw=79:
def print_plain(self, text, redraw = False):
sys.stdout.write("\033[1K\r" + text + os.linesep)
sys.stdout.flush()
if redraw or threading.current_thread().ident != self.main_thread_id:
import readline
#sys.stdout.write("\033[s")
sys.stdout.write(self.clean_prompt + readline.get_line_buffer())
#sys.stdout.write("\033[u\033[B")
sys.stdout.flush()
def complete(self, text, state):
"""Return the next possible completion for 'text'."""
if state == 0:
origline = readline.get_line_buffer()
begidx = readline.get_begidx()
endidx = readline.get_endidx()
if begidx > 0:
cmd, args, foo = self.parseline(origline)
if cmd == '':
compfunc = self.completedefault
else:
try:
compfunc = getattr(self, 'complete_' + cmd.lower())
except AttributeError:
try:
compfunc = self.ctx.lookup_compfunction(cmd)
except AttributeError:
compfunc = self.completedefault
else:
compfunc = self.completenames
arglist = [item.strip() for item in origline.strip().split()]
comp_state = self.get_compstate(text, arglist)
self.completion_matches = compfunc(text, origline, arglist, comp_state, begidx, endidx)
try:
return self.completion_matches[state]
except:
return None
#def get_names(self):
# names = []
# classes = [self.__class__]
# while classes:
# aclass = classes.pop(0)
# if aclass.__bases__:
# classes = classes + list(aclass.__bases__)
# names = names + dir(aclass)
# return names
def complete(self, text, state):
"""Return the next possible completion for 'text'.
If a command has not been entered, then complete against command list.
Otherwise try to call complete_<command> to get list of completions.
"""
if state == 0:
import readline
origline = readline.get_line_buffer()
line = origline.lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
if begidx>0:
cmd, args, foo = self.parseline(line)
if cmd == '':
compfunc = self.completedefault
else:
try:
compfunc = getattr(self, 'complete_' + cmd)
except AttributeError:
compfunc = self.completedefault
else:
compfunc = self.completenames
self.completion_matches = compfunc(text, line, begidx, endidx)
try:
return self.completion_matches[state]
except IndexError:
return None
def completer():
# source: https://gist.github.com/iamatypeofwalrus/5637895
class tabCompleter(object):
def pathCompleter(self,text,state):
line = readline.get_line_buffer().split()
return [x for x in glob(text+'*')][state]
def createListCompleter(self,ll):
pass
def listCompleter(text,state):
line = readline.get_line_buffer()
if not line:
return None
else:
return [c + " " for c in ll if c.startswith(line)][state]
self.listCompleter = listCompleter
t = tabCompleter()
# tool command
t.createListCompleter(["clear", "exit", "banner","exec","restart", "upgrade", 'search'
# modules
# auxiliary modules
,"use auxiliary/gather/ip_gather","use auxiliary/gather/ip_lookup", "use auxiliary/core/pyconverter"
# exploit modules
,"use exploit/windows/ftp/ftpshell_overflow", "use exploit/android/login/login_bypass", "use exploit/windows/http/oracle9i_xdb_pass"])
readline.set_completer_delims('\t')
readline.parse_and_bind("tab: complete")
readline.set_completer(t.listCompleter)
def pathCompleter(self,text,state):
line = readline.get_line_buffer().split()
return [x for x in glob.glob(text+'*')][state]
def complete(self, text, state):
"""Return the next possible completion for 'text'.
If a command has not been entered, then complete against command list.
Otherwise try to call complete_<command> to get list of completions.
"""
if state == 0:
import readline
origline = readline.get_line_buffer()
line = origline.lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
if begidx>0:
cmd, args, foo = self.parseline(line)
if cmd == '':
compfunc = self.completedefault
else:
try:
compfunc = getattr(self, 'complete_' + cmd)
except AttributeError:
compfunc = self.completedefault
else:
compfunc = self.completenames
self.completion_matches = compfunc(text, line, begidx, endidx)
try:
return self.completion_matches[state]
except IndexError:
return None
def complete(self,text,state):
try:
tokens = readline.get_line_buffer().split()
if not tokens or readline.get_line_buffer()[-1] == ' ':
tokens.append('')
results = self.traverse(tokens,self.commands) + [None]
return results[state]
except:
return
#UPNP class for getting, sending and parsing SSDP/SOAP XML data (among other things...)
def complete(self, text, state):
"""Return the next possible completion for 'text'.
If a command has not been entered, then complete against command list.
Otherwise try to call complete_<command> to get list of completions.
"""
if state == 0:
import readline
origline = readline.get_line_buffer()
line = origline.lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
if begidx>0:
cmd, args, foo = self.parseline(line)
if cmd == '':
compfunc = self.completedefault
else:
try:
compfunc = getattr(self, 'complete_' + cmd)
except AttributeError:
compfunc = self.completedefault
else:
compfunc = self.completenames
self.completion_matches = compfunc(text, line, begidx, endidx)
try:
return self.completion_matches[state]
except IndexError:
return None
def complete(self, text, state):
"""Return the next possible completion for 'text'.
If a command has not been entered, then complete against command list.
Otherwise try to call complete_<command> to get list of completions.
"""
if state == 0:
import readline
origline = readline.get_line_buffer()
line = origline.lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
if begidx>0:
cmd, args, foo = self.parseline(line)
if cmd == '':
compfunc = self.completedefault
else:
try:
compfunc = getattr(self, 'complete_' + cmd)
except AttributeError:
compfunc = self.completedefault
else:
compfunc = self.completenames
self.completion_matches = compfunc(text, line, begidx, endidx)
try:
return self.completion_matches[state]
except IndexError:
return None
def complete(self, text, state):
"""Return the next possible completion for 'text'.
If a command has not been entered, then complete against command list.
Otherwise try to call complete_<command> to get list of completions.
"""
if state == 0:
import readline
origline = readline.get_line_buffer()
line = origline.lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
if begidx>0:
cmd, args, foo = self.parseline(line)
if cmd == '':
compfunc = self.completedefault
else:
try:
compfunc = getattr(self, 'complete_' + cmd)
except AttributeError:
compfunc = self.completedefault
else:
compfunc = self.completenames
self.completion_matches = compfunc(text, line, begidx, endidx)
try:
return self.completion_matches[state]
except IndexError:
return None