continued refactor streak
This commit is contained in:
58
utility.py
Normal file
58
utility.py
Normal file
@ -0,0 +1,58 @@
|
||||
from functools import wraps
|
||||
from tempfile import mkdtemp
|
||||
from shutil import rmtree
|
||||
from subprocess import check_output
|
||||
|
||||
|
||||
VERBOSE = True
|
||||
|
||||
def call_verbose(before_message='', after_message='Done!'):
|
||||
def tag(f):
|
||||
@wraps(f)
|
||||
def wrapper(*args, **kwargs):
|
||||
print_opt(before_message, end='', flush=True)
|
||||
f(*args, **kwargs)
|
||||
print_opt(after_message)
|
||||
return wrapper
|
||||
return tag
|
||||
|
||||
def print_opt(*args, **kwargs):
|
||||
if VERBOSE:
|
||||
print(*args, **kwargs)
|
||||
|
||||
|
||||
def yes_no_question(question, default):
|
||||
valid = {"yes": True, "y": True, "ye": True,
|
||||
"no": False, "n": False}
|
||||
if default is None:
|
||||
prompt = " [y/n] "
|
||||
elif default == "yes":
|
||||
prompt = " [Y/n] "
|
||||
elif default == "no":
|
||||
prompt = " [y/N] "
|
||||
else:
|
||||
raise ValueError("Invalid default answer: {}!".format(default))
|
||||
|
||||
while True:
|
||||
print(question + prompt)
|
||||
choice = input().lower()
|
||||
if default is not None and choice == '':
|
||||
return valid[default]
|
||||
elif choice in valid:
|
||||
return valid[choice]
|
||||
else:
|
||||
print("Please respond with 'yes'(y) or 'no'(n)!")
|
||||
|
||||
|
||||
# tempfile.TemporaryDirectory replacement to provide backwards compatibility
|
||||
class temporary_directory:
|
||||
def __enter__(self):
|
||||
self.name = mkdtemp()
|
||||
return self.name
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
rmtree(self.name)
|
||||
|
||||
|
||||
def get_output(*args, **kwargs):
|
||||
return check_output(*args, **kwargs).decode().rstrip('\n')
|
Reference in New Issue
Block a user