remove py2 remnants and use mocks in tests

fstring wip
Run pyupgrade
fix broken pyupgrade fstring
run pyupgrade on plugin dir
fixup! remove py2 remnants and use mocks in tests
small print bugfix
The file=sys.stderr was part of the format(), so an error got printed to
stdout
Drop use of codecs package
Use builtins.open() instead
fixup! remove py2 remnants and use mocks in tests
This commit is contained in:
Peter Schmidbauer 2019-10-31 21:12:55 +01:00
parent b7e2e91af3
commit 9d8d6a83ae
28 changed files with 211 additions and 321 deletions

View file

@ -1,4 +1,4 @@
from __future__ import absolute_import, unicode_literals
import sys
from . import __version__
from . import Journal
@ -6,11 +6,10 @@ from . import util
from .EncryptedJournal import EncryptedJournal
from .util import UserAbort
import os
import codecs
def backup(filename, binary=False):
util.prompt(" Created a backup at {}.backup".format(filename))
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:
contents = original.read()
@ -19,14 +18,14 @@ def backup(filename, binary=False):
def upgrade_jrnl_if_necessary(config_path):
with codecs.open(config_path, "r", "utf-8") as f:
with open(config_path, "r", encoding="utf-8") as f:
config_file = f.read()
if not config_file.strip().startswith("{"):
return
config = util.load_config(config_path)
util.prompt("""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
@ -65,19 +64,19 @@ older versions of jrnl anymore.
longest_journal_name = max([len(journal) for journal in config['journals']])
if encrypted_journals:
util.prompt("\nFollowing encrypted journals will be upgraded to jrnl {}:".format(__version__))
print(f"\nFollowing encrypted journals will be upgraded to jrnl {__version__}:", file=sys.stderr)
for journal, path in encrypted_journals.items():
util.prompt(" {:{pad}} -> {}".format(journal, path, pad=longest_journal_name))
print(" {:{pad}} -> {}".format(journal, path, pad=longest_journal_name), file=sys.stderr)
if plain_journals:
util.prompt("\nFollowing plain text journals will upgraded to jrnl {}:".format(__version__))
print(f"\nFollowing plain text journals will upgraded to jrnl {__version__}:", file=sys.stderr)
for journal, path in plain_journals.items():
util.prompt(" {:{pad}} -> {}".format(journal, path, pad=longest_journal_name))
print(" {:{pad}} -> {}".format(journal, path, pad=longest_journal_name), file=sys.stderr)
if other_journals:
util.prompt("\nFollowing journals will be not be touched:")
print("\nFollowing journals will be not be touched:", file=sys.stderr)
for journal, path in other_journals.items():
util.prompt(" {:{pad}} -> {}".format(journal, path, pad=longest_journal_name))
print(" {:{pad}} -> {}".format(journal, path, pad=longest_journal_name), file=sys.stderr)
try:
cont = util.yesno("\nContinue upgrading jrnl?", default=False)
@ -87,13 +86,13 @@ older versions of jrnl anymore.
raise UserAbort("jrnl NOT upgraded, exiting.")
for journal_name, path in encrypted_journals.items():
util.prompt("\nUpgrading encrypted '{}' journal stored in {}...".format(journal_name, path))
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)
all_journals.append(EncryptedJournal.from_journal(old_journal))
for journal_name, path in plain_journals.items():
util.prompt("\nUpgrading plain text '{}' journal stored in {}...".format(journal_name, path))
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)
all_journals.append(Journal.PlainJournal.from_journal(old_journal))
@ -102,8 +101,9 @@ older versions of jrnl anymore.
failed_journals = [j for j in all_journals if not j.validate_parsing()]
if len(failed_journals) > 0:
util.prompt("\nThe following journal{} failed to upgrade:\n{}".format(
's' if len(failed_journals) > 1 else '', "\n".join(j.name for j in failed_journals))
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
@ -112,10 +112,11 @@ older versions of jrnl anymore.
for j in all_journals:
j.write()
util.prompt("\nUpgrading config...")
print("\nUpgrading config...", file=sys.stderr)
backup(config_path)
util.prompt("\nWe're all done here and you can start enjoying jrnl 2.".format(config_path))
print("\nWe're all done here and you can start enjoying jrnl 2.", file=sys.stderr)
class UpgradeValidationException(Exception):
"""Raised when the contents of an upgraded journal do not match the old journal"""