make sane_value validation a little more general
parent
5e7168c848
commit
8ae384b1cd
|
@ -1265,7 +1265,7 @@ class App(object):
|
||||||
self.loop.stop()
|
self.loop.stop()
|
||||||
call("clear", shell=True)
|
call("clear", shell=True)
|
||||||
try:
|
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)
|
self.network.user_update(user_name=name)
|
||||||
motherfucking_rainbows("~~hello there %s~~" % name)
|
motherfucking_rainbows("~~hello there %s~~" % name)
|
||||||
sleep(0.8)
|
sleep(0.8)
|
||||||
|
@ -1281,7 +1281,7 @@ class App(object):
|
||||||
self.loop.stop()
|
self.loop.stop()
|
||||||
call("clear", shell=True)
|
call("clear", shell=True)
|
||||||
try:
|
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))
|
self.network.user_update(auth_hash=self.network._hash(password))
|
||||||
motherfucking_rainbows("SET NEW PASSWORD")
|
motherfucking_rainbows("SET NEW PASSWORD")
|
||||||
sleep(0.8)
|
sleep(0.8)
|
||||||
|
@ -2368,19 +2368,18 @@ def paren_prompt(text, positive=True, choices=[], function=input, default=None):
|
||||||
return ""
|
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:
|
while 1:
|
||||||
response = paren_prompt(prompt, positive)
|
response = paren_prompt(prompt, positive, function=function)
|
||||||
if allow_empty and response == "":
|
error = validate(response)
|
||||||
|
if not error:
|
||||||
return response
|
return response
|
||||||
try:
|
prompt = str(error) if error != None else ""
|
||||||
network.validate(key, response)
|
|
||||||
except AssertionError as e:
|
|
||||||
prompt = e.description
|
|
||||||
positive = False
|
positive = False
|
||||||
else:
|
|
||||||
return response
|
|
||||||
|
|
||||||
|
|
||||||
def password_loop(prompt, positive=True):
|
def password_loop(prompt, positive=True):
|
||||||
while 1:
|
while 1:
|
||||||
|
@ -2397,15 +2396,14 @@ def password_loop(prompt, positive=True):
|
||||||
return response1
|
return response1
|
||||||
|
|
||||||
|
|
||||||
def nameloop(network, prompt, positive):
|
def nameloop(network, prompt, positive=True):
|
||||||
while 1:
|
def validate_name(name):
|
||||||
name = sane_value(network, "user_name", prompt, positive)
|
try: network.validate("user_name", name)
|
||||||
|
except AssertionError as e:
|
||||||
|
return e.description
|
||||||
if network.user_is_registered(name):
|
if network.user_is_registered(name):
|
||||||
prompt = "%s is already registered" % name
|
return "%s is already registered" % name
|
||||||
positive = False
|
return sane_value(prompt, validate_name, positive)
|
||||||
else:
|
|
||||||
return name
|
|
||||||
|
|
||||||
|
|
||||||
def log_in(network, name="", password=""):
|
def log_in(network, name="", password=""):
|
||||||
"""
|
"""
|
||||||
|
@ -2415,7 +2413,12 @@ def log_in(network, name="", password=""):
|
||||||
curses app.
|
curses app.
|
||||||
"""
|
"""
|
||||||
if not name:
|
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 == "":
|
if name == "":
|
||||||
motherfucking_rainbows("~~W3 4R3 4n0nYm0u5~~")
|
motherfucking_rainbows("~~W3 4R3 4n0nYm0u5~~")
|
||||||
else:
|
else:
|
||||||
|
@ -2430,14 +2433,12 @@ def log_in(network, name="", password=""):
|
||||||
motherfucking_rainbows("~~welcome back {}~~".format(network.user_name))
|
motherfucking_rainbows("~~welcome back {}~~".format(network.user_name))
|
||||||
|
|
||||||
except ConnectionRefusedError:
|
except ConnectionRefusedError:
|
||||||
def login_loop(prompt, positive):
|
def validate_creds(password):
|
||||||
try:
|
try:
|
||||||
password = paren_prompt(prompt, positive, function=getpass)
|
|
||||||
network.set_credentials(name, password)
|
network.set_credentials(name, password)
|
||||||
except ConnectionRefusedError:
|
except ConnectionRefusedError:
|
||||||
login_loop("// R E J E C T E D //.", False)
|
return "// R E J E C T E D //."
|
||||||
|
password = sane_value("Enter your password", validate_creds, function=getpass)
|
||||||
login_loop("Enter your password", True)
|
|
||||||
motherfucking_rainbows("~~welcome back {}~~".format(network.user_name))
|
motherfucking_rainbows("~~welcome back {}~~".format(network.user_name))
|
||||||
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
@ -2448,7 +2449,7 @@ def log_in(network, name="", password=""):
|
||||||
)
|
)
|
||||||
|
|
||||||
if response == "c":
|
if response == "c":
|
||||||
name = nameloop(network, "Pick a new name", True)
|
name = nameloop(network, "Pick a new name")
|
||||||
|
|
||||||
elif response == "n":
|
elif response == "n":
|
||||||
raise InterruptedError
|
raise InterruptedError
|
||||||
|
|
Loading…
Reference in New Issue