# Copyright 2021 (c) Anna Schumaker. import db from gi.repository import GObject from . import metadata class Task(GObject.GObject): def run_task(self): raise NotImplementedError class CommitTask(Task): def run_task(self): db.commit() class FileTask(Task): def __init__(self, library, filepath): Task.__init__(self) self.library = library self.filepath = filepath def run_task(self): if db.track.Table.lookup(self.filepath): return with metadata.Metadata(self.filepath) as meta: artist = db.artist.Table.find(meta.artist(), meta.artistsort()) album = db.album.Table.find(artist, meta.album()) disc = db.disc.Table.find(album, meta.discnumber(), meta.discsubtitle()) year = db.year.Table.find(meta.year()) track = db.track.Table.insert(self.library, artist, album, disc, year, meta.tracknumber(), meta.length(), meta.title(), self.filepath) for genre in meta.genres(): db.genre.Map.insert(db.genre.Table.find(genre), track) db.playlist.TempMap.insert(db.playlist.Table.find("New Tracks"), track) class ImportTask(FileTask): def __init__(self, library, filepath, playcount, lastplayed): FileTask.__init__(self, library, filepath) self.playcount = playcount self.lastplayed = lastplayed def run_task(self): FileTask.run_task(self) if track := db.track.Table.lookup(self.filepath): track.last_played(self.playcount, self.lastplayed) class DirectoryTask(Task): def __init__(self, library, dirpath): Task.__init__(self) self.library = library self.dirpath = dirpath def run_task(self): res = [ ] for f in self.dirpath.iterdir(): if f.is_file(): res.append(FileTask(self.library, f)) elif f.is_dir(): res.append(DirectoryTask(self.library, f)) res.append(CommitTask()) return res