make sane_value validation a little more general

master
magical 2022-08-06 06:42:20 +00:00
parent 5e7168c848
commit 8ae384b1cd
1 changed files with 29 additions and 28 deletions

View File

@ -1265,7 +1265,7 @@ class App(object):
self.loop.stop()
call("clear", shell=True)
try:
name = nameloop(self.network, "Choose a new username", True)
name = nameloop(self.network, "Choose a new username")
self.network.user_update(user_name=name)
motherfucking_rainbows("~~hello there %s~~" % name)
sleep(0.8)
@ -1281,7 +1281,7 @@ class App(object):
self.loop.stop()
call("clear", shell=True)
try:
password = password_loop("Choose a new password. Can be empty", True)
password = password_loop("Choose a new password. Can be empty")
self.network.user_update(auth_hash=self.network._hash(password))
motherfucking_rainbows("SET NEW PASSWORD")
sleep(0.8)
@ -2368,19 +2368,18 @@ def paren_prompt(text, positive=True, choices=[], function=input, default=None):
return ""
def sane_value(network, key, prompt, positive=True, allow_empty=False):
def sane_value(prompt, validate, positive=True, function=input):
"""Prompts for an input with paren_prompt until validate(response)
returns None (or something falsy). Otherwise the returned string is used
as the new prompt.
"""
while 1:
response = paren_prompt(prompt, positive)
if allow_empty and response == "":
response = paren_prompt(prompt, positive, function=function)
error = validate(response)
if not error:
return response
try:
network.validate(key, response)
except AssertionError as e:
prompt = e.description
prompt = str(error) if error != None else ""
positive = False
else:
return response
def password_loop(prompt, positive=True):
while 1:
@ -2397,15 +2396,14 @@ def password_loop(prompt, positive=True):
return response1
def nameloop(network, prompt, positive):
while 1:
name = sane_value(network, "user_name", prompt, positive)
def nameloop(network, prompt, positive=True):
def validate_name(name):
try: network.validate("user_name", name)
except AssertionError as e:
return e.description
if network.user_is_registered(name):
prompt = "%s is already registered" % name
positive = False
else:
return name
return "%s is already registered" % name
return sane_value(prompt, validate_name, positive)
def log_in(network, name="", password=""):
"""
@ -2415,7 +2413,12 @@ def log_in(network, name="", password=""):
curses app.
"""
if not name:
name = sane_value(network, "user_name", "Username", allow_empty=True)
def validate_name(response):
if response != "": # allow empty username
try: network.validate("user_name", response)
except AssertionError as e:
return e.description
name = sane_value("Username", validate_name)
if name == "":
motherfucking_rainbows("~~W3 4R3 4n0nYm0u5~~")
else:
@ -2430,14 +2433,12 @@ def log_in(network, name="", password=""):
motherfucking_rainbows("~~welcome back {}~~".format(network.user_name))
except ConnectionRefusedError:
def login_loop(prompt, positive):
def validate_creds(password):
try:
password = paren_prompt(prompt, positive, function=getpass)
network.set_credentials(name, password)
except ConnectionRefusedError:
login_loop("// R E J E C T E D //.", False)
login_loop("Enter your password", True)
return "// R E J E C T E D //."
password = sane_value("Enter your password", validate_creds, function=getpass)
motherfucking_rainbows("~~welcome back {}~~".format(network.user_name))
except ValueError:
@ -2448,7 +2449,7 @@ def log_in(network, name="", password=""):
)
if response == "c":
name = nameloop(network, "Pick a new name", True)
name = nameloop(network, "Pick a new name")
elif response == "n":
raise InterruptedError