1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-12-22 23:51:03 +00:00
AdvLabDB/advlabdb/scripts/terminal_utils.py

102 lines
2.1 KiB
Python
Raw Normal View History

2022-05-17 09:58:19 +00:00
# No relative imports allowed in this file to be able to run server_setup.py without packages
2022-05-08 15:29:45 +00:00
import subprocess
from getpass import getpass
def run(command, **kwargs):
return subprocess.run(command, shell=True, **kwargs)
def box(message, context=None):
text_line = "| "
if context is not None:
text_line += context + ": "
text_line += message + " |"
separator = "=" * len(text_line)
print()
print(separator)
print(text_line)
print(separator)
print()
def step(message):
continue_message = "-> Press ENTER to continue or Ctrl+C to interrupt the script <-"
upper_separator = "_" * len(continue_message)
print()
print(upper_separator)
box(message, "Next step")
print(continue_message)
getpass("")
print()
def spaced_hl():
print("\n\n" + "_" * 20 + "\n\n")
def validating_input(
prompt,
options=None,
format_function=lambda ans: ans,
check_constraints_function=lambda ans: True,
):
if options is not None:
prompt += " ["
for opt in options[:-1]:
prompt += opt + "/"
prompt += options[-1] + "]: "
lowered_options = [opt.lower() for opt in options]
def adj_check_constraints_function(ans):
return ans.lower() in lowered_options and check_constraints_function(ans)
else:
prompt += ": "
adj_check_constraints_function = check_constraints_function
ans = None
first_run = True
2022-05-17 11:39:08 +00:00
done_validation = False
2022-05-08 15:29:45 +00:00
2022-05-17 11:39:08 +00:00
while not done_validation or not adj_check_constraints_function(ans):
2022-05-08 15:29:45 +00:00
if not first_run:
2022-05-17 11:39:08 +00:00
done_validation = False
print("Invalid input!\n")
2022-05-08 15:29:45 +00:00
else:
first_run = False
ans = input(prompt)
try:
2022-05-17 11:39:08 +00:00
ans = format_function(ans)
2022-05-08 15:29:45 +00:00
except Exception as ex:
2022-05-17 11:39:08 +00:00
continue
else:
done_validation = True
2022-05-08 15:29:45 +00:00
2022-05-17 11:39:08 +00:00
return ans
2022-05-08 15:29:45 +00:00
def confirm(prompt):
ans = validating_input(
prompt,
options=("y", "n"),
format_function=lambda ans: ans.lower(),
)
if ans == "y":
return True
else:
return False