update more modules to use new message handling

This commit is contained in:
Jonathan Wren 2022-05-01 09:22:58 -07:00
parent 48d36ecad9
commit 9eb3e1684b
3 changed files with 47 additions and 18 deletions

View file

@ -54,7 +54,7 @@ def decrypt_content(
set_keychain(keychain, None) set_keychain(keychain, None)
attempt = 1 attempt = 1
while result is None and attempt < max_attempts: 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() password = getpass.getpass()
result = decrypt_func(password) result = decrypt_func(password)
attempt += 1 attempt += 1
@ -79,13 +79,22 @@ class EncryptedJournal(Journal):
if not os.path.exists(filename): if not os.path.exists(filename):
if not os.path.isdir(dirname): if not os.path.isdir(dirname):
os.makedirs(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.create_file(filename)
self.password = create_password(self.name) self.password = create_password(self.name)
print( print_msg(
f"Encrypted journal '{self.name}' created at {filename}", Message(
file=sys.stderr, MsgText.JournalCreated,
MsgType.NORMAL,
{"journal_name": self.name, "path": filename},
)
) )
text = self._load(filename) text = self._load(filename)
@ -179,7 +188,7 @@ def get_keychain(journal_name):
return keyring.get_password("jrnl", journal_name) return keyring.get_password("jrnl", journal_name)
except keyring.errors.KeyringError as e: except keyring.errors.KeyringError as e:
if not isinstance(e, keyring.errors.NoKeyringError): if not isinstance(e, keyring.errors.NoKeyringError):
print("Failed to retrieve keyring", file=sys.stderr) print_msg(Message(MsgText.KeyringRetrievalFailure, MsgType.ERROR))
return "" return ""
@ -196,9 +205,7 @@ def set_keychain(journal_name, password):
keyring.set_password("jrnl", journal_name, password) keyring.set_password("jrnl", journal_name, password)
except keyring.errors.KeyringError as e: except keyring.errors.KeyringError as e:
if isinstance(e, keyring.errors.NoKeyringError): if isinstance(e, keyring.errors.NoKeyringError):
print( msg = Message(MsgText.KeyringBackendNotFound, MsgType.WARNING)
"Keyring backend not found. Please install one of the supported backends by visiting: https://pypi.org/project/keyring/",
file=sys.stderr,
)
else: else:
print("Failed to retrieve keyring", file=sys.stderr) msg = Message(MsgText.KeyringRetrievalFailure, MsgType.ERROR)
print_msg(msg)

View file

@ -13,6 +13,8 @@ avoid any possible overhead for these standalone commands.
""" """
import platform import platform
import sys import sys
from jrnl.output import print_msg
from jrnl.exception import JrnlException from jrnl.exception import JrnlException
from jrnl.messages import Message from jrnl.messages import Message
from jrnl.messages import MsgText 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 = EncryptedJournal.from_journal(journal)
new_journal.write(args.filename) new_journal.write(args.filename)
print( print_msg(
f"Journal encrypted to {args.filename or new_journal.config['journal']}.", Message(
file=sys.stderr, MsgText.JournalEncryptedTo,
MsgType.NORMAL,
{"path": args.filename or new_journal.config["journal"]},
)
) )
# Update the config, if we encrypted in place # 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 = PlainJournal.from_journal(journal)
new_journal.write(args.filename) new_journal.write(args.filename)
print( print_msg(
f"Journal decrypted to {args.filename or new_journal.config['journal']}.", Message(
file=sys.stderr, MsgText.JournalDecryptedTo,
MsgType.NORMAL,
{"path": args.filename or new_journal.config["journal"]},
)
) )
# Update the config, if we decrypted in place # Update the config, if we decrypted in place

View file

@ -111,7 +111,9 @@ class MsgText(Enum):
JournalCreated = "Journal '{journal_name}' created at {filename}" JournalCreated = "Journal '{journal_name}' created at {filename}"
DirectoryCreated = "Directory {directory_name} created" 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}" BackupCreated = "Created a backup at {filename}"
# --- Editor ---# # --- Editor ---#
@ -191,6 +193,8 @@ class MsgText(Enum):
Too many attempts with wrong password Too many attempts with wrong password
""" """
WrongPasswordTryAgain = "Wrong password, try again."
# --- Search --- # # --- Search --- #
NothingToDelete = """ NothingToDelete = """
No entries to delete, because the search returned no results No entries to delete, because the search returned no results
@ -213,6 +217,16 @@ class MsgText(Enum):
# --- Color --- # # --- Color --- #
InvalidColor = "{key} set to invalid color: {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): class Message(NamedTuple):
text: MsgText text: MsgText