Skip to content

Commit

Permalink
add fuzzy input
Browse files Browse the repository at this point in the history
  • Loading branch information
iakov-aws committed Nov 11, 2024
1 parent 958a2be commit ec18f6e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
15 changes: 8 additions & 7 deletions cid/helpers/athena.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,18 @@ def DatabaseName(self) -> str:
athena_databases = self.list_databases()

# check if we have a default database
print(athena_databases)
logger.info(f'athena_databases = {athena_databases}')
default_databases = [database for database in athena_databases if database == self.defaults.get('DatabaseName')]
if 'cid_cur' in athena_databases:
default_databases = ['cid_cur']

# Ask user
choices = list(athena_databases)
if self.defaults.get('DatabaseName') not in choices:
choices.append(self.defaults.get('DatabaseName') + ' (CREATE NEW)')
self._DatabaseName = get_parameter(
param_name='athena-database',
message="Select AWS Athena database to use",
message="Select AWS Athena database to use as default",
choices=choices,
default=default_databases[0] if default_databases else None,
)
Expand Down Expand Up @@ -391,10 +393,9 @@ def wait_for_view(self, view_name: str, poll_interval=1, timeout=60) -> None:


def delete_table(self, name: str, catalog: str=None, database: str=None):
if get_parameter(
if get_yesno_parameter(
param_name=f'confirm-{name}',
message=f'Delete Athena table {name}?',
choices=['yes', 'no'],
default='no') != 'yes':
return False

Expand All @@ -415,10 +416,9 @@ def delete_table(self, name: str, catalog: str=None, database: str=None):
return True

def delete_view(self, name: str, catalog: str=None, database: str=None):
if get_parameter(
if get_yesno_parameter(
param_name=f'confirm-{name}',
message=f'Delete Athena view {name}?',
choices=['yes', 'no'],
default='no') != 'yes':
return False

Expand Down Expand Up @@ -545,7 +545,8 @@ def create_or_update_view(self, view_name, view_query):
param_name='view-' + view_name + '-override',
message=f'The existing view is different. Override?',
choices=['retry diff', 'proceed and override', 'keep existing', 'exit'],
default='retry diff'
default='retry diff',
fuzzy=False,
)
if choice == 'retry diff':
unset_parameter('view-' + view_name + '-override')
Expand Down
45 changes: 28 additions & 17 deletions cid/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from collections.abc import Iterable

import requests
import questionary
from InquirerPy import inquirer
from InquirerPy.base.control import Choice
from boto3.session import Session
from botocore.exceptions import NoCredentialsError, CredentialRetrievalError, NoRegionError, ProfileNotFound

Expand Down Expand Up @@ -183,23 +184,24 @@ def get_yesno_parameter(param_name, message, default=None, break_on_ctrl_c=True)
unset_parameter(param_name)
if default != None:
default = 'yes' if mapping[default] else 'no'
res = get_parameter(param_name, message=message, choices=['yes', 'no'], default=default, break_on_ctrl_c=break_on_ctrl_c)
res = get_parameter(param_name, message=message, choices=['yes', 'no'], default=default, break_on_ctrl_c=break_on_ctrl_c, fuzzy=False)
params[param_name] = (res == 'yes')
return params[param_name]


def get_parameter(param_name, message, choices=None, default=None, none_as_disabled=False, template_variables={}, break_on_ctrl_c=True):
def get_parameter(param_name, message, choices=None, default=None, none_as_disabled=False, template_variables={}, break_on_ctrl_c=True, fuzzy=True):
"""
Check if parameters are provided in the command line and if not, ask user
Check if parameters are provided in the command line and if not, ask user
:param message: text message for user
:param choices: a list or dict for choice. None for text entry. Keys and Values must be strings.
:param default: a default text template
:param none_as_disabled: if True and choices is a dict, all choices with None as a value will be disabled
:param template_variables: a dict with varibles for template
:param template_variables: a dict with variables for template
:param break_on_ctrl_c: if True, exit() if user pressed CTRL+C
:param fuzzy: if True, exit() if user pressed CTRL+C
:returns: a value choosed by user or provided in command line
:returns: a value from user or provided in command line
"""
logger.debug(f'getting param {param_name}')
param_name = param_name.replace('_', '-')
Expand All @@ -220,33 +222,42 @@ def get_parameter(param_name, message, choices=None, default=None, none_as_disab
_choices = []
for key, value in choices.items():
_choices.append(
questionary.Choice(
title=key,
Choice(
name=key,
value=value,
disabled=True if (none_as_disabled and value is None) else False,
enabled=not (none_as_disabled and value is None),
)
)
choices = _choices

print()
if not isatty():
raise Exception(f'Please set parameter {param_name}. Unable to request user in environment={exec_env()}')
result = questionary.select(
message=f'[{param_name}] {message}:',
choices=choices,
default=default,
).ask()
if fuzzy:
result = inquirer.fuzzy(
message=f'[{param_name}] {message}:',
choices=choices,
long_instruction='use arrows or start typing',
match_exact=True,
default=default,
).execute()
else:
result = inquirer.select(
message=f'[{param_name}] {message}:',
choices=choices,
long_instruction='use arrows or start typing',
default=default,
).execute()
else: # it is a text entry
if isinstance(default, str) and template_variables:
print(template_variables)
default=default.format(**template_variables)
print()
if not isatty():
raise Exception(f'Please set parameter {param_name}. Unable to request user in environment={exec_env()}')
result = questionary.text(
result = inquirer.text(
message=f'[{param_name}] {message}:' ,
default=default or '',
).ask()
).execute()
if isinstance(result, str) and template_variables:
result = result.format(**template_variables)
if (break_on_ctrl_c and result is None):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ requests
six>=1.15
tqdm
tzlocal>=4.0
questionary>=1.10
InquirerPy
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ install_requires =
requests
tzlocal>=4.0
six>=1.15
questionary>=1.10
InquirerPy
tqdm

[options.entry_points]
Expand Down

0 comments on commit ec18f6e

Please sign in to comment.