def test_completions(): completer = Completer() for cmdline, point, expected_results in COMPLETIONS: if point == -1: point = len(cmdline) results = set(completer.complete(cmdline, point)) yield check_completer, cmdline, results, expected_results
def test_completions(): environ = { 'AWS_DATA_PATH': os.environ['AWS_DATA_PATH'], 'AWS_DEFAULT_REGION': 'us-east-1', 'AWS_ACCESS_KEY_ID': 'access_key', 'AWS_SECRET_ACCESS_KEY': 'secret_key', 'AWS_CONFIG_FILE': '', } with mock.patch('os.environ', environ): completer = Completer() for cmdline, point, expected_results in COMPLETIONS: if point == -1: point = len(cmdline) results = set(completer.complete(cmdline, point)) yield check_completer, cmdline, results, expected_results
def test_complete_top_level_args(self): commands = { 'subcommands': {}, 'arguments': ['foo', 'bar'] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws --', ['--foo', '--bar'])
def test_completions(): environ = { "AWS_DATA_PATH": os.environ["AWS_DATA_PATH"], "AWS_DEFAULT_REGION": "us-east-1", "AWS_ACCESS_KEY_ID": "access_key", "AWS_SECRET_ACCESS_KEY": "secret_key", "AWS_CA_BUNDLE": "ca_bundle", "AWS_CONFIG_FILE": "", } with mock.patch("os.environ", environ): completer = Completer() completer.clidriver = create_clidriver() for cmdline, point, expected_results in COMPLETIONS: if point == -1: point = len(cmdline) results = set(completer.complete(cmdline, point)) yield check_completer, cmdline, results, expected_results
def __init__(self): self._fuzzy = True self._awscli_completer = AwscliCompleter() completer = WordCompleter([], WORD=True) self._completer = (FuzzyCompleter(completer, WORD=True, enable_fuzzy=self._fuzzy)) self._word_boundary = 0
def test_complete_service_arguments(self): commands = { 'subcommands': { 'foo': {} }, 'arguments': ['baz', 'bin'] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws foo --', ['--baz', '--bin'])
def test_complete_service_arg_with_arg_already_used(self): commands = { 'subcommands': { 'baz': {} }, 'arguments': ['foo', 'bar'] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws baz --foo --f', [])
def test_complete_operation_arg_when_arg_already_used(self): commands = { 'subcommands': { 'foo': {'subcommands': { 'bar': {'arguments': ['baz']} }} }, 'arguments': [] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws foo bar --baz --b', [])
def test_complete_undocumented_command(self): class UndocumentedCommand(CLICommand): _UNDOCUMENTED = True commands = { 'subcommands': { 'foo': {}, 'bar': UndocumentedCommand() }, 'arguments': [] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws ', ['foo'])
def test_complete_partial_service_arguments(self): commands = { 'subcommands': { 'biz': {} }, 'arguments': ['foo', 'bar', 'foobar', 'fubar'] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws biz --f', [ '--foo', '--fubar', '--foobar']) self.assert_completion(completer, 'aws biz --fo', [ '--foo', '--foobar']) self.assert_completion(completer, 'aws biz --foob', ['--foobar'])
def test_complete_on_invalid_service(self): commands = { 'subcommands': { 'foo': {}, 'bar': { 'subcommands': { 'baz': {} } } }, 'arguments': [] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws bin', [])
def setUp(self): super(TestCompleteCustomCommands, self).setUp() custom_arguments = [{'name': 'recursive'}, {'name': 'sse'}] custom_commands = [ self.create_custom_command('mb'), self.create_custom_command('mv'), self.create_custom_command('cp', arguments=custom_arguments) ] custom_service = self.create_custom_command('s3', custom_commands) clidriver = self.clidriver_creator.create_clidriver({ 'subcommands': { 's3': custom_service['command_class'](mock.Mock()), 'foo': {} }, 'arguments': ['bar'] }) self.completer = Completer(clidriver)
def test_complete_positional_argument(self): commands = { 'subcommands': { 'foo': {'subcommands': { 'bar': {'arguments': [ 'baz', CustomArgument('bin', positional_arg=True) ]} }} }, 'arguments': [] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws foo bar --bin ', []) self.assert_completion(completer, 'aws foo bar --bin blah --', ['--baz'])
def test_complete_partial_operation_arguments(self): commands = { 'subcommands': { 'foo': {'subcommands': { 'bar': {'arguments': ['base', 'baz', 'air']} }} }, 'arguments': ['bin'] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws foo bar --b', [ '--base', '--baz', '--bin']) self.assert_completion(completer, 'aws foo bar --ba', [ '--base', '--baz']) self.assert_completion(completer, 'aws foo bar --bas', ['--base']) self.assert_completion(completer, 'aws foo bar --base', [])
def test_complete_partial_service_name(self): commands = { 'subcommands': { 'cloudfront': {}, 'cloudformation': {}, 'cloudhsm': {}, 'sts': {} }, 'arguments': [] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws cloud', [ 'cloudfront', 'cloudformation', 'cloudhsm']) self.assert_completion(completer, 'aws cloudf', [ 'cloudfront', 'cloudformation']) self.assert_completion(completer, 'aws cloudfr', ['cloudfront']) self.assert_completion(completer, 'aws cloudfront', [])
def start_server(ns): clients = [] sock = socket() try: sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) sock.bind((ns.address, ns.port)) sock.listen(5) completer = Completer() ns.logfile = None if ns.log: ns.logfile = open(ns.log, 'a') while True: c, a = sock.accept() log(ns, f'New client from {a}') clients.append(c) ClientThread(c, a, completer, ns).start() except: if ns.logfile: try: traceback.print_exc(file=ns.logfile) traceback.print_stack(file=ns.logfile) except: traceback.print_exc() traceback.print_stack() finally: for c in clients: try: c.shutdown(SHUT_RDWR) c.close() except: pass try: sock.shutdown(SHUT_RDWR) sock.close() except: pass if ns.log: try: ns.logfile.close() except: pass
def test_complete_partial_service_commands(self): commands = { 'subcommands': { 'foo': { 'subcommands': { 'barb': { 'arguments': ['nil'] }, 'baz': {}, 'biz': {}, 'foobar': {} } } }, 'arguments': [] } completer = Completer( self.clidriver_creator.create_clidriver(commands)) self.assert_completion(completer, 'aws foo b', ['barb', 'baz', 'biz']) self.assert_completion(completer, 'aws foo ba', ['barb', 'baz']) self.assert_completion(completer, 'aws foo bar', ['barb']) self.assert_completion(completer, 'aws foo barb', [])
class AwsCompleter(Completer): """ Handles aws-cli completions that are returned from the aws-cli Completer object. """ def __init__(self): self._fuzzy = True self._awscli_completer = AwscliCompleter() completer = WordCompleter([], WORD=True) self._completer = (FuzzyCompleter(completer, WORD=True, enable_fuzzy=self._fuzzy)) self._word_boundary = 0 def toggle_fuzzy(self): """Toggle fuzzy completion on/off.""" self._fuzzy = not self._fuzzy self._completer.enable_fuzzy = to_filter(self._fuzzy) def get_completions(self, document, c_e): """ Retrieve all possible aws-cli completions. Overrides prompt toolkit Completer's class get_completions. Retrieves and then yields completions of aws-cli commands, subcommands and arguments. :param document: a prompt toolkit Document object, upon which the completions are done. It contains the input text and current cursor position. :type Document :rtype None """ text = document.text_before_cursor word = document.get_word_before_cursor(WORD=True) completions = [text_type(c) for c in self._aws_completion(text)] if word: if (len(text) < self._word_boundary or not self._completer.completer.words): self._completer.completer.words = self._rebuild(text, word) elif word == '-': self._completer.completer.words = completions new_document = Document(text_type(word)) for c in self._completer.get_completions(new_document, c_e): yield Completion(c.text, -len(word), c.display, c.display_meta) self._word_boundary = len(text) - len(word) else: self._completer.completer.words = completions for c in completions: yield Completion(c, start_position=-len(word)) self._word_boundary = len(text) def _rebuild(self, text, word): # Rebuild all available subcommands/arguments. offset = None if word[0] == '-': offset = len(word) - 1 else: offset = len(word) leading_text = text[:-offset] if offset else text return [text_type(c) for c in self._aws_completion(leading_text)] def _aws_completion(self, commands): try: completions = (self._awscli_completer.complete( commands, len(commands))) except Exception: log.error('Failed to complete aws command.', exc_info=True) completions = list() return completions