whatopus/src/dir/directory.py
grglcy 1c42269f92 iterate contents directly
- provide list of names
2025-07-12 13:21:10 +01:00

38 lines
909 B
Python

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)
def __iter__(self):
return self.contents.__iter__()
@property
def name(self):
return self.path.name
def by_name(self):
return [e.name for e in self.contents]
@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)