1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-11-08 21:21:06 +00:00
AdvLabDB/advlabdb/scripts/terminal_utils.py
2022-07-03 18:26:15 +02:00

103 lines
2.1 KiB
Python

"""
No relative imports allowed in this file to be able to run server_setup.py without packages.
"""
import subprocess # nosec 404
from getpass import getpass
def run(command, **kwargs):
return subprocess.run(command, shell=True, **kwargs) # nosec B602
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
done_validation = False
while not done_validation or not adj_check_constraints_function(ans):
if not first_run:
done_validation = False
print("Invalid input!\n")
else:
first_run = False
ans = input(prompt)
try:
ans = format_function(ans)
except Exception: # nosec B112
continue
else:
done_validation = True
return ans
def confirm(prompt):
ans = validating_input(
prompt,
options=("y", "n"),
format_function=lambda ans: ans.lower(),
)
if ans == "y":
return True
else:
return False