emmental/emmental/db/__init__.py
Anna Schumaker 8a9c90a7ff db: Add a base class for Playlist Tables
This is an implementation of an emmental.db.table.Table that adds
support for creating Playlists, updating playlist properties, sorting
based on playlist name, and displaying playlists with their child
playlists in a tree structure.

Additionally, I create a playlist_properties table in the database to store
properties that all playlists have.

Implements: #17 ("Save currently selected playlist")
Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
2023-04-12 10:44:34 -04:00

54 lines
1.5 KiB
Python

# Copyright 2022 (c) Anna Schumaker
"""Easily work with our underlying sqlite3 database."""
import pathlib
from gi.repository import GObject
from . import connection
from . import playlist
from . import settings
from . import table
SQL_SCRIPT = pathlib.Path(__file__).parent / "emmental.sql"
class Connection(connection.Connection):
"""Connect to the database."""
active_playlist = GObject.Property(type=playlist.Playlist)
def __init__(self):
"""Initialize a sqlite connection."""
super().__init__()
match self("PRAGMA user_version").fetchone()["user_version"]:
case 0:
with open(SQL_SCRIPT) as f:
self._sql.executescript(f.read())
self.settings = settings.Table(self)
def close(self) -> None:
"""Close the database connection."""
self.settings.stop()
super().close()
def load(self) -> None:
"""Load the database tables."""
self.settings.load()
def set_active_playlist(self, plist: playlist.Playlist) -> None:
"""Set the currently active playlist."""
if self.active_playlist is not None:
self.active_playlist.active = False
self.active_playlist = plist
if plist is not None:
plist.active = True
@GObject.Signal(arg_types=(table.Table,))
def table_loaded(self, tbl: table.Table) -> None:
"""Signal that a table has been loaded."""
tbl.loaded = True