本文整理汇总了Python中readline.get_begidx函数的典型用法代码示例。如果您正苦于以下问题:Python get_begidx函数的具体用法?Python get_begidx怎么用?Python get_begidx使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_begidx函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: tab_completer
def tab_completer(text, state):
"""Called by the readline lib to calculate possible completions"""
array_to_match = None
if readline.get_begidx() == 0:
# since we are at the start of the line
# we are matching commands
array_to_match = 'cmds'
match_list = CMD_ARG_DICT.get('cmds', {}).keys()
else:
# we are matching args
cmd_line = readline.get_line_buffer()[0:readline.get_begidx()]
cmd = shlex.split(cmd_line)[-1]
array_to_match = CMD_ARG_DICT.get('cmds', {}).get(cmd)
if array_to_match:
match_list = CMD_ARG_DICT[array_to_match]
else:
array_to_match = CMD_ARG_DICT.get('options', {}).get(cmd)
if array_to_match:
match_list = CMD_ARG_DICT[array_to_match]
else:
array_to_match = 'options'
match_list = CMD_ARG_DICT.get('options',{}).keys()
matches = [item for item in match_list
if item.upper().startswith(text.upper())]
try:
return matches[state]
except IndexError:
return None
示例2: complete
def complete(self, text, state):
response = None
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:
self.current_candidates = sorted(self.options.keys())
else:
try:
if begin == 0:
candidates = self.options.keys()
else:
first = words[0]
candidates = self.options[first]
if being_completed:
self.current_candidates = [
w for w in candidates
if w.startswith(being_completed)
]
else:
self.current_candidates = candidates
except (KeyError, IndexError), err:
self.current_candidates = []
try:
response = self.current_candidates[state]
except IndexError:
response = None
return response
示例3: complete
def complete(text, state):
"""On tab press, return the next possible completion"""
rl_completion_append_character.value = '\0'
global completion_results
if state == 0:
line = readline.get_line_buffer()
if line.startswith(':'):
# Control command completion
completion_results = complete_control_command(line, text)
else:
if line.startswith('!') and text and line.startswith(text):
dropped_exclam = True
text = text[1:]
else:
dropped_exclam = False
completion_results = []
# Complete local paths
completion_results += complete_local_path(text)
# Complete from history
l = len(text)
completion_results += [w + ' ' for w in history_words if \
len(w) > l and w.startswith(text)]
if readline.get_begidx() == 0:
# Completing first word from $PATH
completion_results += [w + ' ' for w in user_commands_in_path \
if len(w) > l and w.startswith(text)]
completion_results = remove_dupes(completion_results)
if dropped_exclam:
completion_results = ['!' + r for r in completion_results]
if state < len(completion_results):
return completion_results[state]
completion_results = None
示例4: get_begidx
def get_begidx ( self ):
if util.isPlatformWindows ():
import pyreadline
return Readline ().get_begidx ()
else:
import readline
return readline.get_begidx ()
示例5: complete_readline
def complete_readline(self, text, state):
log.debug("complete_readline: text:%s, state:%s", text, state)
response = None
if state == 0:
# This is the first time we are called for this text, so build a match list.
import readline
line = readline.get_line_buffer()
words = line.split()
index = readline.get_begidx()
current = text
first = ""
if len(words) > 0:
first = words[0]
try:
if len(current) == 0:
previous = words[-1]
else:
previous = words[-2]
except IndexError:
previous = ""
self.current_candidates = self.complete(first, current, previous, words, index)
try:
response = self.current_candidates[state]
if len(self.current_candidates) == 1:
# Add space if there is only one candidate
response = "{} ".format(response)
except IndexError:
response = None
log.debug("complete_readline(%s, %s) => %s", repr(text), state, response)
return response
示例6: complete
def complete(self, text, state):
if state == 0:
origline = readline.get_line_buffer()
line = origline.lstrip()
# in case '|', ';', '&' used, take last part of line to complete
line = re.split('&|\||;', line)[-1].lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
if line.split(' ')[0] == 'sudo' and len(line.split(' ')) <= 2:
compfunc = self.completesudo
elif len (line.split(' ')) > 1 \
and line.split(' ')[0] in self.conf['allowed']:
compfunc = self.completechdir
elif 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
示例7: complete
def complete(self, text, state, plugin_scope=None):
'''
'''
if state is 0:
original_line = readline.get_line_buffer()
completion_line = original_line[0:readline.get_endidx()]
self.logger.debug('Command auto completion, user input: "%s", state is %s, completion_type is %s' \
% (original_line, state, readline.get_completion_type()))
self.logger.debug(' begidx=%s endidx=%s completion_line="%s"' % (readline.get_begidx(), readline.get_endidx(), completion_line))
self.completion_user_input = sysadmintoolkit.userinput.UserInput(completion_line, self, plugin_scope, completion=True)
self.logger.debug('Completion matches are %s' % self.completion_user_input.completion_matches)
if len(self.completion_user_input.completion_matches) is 0 or \
(self.completion_user_input.status is 'label_conflict' and self.completion_user_input.input_keyword_list[-1] is not '' and self.completion_user_input.rest_of_line is not ''):
self.print_error_on_user_input(self.completion_user_input)
self.completion_matches = []
elif len(self.completion_user_input.completion_matches) is 1:
self.completion_user_input.completion_matches[0] += ' '
try:
return self.completion_user_input.completion_matches[state]
except IndexError:
return None
示例8: complete
def complete(self, text, state):
if state == 0:
import readline
line_buf = readline.get_line_buffer()
line = line_buf.lstrip()
strip_len = len(line_buf) - len(line)
begidx = readline.get_begidx() - strip_len
endidx = readline.get_endidx() - strip_len
if begidx > 0:
cmd, arg, 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
示例9: complete
def complete(self, text, state):
reponse = None
if state == 0:
origline = readline.get_line_buffer()
begin = readline.get_begidx()
end = readline.get_endidx()
being_completed = origline[begin:end]
words = origline.split()
try:
if begin == end:
candidates = self.getCandidateList(words, [self.entry])
else:
candidates = self.getCandidateList(words[0:-1], [self.entry])
if being_completed:
self.candidates = [ w+' ' for w in candidates
if w.startswith(being_completed) ]
else:
self.candidates = candidates
except err:
self.candidates = []
try:
response = self.candidates[state]
except IndexError:
reponse = None
return response
示例10: complete
def complete(self, text, state):
response = None
if state == 0:
# This is the first time for this text, so build a match list.
origline = readline.get_line_buffer()
begin = readline.get_begidx()
end = readline.get_endidx()
being_completed = origline[begin:end]
words = origline.split()
if not words:
self.current_candidates = sorted(self.options.keys())
else:
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.startswith(being_completed) ]
else:
# matching empty string so use all candidates
self.current_candidates = candidates
except (KeyError, IndexError), err:
self.current_candidates = []
示例11: complete
def complete(self, text, state):
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)
compfunc = self.pupy_completer.complete
except AttributeError:
compfunc = self.completedefault
else:
compfunc = self.completenames
self.completion_matches = compfunc(text, line, begidx, endidx)
try:
if self.completion_matches:
return self.completion_matches[state]
except IndexError:
return None
示例12: complete
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:
origline = readline.get_line_buffer()
line = origline.lstrip()
# in case '|', ';', '&' used, take last part of line to complete
line = re.split('&|\||;', line)[-1].lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
if line.split(' ')[0] == 'sudo' and len(line.split(' ')) <= 2:
compfunc = self.completesudo
elif len (line.split(' ')) > 1 \
and line.split(' ')[0] in self.conf['allowed']:
compfunc = self.completechdir
elif 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
示例13: complete
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:
original_line = readline.get_line_buffer()
line = original_line.lstrip()
stripped = len(original_line) - len(line)
start_index = readline.get_begidx() - stripped
end_index = readline.get_endidx() - stripped
if start_index > 0:
cmd, args = self.parse_line(line)
if cmd == '':
complete_function = self.default_completer
else:
try:
complete_function = getattr(self, 'complete_' + cmd)
except AttributeError:
complete_function = self.default_completer
else:
complete_function = self.raw_command_completer
self.completion_matches = complete_function(
text, line, start_index, end_index
)
try:
return self.completion_matches[state]
except IndexError:
return None
示例14: complete
def complete(self, text, state):
response = None
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:
self.current_candidates = sorted(self.options)
else:
try:
if begin == 0:
candidates = self.allcmd
else:
if origline.endswith(' '):words.append('')
basedir,basefile = os.path.split(words[-1])
if words[0].lower()=="select":
candidates=alllocalpath("%s/cheung/data/hosts/"%HOME)
else:
candidates = alllocalpath(basedir)
being_completed = basefile
if being_completed:
self.current_candidates = [ w for w in candidates
if w.startswith(being_completed) ]
else:
self.current_candidates = candidates
except (KeyError, IndexError), err:
self.current_candidates = []
示例15: complete
def complete(self, text, state): # pylint: disable=unused-argument
'''
Tab complete for the current step.
'''
if state == 0:
parser = StatementParser(self.features)
begidx = readline.get_begidx()
endidx = readline.get_endidx()
line = readline.get_line_buffer()
prefix = line[begidx:endidx] if line else ''
line = line[:endidx]
if self.complete_single_token:
# treat the entire line as a single token
args = [line]
else:
# tokenize the line
tokens = parser.tokenize(line)
tokens = parser.condense(tokens)
args = [t.text for t in tokens if isinstance(t, StringToken)]
self.completions = self.active_step.complete(self, args, prefix)
if state < len(self.completions):
return self.completions[state]
return None