forked from treeverse/dvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt.py
More file actions
56 lines (40 loc) 路 1.23 KB
/
Copy pathprompt.py
File metadata and controls
56 lines (40 loc) 路 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Manages user prompts."""
import logging
import sys
from getpass import getpass
logger = logging.getLogger(__name__)
def ask(prompt, limited_to=None):
if not sys.stdout.isatty():
return None
while True:
try:
answer = input(prompt + " ").lower()
except EOFError:
return None
if not limited_to:
return answer
if answer in limited_to:
return answer
logger.info(
"Your response must be one of: {options}. "
"Please try again.".format(options=limited_to)
)
def confirm(statement):
"""Ask the user for confirmation about the specified statement.
Args:
statement (unicode): statement to ask the user confirmation about.
Returns:
bool: whether or not specified statement was confirmed.
"""
prompt = f"{statement} [y/n]"
answer = ask(prompt, limited_to=["yes", "no", "y", "n"])
return answer and answer.startswith("y")
def password(statement):
"""Ask the user for a password.
Args:
statement (str): string to prompt the user with.
Returns:
str: password entered by the user.
"""
logger.info(f"{statement}: ")
return getpass("")