From 1593e7e76863eb5c750b3d5b1cbc8f0e8743c483 Mon Sep 17 00:00:00 2001 From: beaverking1212 Date: Thu, 20 Apr 2023 23:27:11 +0000 Subject: [PATCH 1/5] Always cache credentials --- mopidy_spotify/backend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mopidy_spotify/backend.py b/mopidy_spotify/backend.py index d3f949cb..56e9c398 100644 --- a/mopidy_spotify/backend.py +++ b/mopidy_spotify/backend.py @@ -42,8 +42,8 @@ def __init__(self, *args, **kwargs): def on_source_setup(self, source): for prop in ["username", "password", "bitrate"]: source.set_property(prop, str(self._config[prop])) + source.set_property("cache-credentials", self._cache_location) if self._config["allow_cache"]: - source.set_property("cache-credentials", self._cache_location) source.set_property("cache-files", self._cache_location) source.set_property( "cache-max-size", self._config["cache_size"] * 1048576 From c84755083dc8308409ba2b116bfcdfbbcd72f329 Mon Sep 17 00:00:00 2001 From: beaverking1212 Date: Thu, 20 Apr 2023 23:57:07 +0000 Subject: [PATCH 2/5] Use dedicated folder for credentials cache --- mopidy_spotify/backend.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mopidy_spotify/backend.py b/mopidy_spotify/backend.py index 56e9c398..ed30f52a 100644 --- a/mopidy_spotify/backend.py +++ b/mopidy_spotify/backend.py @@ -1,3 +1,5 @@ +import os + import pykka from mopidy import backend @@ -37,12 +39,17 @@ class SpotifyPlaybackProvider(backend.PlaybackProvider): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._cache_location = Extension().get_cache_dir(self.backend._config) + self._data_location = Extension().get_data_dir(self.backend._config) self._config = self.backend._config["spotify"] def on_source_setup(self, source): for prop in ["username", "password", "bitrate"]: source.set_property(prop, str(self._config[prop])) - source.set_property("cache-credentials", self._cache_location) + credentials_dir = os.path.join(self._data_location, "credentials-cache") + if not os.path.exists(credentials_dir): + os.makedirs(self._data_location, exist_ok=True) + os.mkdir(credentials_dir, 0o700) + source.set_property("cache-credentials", credentials_dir) if self._config["allow_cache"]: source.set_property("cache-files", self._cache_location) source.set_property( From ed6d81f5cc4182d9b8438f4ccf3592610c2a489a Mon Sep 17 00:00:00 2001 From: beaverking1212 Date: Fri, 21 Apr 2023 00:13:16 +0000 Subject: [PATCH 3/5] Fix tests --- tests/test_playback.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/test_playback.py b/tests/test_playback.py index 97581795..6caeb85c 100644 --- a/tests/test_playback.py +++ b/tests/test_playback.py @@ -1,3 +1,5 @@ +import os + from unittest import mock import pytest @@ -35,12 +37,14 @@ def test_on_source_setup_sets_properties(config, provider): mock_source = mock.MagicMock() provider.on_source_setup(mock_source) spotify_cache_dir = backend.Extension.get_cache_dir(config) + spotify_data_dir = backend.Extension.get_data_dir(config) + cred_dir = os.path.join(spotify_data_dir, "credentials-cache") assert mock_source.set_property.mock_calls == [ mock.call("username", "alice"), mock.call("password", "password"), mock.call("bitrate", "160"), - mock.call("cache-credentials", spotify_cache_dir), + mock.call("cache-credentials", cred_dir), mock.call("cache-files", spotify_cache_dir), mock.call("cache-max-size", 8589934592), ] @@ -50,11 +54,14 @@ def test_on_source_setup_without_caching(config, provider): config["spotify"]["allow_cache"] = False mock_source = mock.MagicMock() provider.on_source_setup(mock_source) + spotify_data_dir = backend.Extension.get_data_dir(config) + cred_dir = os.path.join(spotify_data_dir, "credentials-cache") assert mock_source.set_property.mock_calls == [ mock.call("username", "alice"), mock.call("password", "password"), mock.call("bitrate", "160"), + mock.call("cache-credentials", cred_dir), ] From ce6eb0661a69d5a505d5e7fa2d106c53f571830c Mon Sep 17 00:00:00 2001 From: beaverking1212 Date: Fri, 21 Apr 2023 20:44:43 +0000 Subject: [PATCH 4/5] Use pathlib --- mopidy_spotify/backend.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/mopidy_spotify/backend.py b/mopidy_spotify/backend.py index ed30f52a..be79b9f6 100644 --- a/mopidy_spotify/backend.py +++ b/mopidy_spotify/backend.py @@ -1,5 +1,3 @@ -import os - import pykka from mopidy import backend @@ -42,14 +40,14 @@ def __init__(self, *args, **kwargs): self._data_location = Extension().get_data_dir(self.backend._config) self._config = self.backend._config["spotify"] + self._credentials_dir = self._data_location / "credentials-cache" + if not self._credentials_dir.exists(): + self._credentials_dir.mkdir(mode=0o700) + def on_source_setup(self, source): for prop in ["username", "password", "bitrate"]: source.set_property(prop, str(self._config[prop])) - credentials_dir = os.path.join(self._data_location, "credentials-cache") - if not os.path.exists(credentials_dir): - os.makedirs(self._data_location, exist_ok=True) - os.mkdir(credentials_dir, 0o700) - source.set_property("cache-credentials", credentials_dir) + source.set_property("cache-credentials", self._credentials_dir) if self._config["allow_cache"]: source.set_property("cache-files", self._cache_location) source.set_property( From 953f12b27f0014616d6fded73f55071fc3cf9428 Mon Sep 17 00:00:00 2001 From: beaverking1212 Date: Fri, 21 Apr 2023 20:53:20 +0000 Subject: [PATCH 5/5] Fix tests --- tests/test_playback.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/test_playback.py b/tests/test_playback.py index 6caeb85c..b8dbb570 100644 --- a/tests/test_playback.py +++ b/tests/test_playback.py @@ -1,5 +1,3 @@ -import os - from unittest import mock import pytest @@ -38,7 +36,7 @@ def test_on_source_setup_sets_properties(config, provider): provider.on_source_setup(mock_source) spotify_cache_dir = backend.Extension.get_cache_dir(config) spotify_data_dir = backend.Extension.get_data_dir(config) - cred_dir = os.path.join(spotify_data_dir, "credentials-cache") + cred_dir = spotify_data_dir / "credentials-cache" assert mock_source.set_property.mock_calls == [ mock.call("username", "alice"), @@ -55,7 +53,7 @@ def test_on_source_setup_without_caching(config, provider): mock_source = mock.MagicMock() provider.on_source_setup(mock_source) spotify_data_dir = backend.Extension.get_data_dir(config) - cred_dir = os.path.join(spotify_data_dir, "credentials-cache") + cred_dir = spotify_data_dir / "credentials-cache" assert mock_source.set_property.mock_calls == [ mock.call("username", "alice"),