Got to where all I have to do is hook up the cache manager

This commit is contained in:
Sumner Evans
2020-04-06 00:05:13 -06:00
parent 8b9098b217
commit bd7336c96f
6 changed files with 33 additions and 30 deletions

View File

@@ -46,6 +46,7 @@ class SublimeMusicApp(Gtk.Application):
self.window: Optional[Gtk.Window] = None
self.app_config = AppConfiguration.load_from_file(config_file)
self.player = None
self.dbus_manager: Optional[DBusManager] = None
self.connect('shutdown', self.on_app_shutdown)
@@ -197,13 +198,13 @@ class SublimeMusicApp(Gtk.Application):
time_observer,
on_track_end,
on_player_event,
self.app_config.state.config,
self.app_config,
)
self.chromecast_player = ChromecastPlayer(
time_observer,
on_track_end,
on_player_event,
self.app_config.state.config,
self.app_config,
)
self.player = self.mpv_player
@@ -231,7 +232,7 @@ class SublimeMusicApp(Gtk.Application):
connection,
self.on_dbus_method_call,
self.on_dbus_set_property,
lambda: (self.app_config.state, getattr(self, 'player', None)),
lambda: (self.app_config, self.player),
)
return True
@@ -665,11 +666,11 @@ class SublimeMusicApp(Gtk.Application):
def on_connected_server_changed(
self,
action: Any,
current_server: GLib.Variant,
current_server_index: int,
):
if self.app_config.server:
self.app_config.save()
self.app_config.current_server = current_server
self.app_config.current_server_index = current_server_index
self.app_config.save()
self.reset_state()
@@ -880,8 +881,7 @@ class SublimeMusicApp(Gtk.Application):
def update_window(self, force: bool = False):
if not self.window:
return
GLib.idle_add(
lambda: self.window.update(self.app_config, force=force))
GLib.idle_add(lambda: self.window.update(self.app_config, force=force))
def update_play_state_from_server(self, prompt_confirm: bool = False):
# TODO (#129): need to make the up next list loading for the duration

View File

@@ -1,7 +1,7 @@
import hashlib
import os
import pickle
from dataclasses import asdict, dataclass, field
from dataclasses import asdict, dataclass, field, fields
from enum import Enum
from pathlib import Path
from typing import List, Optional
@@ -37,7 +37,7 @@ class ReplayGainType(Enum):
}[replay_gain_type.lower()]
@dataclass
@dataclass(unsafe_hash=True)
class ServerConfiguration:
name: str = 'Default'
server_address: str = 'http://yourhost'
@@ -74,7 +74,7 @@ class ServerConfiguration:
@dataclass
class AppConfiguration:
servers: List[ServerConfiguration] = field(default_factory=list)
current_server: int = -1
current_server_index: int = -1
cache_location: str = ''
max_cache_size_mb: int = -1 # -1 means unlimited
always_stream: bool = False # always stream instead of downloading songs
@@ -90,12 +90,14 @@ class AppConfiguration:
@staticmethod
def load_from_file(filename: Path) -> 'AppConfiguration':
args = {}
if filename.exists():
with open(filename, 'r') as f:
config = AppConfiguration(**yaml.load(f, Loader=yaml.CLoader))
else:
config = AppConfiguration()
field_names = {f.name for f in fields(AppConfiguration)}
args = yaml.load(f, Loader=yaml.CLoader).items()
args = dict(filter(lambda kv: kv[0] in field_names, args))
config = AppConfiguration(**args)
config.filename = filename
return config
@@ -122,8 +124,8 @@ class AppConfiguration:
@property
def server(self) -> Optional[ServerConfiguration]:
if 0 <= self.current_server < len(self.servers):
return self.servers[self.current_server]
if 0 <= self.current_server_index < len(self.servers):
return self.servers[self.current_server_index]
return None
@@ -136,12 +138,12 @@ class AppConfiguration:
# If already retrieved, and the server hasn't changed, then return the
# state. Don't use strhash because that is much more expensive of an
# operation.
if self._current_server_hash != hash(server) or self._state:
if self._current_server_hash != hash(server) or not self._state:
self._current_server_hash = hash(server)
if self.state_file_location.exists():
try:
with open(self.state_file_location, 'rb') as f:
self._state = pickle.load(f)
self._state = UIState(**pickle.load(f))
except Exception:
# Just ignore any errors, it is only UI state.
self._state = UIState()
@@ -149,21 +151,22 @@ class AppConfiguration:
return self._state
@property
def state_file_location(self):
def state_file_location(self) -> Path:
assert self.server is not None
server_hash = self.server.strhash()
state_file_location = Path(
os.environ.get('XDG_DATA_HOME') or '~/.local/share')
state_file_location = state_file_location.expanduser().joinpath(
return state_file_location.expanduser().joinpath(
'sublime-music', server_hash, 'state.pickle')
def save(self):
# Save the config as YAML.
self.filename.parent().mkdir(parents=True, exist_ok=True)
self.filename.parent.mkdir(parents=True, exist_ok=True)
with open(self.filename, 'w+') as f:
f.write(yaml.dump(asdict(self)))
# Save the state for the current server.
self.state_file_location.parent().mkdir(parents=True, exist_ok=True)
self.state_file_location.parent.mkdir(parents=True, exist_ok=True)
with open(self.state_file_location, 'wb+') as f:
pickle.dump(self.state, f)
pickle.dump(asdict(self.state), f)

View File

@@ -3,7 +3,7 @@ import os
import re
from collections import defaultdict
from typing import Any, Callable, DefaultDict, Dict, List, Tuple
from typing import Any, Callable, DefaultDict, Dict, List, Optional, Tuple
from deepdiff import DeepDiff
from gi.repository import Gio, GLib
@@ -49,7 +49,8 @@ class DBusManager:
], None],
on_set_property: Callable[
[Gio.DBusConnection, str, str, str, str, GLib.Variant], None],
get_state_and_player: Callable[[], Tuple[AppConfiguration, Player]],
get_state_and_player: Callable[[], Tuple[AppConfiguration,
Optional[Player]]],
):
self.get_state_and_player = get_state_and_player
self.do_on_method_call = do_on_method_call

View File

@@ -383,8 +383,9 @@ class AlbumsGrid(Gtk.Overlay):
):
if order_token < self.latest_applied_order_ratchet:
return
if app_config.server is None:
return
assert app_config.server is not None
new_hash = app_config.server.strhash()
server_changed = self.server_hash != new_hash
self.server_hash = new_hash

View File

@@ -95,7 +95,7 @@ class ConfigureServersDialog(Gtk.Dialog):
)
self.server_configs = config.servers
self.selected_server_index = config.current_server
self.selected_server_index = config.current_server_index
self.set_default_size(500, 300)
# Flow box to hold the server list and the buttons.

View File

@@ -73,11 +73,9 @@ class MainWindow(Gtk.ApplicationWindow):
def update(self, app_config: AppConfiguration, force: bool = False):
# Update the Connected to label on the popup menu.
if app_config.current_server >= 0:
server_name = app_config.servers[
app_config.current_server].name
if app_config.server:
self.connected_to_label.set_markup(
f'<b>Connected to {server_name}</b>')
f'<b>Connected to {app_config.server.name}</b>')
else:
self.connected_to_label.set_markup(
f'<span style="italic">Not Connected to a Server</span>')