utils.py
Go to the documentation of this file.
1 import sys
2 from builtins import input
3 
4 
5 def is_tool(name):
6  """!
7  Check whether an executable exists on PATH.
8  @param name Name of the executable
9  @return True if executable exists, False otherwise
10  """
11  from whichcraft import which
12 
13  return which(name) is not None
14 
15 
16 def query_yes_no(question, default="yes"):
17  """!
18  Ask a yes/no question via raw_input() and return their answer.
19  @param question The question that is presented to the user.
20  @param default The presumed answer if the user just hits <Enter>.
21  It must be "yes" (the default), "no" or None (meaning
22  an answer is required of the user).
23  @return True if the answer is "yes", False otherwise
24  """
25  valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False}
26  if default is None:
27  prompt = " [y/n] "
28  elif default == "yes":
29  prompt = " [Y/n] "
30  elif default == "no":
31  prompt = " [y/N] "
32  else:
33  raise ValueError("invalid default answer: '%s'" % default)
34 
35  while True:
36  sys.stdout.write(question + prompt)
37  choice = input().lower()
38  if default is not None and choice == "":
39  return valid[default]
40  elif choice in valid:
41  return valid[choice]
42  else:
43  print("Please respond with 'yes' or 'no' " "(or 'y' or 'n').")
44 
45 
46 def write_flush(msg):
47  """!Write a message to standard output and flush the buffer"""
48  sys.stdout.write(msg)
49  sys.stdout.flush()
def write_flush(msg)
Write a message to standard output and flush the buffer.
Definition: utils.py:46
def is_tool(name)
Check whether an executable exists on PATH.
Definition: utils.py:5
def query_yes_no(question, default="yes")
Ask a yes/no question via raw_input() and return their answer.
Definition: utils.py:16


leo_fw
Author(s): Błażej Sowa
autogenerated on Mon Feb 28 2022 22:43:59