Skip to content

Commit

Permalink
Ensure creation of directories
Browse files Browse the repository at this point in the history
  • Loading branch information
kinuax committed May 7, 2024
1 parent 12e8ced commit 8acd02b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion rolabesti/config/__init__.py
Original file line number Diff line number Diff line change
@@ -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
15 changes: 10 additions & 5 deletions rolabesti/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions rolabesti/config/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
from functools import lru_cache
from pathlib import Path

from pydantic import ValidationError
from tomlkit import dumps

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."""
Expand Down
5 changes: 1 addition & 4 deletions rolabesti/controllers/controller.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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()

Expand Down

0 comments on commit 8acd02b

Please sign in to comment.