mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 16:48:31 +02:00
Move journal_name out of cli and into separate function
It's now part of args, and it's parsed into a function after the config is available
This commit is contained in:
parent
5564d6a5a4
commit
ef9cd5c2bf
3 changed files with 32 additions and 22 deletions
30
jrnl/cli.py
30
jrnl/cli.py
|
@ -25,8 +25,8 @@ import platform
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from . import install, plugins, util
|
from . import install, plugins, util
|
||||||
from .util import list_journals
|
|
||||||
from .parsing import parse_args_before_config
|
from .parsing import parse_args_before_config
|
||||||
|
from .parsing import parse_args_after_config
|
||||||
from .Journal import PlainJournal, open_journal
|
from .Journal import PlainJournal, open_journal
|
||||||
from .util import WARNING_COLOR, ERROR_COLOR, RESET_COLOR, UserAbort
|
from .util import WARNING_COLOR, ERROR_COLOR, RESET_COLOR, UserAbort
|
||||||
|
|
||||||
|
@ -159,24 +159,14 @@ Python 3.7 (or higher) soon.
|
||||||
args.postconfig_cmd(config=config, args=args)
|
args.postconfig_cmd(config=config, args=args)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
args = parse_args_after_config(args, config)
|
||||||
|
|
||||||
log.debug('Using configuration "%s"', config)
|
log.debug('Using configuration "%s"', config)
|
||||||
original_config = config.copy()
|
original_config = config.copy()
|
||||||
|
|
||||||
# If the first textual argument points to a journal file,
|
config = util.scope_config(config, args.journal_name)
|
||||||
# use this!
|
|
||||||
|
|
||||||
journal_name = install.DEFAULT_JOURNAL_KEY
|
log.debug('Using journal "%s"', args.journal_name)
|
||||||
if args.text and args.text[0] in config["journals"]:
|
|
||||||
journal_name = args.text[0]
|
|
||||||
args.text = args.text[1:]
|
|
||||||
elif install.DEFAULT_JOURNAL_KEY not in config["journals"]:
|
|
||||||
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)
|
|
||||||
|
|
||||||
log.debug('Using journal "%s"', journal_name)
|
|
||||||
|
|
||||||
mode_compose, mode_export, mode_import = guess_mode(args, config)
|
mode_compose, mode_export, mode_import = guess_mode(args, config)
|
||||||
|
|
||||||
|
@ -188,7 +178,7 @@ Python 3.7 (or higher) soon.
|
||||||
|
|
||||||
# This is where we finally open the journal!
|
# This is where we finally open the journal!
|
||||||
try:
|
try:
|
||||||
journal = open_journal(journal_name, config)
|
journal = open_journal(args.journal_name, config)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("[Interrupted while opening journal]", file=sys.stderr)
|
print("[Interrupted while opening journal]", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
@ -231,9 +221,9 @@ Python 3.7 (or higher) soon.
|
||||||
# Writing mode
|
# Writing mode
|
||||||
elif mode_compose:
|
elif mode_compose:
|
||||||
raw = " ".join(args.text).strip()
|
raw = " ".join(args.text).strip()
|
||||||
log.debug('Appending raw line "%s" to journal "%s"', raw, journal_name)
|
log.debug('Appending raw line "%s" to journal "%s"', raw, args.journal_name)
|
||||||
journal.new_entry(raw)
|
journal.new_entry(raw)
|
||||||
print(f"[Entry added to {journal_name} journal]", file=sys.stderr)
|
print(f"[Entry added to {args.journal_name} journal]", file=sys.stderr)
|
||||||
journal.write()
|
journal.write()
|
||||||
|
|
||||||
if not mode_compose:
|
if not mode_compose:
|
||||||
|
@ -271,7 +261,7 @@ Python 3.7 (or higher) soon.
|
||||||
# Not encrypting to a separate file: update config!
|
# Not encrypting to a separate file: update config!
|
||||||
if not args.encrypt:
|
if not args.encrypt:
|
||||||
update_config(
|
update_config(
|
||||||
original_config, {"encrypt": True}, journal_name, force_local=True
|
original_config, {"encrypt": True}, args.journal_name, force_local=True
|
||||||
)
|
)
|
||||||
install.save_config(original_config)
|
install.save_config(original_config)
|
||||||
|
|
||||||
|
@ -280,7 +270,7 @@ Python 3.7 (or higher) soon.
|
||||||
# Not decrypting to a separate file: update config!
|
# Not decrypting to a separate file: update config!
|
||||||
if not args.decrypt:
|
if not args.decrypt:
|
||||||
update_config(
|
update_config(
|
||||||
original_config, {"encrypt": False}, journal_name, force_local=True
|
original_config, {"encrypt": False}, args.journal_name, force_local=True
|
||||||
)
|
)
|
||||||
install.save_config(original_config)
|
install.save_config(original_config)
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ from .commands import preconfig_version
|
||||||
from .commands import preconfig_diagnostic
|
from .commands import preconfig_diagnostic
|
||||||
from .commands import postconfig_list
|
from .commands import postconfig_list
|
||||||
from .util import deprecated_cmd
|
from .util import deprecated_cmd
|
||||||
|
from .util import get_journal_name
|
||||||
|
|
||||||
|
|
||||||
class WrappingFormatter(argparse.RawDescriptionHelpFormatter):
|
class WrappingFormatter(argparse.RawDescriptionHelpFormatter):
|
||||||
|
@ -274,5 +275,9 @@ def parse_args_before_config(args=None):
|
||||||
return parser.parse_intermixed_args(args)
|
return parser.parse_intermixed_args(args)
|
||||||
|
|
||||||
|
|
||||||
def parse_args_after_config(args=None):
|
def parse_args_after_config(args, config):
|
||||||
return None
|
# print(str(args)) # @todo take this out
|
||||||
|
|
||||||
|
args = get_journal_name(args, config)
|
||||||
|
|
||||||
|
return args
|
||||||
|
|
15
jrnl/util.py
15
jrnl/util.py
|
@ -321,3 +321,18 @@ def list_journals(config):
|
||||||
journal, ml, cfg["journal"] if isinstance(cfg, dict) else cfg
|
journal, ml, cfg["journal"] if isinstance(cfg, dict) else cfg
|
||||||
)
|
)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def get_journal_name(args, config):
|
||||||
|
from . import install
|
||||||
|
|
||||||
|
args.journal_name = install.DEFAULT_JOURNAL_KEY
|
||||||
|
if args.text and args.text[0] in config["journals"]:
|
||||||
|
args.journal_name = args.text[0]
|
||||||
|
args.text = args.text[1:]
|
||||||
|
elif install.DEFAULT_JOURNAL_KEY not in config["journals"]:
|
||||||
|
print("No default journal configured.", file=sys.stderr)
|
||||||
|
print(list_journals(config), file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
return args
|
||||||
|
|
Loading…
Add table
Reference in a new issue