Got some more things refactored so they actually work

This commit is contained in:
Sumner Evans
2020-04-06 08:47:19 -06:00
parent bd7336c96f
commit a695be3340
5 changed files with 50 additions and 57 deletions

View File

@@ -959,7 +959,7 @@ class SublimeMusicApp(Gtk.Application):
def do_play_song(song: Child):
uri, stream = CacheManager.get_song_filename_or_stream(
song,
force_stream=self.app_config.state.config.always_stream,
force_stream=self.app_config.always_stream,
)
# Prevent it from doing the thing where it continually loads
# songs when it has to download.

View File

@@ -32,6 +32,18 @@ from typing import (
import requests
from fuzzywuzzy import fuzz
try:
import gi
gi.require_version('NM', '1.0')
from gi.repository import NM
networkmanager_imported = True
except Exception:
# I really don't care what kind of exception it is, all that matters is the
# import failed for some reason.
logging.warning(
'Unable to import NM from GLib. Detection of SSID will be disabled.')
networkmanager_imported = False
from .config import AppConfiguration
from .server import Server
from .server.api_object import APIObject
@@ -300,11 +312,7 @@ class CacheManager(metaclass=Singleton):
download_set_lock = threading.Lock()
current_downloads: Set[str] = set()
def __init__(
self,
app_config: AppConfiguration,
current_ssids: Set[str],
):
def __init__(self, app_config: AppConfiguration):
self.app_config = app_config
assert self.app_config.server is not None
self.app_config.server
@@ -312,7 +320,7 @@ class CacheManager(metaclass=Singleton):
# If connected to the "Local Network SSID", use the "Local Network
# Address" instead of the "Server Address".
hostname = self.app_config.server.server_address
if self.app_config.server.local_network_ssid in current_ssids:
if self.app_config.server.local_network_ssid in self.current_ssids:
hostname = self.app_config.server.local_network_address
self.server = Server(
@@ -327,6 +335,29 @@ class CacheManager(metaclass=Singleton):
self.load_cache_info()
@property
def current_ssids(self) -> Set[str]:
if not networkmanager_imported:
return set()
self.networkmanager_client = NM.Client.new()
self.nmclient_initialized = False
self._current_ssids: Set[str] = set()
if not self.nmclient_initialized:
# Only look at the active WiFi connections.
for ac in self.networkmanager_client.get_active_connections():
if ac.get_connection_type() != '802-11-wireless':
continue
devs = ac.get_devices()
if len(devs) != 1:
continue
if devs[0].get_device_type() != NM.DeviceType.WIFI:
continue
self._current_ssids.add(ac.get_id())
return self._current_ssids
def load_cache_info(self):
cache_meta_file = self.calculate_abs_path('.cache_meta')
@@ -1084,9 +1115,7 @@ class CacheManager(metaclass=Singleton):
raise Exception('Do not instantiate the CacheManager.')
@staticmethod
def reset(app_config: AppConfiguration, current_ssids: Set[str]):
def reset(app_config: AppConfiguration):
CacheManager._instance = CacheManager.__CacheManagerInternal(
app_config,
current_ssids,
)
app_config)
similarity_ratio.cache_clear()

View File

@@ -148,6 +148,10 @@ class AppConfiguration:
# Just ignore any errors, it is only UI state.
self._state = UIState()
# Do the import in the function to avoid circular imports.
from sublime.cache_manager import CacheManager
CacheManager.reset(self)
return self._state
@property

View File

@@ -49,10 +49,10 @@ class DBusManager:
], None],
on_set_property: Callable[
[Gio.DBusConnection, str, str, str, str, GLib.Variant], None],
get_state_and_player: Callable[[], Tuple[AppConfiguration,
get_config_and_player: Callable[[], Tuple[AppConfiguration,
Optional[Player]]],
):
self.get_state_and_player = get_state_and_player
self.get_config_and_player = get_config_and_player
self.do_on_method_call = do_on_method_call
self.on_set_property = on_set_property
self.connection = connection
@@ -179,7 +179,8 @@ class DBusManager:
def property_dict(self) -> Dict[str, Any]:
if not CacheManager.ready():
return {}
state, player = self.get_state_and_player()
config, player = self.get_config_and_player()
state = config.state
has_current_song = state.current_song is not None
has_next_song = False
if state.repeat_type in (RepeatType.REPEAT_QUEUE,

View File

@@ -1,19 +1,6 @@
import logging
from dataclasses import dataclass, field
from enum import Enum
from typing import Dict, List, Optional, Set
try:
import gi
gi.require_version('NM', '1.0')
from gi.repository import NM
networkmanager_imported = True
except Exception:
# I really don't care what kind of exception it is, all that matters is the
# import failed for some reason.
logging.warning(
'Unable to import NM from GLib. Detection of SSID will be disabled.')
networkmanager_imported = False
from typing import Dict, List, Optional
from sublime.server.api_objects import Child
@@ -74,34 +61,6 @@ class UIState:
active_playlist_id: Optional[str] = None
def __post_init__(self):
if networkmanager_imported:
self.networkmanager_client = NM.Client.new()
self.nmclient_initialized = False
self._current_ssids: Set[str] = set()
def migrate(self):
pass
@property
def current_ssids(self) -> Set[str]:
if not networkmanager_imported:
return set()
if not self.nmclient_initialized:
# Only look at the active WiFi connections.
for ac in self.networkmanager_client.get_active_connections():
if ac.get_connection_type() != '802-11-wireless':
continue
devs = ac.get_devices()
if len(devs) != 1:
continue
if devs[0].get_device_type() != NM.DeviceType.WIFI:
continue
self._current_ssids.add(ac.get_id())
return self._current_ssids
@property
def current_song(self) -> Optional[Child]:
if (not self.play_queue or self.current_song_index < 0