From dbeb2c212841b4d482921a1d26b856e1a4ca3463 Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Sat, 24 Sep 2022 15:31:09 -0700 Subject: [PATCH] move prompt into prompt.py --- jrnl/encryption/BasePasswordEncryption.py | 21 +++------------------ jrnl/prompt.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/jrnl/encryption/BasePasswordEncryption.py b/jrnl/encryption/BasePasswordEncryption.py index dc45c3a6..bca59093 100644 --- a/jrnl/encryption/BasePasswordEncryption.py +++ b/jrnl/encryption/BasePasswordEncryption.py @@ -1,13 +1,9 @@ # Copyright © 2012-2022 jrnl contributors # License: https://www.gnu.org/licenses/gpl-3.0.html from jrnl.encryption.BaseEncryption import BaseEncryption -from jrnl.exception import JrnlException from jrnl.keyring import get_keyring_password -from jrnl.messages import Message -from jrnl.messages import MsgStyle -from jrnl.messages import MsgText -from jrnl.output import print_msg from jrnl.prompt import create_password +from jrnl.prompt import prompt_password class BasePasswordEncryption(BaseEncryption): @@ -51,17 +47,6 @@ class BasePasswordEncryption(BaseEncryption): return result def _prompt_password(self): - if self._attempts >= self._max_attempts: - raise JrnlException( - Message(MsgText.PasswordMaxTriesExceeded, MsgStyle.ERROR) - ) - - if self._attempts > 0: - print_msg(Message(MsgText.WrongPasswordTryAgain, MsgStyle.WARNING)) - - self._attempts += 1 - self.password = print_msg( - Message(MsgText.Password, MsgStyle.PROMPT), - get_input=True, - hide_input=True, + self._attempts, self.password = prompt_password( + self._attempts, self._max_attempts ) diff --git a/jrnl/prompt.py b/jrnl/prompt.py index 320c5cab..8150ddb3 100644 --- a/jrnl/prompt.py +++ b/jrnl/prompt.py @@ -1,6 +1,7 @@ # Copyright © 2012-2022 jrnl contributors # License: https://www.gnu.org/licenses/gpl-3.0.html +from jrnl.exception import JrnlException from jrnl.messages import Message from jrnl.messages import MsgStyle from jrnl.messages import MsgText @@ -42,6 +43,21 @@ def create_password(journal_name: str) -> str: return pw +def prompt_password(attempts: int, max_attempts: int) -> tuple[int, str | None]: + if attempts >= max_attempts: + raise JrnlException(Message(MsgText.PasswordMaxTriesExceeded, MsgStyle.ERROR)) + + if attempts > 0: + print_msg(Message(MsgText.WrongPasswordTryAgain, MsgStyle.WARNING)) + + attempts += 1 + return attempts, print_msg( + Message(MsgText.Password, MsgStyle.PROMPT), + get_input=True, + hide_input=True, + ) + + def yesno(prompt: Message, default: bool = True) -> bool: response = print_msgs( [