From 8ae384b1cda519c1ff105c49898e0b200bc3af3a Mon Sep 17 00:00:00 2001 From: magical Date: Sat, 6 Aug 2022 06:42:20 +0000 Subject: [PATCH] make sane_value validation a little more general --- clients/urwid/main.py | 57 ++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/clients/urwid/main.py b/clients/urwid/main.py index e8a1e9d..f81fbc7 100644 --- a/clients/urwid/main.py +++ b/clients/urwid/main.py @@ -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 - positive = False - else: - return response - + prompt = str(error) if error != None else "" + positive = False 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