From af908f51ca6c16afbbf7ef541502ea64bd7c5408 Mon Sep 17 00:00:00 2001 From: grglcy Date: Wed, 2 Jul 2025 20:10:59 +0100 Subject: [PATCH] create dir package - create classes for common directory types --- src/dir/__init__.py | 4 +++ src/dir/album.py | 17 ++++++++++++ src/dir/artist.py | 19 +++++++++++++ src/dir/directory.py | 27 ++++++++++++++++++ src/dir/file/__init__.py | 4 +++ src/dir/file/art.py | 7 +++++ src/dir/file/file.py | 60 ++++++++++++++++++++++++++++++++++++++++ src/dir/file/miscfile.py | 7 +++++ src/dir/file/track.py | 7 +++++ src/dir/root.py | 18 ++++++++++++ 10 files changed, 170 insertions(+) create mode 100644 src/dir/__init__.py create mode 100644 src/dir/album.py create mode 100644 src/dir/artist.py create mode 100644 src/dir/directory.py create mode 100644 src/dir/file/__init__.py create mode 100644 src/dir/file/art.py create mode 100644 src/dir/file/file.py create mode 100644 src/dir/file/miscfile.py create mode 100644 src/dir/file/track.py create mode 100644 src/dir/root.py diff --git a/src/dir/__init__.py b/src/dir/__init__.py new file mode 100644 index 0000000..cbf81fd --- /dev/null +++ b/src/dir/__init__.py @@ -0,0 +1,4 @@ +from .directory import Directory +from .root import Root +from .artist import Artist +from .album import Album diff --git a/src/dir/album.py b/src/dir/album.py new file mode 100644 index 0000000..20fb30f --- /dev/null +++ b/src/dir/album.py @@ -0,0 +1,17 @@ +from .directory import Directory +from pathlib import Path +from log import Log + + +class Album(Directory): + def __init__(self, path: Path, log: Log): + super().__init__(path, log, 'ALB') + + def populate(self, log: Log) -> list: + contents = list() + for e in self.path.iterdir(): + if e.is_file(): + contents.append(self.create_file(e)) + elif e.is_dir(): + self.log.warning('POP', f"Directory {e} ignored.") + return contents \ No newline at end of file diff --git a/src/dir/artist.py b/src/dir/artist.py new file mode 100644 index 0000000..2085769 --- /dev/null +++ b/src/dir/artist.py @@ -0,0 +1,19 @@ +from log import Log + +from .directory import Directory +from .album import Album +from pathlib import Path + + +class Artist(Directory): + def __init__(self, path: Path, log: Log): + super().__init__(path, log, 'ART') + + def populate(self, log: Log) -> list: + contents = list() + for e in self.path.iterdir(): + if e.is_file(): + self.log.warning('POP', f"File {e} ignored.") + elif e.is_dir(): + contents.append(Album(e, log)) + return contents \ No newline at end of file diff --git a/src/dir/directory.py b/src/dir/directory.py new file mode 100644 index 0000000..81494d5 --- /dev/null +++ b/src/dir/directory.py @@ -0,0 +1,27 @@ +from pathlib import Path +from .file import File, Track, Art, MiscFile +from abc import ABC, abstractmethod +from log import Log, LogCat + + +class Directory(ABC): + def __init__(self, path: Path, log: Log, logcat: str): + self.path = path + self.log = LogCat(log.queue, logcat) + + self.contents = self.populate(log) + + @abstractmethod + def populate(self, log: Log) -> list: + raise NotImplementedError + + @staticmethod + def create_file(file: Path) -> File: + suffix = file.suffix + + if suffix in ['.flac']: + return Track(file) + elif suffix in ['.jpg', '.jpeg', '.png']: + return Art(file) + else: + return MiscFile(file) diff --git a/src/dir/file/__init__.py b/src/dir/file/__init__.py new file mode 100644 index 0000000..240d792 --- /dev/null +++ b/src/dir/file/__init__.py @@ -0,0 +1,4 @@ +from .file import File +from .track import Track +from .miscfile import MiscFile +from .art import Art diff --git a/src/dir/file/art.py b/src/dir/file/art.py new file mode 100644 index 0000000..fb7efd6 --- /dev/null +++ b/src/dir/file/art.py @@ -0,0 +1,7 @@ +from . import File +from pathlib import Path + + +class Art(File): + def __init__(self, location: Path): + super().__init__(location) diff --git a/src/dir/file/file.py b/src/dir/file/file.py new file mode 100644 index 0000000..272b84b --- /dev/null +++ b/src/dir/file/file.py @@ -0,0 +1,60 @@ +from pathlib import Path +from abc import ABC + + +audio_extensions = ['.flac'] +art_extensions = ['.jpg', '.jpeg', '.png'] + +class File(ABC): + def __init__(self, location: Path): + self.path = location + + def __str__(self): + return str(self.path) + + @property + def is_audio(self): + return self.extension in audio_extensions + + @property + def is_art(self): + return self.extension in art_extensions + + @property + def art_named_correctly(self): + return self.filename == "cover.jpg" + + @property + def filename(self) -> str: + return self.path.name + + @property + def stem(self) -> str: + return self.path.stem + + @property + def extension(self) -> str: + return self.path.suffix + + @property + def artist(self) -> Path: + return self.path.parent.parent + + def artist_out(self, root: Path) -> Path: + return root / self.artist.parts[-1] + + @property + def album(self) -> Path: + return self.path.parent + + def album_out(self, root: Path) -> Path: + return root /self.artist_out(root) / self.album.parts[-1] + + def artist_output(self, root: Path) -> Path: + return root / self.artist.parts[-1] + + def album_output(self, root: Path) -> Path: + return root / self.artist.parts[-1] / self.album.parts[-1] + + def output_file(self, root: Path, extension: str) -> Path: + return root / self.artist.parts[-1] / self.album.parts[-1] / f"{self.stem}.{extension}" diff --git a/src/dir/file/miscfile.py b/src/dir/file/miscfile.py new file mode 100644 index 0000000..ba4f609 --- /dev/null +++ b/src/dir/file/miscfile.py @@ -0,0 +1,7 @@ +from . import File +from pathlib import Path + + +class MiscFile(File): + def __init__(self, location: Path): + super().__init__(location) diff --git a/src/dir/file/track.py b/src/dir/file/track.py new file mode 100644 index 0000000..4c306e7 --- /dev/null +++ b/src/dir/file/track.py @@ -0,0 +1,7 @@ +from . import File +from pathlib import Path + + +class Track(File): + def __init__(self, location: Path): + super().__init__(location) diff --git a/src/dir/root.py b/src/dir/root.py new file mode 100644 index 0000000..730fe64 --- /dev/null +++ b/src/dir/root.py @@ -0,0 +1,18 @@ +from .directory import Directory +from .artist import Artist +from pathlib import Path +from log import Log + + +class Root(Directory): + def __init__(self, path: Path, log: Log): + super().__init__(path, log, 'ROOT') + + def populate(self, log: Log) -> list: + contents = list() + for e in self.path.iterdir(): + if e.is_file(): + self.log.warning('POP', f"File {e} ignored.") + elif e.is_dir(): + contents.append(Artist(e, log)) + return contents \ No newline at end of file