Move import to be standalone command to reduce clutter in cli.py

This commit is contained in:
Jonathan Wren 2020-07-03 14:29:00 -07:00
parent ce07fedc06
commit a54ed90259
9 changed files with 113 additions and 84 deletions

View file

@ -371,7 +371,7 @@ class LegacyJournal(Journal):
return entries
def open_journal(name, config, legacy=False):
def open_journal(journal_name, config, legacy=False):
"""
Creates a normal, encrypted or DayOne journal based on the passed config.
If legacy is True, it will open Journals with legacy classes build for
@ -394,11 +394,18 @@ def open_journal(name, config, legacy=False):
if not config["encrypt"]:
if legacy:
return LegacyJournal(name, **config).open()
return PlainJournal(name, **config).open()
else:
from . import EncryptedJournal
return LegacyJournal(journal_name, **config).open()
return PlainJournal(journal_name, **config).open()
from . import EncryptedJournal
try:
if legacy:
return EncryptedJournal.LegacyEncryptedJournal(name, **config).open()
return EncryptedJournal.EncryptedJournal(name, **config).open()
return EncryptedJournal.LegacyEncryptedJournal(
journal_name, **config
).open()
return EncryptedJournal.EncryptedJournal(journal_name, **config).open()
except KeyboardInterrupt:
# Since encrypted journals prompt for a password, it's easy for a user to ctrl+c out
print("[Interrupted while opening journal]", file=sys.stderr)
sys.exit(1)

View file

@ -25,10 +25,10 @@ import platform
import sys
from . import install, plugins, util
from .parsing import parse_args_before_config
from .parsing import parse_args_after_config
from .parsing import parse_args
from .Journal import PlainJournal, open_journal
from .util import WARNING_COLOR, ERROR_COLOR, RESET_COLOR, UserAbort
from .util import get_journal_name
log = logging.getLogger(__name__)
logging.getLogger("keyring.backend").setLevel(logging.ERROR)
@ -38,12 +38,7 @@ def guess_mode(args, config):
"""Guesses the mode (compose, read or export) from the given arguments"""
compose = True
export = False
import_ = False
if args.import_ is not False:
compose = False
export = False
import_ = True
elif (
if (
args.decrypt is not False
or args.encrypt is not False
or args.export is not False
@ -70,7 +65,7 @@ def guess_mode(args, config):
# No date and only tags?
compose = False
return compose, export, import_
return compose, export
def encrypt(journal, filename=None):
@ -139,7 +134,7 @@ Python 3.7 (or higher) soon.
if manual_args is None:
manual_args = sys.argv[1:]
args = parse_args_before_config(manual_args)
args = parse_args(manual_args)
configure_logger(args.debug)
# Run command if possible before config is available
@ -150,38 +145,24 @@ Python 3.7 (or higher) soon.
# Load the config
try:
config = install.load_or_install_jrnl()
original_config = config.copy()
args = get_journal_name(args, config)
config = util.scope_config(config, args.journal_name)
except UserAbort as err:
print(f"\n{err}", file=sys.stderr)
sys.exit(1)
# Run command now that config is available
# Run post-config command now that config is ready
if callable(args.postconfig_cmd):
args.postconfig_cmd(config=config, args=args)
args.postconfig_cmd(args=args, config=config)
sys.exit(0)
args = parse_args_after_config(args, config)
# --- All the standalone commands are now done --- #
log.debug('Using configuration "%s"', config)
original_config = config.copy()
# Get the journal we're going to be working with
journal = open_journal(args.journal_name, config)
config = util.scope_config(config, args.journal_name)
log.debug('Using journal "%s"', args.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"
else:
_exit_multiline_code = "press Ctrl+D"
# This is where we finally open the journal!
try:
journal = open_journal(args.journal_name, config)
except KeyboardInterrupt:
print("[Interrupted while opening journal]", file=sys.stderr)
sys.exit(1)
mode_compose, mode_export = guess_mode(args, config)
if mode_compose and not args.text:
if not sys.stdin.isatty():
@ -201,25 +182,24 @@ Python 3.7 (or higher) soon.
raw = util.get_text_from_editor(config, template)
else:
try:
_how_to_quit = (
"Ctrl+z and then Enter" if "win32" in sys.platform else "Ctrl+d"
)
print(
"[Compose Entry; " + _exit_multiline_code + " to finish writing]\n",
f"[Writing Entry; on a blank line, press {_how_to_quit} to finish writing]\n",
file=sys.stderr,
)
raw = sys.stdin.read()
except KeyboardInterrupt:
print("[Entry NOT saved to journal.]", file=sys.stderr)
print("[Entry NOT saved to journal]", file=sys.stderr)
sys.exit(0)
if raw:
args.text = [raw]
else:
sys.exit()
# Import mode
if mode_import:
plugins.get_importer(args.import_).import_(journal, args.input)
# Writing mode
elif mode_compose:
if mode_compose:
raw = " ".join(args.text).strip()
log.debug('Appending raw line "%s" to journal "%s"', raw, args.journal_name)
journal.new_entry(raw)
@ -242,7 +222,7 @@ Python 3.7 (or higher) soon.
journal.limit(args.limit)
# Reading mode
if not mode_compose and not mode_export and not mode_import:
if not mode_compose and not mode_export:
print(journal.pprint())
# Various export modes

View file

@ -21,3 +21,14 @@ def postconfig_list(config, **kwargs):
from .util import list_journals
print(list_journals(config))
def postconfig_import(args, config, **kwargs):
from .plugins import get_importer
from .Journal import open_journal
# Requires opening the journal
journal = open_journal(args.journal_name, config)
format = args.export if args.export else "jrnl"
get_importer(format).import_(journal, args.input)

View file

@ -117,14 +117,15 @@ def load_or_install_jrnl():
upgrade_config(config)
verify_config(config)
return config
else:
log.debug("Configuration file not found, installing jrnl...")
try:
config = install()
except KeyboardInterrupt:
raise UserAbort("Installation aborted")
return config
log.debug('Using configuration "%s"', config)
return config
def install():

View file

@ -8,8 +8,8 @@ from .plugins import EXPORT_FORMATS
from .commands import preconfig_version
from .commands import preconfig_diagnostic
from .commands import postconfig_list
from .commands import postconfig_import
from .util import deprecated_cmd
from .util import get_journal_name
class WrappingFormatter(argparse.RawDescriptionHelpFormatter):
@ -18,7 +18,7 @@ class WrappingFormatter(argparse.RawDescriptionHelpFormatter):
return textwrap.wrap(text, width=56)
def parse_args_before_config(args=[]):
def parse_args(args=[]):
"""
Argument parsing that is doable before the config is available.
Everything else goes into "text" for later parsing.
@ -27,7 +27,12 @@ def parse_args_before_config(args=[]):
formatter_class=WrappingFormatter,
add_help=False,
description="The command-line note-taking and journaling app.",
epilog="",
epilog=textwrap.dedent(
"""
Thank you to all of our contributors! Come see the whole list of code and
financial contributors at https://github.com/jrnl-org/jrnl. And special
thanks to Bad Lip Reading for the Yoda joke in the Writing section above."""
),
)
optional = parser.add_argument_group("Optional Arguments")
@ -108,13 +113,11 @@ def parse_args_before_config(args=[]):
)
standalone.add_argument(
"--import",
action="store_const",
metavar="TYPE",
dest="import_",
choices=IMPORT_FORMATS,
dest="postconfig_cmd",
const=postconfig_import,
help=f"Import entries into your journal. TYPE can be: {util.oxford_list(IMPORT_FORMATS)} (default: jrnl)",
default=False,
const="jrnl",
nargs="?",
)
standalone.add_argument(
"-i",
@ -132,17 +135,17 @@ def parse_args_before_config(args=[]):
The date and the following colon ("yesterday:") are optional. If you leave
them out, "now" will be used:
jrnl Then I rolled the log over, and underneath was a tiny little stick.
jrnl Then I rolled the log over.
Also, you can mark extra special entries ("star" them) with an asterisk:
jrnl *That log had a child!
jrnl *And underneath was a tiny little stick.
Please note that asterisks might be a special character in your shell, so you
might have to escape them. When in doubt about escaping, put single quotes
around your entire entry:
might have to escape them. When in doubt about escaping, put quotes around
your entire entry:
jrnl 'saturday at 8pm: *Always pass on what you have learned. -Yoda'"""
jrnl "saturday at 2am: *Then I was like 'That log had a child!'" """
composing = parser.add_argument_group(
"Writing", textwrap.dedent(compose_msg).strip()
@ -270,11 +273,3 @@ def parse_args_before_config(args=[]):
# return parser.parse_args(args)
return parser.parse_intermixed_args(args)
def parse_args_after_config(args, config):
# print(str(args)) # @todo take this out
args = get_journal_name(args, config)
return args

View file

@ -150,7 +150,6 @@ def scope_config(config, journal_name):
else:
# But also just give them a string to point to the journal file
config["journal"] = journal_conf
config.pop("journals")
return config
@ -335,4 +334,5 @@ def get_journal_name(args, config):
print(list_journals(config), file=sys.stderr)
sys.exit(1)
log.debug("Using journal name: %s", args.journal_name)
return args