mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-17 11:38:32 +02:00
Change journal name validation
Journal name validation used to happen before postconfig commands could have a chance to run, so now each command is responsible for its own error-checking of the journal name. Added a new decorator and function that makes this error-checking easier. Co-authored-by: Micah Jerome Ellison <micah.jerome.ellison@gmail.com>
This commit is contained in:
parent
3ac2b95156
commit
2130af2e39
7 changed files with 48 additions and 12 deletions
|
@ -8,6 +8,7 @@ import re
|
||||||
|
|
||||||
from jrnl import Entry
|
from jrnl import Entry
|
||||||
from jrnl import time
|
from jrnl import time
|
||||||
|
from jrnl.config import validate_journal_name
|
||||||
from jrnl.messages import Message
|
from jrnl.messages import Message
|
||||||
from jrnl.messages import MsgStyle
|
from jrnl.messages import MsgStyle
|
||||||
from jrnl.messages import MsgText
|
from jrnl.messages import MsgText
|
||||||
|
@ -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
|
If legacy is True, it will open Journals with legacy classes build for
|
||||||
backwards compatibility with jrnl 1.x
|
backwards compatibility with jrnl 1.x
|
||||||
"""
|
"""
|
||||||
|
validate_journal_name(journal_name, config)
|
||||||
config = config.copy()
|
config = config.copy()
|
||||||
config["journal"] = expand_path(config["journal"])
|
config["journal"] = expand_path(config["journal"])
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ avoid any possible overhead for these standalone commands.
|
||||||
import platform
|
import platform
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from jrnl.config import cmd_requires_valid_journal_name
|
||||||
from jrnl.exception import JrnlException
|
from jrnl.exception import JrnlException
|
||||||
from jrnl.messages import Message
|
from jrnl.messages import Message
|
||||||
from jrnl.messages import MsgStyle
|
from jrnl.messages import MsgStyle
|
||||||
|
@ -62,6 +63,7 @@ def postconfig_list(args, config, **kwargs):
|
||||||
print(list_journals(config, args.export))
|
print(list_journals(config, args.export))
|
||||||
|
|
||||||
|
|
||||||
|
@cmd_requires_valid_journal_name
|
||||||
def postconfig_import(args, config, **kwargs):
|
def postconfig_import(args, config, **kwargs):
|
||||||
from jrnl.Journal import open_journal
|
from jrnl.Journal import open_journal
|
||||||
from jrnl.plugins import get_importer
|
from jrnl.plugins import get_importer
|
||||||
|
@ -73,6 +75,7 @@ def postconfig_import(args, config, **kwargs):
|
||||||
get_importer(format).import_(journal, args.filename)
|
get_importer(format).import_(journal, args.filename)
|
||||||
|
|
||||||
|
|
||||||
|
@cmd_requires_valid_journal_name
|
||||||
def postconfig_encrypt(args, config, original_config, **kwargs):
|
def postconfig_encrypt(args, config, original_config, **kwargs):
|
||||||
"""
|
"""
|
||||||
Encrypt a journal in place, or optionally to a new file
|
Encrypt a journal in place, or optionally to a new file
|
||||||
|
@ -123,6 +126,7 @@ def postconfig_encrypt(args, config, original_config, **kwargs):
|
||||||
save_config(original_config)
|
save_config(original_config)
|
||||||
|
|
||||||
|
|
||||||
|
@cmd_requires_valid_journal_name
|
||||||
def postconfig_decrypt(args, config, original_config, **kwargs):
|
def postconfig_decrypt(args, config, original_config, **kwargs):
|
||||||
"""Decrypts into new file. If filename is not set, we encrypt the journal file itself."""
|
"""Decrypts into new file. If filename is not set, we encrypt the journal file itself."""
|
||||||
from jrnl.config import update_config
|
from jrnl.config import update_config
|
||||||
|
|
|
@ -213,14 +213,27 @@ def get_journal_name(args, config):
|
||||||
args.journal_name = potential_journal_name
|
args.journal_name = potential_journal_name
|
||||||
args.text = args.text[1:]
|
args.text = args.text[1:]
|
||||||
|
|
||||||
if args.journal_name not in config["journals"]:
|
|
||||||
raise JrnlException(
|
|
||||||
Message(
|
|
||||||
MsgText.NoDefaultJournal,
|
|
||||||
MsgStyle.ERROR,
|
|
||||||
{"journals": list_journals(config)},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
logging.debug("Using journal name: %s", args.journal_name)
|
logging.debug("Using journal name: %s", args.journal_name)
|
||||||
return args
|
return args
|
||||||
|
|
||||||
|
|
||||||
|
def cmd_requires_valid_journal_name(func):
|
||||||
|
def wrapper(args, config, original_config):
|
||||||
|
validate_journal_name(args.journal_name, config)
|
||||||
|
func(args, config, original_config)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
def validate_journal_name(journal_name, config):
|
||||||
|
if journal_name not in config["journals"]:
|
||||||
|
raise JrnlException(
|
||||||
|
Message(
|
||||||
|
MsgText.NoNamedJournal,
|
||||||
|
MsgStyle.ERROR,
|
||||||
|
{
|
||||||
|
"journal_name": journal_name,
|
||||||
|
"journals": list_journals(config),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
|
@ -101,7 +101,7 @@ class MsgText(Enum):
|
||||||
{template}
|
{template}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
NoDefaultJournal = "No default journal configured\n{journals}"
|
NoNamedJournal = "No '{journal_name}' journal configured\n{journals}"
|
||||||
|
|
||||||
DoesNotExist = "{name} does not exist"
|
DoesNotExist = "{name} does not exist"
|
||||||
|
|
||||||
|
|
|
@ -67,7 +67,7 @@ Feature: Multiple journals
|
||||||
Given the config "no_default_journal.yaml" exists
|
Given the config "no_default_journal.yaml" exists
|
||||||
And we use the config "basic_onefile.yaml"
|
And we use the config "basic_onefile.yaml"
|
||||||
When we run "jrnl --cf no_default_journal.yaml a long day in the office"
|
When we run "jrnl --cf no_default_journal.yaml 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 using an alternate config
|
Scenario: Don't crash if no file exists for a configured encrypted journal using an alternate config
|
||||||
Given the config "multiple.yaml" exists
|
Given the config "multiple.yaml" exists
|
||||||
|
|
|
@ -612,3 +612,20 @@ Feature: Custom formats
|
||||||
config_path: .+basic_onefile\.yaml
|
config_path: .+basic_onefile\.yaml
|
||||||
journals:
|
journals:
|
||||||
default: features/journals/basic_onefile\.journal
|
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
|
||||||
|
|
|
@ -82,7 +82,7 @@ Feature: Multiple journals
|
||||||
Scenario: Don't crash if no default journal is specified
|
Scenario: Don't crash if no default journal is specified
|
||||||
Given we use the config "no_default_journal.yaml"
|
Given we use the config "no_default_journal.yaml"
|
||||||
When we run "jrnl a long day in the office"
|
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
|
Scenario: Don't crash if no file exists for a configured encrypted journal
|
||||||
Given we use the config "multiple.yaml"
|
Given we use the config "multiple.yaml"
|
||||||
|
|
Loading…
Add table
Reference in a new issue