From 6af11b563f17ea50091119649388adf43b52073d Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Thu, 23 Jul 2015 16:36:35 +0200 Subject: [PATCH] playlists: Add parse() that detects format and parses --- mopidy/internal/playlists.py | 13 +++++++++++++ tests/internal/test_playlists.py | 19 +++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/mopidy/internal/playlists.py b/mopidy/internal/playlists.py index ace7eaba..f339b357 100644 --- a/mopidy/internal/playlists.py +++ b/mopidy/internal/playlists.py @@ -14,6 +14,19 @@ except ImportError: import xml.etree.ElementTree as elementtree +def parse(data): + handlers = { + detect_m3u_header: parse_m3u, + detect_pls_header: parse_pls, + detect_asx_header: parse_asx, + detect_xspf_header: parse_xspf, + } + for detector, parser in handlers.items(): + if detector(data): + return list(parser(data)) + return [] + + def detect_m3u_header(data): return data[0:7].upper() == b'#EXTM3U' diff --git a/tests/internal/test_playlists.py b/tests/internal/test_playlists.py index dfe2b71b..c53a33af 100644 --- a/tests/internal/test_playlists.py +++ b/tests/internal/test_playlists.py @@ -4,6 +4,8 @@ from __future__ import absolute_import, unicode_literals import unittest +import pytest + from mopidy.internal import playlists @@ -75,6 +77,20 @@ XSPF = b""" """ +EXPECTED = [b'file:///tmp/foo', b'file:///tmp/bar', b'file:///tmp/baz'] + + +@pytest.mark.parametrize('data,result', [ + (BAD, []), + (M3U, EXPECTED), + (PLS, EXPECTED), + (ASX, EXPECTED), + (SIMPLE_ASX, EXPECTED), + (XSPF, EXPECTED), +]) +def test_parse(data, result): + assert playlists.parse(data) == result + class BasePlaylistTest(object): valid = None @@ -90,8 +106,7 @@ class BasePlaylistTest(object): def test_parse_valid_playlist(self): uris = list(self.parse(self.valid)) - expected = [b'file:///tmp/foo', b'file:///tmp/bar', b'file:///tmp/baz'] - self.assertEqual(uris, expected) + self.assertEqual(uris, EXPECTED) def test_parse_invalid_playlist(self): uris = list(self.parse(self.invalid))