(#770) run black formatter on codebase for standardization

This commit is contained in:
Jonathan Wren 2019-12-21 11:47:02 -08:00
parent 9664924096
commit 46c4c88231
24 changed files with 850 additions and 427 deletions

View file

@ -11,9 +11,9 @@ import os
def backup(filename, binary=False):
print(f" Created a backup at {filename}.backup", file=sys.stderr)
filename = os.path.expanduser(os.path.expandvars(filename))
with open(filename, 'rb' if binary else 'r') as original:
with open(filename, "rb" if binary else "r") as original:
contents = original.read()
with open(filename + ".backup", 'wb' if binary else 'w') as backup:
with open(filename + ".backup", "wb" if binary else "w") as backup:
backup.write(contents)
@ -25,7 +25,8 @@ def upgrade_jrnl_if_necessary(config_path):
config = util.load_config(config_path)
print("""Welcome to jrnl {}.
print(
"""Welcome to jrnl {}.
It looks like you've been using an older version of jrnl until now. That's
okay - jrnl will now upgrade your configuration and journal files. Afterwards
@ -39,18 +40,21 @@ you can enjoy all of the great new features that come with jrnl 2:
Please note that jrnl 1.x is NOT forward compatible with this version of jrnl.
If you choose to proceed, you will not be able to use your journals with
older versions of jrnl anymore.
""".format(__version__))
""".format(
__version__
)
)
encrypted_journals = {}
plain_journals = {}
other_journals = {}
all_journals = []
for journal_name, journal_conf in config['journals'].items():
for journal_name, journal_conf in config["journals"].items():
if isinstance(journal_conf, dict):
path = journal_conf.get("journal")
encrypt = journal_conf.get("encrypt")
else:
encrypt = config.get('encrypt')
encrypt = config.get("encrypt")
path = journal_conf
path = os.path.expanduser(path)
@ -62,21 +66,36 @@ older versions of jrnl anymore.
else:
plain_journals[journal_name] = path
longest_journal_name = max([len(journal) for journal in config['journals']])
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)
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)
print(
" {:{pad}} -> {}".format(journal, path, pad=longest_journal_name),
file=sys.stderr,
)
if plain_journals:
print(f"\nFollowing plain text journals will upgraded to jrnl {__version__}:", file=sys.stderr)
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(
" {:{pad}} -> {}".format(journal, path, pad=longest_journal_name),
file=sys.stderr,
)
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(
" {:{pad}} -> {}".format(journal, path, pad=longest_journal_name),
file=sys.stderr,
)
try:
cont = util.yesno("\nContinue upgrading jrnl?", default=False)
@ -86,24 +105,37 @@ older versions of jrnl anymore.
raise UserAbort("jrnl NOT upgraded, exiting.")
for journal_name, path in encrypted_journals.items():
print(f"\nUpgrading encrypted '{journal_name}' journal stored in {path}...", file=sys.stderr)
print(
f"\nUpgrading encrypted '{journal_name}' journal stored in {path}...",
file=sys.stderr,
)
backup(path, binary=True)
old_journal = Journal.open_journal(journal_name, util.scope_config(config, journal_name), legacy=True)
old_journal = Journal.open_journal(
journal_name, util.scope_config(config, journal_name), legacy=True
)
all_journals.append(EncryptedJournal.from_journal(old_journal))
for journal_name, path in plain_journals.items():
print(f"\nUpgrading plain text '{journal_name}' journal stored in {path}...", file=sys.stderr)
print(
f"\nUpgrading plain text '{journal_name}' journal stored in {path}...",
file=sys.stderr,
)
backup(path)
old_journal = Journal.open_journal(journal_name, util.scope_config(config, journal_name), legacy=True)
old_journal = Journal.open_journal(
journal_name, util.scope_config(config, journal_name), legacy=True
)
all_journals.append(Journal.PlainJournal.from_journal(old_journal))
# loop through lists to validate
failed_journals = [j for j in all_journals if not j.validate_parsing()]
if len(failed_journals) > 0:
print("\nThe following journal{} failed to upgrade:\n{}".format(
's' if len(failed_journals) > 1 else '', "\n".join(j.name for j in failed_journals)),
file=sys.stderr
print(
"\nThe following journal{} failed to upgrade:\n{}".format(
"s" if len(failed_journals) > 1 else "",
"\n".join(j.name for j in failed_journals),
),
file=sys.stderr,
)
raise UpgradeValidationException
@ -120,4 +152,5 @@ older versions of jrnl anymore.
class UpgradeValidationException(Exception):
"""Raised when the contents of an upgraded journal do not match the old journal"""
pass