From fe03ab66e91afac708f1e5186499ae531bdbf7a9 Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Sun, 16 Aug 2020 12:31:57 -0700 Subject: [PATCH] move run function out of cli and into jrnl --- features/steps/core.py | 8 ++--- jrnl/__main__.py | 6 ++-- jrnl/cli.py | 67 +++++++----------------------------------- jrnl/jrnl.py | 52 ++++++++++++++++++++++++++++++++ jrnl/parse_args.py | 15 ++++++---- pyproject.toml | 2 +- 6 files changed, 81 insertions(+), 69 deletions(-) diff --git a/features/steps/core.py b/features/steps/core.py index 76bc8d32..8ab427a2 100644 --- a/features/steps/core.py +++ b/features/steps/core.py @@ -16,9 +16,9 @@ import yaml from jrnl import Journal from jrnl import __version__ -from jrnl import cli from jrnl import install from jrnl import plugins +from jrnl.cli import cli from jrnl.config import load_config from jrnl.os_compat import on_windows @@ -136,7 +136,7 @@ def open_editor_and_enter(context, method, text=""): patch("subprocess.call", side_effect=_mock_editor_function), \ patch("sys.stdin.isatty", return_value=True) \ : - cli.run(["--edit"]) + cli(["--edit"]) # fmt: on @@ -200,7 +200,7 @@ def run_with_input(context, command, inputs=""): patch("sys.stdin.read", side_effect=text) as mock_read \ : try: - cli.run(args or []) + cli(args or []) context.exit_status = 0 except SystemExit as e: context.exit_status = e.code @@ -236,7 +236,7 @@ def run(context, command, text="", cache_dir=None): with patch("sys.argv", args), patch( "subprocess.call", side_effect=_mock_editor ), patch("sys.stdin.read", side_effect=lambda: text): - cli.run(args[1:]) + cli(args[1:]) context.exit_status = 0 except SystemExit as e: context.exit_status = e.code diff --git a/jrnl/__main__.py b/jrnl/__main__.py index 2b46147a..a67d0add 100644 --- a/jrnl/__main__.py +++ b/jrnl/__main__.py @@ -1,5 +1,7 @@ #!/usr/bin/env python -from . import cli +import sys + +from .cli import cli if __name__ == "__main__": - cli.run() + sys.exit(cli()) diff --git a/jrnl/cli.py b/jrnl/cli.py index 3d30e84d..bb8eba24 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -19,12 +19,7 @@ import logging import sys -from . import install -from . import jrnl -from .Journal import open_journal -from .config import get_journal_name -from .config import scope_config -from .exception import UserAbort +from .jrnl import run from .parse_args import parse_args @@ -37,58 +32,16 @@ def configure_logger(debug=False): logging.getLogger("keyring.backend").setLevel(logging.ERROR) -def run(manual_args=None): - """ - Flow: - 1. Parse cli arguments - 2. Run standalone command if it doesn't require config (help, version, etc), then exit - 3. Load config - 4. Run standalone command if it does require config (encrypt, decrypt, etc), then exit - 5. Load specified journal - 6. Start write mode, or search mode - 7. Profit - """ - if manual_args is None: - manual_args = sys.argv[1:] - - args = parse_args(manual_args) - configure_logger(args.debug) - logging.debug("Parsed args: %s", args) - - # Run command if possible before config is available - if callable(args.preconfig_cmd): - args.preconfig_cmd(args) - sys.exit(0) - - # Load the config, and extract journal name +def cli(manual_args=None): try: - config = install.load_or_install_jrnl() - original_config = config.copy() - args = get_journal_name(args, config) - config = scope_config(config, args.journal_name) - except UserAbort as err: - print(f"\n{err}", file=sys.stderr) - sys.exit(1) + if manual_args is None: + manual_args = sys.argv[1:] - # Run post-config command now that config is ready - if callable(args.postconfig_cmd): - args.postconfig_cmd(args=args, config=config, original_config=original_config) - sys.exit(0) + args = parse_args(manual_args) + configure_logger(args.debug) + logging.debug("Parsed args: %s", args) - # --- All the standalone commands are now done --- # + return run(args) - # Get the journal we're going to be working with - journal = open_journal(args.journal_name, config) - - kwargs = { - "args": args, - "config": config, - "journal": journal, - } - - if jrnl._is_write_mode(**kwargs): - jrnl.write_mode(**kwargs) - else: - jrnl.search_mode(**kwargs) - - # All done! + except KeyboardInterrupt: + return 1 diff --git a/jrnl/jrnl.py b/jrnl/jrnl.py index 4051d876..b691bb5a 100644 --- a/jrnl/jrnl.py +++ b/jrnl/jrnl.py @@ -3,12 +3,64 @@ import sys from . import install from . import plugins +from .Journal import open_journal from .color import ERROR_COLOR from .color import RESET_COLOR +from .config import get_journal_name +from .config import scope_config from .editor import get_text_from_editor +from .exception import UserAbort from .os_compat import on_windows +def run(args): + """ + Flow: + 1. Run standalone command if it doesn't require config (help, version, etc), then exit + 2. Load config + 3. Run standalone command if it does require config (encrypt, decrypt, etc), then exit + 4. Load specified journal + 5. Start write mode, or search mode + 6. Profit + """ + + # Run command if possible before config is available + if callable(args.preconfig_cmd): + return args.preconfig_cmd(args) + + # Load the config, and extract journal name + try: + config = install.load_or_install_jrnl() + original_config = config.copy() + args = get_journal_name(args, config) + config = scope_config(config, args.journal_name) + except UserAbort as err: + print(f"\n{err}", file=sys.stderr) + sys.exit(1) + + # Run post-config command now that config is ready + if callable(args.postconfig_cmd): + return args.postconfig_cmd( + args=args, config=config, original_config=original_config + ) + + # --- All the standalone commands are now done --- # + + # Get the journal we're going to be working with + journal = open_journal(args.journal_name, config) + + kwargs = { + "args": args, + "config": config, + "journal": journal, + } + + if _is_write_mode(**kwargs): + write_mode(**kwargs) + else: + search_mode(**kwargs) + + def _is_write_mode(args, config, **kwargs): """Determines if we are in write mode (as opposed to search mode)""" write_mode = True diff --git a/jrnl/parse_args.py b/jrnl/parse_args.py index b1baa2a1..f5043f8f 100644 --- a/jrnl/parse_args.py +++ b/jrnl/parse_args.py @@ -2,8 +2,13 @@ import argparse import re import textwrap -from .commands import deprecated_cmd -from .commands import output +from .commands import postconfig_decrypt +from .commands import postconfig_encrypt +from .commands import postconfig_import +from .commands import postconfig_list +from .commands import preconfig_diagnostic +from .commands import preconfig_version +from .output import deprecated_cmd from .plugins import EXPORT_FORMATS from .plugins import IMPORT_FORMATS from .plugins import util @@ -98,23 +103,23 @@ def parse_args(args=[]): help="Encrypt selected journal with a password", action="store_const", metavar="TYPE", - dest="postconfig_cmd", const=postconfig_encrypt, + dest="postconfig_cmd", ) standalone.add_argument( "--decrypt", help="Decrypt selected journal and store it in plain text", action="store_const", metavar="TYPE", - dest="postconfig_cmd", const=postconfig_decrypt, + dest="postconfig_cmd", ) standalone.add_argument( "--import", action="store_const", metavar="TYPE", - dest="postconfig_cmd", const=postconfig_import, + dest="postconfig_cmd", help=f""" Import entries from another journal. diff --git a/pyproject.toml b/pyproject.toml index a0723e8f..9b4c81f4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,7 +40,7 @@ pyflakes = "^2.2.0" pytest = "^5.4.3" [tool.poetry.scripts] -jrnl = 'jrnl.cli:run' +jrnl = 'jrnl.cli:cli' [tool.isort] multi_line_output = 7