From ce3d23f8cc7e38de294c6dc86dcdcf5d29deba5c Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Mon, 26 Apr 2010 22:29:21 +0200 Subject: [PATCH] Add m3u tests --- mopidy/utils.py | 23 ++++++++++++-- tests/data/comment.m3u | 2 ++ tests/data/empty.m3u | 0 tests/data/encoding.m3u | 1 + tests/data/one.m3u | 1 + tests/utils_test.py | 66 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 tests/data/comment.m3u create mode 100644 tests/data/empty.m3u create mode 100644 tests/data/encoding.m3u create mode 100644 tests/data/one.m3u create mode 100644 tests/utils_test.py diff --git a/mopidy/utils.py b/mopidy/utils.py index a37aa6f1..0275e055 100644 --- a/mopidy/utils.py +++ b/mopidy/utils.py @@ -2,6 +2,7 @@ import logging from multiprocessing.reduction import reduce_connection import os import pickle +import urllib logger = logging.getLogger('mopidy.utils') @@ -109,7 +110,25 @@ def m3u_to_uris(file_path): - Relative paths of songs should be with respect to location of M3U. - Paths are normaly platform specific. - Lines starting with # should be ignored. - - m3u files are latin-1, m3u8 files UTF-8 + - m3u files are latin-1. - This function does not bother with Extended M3U directives. """ - pass + + uris = [] + folder = os.path.dirname(file_path) + + with open(file_path) as m3u: + for line in m3u.readlines(): + line = line.strip().decode('latin1') + + if line.startswith('#'): + continue + + if line.startswith('file:'): + uris.append(line) + else: + file = os.path.join(folder, line) + path = urllib.pathname2url(file.encode('utf-8')) + uris.append('file:' + path) + + return uris diff --git a/tests/data/comment.m3u b/tests/data/comment.m3u new file mode 100644 index 00000000..af37f706 --- /dev/null +++ b/tests/data/comment.m3u @@ -0,0 +1,2 @@ +# test +song1.mp3 diff --git a/tests/data/empty.m3u b/tests/data/empty.m3u new file mode 100644 index 00000000..e69de29b diff --git a/tests/data/encoding.m3u b/tests/data/encoding.m3u new file mode 100644 index 00000000..383aa526 --- /dev/null +++ b/tests/data/encoding.m3u @@ -0,0 +1 @@ +æøå.mp3 diff --git a/tests/data/one.m3u b/tests/data/one.m3u new file mode 100644 index 00000000..c7a3d081 --- /dev/null +++ b/tests/data/one.m3u @@ -0,0 +1 @@ +song1.mp3 diff --git a/tests/utils_test.py b/tests/utils_test.py new file mode 100644 index 00000000..4531745d --- /dev/null +++ b/tests/utils_test.py @@ -0,0 +1,66 @@ +#encoding: utf-8 + +import os +import tempfile +import unittest +import urllib + +from mopidy.utils import m3u_to_uris + +def data(name): + folder = os.path.dirname(__file__) + folder = os.path.join(folder, 'data') + folder = os.path.abspath(folder) + return os.path.join(folder, name) + + +song1_path = data('song1.mp3') +song2_path = data('song2.mp3') +encoded_path = data(u'æøå.mp3') +song1_uri = 'file:' + urllib.pathname2url(song1_path) +song2_uri = 'file:' + urllib.pathname2url(song2_path) +encoded_uri = 'file:' + urllib.pathname2url(encoded_path.encode('utf-8')) + + +class M3UToUriTest(unittest.TestCase): + def test_empty_file(self): + uris = m3u_to_uris(data('empty.m3u')) + self.assertEqual([], uris) + + def test_basic_file(self): + uris = m3u_to_uris(data('one.m3u')) + self.assertEqual([song1_uri], uris) + + def test_file_with_comment(self): + uris = m3u_to_uris(data('comment.m3u')) + self.assertEqual([song1_uri], uris) + + def test_file_with_absolute_files(self): + with tempfile.NamedTemporaryFile() as file: + file.write(song1_path) + file.flush() + + uris = m3u_to_uris(file.name) + self.assertEqual([song1_uri], uris) + + def test_file_with_multiple_absolute_files(self): + with tempfile.NamedTemporaryFile() as file: + file.write(song1_path+'\n') + file.write('# comment \n') + file.write(song2_path) + file.flush() + + uris = m3u_to_uris(file.name) + self.assertEqual([song1_uri, song2_uri], uris) + + def test_file_with_uri(self): + with tempfile.NamedTemporaryFile() as file: + file.write(song1_uri) + file.flush() + + uris = m3u_to_uris(file.name) + self.assertEqual([song1_uri], uris) + + def test_encoding_is_latin1(self): + uris = m3u_to_uris(data('encoding.m3u')) + self.assertEqual([encoded_uri], uris)