rewrite some tail call loops as normal loops

master
magical 2022-08-06 06:20:16 +00:00
parent 2e54939345
commit 5e7168c848
1 changed files with 32 additions and 22 deletions

View File

@ -2368,33 +2368,43 @@ def paren_prompt(text, positive=True, choices=[], function=input, default=None):
return "" return ""
def sane_value(network, key, prompt, positive=True, return_empty=False): def sane_value(network, key, prompt, positive=True, allow_empty=False):
response = paren_prompt(prompt, positive) while 1:
if return_empty and response == "": response = paren_prompt(prompt, positive)
return response if allow_empty and response == "":
try: network.validate(key, response) return response
except AssertionError as e: try:
return sane_value(network, key, e.description, False) network.validate(key, response)
return response except AssertionError as e:
prompt = e.description
positive = False
else:
return response
def password_loop(prompt, positive=True): def password_loop(prompt, positive=True):
response1 = paren_prompt(prompt, positive, function=getpass) while 1:
if response1 == "": response1 = paren_prompt(prompt, positive, function=getpass)
confprompt = "Confirm empty password" if response1 == "":
else: confprompt = "Confirm empty password"
confprompt = "Confirm it" else:
response2 = paren_prompt(confprompt, function=getpass) confprompt = "Confirm it"
if response1 != response2: response2 = paren_prompt(confprompt, function=getpass)
return password_loop("Those didnt match. Try again", False) if response1 != response2:
return response1 prompt = "Those didnt match. Try again"
positive = False
else:
return response1
def nameloop(network, prompt, positive): def nameloop(network, prompt, positive):
name = sane_value(network, "user_name", prompt, positive) while 1:
if network.user_is_registered(name): name = sane_value(network, "user_name", prompt, positive)
return nameloop(network, "%s is already registered" % name, False) if network.user_is_registered(name):
return name prompt = "%s is already registered" % name
positive = False
else:
return name
def log_in(network, name="", password=""): def log_in(network, name="", password=""):
@ -2405,7 +2415,7 @@ def log_in(network, name="", password=""):
curses app. curses app.
""" """
if not name: if not name:
name = sane_value(network, "user_name", "Username", return_empty=True) name = sane_value(network, "user_name", "Username", allow_empty=True)
if name == "": if name == "":
motherfucking_rainbows("~~W3 4R3 4n0nYm0u5~~") motherfucking_rainbows("~~W3 4R3 4n0nYm0u5~~")
else: else: