Skip to content

Commit

Permalink
fix: ensure config directory exists when creating config file
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mathieu-lemay committed Jun 23, 2024
1 parent 6eba20f commit 7951248
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions application/util/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import os

from pydantic import BaseModel
from yaml import dump, safe_load

Expand Down Expand Up @@ -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)

0 comments on commit 7951248

Please sign in to comment.