Update parse_m3u to allow caller to decide what location playlist is relative to.
This commit is contained in:
parent
c2e1b0d672
commit
6cc57701f9
@ -7,7 +7,7 @@ import shutil
|
||||
from pykka.actor import ThreadingActor
|
||||
from pykka.registry import ActorRegistry
|
||||
|
||||
from mopidy import audio, core, settings, DATA_PATH
|
||||
from mopidy import audio, core, settings
|
||||
from mopidy.backends import base
|
||||
from mopidy.models import Playlist, Track, Album
|
||||
|
||||
@ -88,7 +88,7 @@ class LocalStoredPlaylistsProvider(base.BaseStoredPlaylistsProvider):
|
||||
for m3u in glob.glob(os.path.join(self._folder, '*.m3u')):
|
||||
name = os.path.basename(m3u)[:-len('.m3u')]
|
||||
tracks = []
|
||||
for uri in parse_m3u(m3u):
|
||||
for uri in parse_m3u(m3u, settings.LOCAL_MUSIC_PATH):
|
||||
try:
|
||||
tracks.append(self.backend.library.lookup(uri))
|
||||
except LookupError, e:
|
||||
|
||||
@ -7,7 +7,7 @@ from mopidy.models import Track, Artist, Album
|
||||
from mopidy.utils import locale_decode
|
||||
from mopidy.utils.path import path_to_uri
|
||||
|
||||
def parse_m3u(file_path):
|
||||
def parse_m3u(file_path, music_folder):
|
||||
"""
|
||||
Convert M3U file list of uris
|
||||
|
||||
@ -29,8 +29,6 @@ def parse_m3u(file_path):
|
||||
"""
|
||||
|
||||
uris = []
|
||||
folder = os.path.dirname(file_path)
|
||||
|
||||
try:
|
||||
with open(file_path) as m3u:
|
||||
contents = m3u.readlines()
|
||||
@ -48,7 +46,7 @@ def parse_m3u(file_path):
|
||||
if line.startswith('file://'):
|
||||
uris.append(line)
|
||||
else:
|
||||
path = path_to_uri(folder, line)
|
||||
path = path_to_uri(music_folder, line)
|
||||
uris.append(path)
|
||||
|
||||
return uris
|
||||
|
||||
@ -9,6 +9,7 @@ from mopidy.models import Track, Artist, Album
|
||||
|
||||
from tests import unittest, path_to_data_dir
|
||||
|
||||
data_dir = path_to_data_dir('')
|
||||
song1_path = path_to_data_dir('song1.mp3')
|
||||
song2_path = path_to_data_dir('song2.mp3')
|
||||
encoded_path = path_to_data_dir(u'æøå.mp3')
|
||||
@ -21,22 +22,32 @@ encoded_uri = path_to_uri(encoded_path)
|
||||
|
||||
class M3UToUriTest(unittest.TestCase):
|
||||
def test_empty_file(self):
|
||||
uris = parse_m3u(path_to_data_dir('empty.m3u'))
|
||||
uris = parse_m3u(path_to_data_dir('empty.m3u'), data_dir)
|
||||
self.assertEqual([], uris)
|
||||
|
||||
def test_basic_file(self):
|
||||
uris = parse_m3u(path_to_data_dir('one.m3u'))
|
||||
uris = parse_m3u(path_to_data_dir('one.m3u'), data_dir)
|
||||
self.assertEqual([song1_uri], uris)
|
||||
|
||||
def test_file_with_comment(self):
|
||||
uris = parse_m3u(path_to_data_dir('comment.m3u'))
|
||||
uris = parse_m3u(path_to_data_dir('comment.m3u'), data_dir)
|
||||
self.assertEqual([song1_uri], uris)
|
||||
|
||||
def test_file_is_relative_to_correct_folder(self):
|
||||
with tempfile.NamedTemporaryFile(delete=False) as tmp:
|
||||
tmp.write('song1.mp3')
|
||||
try:
|
||||
uris = parse_m3u(tmp.name, data_dir)
|
||||
self.assertEqual([song1_uri], uris)
|
||||
finally:
|
||||
if os.path.exists(tmp.name):
|
||||
os.remove(tmp.name)
|
||||
|
||||
def test_file_with_absolute_files(self):
|
||||
with tempfile.NamedTemporaryFile(delete=False) as tmp:
|
||||
tmp.write(song1_path)
|
||||
try:
|
||||
uris = parse_m3u(tmp.name)
|
||||
uris = parse_m3u(tmp.name, data_dir)
|
||||
self.assertEqual([song1_uri], uris)
|
||||
finally:
|
||||
if os.path.exists(tmp.name):
|
||||
@ -48,29 +59,28 @@ class M3UToUriTest(unittest.TestCase):
|
||||
tmp.write('# comment \n')
|
||||
tmp.write(song2_path)
|
||||
try:
|
||||
uris = parse_m3u(tmp.name)
|
||||
uris = parse_m3u(tmp.name, data_dir)
|
||||
self.assertEqual([song1_uri, song2_uri], uris)
|
||||
finally:
|
||||
if os.path.exists(tmp.name):
|
||||
os.remove(tmp.name)
|
||||
|
||||
|
||||
def test_file_with_uri(self):
|
||||
with tempfile.NamedTemporaryFile(delete=False) as tmp:
|
||||
tmp.write(song1_uri)
|
||||
try:
|
||||
uris = parse_m3u(tmp.name)
|
||||
uris = parse_m3u(tmp.name, data_dir)
|
||||
self.assertEqual([song1_uri], uris)
|
||||
finally:
|
||||
if os.path.exists(tmp.name):
|
||||
os.remove(tmp.name)
|
||||
|
||||
def test_encoding_is_latin1(self):
|
||||
uris = parse_m3u(path_to_data_dir('encoding.m3u'))
|
||||
uris = parse_m3u(path_to_data_dir('encoding.m3u'), data_dir)
|
||||
self.assertEqual([encoded_uri], uris)
|
||||
|
||||
def test_open_missing_file(self):
|
||||
uris = parse_m3u(path_to_data_dir('non-existant.m3u'))
|
||||
uris = parse_m3u(path_to_data_dir('non-existant.m3u'), data_dir)
|
||||
self.assertEqual([], uris)
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user