1"""Manages user prompts.""" 2 3from __future__ import unicode_literals 4from __future__ import print_function 5 6import sys 7from getpass import getpass 8 9from dvc.progress import progress_aware 10from dvc.utils.compat import input 11 12 13def _ask(prompt, limited_to=None): 14 if not sys.stdout.isatty(): 15 return None 16 17 while True: 18 try: 19 answer = input(prompt + " ").lower() 20 except EOFError: 21 return None 22 23 if not limited_to: 24 return answer 25 26 if answer in limited_to: 27 return answer 28 29 print( 30 "Your response must be one of: {options}. " 31 "Please try again.".format(options=limited_to) 32 ) 33 34 35@progress_aware 36def confirm(statement): 37 """Ask the user for confirmation about the specified statement. 38 39 Args: 40 statement (unicode): statement to ask the user confirmation about. 41 42 Returns: 43 bool: whether or not specified statement was confirmed. 44 """ 45 prompt = "{statement} [y/n]".format(statement=statement) 46 answer = _ask(prompt, limited_to=["yes", "no", "y", "n"]) 47 return answer and answer.startswith("y") 48 49 50@progress_aware 51def password(statement): 52 """Ask the user for a password. 53 54 Args: 55 statement (str): string to prompt the user with. 56 57 Returns: 58 str: password entered by the user. 59 """ 60 prompt = "{statement}: ".format(statement=statement) 61 return getpass(prompt) 62