KrystianD.mikrotik_configur.../mikrotik_configurator/utils.py

36 lines
946 B
Python
Raw Permalink Normal View History

2022-11-04 21:20:16 +01:00
import sys
def write_text_file(path, content):
with open(path, "wt", encoding="utf-8") as f:
f.write(content)
def read_text_file(path):
with open(path, "rt") as f:
return f.read()
def query_yes_no(question, default="yes"):
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: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")