コード例 #1
0
ファイル: test_completer.py プロジェクト: cmaxwellau/aws-cli
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
ファイル: test_completer.py プロジェクト: AsherBond/aws-cli
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
ファイル: test_completer.py プロジェクト: rbramwell/aws-cli
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
コード例 #5
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