Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug for new --list --format options when no default journal is specified #1621

Merged
merged 5 commits into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions jrnl/Journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from jrnl import Entry
from jrnl import time
from jrnl.config import validate_journal_name
from jrnl.messages import Message
from jrnl.messages import MsgStyle
from jrnl.messages import MsgText
Expand Down Expand Up @@ -430,6 +431,7 @@ def open_journal(journal_name, config, legacy=False):
If legacy is True, it will open Journals with legacy classes build for
backwards compatibility with jrnl 1.x
"""
validate_journal_name(journal_name, config)
config = config.copy()
config["journal"] = expand_path(config["journal"])

Expand Down
25 changes: 21 additions & 4 deletions jrnl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
Also, please note that all (non-builtin) imports should be scoped to each function to
avoid any possible overhead for these standalone commands.
"""
import argparse
import platform
import sys

from jrnl.config import cmd_requires_valid_journal_name
from jrnl.exception import JrnlException
from jrnl.messages import Message
from jrnl.messages import MsgStyle
Expand Down Expand Up @@ -56,13 +58,16 @@ def preconfig_version(_):
print(output)


def postconfig_list(args, config, **kwargs):
def postconfig_list(args: argparse.Namespace, config: dict, **_) -> int:
from jrnl.output import list_journals

print(list_journals(config, args.export))

return 0

def postconfig_import(args, config, **kwargs):

@cmd_requires_valid_journal_name
def postconfig_import(args: argparse.Namespace, config: dict, **_) -> int:
from jrnl.Journal import open_journal
from jrnl.plugins import get_importer

Expand All @@ -72,8 +77,13 @@ def postconfig_import(args, config, **kwargs):
format = args.export if args.export else "jrnl"
get_importer(format).import_(journal, args.filename)

return 0


def postconfig_encrypt(args, config, original_config, **kwargs):
@cmd_requires_valid_journal_name
def postconfig_encrypt(
args: argparse.Namespace, config: dict, original_config: dict
) -> int:
"""
Encrypt a journal in place, or optionally to a new file
"""
Expand Down Expand Up @@ -122,8 +132,13 @@ def postconfig_encrypt(args, config, original_config, **kwargs):
)
save_config(original_config)

return 0

def postconfig_decrypt(args, config, original_config, **kwargs):

@cmd_requires_valid_journal_name
def postconfig_decrypt(
args: argparse.Namespace, config: dict, original_config: dict
) -> int:
"""Decrypts into new file. If filename is not set, we encrypt the journal file itself."""
from jrnl.config import update_config
from jrnl.install import save_config
Expand All @@ -149,3 +164,5 @@ def postconfig_decrypt(args, config, original_config, **kwargs):
original_config, {"encrypt": False}, args.journal_name, force_local=True
)
save_config(original_config)

return 0
27 changes: 21 additions & 6 deletions jrnl/config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Copyright © 2012-2022 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html

import argparse
import logging
import os
from typing import Callable

import colorama
import xdg.BaseDirectory
Expand Down Expand Up @@ -213,14 +215,27 @@ def get_journal_name(args, config):
args.journal_name = potential_journal_name
args.text = args.text[1:]

if args.journal_name not in config["journals"]:
logging.debug("Using journal name: %s", args.journal_name)
return args


def cmd_requires_valid_journal_name(func: Callable) -> Callable:
def wrapper(args: argparse.Namespace, config: dict, original_config: dict):
validate_journal_name(args.journal_name, config)
func(args=args, config=config, original_config=original_config)

return wrapper


def validate_journal_name(journal_name: str, config: dict) -> None:
if journal_name not in config["journals"]:
raise JrnlException(
Message(
MsgText.NoDefaultJournal,
MsgText.NoNamedJournal,
MsgStyle.ERROR,
{"journals": list_journals(config)},
{
"journal_name": journal_name,
"journals": list_journals(config),
},
),
)

logging.debug("Using journal name: %s", args.journal_name)
return args
2 changes: 1 addition & 1 deletion jrnl/messages/MsgText.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def __str__(self) -> str:
{template}
"""

NoDefaultJournal = "No default journal configured\n{journals}"
NoNamedJournal = "No '{journal_name}' journal configured\n{journals}"

DoesNotExist = "{name} does not exist"

Expand Down
6 changes: 3 additions & 3 deletions tests/bdd/features/config_file.feature
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ Feature: Multiple journals
Then the output should contain "sell my junk on ebay and make lots of money"

Scenario: Don't crash if no default journal is specified using an alternate config
Given the config "bug343.yaml" exists
Given the config "no_default_journal.yaml" exists
And we use the config "basic_onefile.yaml"
When we run "jrnl --cf bug343.yaml a long day in the office"
Then the output should contain "No default journal configured"
When we run "jrnl --cf no_default_journal.yaml a long day in the office"
Then the output should contain "No 'default' journal configured"

Scenario: Don't crash if no file exists for a configured encrypted journal using an alternate config
Given the config "multiple.yaml" exists
Expand Down
17 changes: 17 additions & 0 deletions tests/bdd/features/format.feature
Original file line number Diff line number Diff line change
Expand Up @@ -612,3 +612,20 @@ Feature: Custom formats
config_path: .+basic_onefile\.yaml
journals:
default: features/journals/basic_onefile\.journal

Scenario: Export journal list to formats with no default journal
Given we use the config "no_default_journal.yaml"
When we run "jrnl --list"
Then the output should match
Journals defined in config \(.+no_default_journal\.yaml\)
\* simple -> features/journals/simple\.journal
\* work -> features/journals/work\.journal
When we run "jrnl --list --format json"
Then the output should match
{"config_path": ".+no_default_journal\.yaml", "journals": {"simple": "features/journals/simple\.journal", "work": "features/journals/work\.journal"}}
When we run "jrnl --list --format yaml"
Then the output should match
config_path: .+no_default_journal\.yaml
journals:
simple: features/journals/simple\.journal
work: features/journals/work\.journal
4 changes: 2 additions & 2 deletions tests/bdd/features/multiple_journals.feature
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ Feature: Multiple journals
2012-07-23 09:00 sell my junk on ebay and make lots of money

Scenario: Don't crash if no default journal is specified
Given we use the config "bug343.yaml"
Given we use the config "no_default_journal.yaml"
When we run "jrnl a long day in the office"
Then the output should contain "No default journal configured"
Then the output should contain "No 'default' journal configured"

Scenario: Don't crash if no file exists for a configured encrypted journal
Given we use the config "multiple.yaml"
Expand Down