From 2b31f508c9965201fe30e7779484ae7fd66c38ce Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Sun, 29 Dec 2019 14:41:53 -0700 Subject: [PATCH] Started trying to figure out how to listen to the system bus for network changes --- sublime/app.py | 44 ++++++++++++------------- sublime/cache_manager.py | 16 +++++----- sublime/config.py | 21 ++++++------ sublime/dbus_manager.py | 46 ++++++++++++++++----------- sublime/ui/common/album_with_songs.py | 2 +- sublime/ui/settings.py | 1 - 6 files changed, 68 insertions(+), 62 deletions(-) diff --git a/sublime/app.py b/sublime/app.py index 7778460..06742fb 100644 --- a/sublime/app.py +++ b/sublime/app.py @@ -249,14 +249,14 @@ class SublimeMusicApp(Gtk.Application): return True def on_dbus_method_call( - self, - connection, - sender, - path, - interface, - method, - params, - invocation, + self, + connection, + sender, + path, + interface, + method, + params, + invocation, ): second_microsecond_conversion = 1000000 @@ -381,13 +381,13 @@ class SublimeMusicApp(Gtk.Application): invocation.return_value(method(*params) if callable(method) else None) def on_dbus_set_property( - self, - connection, - sender, - path, - interface, - property_name, - value, + self, + connection, + sender, + path, + interface, + property_name, + value, ): def change_loop(new_loop_status): self.state.repeat_type = RepeatType.from_mpris_loop_status( @@ -432,8 +432,8 @@ class SublimeMusicApp(Gtk.Application): dialog = SettingsDialog(self.window, self.state.config) result = dialog.run() if result == Gtk.ResponseType.OK: - self.state.config.show_headers = dialog.data[ - 'show_headers'].get_active() + self.state.config.port_number = int( + dialog.data['port_number'].get_text()) self.state.config.always_stream = dialog.data[ 'always_stream'].get_active() self.state.config.download_on_stream = dialog.data[ @@ -809,11 +809,11 @@ class SublimeMusicApp(Gtk.Application): lambda f: GLib.idle_add(do_update, f)) def play_song( - self, - song_index: int, - reset=False, - old_play_queue=None, - play_queue=None, + self, + song_index: int, + reset=False, + old_play_queue=None, + play_queue=None, ): # Do this the old fashioned way so that we can have access to ``reset`` # in the callback. diff --git a/sublime/cache_manager.py b/sublime/cache_manager.py index 4d180a8..db30529 100644 --- a/sublime/cache_manager.py +++ b/sublime/cache_manager.py @@ -6,6 +6,7 @@ import shutil import json import hashlib +from functools import lru_cache from collections import defaultdict from time import sleep @@ -75,6 +76,12 @@ class SongCacheStatus(Enum): DOWNLOADING = 3 +# This may end up being called a lot, so cache the similarity ratios. +@lru_cache(maxsize=8192) +def similarity_ratio(query, string): + return fuzz.partial_ratio(query.lower(), string.lower()) + + class SearchResult: _artist: Set[Union[Artist, ArtistID3]] = set() _album: Set[Union[Child, AlbumID3]] = set() @@ -100,14 +107,7 @@ class SearchResult: def _to_result(self, it, transform): all_results = sorted( - ( - ( - fuzz.partial_ratio( - self.query.lower(), - transform(x).lower(), - ), - x, - ) for x in it), + ((similarity_ratio(self.query, transform(x)), x) for x in it), key=lambda rx: rx[0], reverse=True, ) diff --git a/sublime/config.py b/sublime/config.py index 1f33e5f..aea27f7 100644 --- a/sublime/config.py +++ b/sublime/config.py @@ -16,16 +16,16 @@ class ServerConfiguration: disable_cert_verify: bool def __init__( - self, - name='Default', - server_address='http://yourhost', - local_network_address='', - local_network_ssid='', - username='', - password='', - browse_by_tags=False, - sync_enabled=True, - disable_cert_verify=False, + self, + name='Default', + server_address='http://yourhost', + local_network_address='', + local_network_ssid='', + username='', + password='', + browse_by_tags=False, + sync_enabled=True, + disable_cert_verify=False, ): self.name = name self.server_address = server_address @@ -57,7 +57,6 @@ class AppConfiguration: current_server: int = -1 _cache_location: str = '' max_cache_size_mb: int = -1 # -1 means unlimited - show_headers: bool = True # show the headers on song lists always_stream: bool = False # always stream instead of downloading songs download_on_stream: bool = True # also download when streaming a song song_play_notification: bool = True diff --git a/sublime/dbus_manager.py b/sublime/dbus_manager.py index 9b34942..c6e3c50 100644 --- a/sublime/dbus_manager.py +++ b/sublime/dbus_manager.py @@ -32,11 +32,11 @@ class DBusManager: current_state = {} def __init__( - self, - connection, - do_on_method_call, - on_set_property, - get_state_and_player, + self, + connection, + do_on_method_call, + on_set_property, + get_state_and_player, ): self.get_state_and_player = get_state_and_player self.do_on_method_call = do_on_method_call @@ -78,29 +78,37 @@ class DBusManager: dbus_name_lost, ) + def system_bus_ready(_, task): + connection = Gio.bus_get_finish(task) + print(connection.signal_subscribe( + None, 'org.freedesktop.DBus.Properties', None, None, None, + Gio.DBusSignalFlags.NONE, lambda *a: print(a), None)) + + Gio.bus_get(Gio.BusType.SYSTEM, None, system_bus_ready) + def shutdown(self): Gio.bus_unown_name(self.bus_number) def on_get_property( - self, - connection, - sender, - path, - interface, - property_name, + self, + connection, + sender, + path, + interface, + property_name, ): value = self.property_dict().get(interface, {}).get(property_name) return DBusManager.to_variant(value) def on_method_call( - self, - connection, - sender, - path, - interface, - method, - params, - invocation, + self, + connection, + sender, + path, + interface, + method, + params, + invocation, ): # TODO I don't even know if this works. if interface == 'org.freedesktop.DBus.Properties': diff --git a/sublime/ui/common/album_with_songs.py b/sublime/ui/common/album_with_songs.py index 780e2b3..228bbea 100644 --- a/sublime/ui/common/album_with_songs.py +++ b/sublime/ui/common/album_with_songs.py @@ -148,7 +148,7 @@ class AlbumWithSongs(Gtk.Box): self.album_songs = Gtk.TreeView( model=self.album_song_store, name='album-songs-list', - headers_visible=False, # TODO use the config value for this + headers_visible=False, margin_top=15, margin_left=10, margin_right=10, diff --git a/sublime/ui/settings.py b/sublime/ui/settings.py index aace4af..9b24065 100644 --- a/sublime/ui/settings.py +++ b/sublime/ui/settings.py @@ -16,7 +16,6 @@ class SettingsDialog(EditFormDialog): ), ] boolean_fields = [ - ('Show headers on song lists', 'show_headers'), ('Always stream songs', 'always_stream'), ('When streaming, also download song', 'download_on_stream'), (