create dir package

- create classes for common directory types
This commit is contained in:
George Lacey 2025-07-02 20:10:59 +01:00
parent 0889c40bb5
commit af908f51ca
10 changed files with 170 additions and 0 deletions

4
src/dir/__init__.py Normal file
View File

@ -0,0 +1,4 @@
from .directory import Directory
from .root import Root
from .artist import Artist
from .album import Album

17
src/dir/album.py Normal file
View File

@ -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

19
src/dir/artist.py Normal file
View File

@ -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

27
src/dir/directory.py Normal file
View File

@ -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)

4
src/dir/file/__init__.py Normal file
View File

@ -0,0 +1,4 @@
from .file import File
from .track import Track
from .miscfile import MiscFile
from .art import Art

7
src/dir/file/art.py Normal file
View File

@ -0,0 +1,7 @@
from . import File
from pathlib import Path
class Art(File):
def __init__(self, location: Path):
super().__init__(location)

60
src/dir/file/file.py Normal file
View File

@ -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}"

7
src/dir/file/miscfile.py Normal file
View File

@ -0,0 +1,7 @@
from . import File
from pathlib import Path
class MiscFile(File):
def __init__(self, location: Path):
super().__init__(location)

7
src/dir/file/track.py Normal file
View File

@ -0,0 +1,7 @@
from . import File
from pathlib import Path
class Track(File):
def __init__(self, location: Path):
super().__init__(location)

18
src/dir/root.py Normal file
View File

@ -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