Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/luna/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from dataclasses import dataclass, asdict
from pathlib import Path
from string import Formatter
import json

DEFAULT_EXTENSIONS = (".mp3", ".flac", ".m4a", ".aac", ".ogg", ".opus", ".wav", ".wma")
_CONFIG_FIELDS = {"roots", "extensions", "ignored_paths", "max_workers", "filename_template"}
_SUPPORTED_FILENAME_FIELDS = {"track", "artist", "title", "album", "album_artist", "disc", "year", "genre", "format"}


@dataclass
Expand All @@ -19,6 +21,20 @@ def defaults(cls):
return cls([], list(DEFAULT_EXTENSIONS), [])


def _validate_filename_template(template: str) -> None:
formatter = Formatter()
try:
parts = list(formatter.parse(template))
except ValueError as exc:
raise ValueError(f"Configuration 'filename_template' is invalid: {exc}") from exc

for _, field_name, format_spec, _ in parts:
if field_name is not None and field_name not in _SUPPORTED_FILENAME_FIELDS:
raise ValueError(f"Configuration 'filename_template' uses unsupported field: {field_name}")
if format_spec:
_validate_filename_template(format_spec)


def validate_profile(profile: LibraryProfile) -> LibraryProfile:
if not isinstance(profile.roots, list) or not all(isinstance(value, str) and value.strip() for value in profile.roots):
raise ValueError("Configuration 'roots' must be a list of non-empty strings.")
Expand All @@ -30,6 +46,7 @@ def validate_profile(profile: LibraryProfile) -> LibraryProfile:
raise ValueError("Configuration 'max_workers' must be a positive integer.")
if not isinstance(profile.filename_template, str) or not profile.filename_template.strip():
raise ValueError("Configuration 'filename_template' must be a non-empty string.")
_validate_filename_template(profile.filename_template)
return profile


Expand Down
16 changes: 16 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ def test_invalid_filename_template_is_rejected(self):
with self.assertRaisesRegex(ValueError, "filename_template.*non-empty string"):
validate_profile(LibraryProfile([], [".mp3"], [], filename_template=""))

def test_all_supported_filename_placeholders_are_valid(self):
template = "{track} {artist} {title} {album} {album_artist} {disc} {year} {genre} {format}"
self.assertEqual(validate_profile(LibraryProfile([], [".mp3"], [], filename_template=template)).filename_template, template)

def test_unsupported_filename_placeholder_is_rejected(self):
with self.assertRaisesRegex(ValueError, "filename_template.*unsupported field.*banana"):
validate_profile(LibraryProfile([], [".mp3"], [], filename_template="{banana}"))

def test_malformed_filename_template_is_rejected(self):
with self.assertRaisesRegex(ValueError, "filename_template.*invalid"):
validate_profile(LibraryProfile([], [".mp3"], [], filename_template="{title"))

def test_nested_unsupported_filename_placeholder_is_rejected(self):
with self.assertRaisesRegex(ValueError, "filename_template.*unsupported field.*width"):
validate_profile(LibraryProfile([], [".mp3"], [], filename_template="{title:{width}}"))

def test_unknown_configuration_fields_are_rejected(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "config.json"
Expand Down
Loading