From 18bae71d952522a6b96aa93fd80d5b298150b6bc Mon Sep 17 00:00:00 2001 From: Jonathan van der Steege Date: Mon, 20 Jun 2022 22:48:59 +0200 Subject: [PATCH] Make use of ruamel DuplicateKeyError exception --- jrnl/config.py | 23 ++++++++++++++--------- jrnl/messages/MsgText.py | 7 ++++--- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/jrnl/config.py b/jrnl/config.py index 7d2a6dbe..db765d9d 100644 --- a/jrnl/config.py +++ b/jrnl/config.py @@ -6,6 +6,7 @@ import os import colorama from ruamel.yaml import YAML +from ruamel.yaml import constructor import xdg.BaseDirectory from . import __version__ @@ -162,21 +163,25 @@ def verify_config_colors(config): def load_config(config_path): """Tries to load a config file from YAML.""" - # If duplicate keys at same level in config file, print warning - duplicate_keys = config_duplicate_keys(config_path) - if duplicate_keys: + 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, - {"duplicate_keys": duplicate_keys}, + { + "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) + 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): diff --git a/jrnl/messages/MsgText.py b/jrnl/messages/MsgText.py index f1518329..c4b8df97 100644 --- a/jrnl/messages/MsgText.py +++ b/jrnl/messages/MsgText.py @@ -198,9 +198,10 @@ class MsgText(Enum): ConfigDoubleKeys = """ Warning: One or more keys appear multiple times at the same level - in your configuration file: - {duplicate_keys} - """ + in your configuration file. + + {error_message} + """ # --- Password --- # Password = "Password:"