From 7951248fb328f6dddd28df0c255fb7d26293d2f5 Mon Sep 17 00:00:00 2001 From: Mathieu Lemay Date: Sun, 23 Jun 2024 11:36:14 -0400 Subject: [PATCH] fix: ensure config directory exists when creating config file If the config file directory doesn't already exist, the application will crash on startup while trying to create the default configuration. Fix this by creating the directory. Drive-by: Replace os.path functions by pathlib's functions --- application/util/config.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/application/util/config.py b/application/util/config.py index 445521e..a0ff8a2 100644 --- a/application/util/config.py +++ b/application/util/config.py @@ -1,5 +1,3 @@ -import os - from pydantic import BaseModel from yaml import dump, safe_load @@ -39,13 +37,14 @@ def create_default_config(): "toggle-scroll": "s", }, } - with open(CONFIG_PATH, "w") as file: + CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True) + with CONFIG_PATH.open("w") as file: dump(default_config, file) def load_config(): - if not os.path.exists(CONFIG_PATH): + if not CONFIG_PATH.exists(): create_default_config() - with open(CONFIG_PATH, "r") as file: + with CONFIG_PATH.open() as file: config_dict = safe_load(file) return Config(**config_dict)