From 082e665a5038e0bfb7fafb89b0c058c1045b3aa0 Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Mon, 16 May 2022 12:04:08 -0700 Subject: [PATCH] update create_password and yesno prompt functions for new messaging --- jrnl/messages.py | 14 ++++++-------- jrnl/prompt.py | 14 ++++++-------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/jrnl/messages.py b/jrnl/messages.py index f0ae6ddf..92dded07 100644 --- a/jrnl/messages.py +++ b/jrnl/messages.py @@ -248,15 +248,13 @@ class MsgText(Enum): """ # --- Password --- # - PasswordMaxTriesExceeded = """ - Too many attempts with wrong password - """ - - PasswordCanNotBeEmpty = """ - Password can't be empty! - """ - + PasswordFirstEntry = "Enter new password: " + PasswordConfirmEntry = "Enter password again: " + PasswordMaxTriesExceeded = "Too many attempts with wrong password" + PasswordCanNotBeEmpty = "Password can't be empty!" + PasswordDidNotMatch = "Passwords did not match, please try again" WrongPasswordTryAgain = "Wrong password, try again" + PasswordStoreInKeychain = "Do you want to store the password in your keychain?" # --- Search --- # NothingToDelete = """ diff --git a/jrnl/prompt.py b/jrnl/prompt.py index 1123026e..caf5d56a 100644 --- a/jrnl/prompt.py +++ b/jrnl/prompt.py @@ -10,27 +10,25 @@ from jrnl.messages import MsgStyle from jrnl.output import print_msg -def create_password( - journal_name: str, prompt: str = "Enter password for new journal: " -) -> str: +def create_password(journal_name: str) -> str: while True: - pw = getpass.getpass(prompt) + pw = getpass.getpass(str(MsgText.PasswordFirstEntry)) if not pw: print_msg(Message(MsgText.PasswordCanNotBeEmpty, MsgStyle.PLAIN)) continue - elif pw == getpass.getpass("Enter password again: "): + elif pw == getpass.getpass(str(MsgText.PasswordConfirmEntry)): break - print("Passwords did not match, please try again", file=sys.stderr) + print_msg(Message(MsgText.PasswordDidNotMatch, MsgStyle.ERROR)) - if yesno("Do you want to store the password in your keychain?", default=True): + if yesno(str(MsgText.PasswordStoreInKeychain), default=True): from .EncryptedJournal import set_keychain set_keychain(journal_name, pw) return pw -def yesno(prompt, default=True): +def yesno(prompt: str, default: bool = True): prompt = f"{prompt.strip()} {'[Y/n]' if default else '[y/N]'} " response = input(prompt) return {"y": True, "n": False}.get(response.lower().strip(), default)