From 8acd02ba77cf72ced02e72342cd18ba91e2035d3 Mon Sep 17 00:00:00 2001 From: Kinuax Date: Tue, 7 May 2024 13:28:23 +0200 Subject: [PATCH] Ensure creation of directories --- rolabesti/config/__init__.py | 2 +- rolabesti/config/settings.py | 15 ++++++++++----- rolabesti/config/utils.py | 9 +++++++++ rolabesti/controllers/controller.py | 5 +---- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/rolabesti/config/__init__.py b/rolabesti/config/__init__.py index 4d7dc3b..a92e7f5 100644 --- a/rolabesti/config/__init__.py +++ b/rolabesti/config/__init__.py @@ -1,2 +1,2 @@ -from .settings import max_overlap_length, tinydb_directory, tinydb_file +from .settings import max_overlap_length, tinydb_file from .utils import get_settings, reset_settings, store_settings diff --git a/rolabesti/config/settings.py b/rolabesti/config/settings.py index a6682fb..b0249e4 100644 --- a/rolabesti/config/settings.py +++ b/rolabesti/config/settings.py @@ -10,14 +10,19 @@ TomlConfigSettingsSource, ) +from .utils import create_directories from rolabesti import __app_name__ from rolabesti.models import Sortings max_overlap_length = 30 -tinydb_directory = user_data_path(__app_name__) -tinydb_file = tinydb_directory / "tracks.json" -toml_file = user_config_path(__app_name__) / "config.toml" +copy_path = user_documents_path() +music_path = user_music_path() +tinydb_path = user_data_path(__app_name__) +toml_path = user_config_path(__app_name__) +create_directories([copy_path, music_path, tinydb_path, toml_path]) +tinydb_file = tinydb_path / "tracks.json" +toml_file = toml_path / "config.toml" class Databases(str, Enum): @@ -31,8 +36,8 @@ class Settings(BaseSettings): max_tracklist_length: NonNegativeInt = 60 sorting: Sortings = Sortings.random overlap_length: int = Field(3, ge=0, le=max_overlap_length) - music_directory: DirectoryPath = user_music_path() - copy_directory: DirectoryPath = user_documents_path() + music_directory: DirectoryPath = music_path + copy_directory: DirectoryPath = copy_path database: Databases = Databases.tinydb model_config = SettingsConfigDict( toml_file=toml_file, diff --git a/rolabesti/config/utils.py b/rolabesti/config/utils.py index d728827..09e36a1 100644 --- a/rolabesti/config/utils.py +++ b/rolabesti/config/utils.py @@ -1,4 +1,5 @@ from functools import lru_cache +from pathlib import Path from pydantic import ValidationError from tomlkit import dumps @@ -6,6 +7,14 @@ from .settings import Settings, toml_file +def create_directories(directories: list[Path]) -> None: + # Ensure directories are created. + for path in directories: + if not path.exists(): + path.mkdir(parents=True) + print(f"created {path}") # TODO remove + + @lru_cache def get_settings() -> Settings: """Return the current settings.""" diff --git a/rolabesti/controllers/controller.py b/rolabesti/controllers/controller.py index 0d61e3f..e3962f5 100644 --- a/rolabesti/controllers/controller.py +++ b/rolabesti/controllers/controller.py @@ -1,7 +1,6 @@ -import os from abc import ABC, abstractmethod -from rolabesti.config import get_settings, tinydb_directory, tinydb_file +from rolabesti.config import get_settings, tinydb_file from rolabesti.database import TinyDB from rolabesti.logger import Logger @@ -14,8 +13,6 @@ def __init__(self, parameters: dict) -> None: self.parameters = parameters match settings.database: case "tinydb": - if not tinydb_directory.exists(): - os.mkdir(tinydb_directory) self.db = TinyDB(tinydb_file) self.logger = Logger()