def get_completions(self, line, prefix): tokens = self.parser.tokenize(line) cmd_name = "" loc = None args = [] next_arg = True ret = [] for token in tokens: if isinstance(token, StringToken): if not cmd_name: cmd_name = token.text loc = 'name' elif loc == 'name': cmd_name += token.text else: if next_arg: args.append(token.text) next_arg = False else: args[-1] += token.text elif isinstance(token, OperatorToken): if token.operator in ('|', ';', '&&', '||'): cmd_name = None args = [] next_arg = True elif token.operator in ('>', '<', '>>'): loc = 'path' args = [] elif isinstance(token, WhitespaceToken): if loc == 'name': loc = None next_arg = True if loc == 'path': ret = path_completer(''.join(args)) elif not cmd_name or loc == 'name': if is_path_prefix(cmd_name): ret = path_completer(cmd_name) else: ret = self.get_command_name_completions(cmd_name) else: if cmd_name not in self.commands: ret = [] else: if next_arg: args.append('') cmd = self.commands[cmd_name] ret = cmd.complete(self, args, prefix) return ret
def test_no_matches(self): self.listdir_mock.return_value = ['file', 'dir'] self.isdir_mock.side_effect = [True, False, True] self.exists_mock.return_value = True ans = path_completer(os.path.join('dir', 'fz'), 'fz') assert ans == []
def test_simple_multi(self): self.listdir_mock.return_value = ['file1', 'file2', 'f_dir1', 'test1'] self.isdir_mock.side_effect = [True, False, False, True, False] self.exists_mock.return_value = True ans = path_completer(os.path.join('dir', 'f'), 'f') assert ans == ['file1\0', 'file2\0', 'f_dir1' + os.path.sep]
def complete(self, shell, args, prefix): if len(args) == 1: return [x for x in ("clear", "delete", "list", "load", "save") if x.startswith(prefix)] if len(args) == 2: if args[0] == "save" or args[0] == "load": return path_completer(args[-1]) return []
def complete(self, shell, args, prefix): if len(args) == 1: return [x for x in ('clear', 'delete', 'exec', 'list', 'load', 'save') if x.startswith(prefix)] if len(args) == 2: if args[0] == 'save' or args[0] == 'load': return path_completer(shell, args, prefix) return []
def wizard_step_path_completer(shell, args, prefix): # pylint: disable=unused-argument ''' Complete a path within a wizard step. This completer will only return consistent results for wizards that have ``complete_single_token = True``. ''' return [ i.replace('\0', '') for i in path_completer(args[-1] if args else prefix, prefix) ]
def complete(self, shell, args, prefix): return path_completer(args[-1], prefix=prefix)
def test_cwd_not_exist(self): self.listdir_mock.return_value = ['file1'] self.isdir_mock.side_effect = [True] self.exists_mock.return_value = False assert path_completer('f', 'f') == []
def test_path_sep_token(self): self.listdir_mock.return_value = ['file1', 'dir1'] self.isdir_mock.side_effect = [True, False, True] self.exists_mock.return_value = True assert path_completer('dir' + os.path.sep, '') == ['file1\0', 'dir1' + os.path.sep]
def test_root_no_prefix(self): self.listdir_mock.return_value = ['bin', 'sys'] self.isdir_mock.side_effect = [True, True, True] self.exists_mock.return_value = True assert path_completer('/', '') == ['bin' + os.path.sep, 'sys' + os.path.sep]
def test_space_no_prefix(self): self.listdir_mock.return_value = ['a file', 'a dir', 'not a file'] self.isdir_mock.side_effect = [True, False, True, False] self.exists_mock.return_value = True assert path_completer('a ', '') == ['file\0', 'dir' + os.path.sep]
def complete(self, shell, args, prefix): return path_completer(args[-1])
def test_no_exists(self): self.listdir_mock.return_value = [] self.isdir_mock.return_value = False self.exists_mock.return_value = False assert path_completer(os.path.join('dir', 'f'), 'f') == []
def complete_path(self, shell, args, prefix): # pylint: disable=unused-argument return path_completer(args[-1], prefix=prefix)
def test_dir_and_file(self): self.listdir_mock.return_value = ['dir', 'dir_file'] self.isdir_mock.side_effect = [True, True, False] self.exists_mock.return_value = True assert path_completer('dir', 'dir') == ['dir' + os.path.sep, 'dir_file\0']
def complete(self, shell, args, prefix): return path_completer(shell, args, prefix)
def get_completions(self, line, prefix): ''' Get the list of completions given a line buffer and a prefix. :param str line: line buffer content up to cursor :param str prefix: readline prefix token :returns list[str]: list of completions ''' try: parser = StatementParser(TabCompletionFeatures(self.features)) tokens = parser.tokenize(line) parser.clean_escapes(tokens) cmd_name = "" loc = None args = [] next_arg = True ret = [] in_quote = None for token in tokens: if isinstance(token, StringToken): in_quote = token.quote if token.open_quote else None if not cmd_name: cmd_name = token.text loc = 'name' elif loc == 'name': cmd_name += token.text else: if next_arg: args.append(token.text) next_arg = False else: args[-1] += token.text elif isinstance(token, OperatorToken): in_quote = None if token.operator in ('|', ';', '&&', '||'): cmd_name = None args = [] next_arg = True elif token.operator in ('>', '<', '>>'): loc = 'path' args = [] elif isinstance(token, WhitespaceToken): in_quote = None if loc == 'name': loc = None next_arg = True if loc == 'path': ret = path_completer(''.join(args), prefix) elif not cmd_name or loc == 'name': if is_path_prefix(cmd_name): ret = path_completer(cmd_name, prefix) else: ret = self.get_command_name_completions(cmd_name) else: if cmd_name not in self.commands: ret = [] else: if next_arg: args.append('') cmd = self.commands[cmd_name] ret = cmd.complete(self, args, prefix) ret = self._clean_completions(ret, in_quote) except: ret = [] return ret