From f655fc700921710e6f0b468cff3a873ce3c285eb Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Fri, 4 Sep 2015 10:20:02 +0200 Subject: [PATCH] ext: Make get_{cache,config,data}_dir() class methods Fixes #1275 --- docs/changelog.rst | 6 ++++++ mopidy/ext.py | 21 ++++++++++++--------- mopidy/local/json.py | 4 ++-- mopidy/m3u/actor.py | 2 +- tests/test_ext.py | 6 +++--- 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 1e816468..f23b68c4 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -28,6 +28,12 @@ Bug fix release. - Core: Fix error in :meth:`~mopidy.core.TracklistController.get_eot_tlid` docstring. (Fixes: :issue:`1269`) +- Extension support: Make :meth:`~mopidy.ext.Extension.get_cache_dir`, + :meth:`~mopidy.ext.Extension.get_config_dir`, and + :meth:`~mopidy.ext.Extension.get_data_dir` class methods, so they can be used + without creating an instance of the :class:`~mopidy.ext.Extension` class. + (Fixes: :issue:`1275`) + - Local: Deprecate :confval:`local/data_dir` and respect :confval:`core/data_dir` instead. This does not change the defaults for desktop users, only system services installed from packages that properly set diff --git a/mopidy/ext.py b/mopidy/ext.py index 199d7ab6..7fd68f96 100644 --- a/mopidy/ext.py +++ b/mopidy/ext.py @@ -60,7 +60,8 @@ class Extension(object): schema['enabled'] = config_lib.Boolean() return schema - def get_cache_dir(self, config): + @classmethod + def get_cache_dir(cls, config): """Get or create cache directory for the extension. Use this directory to cache data that can safely be thrown away. @@ -68,25 +69,27 @@ class Extension(object): :param config: the Mopidy config object :return: string """ - assert self.ext_name is not None + assert cls.ext_name is not None cache_dir_path = bytes(os.path.join(config['core']['cache_dir'], - self.ext_name)) + cls.ext_name)) path.get_or_create_dir(cache_dir_path) return cache_dir_path - def get_config_dir(self, config): + @classmethod + def get_config_dir(cls, config): """Get or create configuration directory for the extension. :param config: the Mopidy config object :return: string """ - assert self.ext_name is not None + assert cls.ext_name is not None config_dir_path = bytes(os.path.join(config['core']['config_dir'], - self.ext_name)) + cls.ext_name)) path.get_or_create_dir(config_dir_path) return config_dir_path - def get_data_dir(self, config): + @classmethod + def get_data_dir(cls, config): """Get or create data directory for the extension. Use this directory to store data that should be persistent. @@ -94,9 +97,9 @@ class Extension(object): :param config: the Mopidy config object :returns: string """ - assert self.ext_name is not None + assert cls.ext_name is not None data_dir_path = bytes(os.path.join(config['core']['data_dir'], - self.ext_name)) + cls.ext_name)) path.get_or_create_dir(data_dir_path) return data_dir_path diff --git a/mopidy/local/json.py b/mopidy/local/json.py index 501990ee..8e8b5b1e 100644 --- a/mopidy/local/json.py +++ b/mopidy/local/json.py @@ -12,7 +12,7 @@ import tempfile import mopidy from mopidy import compat, local, models from mopidy.internal import encoding, timer -from mopidy.local import Extension, search, storage, translator +from mopidy.local import search, storage, translator logger = logging.getLogger(__name__) @@ -116,7 +116,7 @@ class JsonLibrary(local.Library): self._browse_cache = None self._media_dir = config['local']['media_dir'] self._json_file = os.path.join( - Extension().get_data_dir(config), b'library.json.gz') + local.Extension.get_data_dir(config), b'library.json.gz') storage.check_dirs_and_files(config) diff --git a/mopidy/m3u/actor.py b/mopidy/m3u/actor.py index fc4734a2..55257f87 100644 --- a/mopidy/m3u/actor.py +++ b/mopidy/m3u/actor.py @@ -30,7 +30,7 @@ class M3UBackend(pykka.ThreadingActor, backend.Backend): 'Could not create M3U playlists dir: %s', encoding.locale_decode(error)) else: - self._playlists_dir = m3u.Extension().get_data_dir(config) + self._playlists_dir = m3u.Extension.get_data_dir(config) self.playlists = M3UPlaylistsProvider(backend=self) self.library = M3ULibraryProvider(backend=self) diff --git a/tests/test_ext.py b/tests/test_ext.py index 1a6bd538..67c2e4ec 100644 --- a/tests/test_ext.py +++ b/tests/test_ext.py @@ -58,17 +58,17 @@ class TestExtension(object): def test_get_cache_dir_raises_assertion_error(self, extension): config = {'core': {'cache_dir': '/tmp'}} with pytest.raises(AssertionError): # ext_name not set - extension.get_cache_dir(config) + ext.Extension.get_cache_dir(config) def test_get_config_dir_raises_assertion_error(self, extension): config = {'core': {'config_dir': '/tmp'}} with pytest.raises(AssertionError): # ext_name not set - extension.get_config_dir(config) + ext.Extension.get_config_dir(config) def test_get_data_dir_raises_assertion_error(self, extension): config = {'core': {'data_dir': '/tmp'}} with pytest.raises(AssertionError): # ext_name not set - extension.get_data_dir(config) + ext.Extension.get_data_dir(config) class TestLoadExtensions(object):