def complete(self, string): param_info = self.param_info if len(param_info) == 0: return for quote in ["", '"', "'"]: try: values = shlex.split(string + quote + "|") # pipe for cursor pos except ValueError: continue else: break else: # raise the error: shlex.split(string) assert values[-1][-1] == "|" current = values[-1][:-1] values = values[:-1] prefix = string[:len(string) - len(current)] if len(values) < len(param_info): pi = param_info[len(values)] else: pi = param_info[-1] if not pi.multiple: raise ArgError("Too many arguments", len(values), len(param_info)) values = values[len(param_info):] + [current] if pi.multiple: compl = pi.typ.complete(values) else: compl = pi.typ.complete(current) for v in compl: if not shlex._find_unsafe(v): s = prefix + v + quote if s.startswith(string): yield s else: if quote: s = prefix + v.replace(quote, "\\" + quote) + quote if s.startswith(string): yield s else: if v.startswith(current): rem = v[len(current):] if shlex._find_unsafe(rem): rem = '"' + rem.replace('"', r"\"") + '"' yield string + rem
def double_quote(s): """Return a shell-escaped version of the string *s*.""" if not s: return '""' if _find_unsafe(s) is None: return s # use double quotes, and prefix double quotes with a \ # the string $"b is then quoted as "$\"b" return '"' + s.replace('"', '\\\"') + '"'
def complete_executable_with_args(string): """Complete a executable with its args""" # TODO deduplicate with ShellArgParser # Example 1: dir C:/Program" Files/" for quote in ["", '"', "'"]: try: parts = shlex.split(string + quote + "|") # pipe for cursor pos except ValueError: continue else: break else: # raise the error: shelx.split(string) # Ex1 dir # C:/Program files/| assert parts[-1][-1] == "|" current = parts[-1][:-1] parts[-1] = current for v in EXECUTABLE_W_ARGS_COMPLETE_HOOK(parts=parts): v = str(v) if not v.startswith(current): continue v = v[len(current) :] if shlex._find_unsafe(v) is None: yield string + v + quote else: if quote: yield string + v.replace(quote, "\\" + quote) + quote else: yield string + '"' + v.replace('"', r"\"") + '"'
def update_event(self, inp=-1): self.set_output_val( 0, shlex._find_unsafe(self.input(0), self.input(1), self.input(2)))