Update m3u parser to handle missing files better

This commit is contained in:
Thomas Adamcik 2010-04-30 20:56:27 +02:00
parent fa362ce936
commit d8b6d216d3
2 changed files with 23 additions and 12 deletions

View File

@ -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

View File

@ -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