Working on moving playlist details over to AdapterManager

This commit is contained in:
Sumner Evans
2020-04-18 20:39:03 -06:00
parent b7f5543339
commit 6bda8eeca4
13 changed files with 230 additions and 224 deletions

View File

@@ -1,10 +1,14 @@
import logging
import sqlite3
from typing import Any, Dict, List, Optional, Tuple
from dataclasses import asdict
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
from playhouse.sqliteq import SqliteQueueDatabase
from sublime.adapters.api_objects import (Playlist, PlaylistDetails)
from .. import CachingAdapter, ConfigParamDescriptor, CacheMissError
from . import database
from .. import CacheMissError, CachingAdapter, ConfigParamDescriptor
class FilesystemAdapter(CachingAdapter):
@@ -31,35 +35,14 @@ class FilesystemAdapter(CachingAdapter):
):
self.data_directory = data_directory
logging.info('Opening connection to the database.')
self.database_filename = data_directory.joinpath('.cache_meta.db')
database_connection = sqlite3.connect(
self.database_filename,
detect_types=sqlite3.PARSE_DECLTYPES,
database_filename = data_directory.joinpath('cache.db')
self.database = SqliteQueueDatabase(
database_filename,
autorollback=True,
)
# TODO extract this out eventually
c = database_connection.cursor()
c.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name='playlists';"
)
if not c.fetchone():
c.execute(
"""
CREATE TABLE playlists (
id TEXT NOT NULL UNIQUE PRIMARY KEY,
name TEXT NOT NULL,
song_count INTEGER,
duration INTEGER,
created INTEGER,
changed INTEGER,
comment TEXT,
owner TEXT, -- TODO convert to a a FK
public INT,
cover_art TEXT -- TODO convert to a FK
)
""")
c.close()
database.proxy.initialize(self.database)
self.database.connect()
self.database.create_tables(database.ALL_TABLES)
def shutdown(self):
logging.info('Shutdown complete')
@@ -78,18 +61,10 @@ class FilesystemAdapter(CachingAdapter):
can_get_playlists: bool = True
def get_playlists(self) -> List[Playlist]:
database_connection = sqlite3.connect(
self.database_filename,
detect_types=sqlite3.PARSE_DECLTYPES,
)
with database_connection:
playlists = database_connection.execute(
"""
SELECT * from playlists
""").fetchall()
return [Playlist(*p) for p in playlists]
raise CacheMissError()
playlists = list(database.Playlist.select())
if len(playlists) == 0: # TODO not necessarily a cache miss
raise CacheMissError()
return playlists
can_get_playlist_details: bool = True
@@ -107,23 +82,8 @@ class FilesystemAdapter(CachingAdapter):
params: Tuple[Any, ...],
data: Any,
):
database_connection = sqlite3.connect(
self.database_filename,
detect_types=sqlite3.PARSE_DECLTYPES,
)
with database_connection:
if function_name == 'get_playlists':
database_connection.executemany(
"""
INSERT OR IGNORE INTO playlists (id, name, song_count, duration, created, comment, owner, public, cover_art)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (id) DO
UPDATE SET id=?, name=?, song_count=?, duration=?, created=?, comment=?, owner=?, public=?, cover_art=?;
""", [
(
p.id, p.name, p.songCount, p.duration, p.created,
p.comment, p.owner, p.public, p.coverArt, p.id,
p.name, p.songCount, p.duration, p.created,
p.comment, p.owner, p.public, p.coverArt)
for p in data
])
if function_name == 'get_playlists':
(
database.Playlist.insert_many(
map(lambda p: database.Playlist(**asdict(p)),
data)).on_conflict_replace())