diff --git a/jrnl/EncryptedJournal.py b/jrnl/EncryptedJournal.py index 704a091a..a1f65aac 100644 --- a/jrnl/EncryptedJournal.py +++ b/jrnl/EncryptedJournal.py @@ -54,7 +54,7 @@ def decrypt_content( set_keychain(keychain, None) attempt = 1 while result is None and attempt < max_attempts: - print("Wrong password, try again.", file=sys.stderr) + print_msg(Message(MsgText.WrongPasswordTryAgain, MsgType.WARNING)) password = getpass.getpass() result = decrypt_func(password) attempt += 1 @@ -79,13 +79,22 @@ class EncryptedJournal(Journal): if not os.path.exists(filename): if not os.path.isdir(dirname): os.makedirs(dirname) - print(f"[Directory {dirname} created]", file=sys.stderr) + print_msg( + Message( + MsgText.DirectoryCreated, + MsgType.NORMAL, + {"directory_name": dirname}, + ) + ) self.create_file(filename) self.password = create_password(self.name) - print( - f"Encrypted journal '{self.name}' created at {filename}", - file=sys.stderr, + print_msg( + Message( + MsgText.JournalCreated, + MsgType.NORMAL, + {"journal_name": self.name, "path": filename}, + ) ) text = self._load(filename) @@ -179,7 +188,7 @@ def get_keychain(journal_name): return keyring.get_password("jrnl", journal_name) except keyring.errors.KeyringError as e: if not isinstance(e, keyring.errors.NoKeyringError): - print("Failed to retrieve keyring", file=sys.stderr) + print_msg(Message(MsgText.KeyringRetrievalFailure, MsgType.ERROR)) return "" @@ -196,9 +205,7 @@ def set_keychain(journal_name, password): keyring.set_password("jrnl", journal_name, password) except keyring.errors.KeyringError as e: if isinstance(e, keyring.errors.NoKeyringError): - print( - "Keyring backend not found. Please install one of the supported backends by visiting: https://pypi.org/project/keyring/", - file=sys.stderr, - ) + msg = Message(MsgText.KeyringBackendNotFound, MsgType.WARNING) else: - print("Failed to retrieve keyring", file=sys.stderr) + msg = Message(MsgText.KeyringRetrievalFailure, MsgType.ERROR) + print_msg(msg) diff --git a/jrnl/commands.py b/jrnl/commands.py index c795402a..cf7ccbf5 100644 --- a/jrnl/commands.py +++ b/jrnl/commands.py @@ -13,6 +13,8 @@ avoid any possible overhead for these standalone commands. """ import platform import sys + +from jrnl.output import print_msg from jrnl.exception import JrnlException from jrnl.messages import Message from jrnl.messages import MsgText @@ -89,9 +91,12 @@ def postconfig_encrypt(args, config, original_config, **kwargs): new_journal = EncryptedJournal.from_journal(journal) new_journal.write(args.filename) - print( - f"Journal encrypted to {args.filename or new_journal.config['journal']}.", - file=sys.stderr, + print_msg( + Message( + MsgText.JournalEncryptedTo, + MsgType.NORMAL, + {"path": args.filename or new_journal.config["journal"]}, + ) ) # Update the config, if we encrypted in place @@ -114,9 +119,12 @@ def postconfig_decrypt(args, config, original_config, **kwargs): new_journal = PlainJournal.from_journal(journal) new_journal.write(args.filename) - print( - f"Journal decrypted to {args.filename or new_journal.config['journal']}.", - file=sys.stderr, + print_msg( + Message( + MsgText.JournalDecryptedTo, + MsgType.NORMAL, + {"path": args.filename or new_journal.config["journal"]}, + ) ) # Update the config, if we decrypted in place diff --git a/jrnl/messages.py b/jrnl/messages.py index ee4c9e05..ae359d15 100644 --- a/jrnl/messages.py +++ b/jrnl/messages.py @@ -111,7 +111,9 @@ class MsgText(Enum): JournalCreated = "Journal '{journal_name}' created at {filename}" DirectoryCreated = "Directory {directory_name} created" - JournalEncrypted = "Journal will be encrypted." + JournalEncrypted = "Journal will be encrypted" + JournalEncryptedTo = "Journal encrypted to {path}" + JournalDecryptedTo = "Journal decrypted to {path}" BackupCreated = "Created a backup at {filename}" # --- Editor ---# @@ -191,6 +193,8 @@ class MsgText(Enum): Too many attempts with wrong password """ + WrongPasswordTryAgain = "Wrong password, try again." + # --- Search --- # NothingToDelete = """ No entries to delete, because the search returned no results @@ -213,6 +217,16 @@ class MsgText(Enum): # --- Color --- # InvalidColor = "{key} set to invalid color: {color}" + # --- Keyring --- # + KeyringBackendNotFound = """ + Keyring backend not found. + + Please install one of the supported backends by visiting: + https://pypi.org/project/keyring/ + """ + + KeyringRetrievalFailure = "Failed to retrieve keyring" + class Message(NamedTuple): text: MsgText