mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-20 21:18:32 +02:00
move run function out of cli and into jrnl
This commit is contained in:
parent
9ccbcdcc7e
commit
fe03ab66e9
6 changed files with 81 additions and 69 deletions
|
@ -16,9 +16,9 @@ import yaml
|
||||||
|
|
||||||
from jrnl import Journal
|
from jrnl import Journal
|
||||||
from jrnl import __version__
|
from jrnl import __version__
|
||||||
from jrnl import cli
|
|
||||||
from jrnl import install
|
from jrnl import install
|
||||||
from jrnl import plugins
|
from jrnl import plugins
|
||||||
|
from jrnl.cli import cli
|
||||||
from jrnl.config import load_config
|
from jrnl.config import load_config
|
||||||
from jrnl.os_compat import on_windows
|
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("subprocess.call", side_effect=_mock_editor_function), \
|
||||||
patch("sys.stdin.isatty", return_value=True) \
|
patch("sys.stdin.isatty", return_value=True) \
|
||||||
:
|
:
|
||||||
cli.run(["--edit"])
|
cli(["--edit"])
|
||||||
# fmt: on
|
# fmt: on
|
||||||
|
|
||||||
|
|
||||||
|
@ -200,7 +200,7 @@ def run_with_input(context, command, inputs=""):
|
||||||
patch("sys.stdin.read", side_effect=text) as mock_read \
|
patch("sys.stdin.read", side_effect=text) as mock_read \
|
||||||
:
|
:
|
||||||
try:
|
try:
|
||||||
cli.run(args or [])
|
cli(args or [])
|
||||||
context.exit_status = 0
|
context.exit_status = 0
|
||||||
except SystemExit as e:
|
except SystemExit as e:
|
||||||
context.exit_status = e.code
|
context.exit_status = e.code
|
||||||
|
@ -236,7 +236,7 @@ def run(context, command, text="", cache_dir=None):
|
||||||
with patch("sys.argv", args), patch(
|
with patch("sys.argv", args), patch(
|
||||||
"subprocess.call", side_effect=_mock_editor
|
"subprocess.call", side_effect=_mock_editor
|
||||||
), patch("sys.stdin.read", side_effect=lambda: text):
|
), patch("sys.stdin.read", side_effect=lambda: text):
|
||||||
cli.run(args[1:])
|
cli(args[1:])
|
||||||
context.exit_status = 0
|
context.exit_status = 0
|
||||||
except SystemExit as e:
|
except SystemExit as e:
|
||||||
context.exit_status = e.code
|
context.exit_status = e.code
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
from . import cli
|
import sys
|
||||||
|
|
||||||
|
from .cli import cli
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
cli.run()
|
sys.exit(cli())
|
||||||
|
|
59
jrnl/cli.py
59
jrnl/cli.py
|
@ -19,12 +19,7 @@
|
||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from . import install
|
from .jrnl import run
|
||||||
from . import jrnl
|
|
||||||
from .Journal import open_journal
|
|
||||||
from .config import get_journal_name
|
|
||||||
from .config import scope_config
|
|
||||||
from .exception import UserAbort
|
|
||||||
from .parse_args import parse_args
|
from .parse_args import parse_args
|
||||||
|
|
||||||
|
|
||||||
|
@ -37,17 +32,8 @@ def configure_logger(debug=False):
|
||||||
logging.getLogger("keyring.backend").setLevel(logging.ERROR)
|
logging.getLogger("keyring.backend").setLevel(logging.ERROR)
|
||||||
|
|
||||||
|
|
||||||
def run(manual_args=None):
|
def cli(manual_args=None):
|
||||||
"""
|
try:
|
||||||
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:
|
if manual_args is None:
|
||||||
manual_args = sys.argv[1:]
|
manual_args = sys.argv[1:]
|
||||||
|
|
||||||
|
@ -55,40 +41,7 @@ def run(manual_args=None):
|
||||||
configure_logger(args.debug)
|
configure_logger(args.debug)
|
||||||
logging.debug("Parsed args: %s", args)
|
logging.debug("Parsed args: %s", args)
|
||||||
|
|
||||||
# Run command if possible before config is available
|
return run(args)
|
||||||
if callable(args.preconfig_cmd):
|
|
||||||
args.preconfig_cmd(args)
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
# Load the config, and extract journal name
|
except KeyboardInterrupt:
|
||||||
try:
|
return 1
|
||||||
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):
|
|
||||||
args.postconfig_cmd(args=args, config=config, original_config=original_config)
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
# --- 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 jrnl._is_write_mode(**kwargs):
|
|
||||||
jrnl.write_mode(**kwargs)
|
|
||||||
else:
|
|
||||||
jrnl.search_mode(**kwargs)
|
|
||||||
|
|
||||||
# All done!
|
|
||||||
|
|
52
jrnl/jrnl.py
52
jrnl/jrnl.py
|
@ -3,12 +3,64 @@ import sys
|
||||||
|
|
||||||
from . import install
|
from . import install
|
||||||
from . import plugins
|
from . import plugins
|
||||||
|
from .Journal import open_journal
|
||||||
from .color import ERROR_COLOR
|
from .color import ERROR_COLOR
|
||||||
from .color import RESET_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 .editor import get_text_from_editor
|
||||||
|
from .exception import UserAbort
|
||||||
from .os_compat import on_windows
|
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):
|
def _is_write_mode(args, config, **kwargs):
|
||||||
"""Determines if we are in write mode (as opposed to search mode)"""
|
"""Determines if we are in write mode (as opposed to search mode)"""
|
||||||
write_mode = True
|
write_mode = True
|
||||||
|
|
|
@ -2,8 +2,13 @@ import argparse
|
||||||
import re
|
import re
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
from .commands import deprecated_cmd
|
from .commands import postconfig_decrypt
|
||||||
from .commands import output
|
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 EXPORT_FORMATS
|
||||||
from .plugins import IMPORT_FORMATS
|
from .plugins import IMPORT_FORMATS
|
||||||
from .plugins import util
|
from .plugins import util
|
||||||
|
@ -98,23 +103,23 @@ def parse_args(args=[]):
|
||||||
help="Encrypt selected journal with a password",
|
help="Encrypt selected journal with a password",
|
||||||
action="store_const",
|
action="store_const",
|
||||||
metavar="TYPE",
|
metavar="TYPE",
|
||||||
dest="postconfig_cmd",
|
|
||||||
const=postconfig_encrypt,
|
const=postconfig_encrypt,
|
||||||
|
dest="postconfig_cmd",
|
||||||
)
|
)
|
||||||
standalone.add_argument(
|
standalone.add_argument(
|
||||||
"--decrypt",
|
"--decrypt",
|
||||||
help="Decrypt selected journal and store it in plain text",
|
help="Decrypt selected journal and store it in plain text",
|
||||||
action="store_const",
|
action="store_const",
|
||||||
metavar="TYPE",
|
metavar="TYPE",
|
||||||
dest="postconfig_cmd",
|
|
||||||
const=postconfig_decrypt,
|
const=postconfig_decrypt,
|
||||||
|
dest="postconfig_cmd",
|
||||||
)
|
)
|
||||||
standalone.add_argument(
|
standalone.add_argument(
|
||||||
"--import",
|
"--import",
|
||||||
action="store_const",
|
action="store_const",
|
||||||
metavar="TYPE",
|
metavar="TYPE",
|
||||||
dest="postconfig_cmd",
|
|
||||||
const=postconfig_import,
|
const=postconfig_import,
|
||||||
|
dest="postconfig_cmd",
|
||||||
help=f"""
|
help=f"""
|
||||||
Import entries from another journal.
|
Import entries from another journal.
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ pyflakes = "^2.2.0"
|
||||||
pytest = "^5.4.3"
|
pytest = "^5.4.3"
|
||||||
|
|
||||||
[tool.poetry.scripts]
|
[tool.poetry.scripts]
|
||||||
jrnl = 'jrnl.cli:run'
|
jrnl = 'jrnl.cli:cli'
|
||||||
|
|
||||||
[tool.isort]
|
[tool.isort]
|
||||||
multi_line_output = 7
|
multi_line_output = 7
|
||||||
|
|
Loading…
Add table
Reference in a new issue