ext: Make get_{cache,config,data}_dir() class methods

Fixes #1275
This commit is contained in:
Stein Magnus Jodal 2015-09-04 10:20:02 +02:00
parent 5ad76abc3d
commit f655fc7009
5 changed files with 24 additions and 15 deletions

View File

@ -28,6 +28,12 @@ Bug fix release.
- Core: Fix error in :meth:`~mopidy.core.TracklistController.get_eot_tlid` - Core: Fix error in :meth:`~mopidy.core.TracklistController.get_eot_tlid`
docstring. (Fixes: :issue:`1269`) 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 - Local: Deprecate :confval:`local/data_dir` and respect
:confval:`core/data_dir` instead. This does not change the defaults for :confval:`core/data_dir` instead. This does not change the defaults for
desktop users, only system services installed from packages that properly set desktop users, only system services installed from packages that properly set

View File

@ -60,7 +60,8 @@ class Extension(object):
schema['enabled'] = config_lib.Boolean() schema['enabled'] = config_lib.Boolean()
return schema return schema
def get_cache_dir(self, config): @classmethod
def get_cache_dir(cls, config):
"""Get or create cache directory for the extension. """Get or create cache directory for the extension.
Use this directory to cache data that can safely be thrown away. 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 :param config: the Mopidy config object
:return: string :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'], 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) path.get_or_create_dir(cache_dir_path)
return 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. """Get or create configuration directory for the extension.
:param config: the Mopidy config object :param config: the Mopidy config object
:return: string :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'], 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) path.get_or_create_dir(config_dir_path)
return 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. """Get or create data directory for the extension.
Use this directory to store data that should be persistent. Use this directory to store data that should be persistent.
@ -94,9 +97,9 @@ class Extension(object):
:param config: the Mopidy config object :param config: the Mopidy config object
:returns: string :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'], 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) path.get_or_create_dir(data_dir_path)
return data_dir_path return data_dir_path

View File

@ -12,7 +12,7 @@ import tempfile
import mopidy import mopidy
from mopidy import compat, local, models from mopidy import compat, local, models
from mopidy.internal import encoding, timer 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__) logger = logging.getLogger(__name__)
@ -116,7 +116,7 @@ class JsonLibrary(local.Library):
self._browse_cache = None self._browse_cache = None
self._media_dir = config['local']['media_dir'] self._media_dir = config['local']['media_dir']
self._json_file = os.path.join( 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) storage.check_dirs_and_files(config)

View File

@ -30,7 +30,7 @@ class M3UBackend(pykka.ThreadingActor, backend.Backend):
'Could not create M3U playlists dir: %s', 'Could not create M3U playlists dir: %s',
encoding.locale_decode(error)) encoding.locale_decode(error))
else: 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.playlists = M3UPlaylistsProvider(backend=self)
self.library = M3ULibraryProvider(backend=self) self.library = M3ULibraryProvider(backend=self)

View File

@ -58,17 +58,17 @@ class TestExtension(object):
def test_get_cache_dir_raises_assertion_error(self, extension): def test_get_cache_dir_raises_assertion_error(self, extension):
config = {'core': {'cache_dir': '/tmp'}} config = {'core': {'cache_dir': '/tmp'}}
with pytest.raises(AssertionError): # ext_name not set 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): def test_get_config_dir_raises_assertion_error(self, extension):
config = {'core': {'config_dir': '/tmp'}} config = {'core': {'config_dir': '/tmp'}}
with pytest.raises(AssertionError): # ext_name not set 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): def test_get_data_dir_raises_assertion_error(self, extension):
config = {'core': {'data_dir': '/tmp'}} config = {'core': {'data_dir': '/tmp'}}
with pytest.raises(AssertionError): # ext_name not set with pytest.raises(AssertionError): # ext_name not set
extension.get_data_dir(config) ext.Extension.get_data_dir(config)
class TestLoadExtensions(object): class TestLoadExtensions(object):