diff --git a/mopidy/utils.py b/mopidy/utils.py index 754517a7..59752b93 100644 --- a/mopidy/utils.py +++ b/mopidy/utils.py @@ -119,20 +119,26 @@ def parse_m3u(file_path): uris = [] folder = os.path.dirname(file_path) - with open(file_path) as m3u: - for line in m3u.readlines(): - line = line.strip().decode('latin1') + try: + with open(file_path) as m3u: + contents = m3u.readlines() + except IOError, e: + logger.error('Couldn\'t open m3u: %s', e) + return uris - if line.startswith('#'): - continue + for line in contents: + line = line.strip().decode('latin1') - # FIXME what about other URI types? - if line.startswith('file://'): - uris.append(line) - else: - path = os.path.join(folder, line) - path = urllib.pathname2url(path.encode('utf-8')) - uris.append('file://' + path) + if line.startswith('#'): + continue + + # FIXME what about other URI types? + if line.startswith('file://'): + uris.append(line) + else: + path = os.path.join(folder, line) + path = urllib.pathname2url(path.encode('utf-8')) + uris.append('file://' + path) return uris diff --git a/tests/utils_test.py b/tests/utils_test.py index 2258cc10..63e48201 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -58,6 +58,11 @@ class M3UToUriTest(unittest.TestCase): uris = parse_m3u(data_folder('encoding.m3u')) self.assertEqual([encoded_uri], uris) + def test_open_missing_file(self): + uris = parse_m3u(data_folder('non-existant.m3u')) + self.assertEqual([], uris) + + class URItoM3UTest(unittest.TestCase): pass