update create_password and yesno prompt functions for new messaging

This commit is contained in:
Jonathan Wren 2022-05-16 12:04:08 -07:00
parent e5fdf72fb6
commit 082e665a50
2 changed files with 12 additions and 16 deletions

View file

@ -248,15 +248,13 @@ class MsgText(Enum):
""" """
# --- Password --- # # --- Password --- #
PasswordMaxTriesExceeded = """ PasswordFirstEntry = "Enter new password: "
Too many attempts with wrong password PasswordConfirmEntry = "Enter password again: "
""" PasswordMaxTriesExceeded = "Too many attempts with wrong password"
PasswordCanNotBeEmpty = "Password can't be empty!"
PasswordCanNotBeEmpty = """ PasswordDidNotMatch = "Passwords did not match, please try again"
Password can't be empty!
"""
WrongPasswordTryAgain = "Wrong password, try again" WrongPasswordTryAgain = "Wrong password, try again"
PasswordStoreInKeychain = "Do you want to store the password in your keychain?"
# --- Search --- # # --- Search --- #
NothingToDelete = """ NothingToDelete = """

View file

@ -10,27 +10,25 @@ from jrnl.messages import MsgStyle
from jrnl.output import print_msg from jrnl.output import print_msg
def create_password( def create_password(journal_name: str) -> str:
journal_name: str, prompt: str = "Enter password for new journal: "
) -> str:
while True: while True:
pw = getpass.getpass(prompt) pw = getpass.getpass(str(MsgText.PasswordFirstEntry))
if not pw: if not pw:
print_msg(Message(MsgText.PasswordCanNotBeEmpty, MsgStyle.PLAIN)) print_msg(Message(MsgText.PasswordCanNotBeEmpty, MsgStyle.PLAIN))
continue continue
elif pw == getpass.getpass("Enter password again: "): elif pw == getpass.getpass(str(MsgText.PasswordConfirmEntry)):
break 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 from .EncryptedJournal import set_keychain
set_keychain(journal_name, pw) set_keychain(journal_name, pw)
return 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]'} " prompt = f"{prompt.strip()} {'[Y/n]' if default else '[y/N]'} "
response = input(prompt) response = input(prompt)
return {"y": True, "n": False}.get(response.lower().strip(), default) return {"y": True, "n": False}.get(response.lower().strip(), default)