Add Library

This commit is contained in:
Jakob Moser 2025-05-18 21:32:17 +02:00
parent ee91e2016b
commit a503e67c93
Signed by: jakob
GPG Key ID: 3EF2BA2851B3F53C

33
karaokatalog/Library.py Normal file
View File

@ -0,0 +1,33 @@
from pathlib import Path
from typing import Self
from dataclasses import dataclass
from collections.abc import Sequence
from tqdm import tqdm
from karaokatalog.Song import Song
@dataclass(frozen=True)
class Library:
songs: Sequence[Song]
unparseable_song_txts: Sequence[Path]
@classmethod
def from_dir(cls, library_dir: Path) -> Self:
"""
Load a library from a directory by recursively finding all txt files in there
and interpreting them as UltraStar Song TXT files.
"""
maybe_songs_by_path = {
song_txt: Song.from_song_txt(song_txt)
for song_txt in tqdm(library_dir.rglob("*.txt"), unit=" songs")
}
songs = tuple(song for song in maybe_songs_by_path.values() if song)
unparseable_song_txts = tuple(
song_txt for song_txt, song in maybe_songs_by_path.items() if not song
)
return cls(songs, unparseable_song_txts)