Пример #1
0
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
Пример #2
0
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
Пример #3
0
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
Пример #4
0
 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'])
Пример #5
0
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
Пример #6
0
 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
Пример #7
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'])
Пример #8
0
 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', [])
Пример #9
0
 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', [])
Пример #10
0
 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'])
Пример #11
0
 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'])
Пример #12
0
 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', [])
Пример #13
0
 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)
Пример #14
0
 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'])
Пример #15
0
 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', [])
Пример #16
0
 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', [])
Пример #17
0
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
Пример #18
0
 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', [])
Пример #19
0
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