local: Use new config system

This commit is contained in:
Stein Magnus Jodal 2013-04-05 15:40:18 +02:00
parent c9115aa480
commit 775b276169
9 changed files with 73 additions and 84 deletions

View File

@ -16,6 +16,8 @@ class LocalBackend(pykka.ThreadingActor, base.Backend):
def __init__(self, config, audio):
super(LocalBackend, self).__init__()
self.config = config
self.library = LocalLibraryProvider(backend=self)
self.playback = base.BasePlaybackProvider(audio=audio, backend=self)
self.playlists = LocalPlaylistsProvider(backend=self)

View File

@ -2,7 +2,6 @@ from __future__ import unicode_literals
import logging
from mopidy import settings
from mopidy.backends import base
from mopidy.models import Album, SearchResult
@ -15,15 +14,17 @@ class LocalLibraryProvider(base.BaseLibraryProvider):
def __init__(self, *args, **kwargs):
super(LocalLibraryProvider, self).__init__(*args, **kwargs)
self._uri_mapping = {}
self._music_path = self.backend.config['local']['music_path']
self._playlist_path = self.backend.config['local']['playlist_path']
self._tag_cache_file = self.backend.config['local']['tag_cache_file']
self.refresh()
def refresh(self, uri=None):
tracks = parse_mpd_tag_cache(
settings.LOCAL_TAG_CACHE_FILE, settings.LOCAL_MUSIC_PATH)
tracks = parse_mpd_tag_cache(self._tag_cache_file, self._music_path)
logger.info(
'Loading tracks from %s using %s',
settings.LOCAL_MUSIC_PATH, settings.LOCAL_TAG_CACHE_FILE)
self._music_path, self._tag_cache_file)
for track in tracks:
self._uri_mapping[track.uri] = track

View File

@ -5,7 +5,6 @@ import logging
import os
import shutil
from mopidy import settings
from mopidy.backends import base, listener
from mopidy.models import Playlist
from mopidy.utils import formatting, path
@ -19,7 +18,8 @@ logger = logging.getLogger('mopidy.backends.local')
class LocalPlaylistsProvider(base.BasePlaylistsProvider):
def __init__(self, *args, **kwargs):
super(LocalPlaylistsProvider, self).__init__(*args, **kwargs)
self._path = settings.LOCAL_PLAYLIST_PATH
self._music_path = self.backend.config['local']['music_path']
self._playlist_path = self.backend.config['local']['playlist_path']
self.refresh()
def create(self, name):
@ -42,16 +42,16 @@ class LocalPlaylistsProvider(base.BasePlaylistsProvider):
return playlist
def refresh(self):
logger.info('Loading playlists from %s', self._path)
logger.info('Loading playlists from %s', self._playlist_path)
playlists = []
for m3u in glob.glob(os.path.join(self._path, '*.m3u')):
for m3u in glob.glob(os.path.join(self._playlist_path, '*.m3u')):
uri = path.path_to_uri(m3u)
name = os.path.splitext(os.path.basename(m3u))[0]
tracks = []
for track_uri in parse_m3u(m3u, settings.LOCAL_MUSIC_PATH):
for track_uri in parse_m3u(m3u, self._music_path):
try:
# TODO We must use core.library.lookup() to support tracks
# from other backends
@ -86,13 +86,13 @@ class LocalPlaylistsProvider(base.BasePlaylistsProvider):
def _get_m3u_path(self, name):
name = formatting.slugify(name)
file_path = os.path.join(self._path, name + '.m3u')
path.check_file_path_is_inside_base_dir(file_path, self._path)
file_path = os.path.join(self._playlist_path, name + '.m3u')
path.check_file_path_is_inside_base_dir(file_path, self._playlist_path)
return file_path
def _save_m3u(self, playlist):
file_path = path.uri_to_path(playlist.uri)
path.check_file_path_is_inside_base_dir(file_path, self._path)
path.check_file_path_is_inside_base_dir(file_path, self._playlist_path)
with open(file_path, 'w') as file_handle:
for track in playlist.tracks:
if track.uri.startswith('file://'):
@ -103,16 +103,18 @@ class LocalPlaylistsProvider(base.BasePlaylistsProvider):
def _delete_m3u(self, uri):
file_path = path.uri_to_path(uri)
path.check_file_path_is_inside_base_dir(file_path, self._path)
path.check_file_path_is_inside_base_dir(file_path, self._playlist_path)
if os.path.exists(file_path):
os.remove(file_path)
def _rename_m3u(self, playlist):
src_file_path = path.uri_to_path(playlist.uri)
path.check_file_path_is_inside_base_dir(src_file_path, self._path)
path.check_file_path_is_inside_base_dir(
src_file_path, self._playlist_path)
dst_file_path = self._get_m3u_path(playlist.name)
path.check_file_path_is_inside_base_dir(dst_file_path, self._path)
path.check_file_path_is_inside_base_dir(
dst_file_path, self._playlist_path)
shutil.move(src_file_path, dst_file_path)

View File

@ -1,25 +1,17 @@
from __future__ import unicode_literals
import os
import shutil
import tempfile
import pykka
from mopidy import audio, core, settings
from mopidy import audio, core
from mopidy.models import Playlist
from tests import unittest, path_to_data_dir
from tests import unittest
class PlaylistsControllerTest(object):
config = {}
def setUp(self):
settings.LOCAL_PLAYLIST_PATH = tempfile.mkdtemp()
settings.LOCAL_TAG_CACHE_FILE = path_to_data_dir('library_tag_cache')
settings.LOCAL_MUSIC_PATH = path_to_data_dir('')
self.audio = audio.DummyAudio.start().proxy()
self.backend = self.backend_class.start(
config=self.config, audio=self.audio).proxy()
@ -28,11 +20,6 @@ class PlaylistsControllerTest(object):
def tearDown(self):
pykka.ActorRegistry.stop_all()
if os.path.exists(settings.LOCAL_PLAYLIST_PATH):
shutil.rmtree(settings.LOCAL_PLAYLIST_PATH)
settings.runtime.clear()
def test_create_returns_playlist_with_name_set(self):
playlist = self.core.playlists.create('test')
self.assertEqual(playlist.name, 'test')

View File

@ -1,4 +1,5 @@
from mopidy import settings
from __future__ import unicode_literals
from mopidy.backends.local import actor
from tests import unittest, path_to_data_dir
@ -7,12 +8,10 @@ from tests.backends.base import events
class LocalBackendEventsTest(events.BackendEventsTest, unittest.TestCase):
backend_class = actor.LocalBackend
# TODO: setup config
def setUp(self):
settings.LOCAL_TAG_CACHE_FILE = path_to_data_dir('empty_tag_cache')
super(LocalBackendEventsTest, self).setUp()
def tearDown(self):
super(LocalBackendEventsTest, self).tearDown()
settings.runtime.clear()
config = {
'local': {
'music_path': path_to_data_dir(''),
'playlist_path': '',
'tag_cache_file': path_to_data_dir('empty_tag_cache'),
}
}

View File

@ -1,6 +1,5 @@
from __future__ import unicode_literals
from mopidy import settings
from mopidy.backends.local import actor
from tests import unittest, path_to_data_dir
@ -9,15 +8,10 @@ from tests.backends.base.library import LibraryControllerTest
class LocalLibraryControllerTest(LibraryControllerTest, unittest.TestCase):
backend_class = actor.LocalBackend
# TODO: setup config
def setUp(self):
settings.LOCAL_TAG_CACHE_FILE = path_to_data_dir('library_tag_cache')
settings.LOCAL_MUSIC_PATH = path_to_data_dir('')
super(LocalLibraryControllerTest, self).setUp()
def tearDown(self):
settings.runtime.clear()
super(LocalLibraryControllerTest, self).tearDown()
config = {
'local': {
'music_path': path_to_data_dir(''),
'playlist_path': '',
'tag_cache_file': path_to_data_dir('library_tag_cache'),
}
}

View File

@ -1,6 +1,5 @@
from __future__ import unicode_literals
from mopidy import settings
from mopidy.backends.local import actor
from mopidy.core import PlaybackState
from mopidy.models import Track
@ -13,17 +12,15 @@ from tests.backends.local import generate_song
class LocalPlaybackControllerTest(PlaybackControllerTest, unittest.TestCase):
backend_class = actor.LocalBackend
config = {
'local': {
'music_path': path_to_data_dir(''),
'playlist_path': '',
'tag_cache_file': path_to_data_dir('empty_tag_cache'),
}
}
tracks = [
Track(uri=generate_song(i), length=4464) for i in range(1, 4)]
# TODO: setup config
def setUp(self):
settings.LOCAL_TAG_CACHE_FILE = path_to_data_dir('empty_tag_cache')
super(LocalPlaybackControllerTest, self).setUp()
def tearDown(self):
super(LocalPlaybackControllerTest, self).tearDown()
settings.runtime.clear()
def add_track(self, path):
uri = path_to_uri(path_to_data_dir(path))

View File

@ -1,8 +1,9 @@
from __future__ import unicode_literals
import os
import shutil
import tempfile
from mopidy import settings
from mopidy.backends.local import actor
from mopidy.models import Track
from mopidy.utils.path import path_to_uri
@ -17,25 +18,34 @@ class LocalPlaylistsControllerTest(
PlaylistsControllerTest, unittest.TestCase):
backend_class = actor.LocalBackend
# TODO: setup config
config = {
'local': {
'music_path': path_to_data_dir(''),
'tag_cache_file': path_to_data_dir('library_tag_cache'),
}
}
def setUp(self):
settings.LOCAL_TAG_CACHE_FILE = path_to_data_dir('empty_tag_cache')
self.config['local']['playlist_path'] = tempfile.mkdtemp()
self.playlist_path = self.config['local']['playlist_path']
super(LocalPlaylistsControllerTest, self).setUp()
def tearDown(self):
super(LocalPlaylistsControllerTest, self).tearDown()
settings.runtime.clear()
if os.path.exists(self.playlist_path):
shutil.rmtree(self.playlist_path)
def test_created_playlist_is_persisted(self):
path = os.path.join(settings.LOCAL_PLAYLIST_PATH, 'test.m3u')
path = os.path.join(self.playlist_path, 'test.m3u')
self.assertFalse(os.path.exists(path))
self.core.playlists.create('test')
self.assertTrue(os.path.exists(path))
def test_create_slugifies_playlist_name(self):
path = os.path.join(settings.LOCAL_PLAYLIST_PATH, 'test-foo-bar.m3u')
path = os.path.join(self.playlist_path, 'test-foo-bar.m3u')
self.assertFalse(os.path.exists(path))
playlist = self.core.playlists.create('test FOO baR')
@ -43,7 +53,7 @@ class LocalPlaylistsControllerTest(
self.assertTrue(os.path.exists(path))
def test_create_slugifies_names_which_tries_to_change_directory(self):
path = os.path.join(settings.LOCAL_PLAYLIST_PATH, 'test-foo-bar.m3u')
path = os.path.join(self.playlist_path, 'test-foo-bar.m3u')
self.assertFalse(os.path.exists(path))
playlist = self.core.playlists.create('../../test FOO baR')
@ -51,8 +61,8 @@ class LocalPlaylistsControllerTest(
self.assertTrue(os.path.exists(path))
def test_saved_playlist_is_persisted(self):
path1 = os.path.join(settings.LOCAL_PLAYLIST_PATH, 'test1.m3u')
path2 = os.path.join(settings.LOCAL_PLAYLIST_PATH, 'test2-foo-bar.m3u')
path1 = os.path.join(self.playlist_path, 'test1.m3u')
path2 = os.path.join(self.playlist_path, 'test2-foo-bar.m3u')
playlist = self.core.playlists.create('test1')
@ -67,7 +77,7 @@ class LocalPlaylistsControllerTest(
self.assertTrue(os.path.exists(path2))
def test_deleted_playlist_is_removed(self):
path = os.path.join(settings.LOCAL_PLAYLIST_PATH, 'test.m3u')
path = os.path.join(self.playlist_path, 'test.m3u')
self.assertFalse(os.path.exists(path))
playlist = self.core.playlists.create('test')
@ -90,7 +100,7 @@ class LocalPlaylistsControllerTest(
self.assertEqual(track_path, contents.strip())
def test_playlists_are_loaded_at_startup(self):
playlist_path = os.path.join(settings.LOCAL_PLAYLIST_PATH, 'test.m3u')
playlist_path = os.path.join(self.playlist_path, 'test.m3u')
track = Track(uri=path_to_uri(path_to_data_dir('uri2')))
playlist = self.core.playlists.create('test')

View File

@ -1,6 +1,5 @@
from __future__ import unicode_literals
from mopidy import settings
from mopidy.backends.local import actor
from mopidy.models import Track
@ -11,14 +10,12 @@ from tests.backends.local import generate_song
class LocalTracklistControllerTest(TracklistControllerTest, unittest.TestCase):
backend_class = actor.LocalBackend
config = {
'local': {
'music_path': path_to_data_dir(''),
'playlist_path': '',
'tag_cache_file': path_to_data_dir('empty_tag_cache'),
}
}
tracks = [
Track(uri=generate_song(i), length=4464) for i in range(1, 4)]
# TODO: setup config
def setUp(self):
settings.LOCAL_TAG_CACHE_FILE = path_to_data_dir('empty_tag_cache')
super(LocalTracklistControllerTest, self).setUp()
def tearDown(self):
super(LocalTracklistControllerTest, self).tearDown()
settings.runtime.clear()