From 1b6c3edbd3e140b73c64552f145ce6afb94b5d51 Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Sun, 1 May 2022 05:17:39 -0700 Subject: [PATCH] update upgrade module journal summaries to use new message handling --- jrnl/messages.py | 11 +++++++ jrnl/upgrade.py | 85 ++++++++++++++++++++++++++++++------------------ 2 files changed, 65 insertions(+), 31 deletions(-) diff --git a/jrnl/messages.py b/jrnl/messages.py index 3c365b18..275ccdc6 100644 --- a/jrnl/messages.py +++ b/jrnl/messages.py @@ -157,6 +157,17 @@ class MsgText(Enum): ImportAborted = "Entries were NOT imported" + JournalsToUpgrade = """ + The following {journal_type} journals will be upgraded to jrnl {version}: + + """ + + JournalsToIgnore = """ + The following journals will not be touched: + + """ + PaddedJournalName = "{journal_name:{pad}} -> {path}" + # -- Config --- # AltConfigNotFound = """ Alternate configuration file not found at the given path: diff --git a/jrnl/upgrade.py b/jrnl/upgrade.py index f7c7274d..9cb0102c 100644 --- a/jrnl/upgrade.py +++ b/jrnl/upgrade.py @@ -13,6 +13,7 @@ from .config import scope_config from .prompt import yesno from jrnl.output import print_msg +from jrnl.output import print_msgs from jrnl.exception import JrnlException from jrnl.messages import Message from jrnl.messages import MsgText @@ -36,9 +37,7 @@ def backup(filename, binary=False): ) except FileNotFoundError: - print_msg( - Message(MsgText.DoesNotExist, MsgType.WARNING, {"name": filename}) - ) + print_msg(Message(MsgText.DoesNotExist, MsgType.WARNING, {"name": filename})) cont = yesno(f"\nCreate {filename}?", default=False) if not cont: raise JrnlException(Message(MsgText.UpgradeAborted, MsgType.WARNING)) @@ -83,36 +82,41 @@ def upgrade_jrnl(config_path): else: plain_journals[journal_name] = path - longest_journal_name = max([len(journal) for journal in config["journals"]]) - if encrypted_journals: - print( - f"\nFollowing encrypted journals will be upgraded to jrnl {__version__}:", - file=sys.stderr, - ) - for journal, path in encrypted_journals.items(): - print( - " {:{pad}} -> {}".format(journal, path, pad=longest_journal_name), - file=sys.stderr, - ) + kwargs = { + # longest journal name + "pad": max([len(journal) for journal in config["journals"]]), + "version": __version__, + } - if plain_journals: - print( - f"\nFollowing plain text journals will upgraded to jrnl {__version__}:", - file=sys.stderr, - ) - for journal, path in plain_journals.items(): - print( - " {:{pad}} -> {}".format(journal, path, pad=longest_journal_name), - file=sys.stderr, - ) + _print_journal_summary( + journals=encrypted_journals, + header=Message( + MsgText.JournalsToUpgrade, + params={ + "journal_type": "encrypted", + "version": __version__, + }, + ), + **kwargs, + ) - if other_journals: - print("\nFollowing journals will be not be touched:", file=sys.stderr) - for journal, path in other_journals.items(): - print( - " {:{pad}} -> {}".format(journal, path, pad=longest_journal_name), - file=sys.stderr, - ) + _print_journal_summary( + journals=plain_journals, + header=Message( + MsgText.JournalsToUpgrade, + params={ + "journal_type": "plain text", + "version": __version__, + }, + ), + **kwargs, + ) + + _print_journal_summary( + journals=other_journals, + header=Message(MsgText.JournalsToIgnore), + **kwargs, + ) cont = yesno("\nContinue upgrading jrnl?", default=False) if not cont: @@ -169,3 +173,22 @@ def upgrade_jrnl(config_path): def is_old_version(config_path): return is_config_json(config_path) + + +def _print_journal_summary(journals: dict, header: Message, pad: int) -> None: + if not journals: + return + + msgs = [header] + for journal, path in journals.items(): + msgs.append( + Message( + MsgText.PaddedJournalName, + params={ + "journal_name": journal, + "path": path, + "pad": pad, + }, + ) + ) + print_msgs(msgs)