def main_completer_handler(self, text, state):
""" Handler of all input entries, tabs, and history
:param text: input text
:type text: string.
:param state: current state
:type state: string.
"""
response = None
# Build match list on first iteration else continue
if state == 0:
origline = readline.get_line_buffer()
begin = readline.get_begidx()
end = readline.get_endidx()
being_completed = origline[begin:end]
words = origline.split()
if not words:
# option for words list
self.current_candidates = sorted(self.options.keys())
else:
# traverse all words entries and passing accordingly
try:
if begin == 0:
# first word
candidates = self.options.keys()
else:
# later word
first = words[0]
candidates = self.options[first]
if being_completed:
# match options with portion of input being completed
self.current_candidates = [w for w in candidates\
if w.lower().startswith(being_completed.lower())]
else:
# matching empty string so use all candidates
self.current_candidates = candidates
except (KeyError, IndexError):
self.current_candidates = []
# Return the state from the match list if found otherwise return None.
try:
response = self.current_candidates[state]
except IndexError:
# No candidate found for state
response = None
# Response return
return response
python类get_begidx()的实例源码
def complete(self, text, state):
"""Return the next possible completion for 'text'."""
if state == 0:
try:
import readline
except ImportError:
import pyreadline as readline
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 IndexError:
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