Check for duplicate keys in config file (#1511)

* Check for duplicate keys in config file
* Corrected BDD tests
* Unneeded check removed
* Make use of ruamel DuplicateKeyError exception
* Remove unneeded import and function
* slightly reword warning message
* fix merge conflicts
* update tests

Co-authored-by: Jonathan Wren <jonathan@nowandwren.com>
This commit is contained in:
Jonathan van der Steege 2022-07-16 23:21:21 +02:00 committed by GitHub
parent c45bed7f6c
commit 3a5316cedc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 4 deletions

View file

@ -7,6 +7,7 @@ import os
import colorama
import xdg.BaseDirectory
from ruamel.yaml import YAML
from ruamel.yaml import constructor
from jrnl import __version__
from jrnl.exception import JrnlException
@ -159,10 +160,25 @@ def verify_config_colors(config):
def load_config(config_path):
"""Tries to load a config file from YAML."""
with open(config_path, encoding=YAML_FILE_ENCODING) as f:
yaml = YAML(typ="safe")
yaml.allow_duplicate_keys = True
return yaml.load(f)
try:
with open(config_path, encoding=YAML_FILE_ENCODING) as f:
yaml = YAML(typ="safe")
yaml.allow_duplicate_keys = False
return yaml.load(f)
except constructor.DuplicateKeyError as e:
print_msg(
Message(
MsgText.ConfigDoubleKeys,
MsgStyle.WARNING,
{
"error_message": e,
},
)
)
with open(config_path, encoding=YAML_FILE_ENCODING) as f:
yaml = YAML(typ="safe")
yaml.allow_duplicate_keys = True
return yaml.load(f)
def is_config_json(config_path):

View file

@ -198,6 +198,13 @@ class MsgText(Enum):
Configuration updated to newest version at {config_path}
"""
ConfigDoubleKeys = """
There is at least one duplicate key in your configuration file.
Details:
{error_message}
"""
# --- Password --- #
Password = "Password:"
PasswordFirstEntry = "Enter password for journal '{journal_name}': "