Skip to content

Commit

Permalink
Merge pull request #5723 from vz10/cli_parser_update
Browse files Browse the repository at this point in the history
Move getting index file name to DatabaseConnection class
  • Loading branch information
vz10 committed Nov 15, 2020
2 parents 77540d4 + de617a5 commit f278855
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 40 deletions.
5 changes: 1 addition & 4 deletions awscli/autocomplete/completer.py
Expand Up @@ -29,9 +29,6 @@ def __init__(self, parser, completers):
self._parser = parser
self._completers = completers

def parse(self, command_line, index=None):
return self._parser.parse(command_line, index)

def autocomplete(self, command_line, index=None):
"""Attempt to find completion suggestions.
Expand All @@ -42,7 +39,7 @@ def autocomplete(self, command_line, index=None):
:return: A list of ``CompletionResult`` objects.
"""
parsed = self.parse(command_line, index)
parsed = self._parser.parse(command_line, index)
for completer in self._completers:
result = completer.complete(parsed)
if result is not None:
Expand Down
21 changes: 20 additions & 1 deletion awscli/autocomplete/db.py
@@ -1,9 +1,21 @@
import os
import logging
import sqlite3

from awscli import __version__ as cli_version


LOG = logging.getLogger(__name__)

# We may eventually include a pre-generated version of this index as part
# of our shipped distributable, but for now we'll add this to our cache
# dir.
INDEX_DIR = os.path.expanduser(os.path.join('~', '.aws', 'cli', 'cache'))
INDEX_FILE = os.path.join(INDEX_DIR, '%s.index' % cli_version)
BUILTIN_INDEX_FILE = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
'data', 'ac.index'
)

# This is similar to DBConnection in awscli.customization.history.
# I'd like to reuse code, but we also have the contraint that we don't
Expand All @@ -12,9 +24,11 @@
class DatabaseConnection(object):
_JOURNAL_MODE_OFF = 'PRAGMA journal_mode=OFF'

def __init__(self, db_filename):
def __init__(self, db_filename=None):
self._db_conn = None
self._db_filename = db_filename
if self._db_filename is None:
self._db_filename = self._get_index_filename()

@property
def _connection(self):
Expand All @@ -37,3 +51,8 @@ def _ensure_database_setup(self):
# CLI is installed and in-practice, the index is only ever read from
# (except when we need to generate it).
self.execute(self._JOURNAL_MODE_OFF)

def _get_index_filename(self):
if os.path.isfile(INDEX_FILE):
return INDEX_FILE
return BUILTIN_INDEX_FILE
2 changes: 1 addition & 1 deletion awscli/autocomplete/local/model.py
Expand Up @@ -68,7 +68,7 @@ class ModelIndex(object):
parent = :parent
"""

def __init__(self, db_filename):
def __init__(self, db_filename=None):
self._db_filename = db_filename
self._db_connection = None

Expand Down
21 changes: 0 additions & 21 deletions awscli/autocomplete/main.py
Expand Up @@ -15,34 +15,18 @@
# This is the main entry point for auto-completion. This is imported
# everytime a user hits <TAB>. Try to avoid any expensive module level
# work or really heavyweight imports. Prefer to lazy load as much as possible.
import os

from awscli import __version__ as cli_version
from awscli.autocomplete import parser, completer, filters
from awscli.autocomplete.local import model, basic, fetcher
from awscli.autocomplete import serverside
from awscli.autocomplete import custom


# We may eventually include a pre-generated version of this index as part
# of our shipped distributable, but for now we'll add this to our cache
# dir.
INDEX_DIR = os.path.expanduser(os.path.join('~', '.aws', 'cli', 'cache'))
INDEX_FILE = os.path.join(INDEX_DIR, '%s.index' % cli_version)
BUILTIN_INDEX_FILE = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
'data', 'ac.index'
)


def create_autocompleter(index_filename=None, custom_completers=None,
driver=None, response_filter=None):
if response_filter is None:
response_filter = filters.startswith_filter
if custom_completers is None:
custom_completers = custom.get_custom_completers()
if index_filename is None:
index_filename = _get_index_filename()
index = model.ModelIndex(index_filename)
cli_parser = parser.CLIParser(index)
cli_driver_fetcher = None
Expand All @@ -63,11 +47,6 @@ def create_autocompleter(index_filename=None, custom_completers=None,
return cli_completer


def _get_index_filename():
if os.path.isfile(INDEX_FILE):
return INDEX_FILE
return BUILTIN_INDEX_FILE


def autocomplete(command_line, position=None):
completer = create_autocompleter()
Expand Down
9 changes: 7 additions & 2 deletions awscli/autoprompt/prompttoolkit.py
Expand Up @@ -21,6 +21,8 @@
from prompt_toolkit.document import Document

from awscli.logger import LOG_FORMAT
from awscli.autocomplete import parser
from awscli.autocomplete.local import model
from awscli.autoprompt.doc import DocsGetter
from awscli.autoprompt.factory import PromptToolkitFactory
from awscli.autoprompt.logger import PromptToolkitHandler
Expand Down Expand Up @@ -55,7 +57,7 @@ class PromptToolkitPrompter:
"""
def __init__(self, completion_source, driver, completer=None,
factory=None, app=None):
factory=None, app=None, cli_parser=None):
self._completion_source = completion_source
if completer is None:
completer = PromptToolkitCompleter(self._completion_source)
Expand All @@ -64,6 +66,9 @@ def __init__(self, completion_source, driver, completer=None,
self._completer = ThreadedCompleter(completer)
if factory is None:
factory = PromptToolkitFactory(completer=self._completer)
self._parser = cli_parser
if self._parser is None:
self._parser = parser.CLIParser(model.ModelIndex())
self._factory = factory
self._input_buffer = None
self._doc_buffer = None
Expand Down Expand Up @@ -131,7 +136,7 @@ def _set_app_defaults(self, app):
app.debug = False

def _update_doc_window_contents(self, *args):
parsed_args = self._completion_source.parse(
parsed_args = self._parser.parse(
'aws ' + self._input_buffer.document.text)
content = self._docs_getter.get_docs(parsed_args)
# The only way to "modify" a read-only buffer in prompt_toolkit is to
Expand Down
7 changes: 3 additions & 4 deletions scripts/gen-ac-index
Expand Up @@ -8,7 +8,6 @@ import argparse
from awscli.autocomplete.local import indexer
from awscli.autocomplete.serverside.indexer import APICallIndexer
from awscli.autocomplete import db
from awscli.autocomplete.main import INDEX_FILE, BUILTIN_INDEX_FILE
from awscli.autocomplete import generator
from awscli import clidriver

Expand All @@ -18,17 +17,17 @@ def main():
parser.add_argument('--include-builtin-index', action='store_true',
help=("Also generate builtin index as well as the "
"INDEX_LOCATION."))
parser.add_argument('--index-location', default=INDEX_FILE,
parser.add_argument('--index-location', default=db.INDEX_FILE,
help=(
'Location to write the index file. '
'Defaults to ' + INDEX_FILE))
'Defaults to ' + db.INDEX_FILE))
args = parser.parse_args()
index_dir = os.path.dirname(os.path.abspath(args.index_location))
if not os.path.isdir(index_dir):
os.makedirs(index_dir)
_generate_index(args.index_location)
if args.include_builtin_index:
_generate_index(BUILTIN_INDEX_FILE)
_generate_index(db.BUILTIN_INDEX_FILE)


def _generate_index(filename):
Expand Down
16 changes: 9 additions & 7 deletions tests/functional/autoprompt/test_prompttoolkit.py
Expand Up @@ -22,8 +22,8 @@
from prompt_toolkit.utils import Event

from awscli.autocomplete.main import create_autocompleter
from awscli.autocomplete import generator, filters
from awscli.autocomplete.local import indexer
from awscli.autocomplete import generator, filters, parser
from awscli.autocomplete.local import indexer, model
from awscli.clidriver import create_clidriver
from awscli.autoprompt.factory import PromptToolkitFactory
from awscli.autoprompt.prompttoolkit import (
Expand Down Expand Up @@ -124,10 +124,11 @@ class BasicPromptToolkitTest(unittest.TestCase):
def setUpClass(cls):
cls.test_file_creator = FileCreator()
basename = 'tmpfile-%s' % str(random_chars(8))
full_filename = cls.test_file_creator.full_path(basename)
_generate_index(full_filename)
index_filename = cls.test_file_creator.full_path(basename)
_generate_index(index_filename)
cls.cli_parser = parser.CLIParser(model.ModelIndex(index_filename))
cls.completion_source = create_autocompleter(
full_filename, response_filter=filters.fuzzy_filter)
index_filename, response_filter=filters.fuzzy_filter)

history = {
'version': 1,
Expand Down Expand Up @@ -155,7 +156,8 @@ def setUp(self):
self.prompter = PromptToolkitPrompter(
completion_source=self.completion_source,
driver=self.driver,
factory=self.factory
factory=self.factory,
cli_parser=self.cli_parser
)
self.prompter.args = []
self.prompter.input_buffer = self.factory.create_input_buffer()
Expand Down Expand Up @@ -238,7 +240,7 @@ def test_handle_args_with_spaces(self):
original_args = ['iam', 'create-role', '--description', 'With spaces']
prompter = PromptToolkitPrompter(
completion_source=self.completion_source, driver=self.driver,
app=FakeApplication())
app=FakeApplication(), cli_parser=self.cli_parser)
prompter.input_buffer = self.factory.create_input_buffer()
prompter.doc_buffer = self.factory.create_doc_buffer()
args = prompter.prompt_for_args(original_args)
Expand Down

0 comments on commit f278855

Please sign in to comment.