Files
sublime-music/sublime/adapters/filesystem/adapter.py

329 lines
12 KiB
Python

import logging
import shutil
import threading
from dataclasses import asdict
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, Optional, Sequence, Tuple
from sublime import util
from sublime.adapters import api_objects as API
from . import models
from .. import CacheMissError, CachingAdapter, ConfigParamDescriptor, SongCacheStatus
class FilesystemAdapter(CachingAdapter):
"""
Defines an adapter which retrieves its data from the local filesystem.
"""
# Configuration and Initialization Properties
# ==================================================================================
@staticmethod
def get_config_parameters() -> Dict[str, ConfigParamDescriptor]:
return {
# TODO: download on play?
}
@staticmethod
def verify_configuration(config: Dict[str, Any]) -> Dict[str, Optional[str]]:
return {}
def __init__(
self, config: dict, data_directory: Path, is_cache: bool = False,
):
self.data_directory = data_directory
self.cover_art_dir = self.data_directory.joinpath("cover_art")
self.music_dir = self.data_directory.joinpath("music")
self.cover_art_dir.mkdir(parents=True, exist_ok=True)
self.music_dir.mkdir(parents=True, exist_ok=True)
self.is_cache = is_cache
self.db_write_lock: threading.Lock = threading.Lock()
database_filename = data_directory.joinpath("cache.db")
models.database.init(database_filename)
models.database.connect()
with self.db_write_lock, models.database.atomic():
models.database.create_tables(models.ALL_TABLES)
def shutdown(self):
logging.info("Shutdown complete")
# Usage and Availability Properties
# ==================================================================================
can_be_cached = False # Can't be cached (there's no need).
can_service_requests = True # Can always be used to service requests.
can_get_playlists = True
can_get_playlist_details = True
can_get_cover_art_uri = True
can_get_song_uri = True
supported_schemes = ("file",)
# Data Helper Methods
# ==================================================================================
# Data Retrieval Methods
# ==================================================================================
def get_cached_status(self, song: API.Song) -> SongCacheStatus:
# TODO: change this path to be the correct dir
relative_path = models.Song.get_by_id(song.id).path
cache_path = self.music_dir.joinpath(relative_path)
if cache_path.exists():
# TODO check if path is permanently cached
return SongCacheStatus.CACHED
return SongCacheStatus.NOT_CACHED
def get_playlists(self) -> Sequence[API.Playlist]:
playlists = list(models.Playlist.select())
if self.is_cache:
# Determine if the adapter has ingested data for get_playlists before, and
# if not, cache miss.
cache_key = CachingAdapter.CachedDataKey.PLAYLISTS
if not models.CacheInfo.get_or_none(
models.CacheInfo.cache_key == cache_key
):
raise CacheMissError(partial_data=playlists)
return playlists
def get_playlist_details(self, playlist_id: str) -> API.PlaylistDetails:
playlist = models.Playlist.get_or_none(models.Playlist.id == playlist_id)
# Handle the case that this is the ground truth adapter.
if not self.is_cache:
if not playlist:
raise Exception(f"Playlist {playlist_id} does not exist.")
return playlist
# If we haven't ingested data for this playlist before, raise a CacheMissError
# with the partial playlist data.
cache_key = CachingAdapter.CachedDataKey.PLAYLIST_DETAILS
cache_info = models.CacheInfo.get_or_none(
models.CacheInfo.cache_key == cache_key,
models.CacheInfo.params_hash == util.params_hash(playlist_id),
)
if not cache_info:
raise CacheMissError(partial_data=playlist)
return playlist
def get_cover_art_uri(self, cover_art_id: str, scheme: str) -> str:
params_hash = util.params_hash(cover_art_id)
cover_art_filename = self.cover_art_dir.joinpath(params_hash)
# Handle the case that this is the ground truth adapter.
if not self.is_cache:
if not cover_art_filename.exists:
raise Exception(f"Cover Art for {cover_art_id} does not exist.")
return str(cover_art_filename)
if not cover_art_filename.exists():
raise CacheMissError()
cache_key = CachingAdapter.CachedDataKey.COVER_ART_FILE
cache_info = models.CacheInfo.get_or_none(
models.CacheInfo.cache_key == cache_key,
models.CacheInfo.params_hash == params_hash,
)
if not cache_info:
raise CacheMissError(partial_data=str(cover_art_filename))
return str(cover_art_filename)
def get_song_uri(self, song_id: str, scheme: str, stream=False) -> str:
song = models.Song.get_or_none(song_id)
if not song:
if self.is_cache:
raise CacheMissError()
else:
raise Exception(f"Song {song_id} does not exist.")
music_filename = self.music_dir.joinpath(song.path)
# Handle the case that this is the ground truth adapter.
if not self.is_cache:
if not music_filename.exists:
raise Exception(f"Music File for song {song_id} does not exist.")
return str(music_filename)
if not music_filename.exists():
raise CacheMissError()
cache_key = CachingAdapter.CachedDataKey.SONG_FILE
cache_info = models.CacheInfo.get_or_none(
models.CacheInfo.cache_key == cache_key,
models.CacheInfo.params_hash == util.params_hash(song_id),
)
if not cache_info:
raise CacheMissError(partial_data=str(music_filename))
return str(music_filename)
# Data Ingestion Methods
# ==================================================================================
def ingest_new_data(
self,
data_key: "CachingAdapter.CachedDataKey",
params: Tuple[Any, ...],
data: Any,
):
assert self.is_cache, "FilesystemAdapter is not in cache mode!"
# Wrap the actual ingestion function in a database lock, and an atomic
# transaction.
with self.db_write_lock, models.database.atomic():
self._do_ingest_new_data(data_key, params, data)
def invalidate_data(
self, function: "CachingAdapter.CachedDataKey", params: Tuple[Any, ...]
):
assert self.is_cache, "FilesystemAdapter is not in cache mode!"
# Wrap the actual ingestion function in a database lock, and an atomic
# transaction.
with self.db_write_lock, models.database.atomic():
self._do_invalidate_data(function, params)
def delete_data(
self, function: "CachingAdapter.CachedDataKey", params: Tuple[Any, ...]
):
assert self.is_cache, "FilesystemAdapter is not in cache mode!"
# Wrap the actual ingestion function in a database lock, and an atomic
# transaction.
with self.db_write_lock, models.database.atomic():
self._do_delete_data(function, params)
def _do_ingest_new_data(
self,
data_key: "CachingAdapter.CachedDataKey",
params: Tuple[Any, ...],
data: Any,
):
params_hash = util.params_hash(*params)
models.CacheInfo.insert(
cache_key=data_key,
params_hash=params_hash,
last_ingestion_time=datetime.now(),
).on_conflict_replace().execute()
if data_key == CachingAdapter.CachedDataKey.PLAYLISTS:
models.Playlist.insert_many(
map(asdict, data)
).on_conflict_replace().execute()
models.Playlist.delete().where(
models.Playlist.id.not_in([p.id for p in data])
).execute()
elif data_key == CachingAdapter.CachedDataKey.PLAYLIST_DETAILS:
playlist_data = asdict(data)
playlist, playlist_created = models.Playlist.get_or_create(
id=playlist_data["id"], defaults=playlist_data
)
# Handle the songs.
songs = []
for song_data in playlist_data["songs"]:
song, song_created = models.Song.get_or_create(
id=song_data["id"], defaults=song_data
)
keys = ("title", "duration", "path")
if not song_created:
for key in keys:
setattr(song, key, song_data[key])
song.save()
songs.append(song)
playlist.songs = songs
del playlist_data["songs"]
# Update the values if the playlist already existed.
if not playlist_created:
for k, v in playlist_data.items():
setattr(playlist, k, v)
playlist.save()
elif data_key == CachingAdapter.CachedDataKey.COVER_ART_FILE:
# ``data`` is the filename of the tempfile in this case
shutil.copy(str(data), str(self.cover_art_dir.joinpath(params_hash)))
elif data_key == CachingAdapter.CachedDataKey.SONG_FILE:
relative_path = models.Song.get_by_id(params[0]).path
absolute_path = self.music_dir.joinpath(relative_path)
absolute_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(str(data), str(absolute_path))
def _invalidate_cover_art(self, cover_art_id: str):
models.CacheInfo.delete().where(
models.CacheInfo.cache_key == CachingAdapter.CachedDataKey.COVER_ART_FILE,
models.CacheInfo.params_hash == util.params_hash(cover_art_id),
).execute()
def _do_invalidate_data(
self, data_key: "CachingAdapter.CachedDataKey", params: Tuple[Any, ...],
):
models.CacheInfo.delete().where(
models.CacheInfo.cache_key == data_key,
models.CacheInfo.params_hash == util.params_hash(*params),
).execute()
if data_key == CachingAdapter.CachedDataKey.PLAYLIST_DETAILS:
# Invalidate the corresponding cover art.
playlist = models.Playlist.get_or_none(models.Playlist.id == params[0])
if not playlist:
return
if playlist.cover_art:
self._invalidate_cover_art(playlist.cover_art)
elif data_key == CachingAdapter.CachedDataKey.SONG_FILE:
# Invalidate the corresponding cover art.
song = models.Song.get_or_none(models.Song.id == params[0])
if not song:
return
if song.cover_art:
self._invalidate_cover_art(song.cover_art)
def _do_delete_data(
self, data_key: "CachingAdapter.CachedDataKey", params: Tuple[Any, ...],
):
# Delete it from the cache info.
models.CacheInfo.delete().where(
models.CacheInfo.cache_key == data_key,
models.CacheInfo.params_hash == util.params_hash(*params),
).execute()
def delete_cover_art(cover_art_filename):
cover_art_params_hash = util.params_hash(playlist.cover_art)
if cover_art_file := self.cover_art_dir.joinpath(cover_art_params_hash):
cover_art_file.unlink(missing_ok=True)
self._invalidate_cover_art(playlist.cover_art)
if data_key == CachingAdapter.CachedDataKey.PLAYLIST_DETAILS:
# Delete the playlist and corresponding cover art.
playlist = models.Playlist.get_or_none(models.Playlist.id == params[0])
if not playlist:
return
if playlist.cover_art:
delete_cover_art(playlist.cover_art)
playlist.delete_instance()
elif data_key == CachingAdapter.CachedDataKey.SONG_FILE:
# Delete the song and corresponding cover art.
song = models.Song.get_or_none(models.Song.id == params[0])
if not song:
return
if song.cover_art:
delete_cover_art(song.cover_art)
song.delete_instance()