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,5 +1,4 @@
#!/usr/bin/env python
# encoding: utf-8
"""
jrnl
@ -7,8 +6,6 @@
license: MIT, see LICENSE for more details.
"""
from __future__ import unicode_literals
from __future__ import absolute_import
from . import Journal
from . import util
from . import install
@ -91,7 +88,7 @@ def encrypt(journal, filename=None):
if util.yesno("Do you want to store the password in your keychain?", default=True):
util.set_keychain(journal.name, journal.config['password'])
util.prompt("Journal encrypted to {0}.".format(filename or new_journal.config['journal']))
print("Journal encrypted to {}.".format(filename or new_journal.config['journal']), file=sys.stderr)
def decrypt(journal, filename=None):
@ -102,12 +99,12 @@ def decrypt(journal, filename=None):
new_journal = Journal.PlainJournal(filename, **journal.config)
new_journal.entries = journal.entries
new_journal.write(filename)
util.prompt("Journal decrypted to {0}.".format(filename or new_journal.config['journal']))
print("Journal decrypted to {}.".format(filename or new_journal.config['journal']), file=sys.stderr)
def list_journals(config):
"""List the journals specified in the configuration file"""
result = "Journals defined in {}\n".format(install.CONFIG_FILE_PATH)
result = f"Journals defined in {install.CONFIG_FILE_PATH}\n"
ml = min(max(len(k) for k in config['journals']), 20)
for journal, cfg in config['journals'].items():
result += " * {:{}} -> {}\n".format(journal, ml, cfg['journal'] if isinstance(cfg, dict) else cfg)
@ -138,20 +135,19 @@ def configure_logger(debug=False):
def run(manual_args=None):
args = parse_args(manual_args)
configure_logger(args.debug)
args.text = [p.decode('utf-8') if util.PY2 and not isinstance(p, unicode) else p for p in args.text]
if args.version:
version_str = "{0} version {1}".format(jrnl.__title__, jrnl.__version__)
print(util.py2encode(version_str))
version_str = f"{jrnl.__title__} version {jrnl.__version__}"
print(version_str)
sys.exit(0)
try:
config = install.load_or_install_jrnl()
except UserAbort as err:
util.prompt("\n{}".format(err))
print(f"\n{err}", file=sys.stderr)
sys.exit(1)
if args.ls:
util.prnt(list_journals(config))
print(list_journals(config))
sys.exit(0)
log.debug('Using configuration "%s"', config)
@ -161,11 +157,11 @@ def run(manual_args=None):
# use this!
journal_name = args.text[0] if (args.text and args.text[0] in config['journals']) else 'default'
if journal_name is not 'default':
if journal_name != 'default':
args.text = args.text[1:]
elif "default" not in config['journals']:
util.prompt("No default journal configured.")
util.prompt(list_journals(config))
print("No default journal configured.", file=sys.stderr)
print(list_journals(config), file=sys.stderr)
sys.exit(1)
config = util.scope_config(config, journal_name)
@ -175,12 +171,12 @@ def run(manual_args=None):
try:
args.limit = int(args.text[0].lstrip("-"))
args.text = args.text[1:]
except:
except ValueError:
pass
log.debug('Using journal "%s"', journal_name)
mode_compose, mode_export, mode_import = guess_mode(args, config)
# How to quit writing?
if "win32" in sys.platform:
_exit_multiline_code = "on a blank line, press Ctrl+Z and then Enter"
@ -190,21 +186,22 @@ def run(manual_args=None):
if mode_compose and not args.text:
if not sys.stdin.isatty():
# Piping data into jrnl
raw = util.py23_read()
raw = sys.stdin.read()
elif config['editor']:
template = ""
if config['template']:
try:
template = open(config['template']).read()
except:
util.prompt("[Could not read template at '']".format(config['template']))
except OSError:
print(f"[Could not read template at '{config['template']}']", file=sys.stderr)
sys.exit(1)
raw = util.get_text_from_editor(config, template)
else:
try:
raw = util.py23_read("[Compose Entry; " + _exit_multiline_code + " to finish writing]\n")
print("[Compose Entry; " + _exit_multiline_code + " to finish writing]\n", file=sys.stderr)
raw = sys.stdin.read()
except KeyboardInterrupt:
util.prompt("[Entry NOT saved to journal.]")
print("[Entry NOT saved to journal.]", file=sys.stderr)
sys.exit(0)
if raw:
args.text = [raw]
@ -215,7 +212,7 @@ def run(manual_args=None):
try:
journal = Journal.open_journal(journal_name, config)
except KeyboardInterrupt:
util.prompt("[Interrupted while opening journal]".format(journal_name))
print(f"[Interrupted while opening journal]", file=sys.stderr)
sys.exit(1)
# Import mode
@ -225,11 +222,9 @@ def run(manual_args=None):
# Writing mode
elif mode_compose:
raw = " ".join(args.text).strip()
if util.PY2 and type(raw) is not unicode:
raw = raw.decode(sys.getfilesystemencoding())
log.debug('Appending raw line "%s" to journal "%s"', raw, journal_name)
journal.new_entry(raw)
util.prompt("[Entry added to {0} journal]".format(journal_name))
print(f"[Entry added to {journal_name} journal]", file=sys.stderr)
journal.write()
if not mode_compose:
@ -246,14 +241,14 @@ def run(manual_args=None):
# Reading mode
if not mode_compose and not mode_export and not mode_import:
print(util.py2encode(journal.pprint()))
print(journal.pprint())
# Various export modes
elif args.short:
print(util.py2encode(journal.pprint(short=True)))
print(journal.pprint(short=True))
elif args.tags:
print(util.py2encode(plugins.get_exporter("tags").export(journal)))
print(plugins.get_exporter("tags").export(journal))
elif args.export is not False:
exporter = plugins.get_exporter(args.export)
@ -275,7 +270,8 @@ def run(manual_args=None):
elif args.edit:
if not config['editor']:
util.prompt("[{1}ERROR{2}: You need to specify an editor in {0} to use the --edit function.]".format(install.CONFIG_FILE_PATH, ERROR_COLOR, RESET_COLOR))
print("[{1}ERROR{2}: You need to specify an editor in {0} to use the --edit function.]"
.format(install.CONFIG_FILE_PATH, ERROR_COLOR, RESET_COLOR), file=sys.stderr)
sys.exit(1)
other_entries = [e for e in old_entries if e not in journal.entries]
# Edit
@ -286,11 +282,11 @@ def run(manual_args=None):
num_edited = len([e for e in journal.entries if e.modified])
prompts = []
if num_deleted:
prompts.append("{0} {1} deleted".format(num_deleted, "entry" if num_deleted == 1 else "entries"))
prompts.append("{} {} deleted".format(num_deleted, "entry" if num_deleted == 1 else "entries"))
if num_edited:
prompts.append("{0} {1} modified".format(num_edited, "entry" if num_deleted == 1 else "entries"))
prompts.append("{} {} modified".format(num_edited, "entry" if num_deleted == 1 else "entries"))
if prompts:
util.prompt("[{0}]".format(", ".join(prompts).capitalize()))
print("[{}]".format(", ".join(prompts).capitalize()), file=sys.stderr)
journal.entries += other_entries
journal.sort()
journal.write()