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 = [] uris = []
folder = os.path.dirname(file_path) folder = os.path.dirname(file_path)
with open(file_path) as m3u: try:
for line in m3u.readlines(): with open(file_path) as m3u:
line = line.strip().decode('latin1') contents = m3u.readlines()
except IOError, e:
logger.error('Couldn\'t open m3u: %s', e)
return uris
if line.startswith('#'): for line in contents:
continue line = line.strip().decode('latin1')
# FIXME what about other URI types? if line.startswith('#'):
if line.startswith('file://'): continue
uris.append(line)
else: # FIXME what about other URI types?
path = os.path.join(folder, line) if line.startswith('file://'):
path = urllib.pathname2url(path.encode('utf-8')) uris.append(line)
uris.append('file://' + path) else:
path = os.path.join(folder, line)
path = urllib.pathname2url(path.encode('utf-8'))
uris.append('file://' + path)
return uris return uris

View File

@ -58,6 +58,11 @@ class M3UToUriTest(unittest.TestCase):
uris = parse_m3u(data_folder('encoding.m3u')) uris = parse_m3u(data_folder('encoding.m3u'))
self.assertEqual([encoded_uri], uris) 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): class URItoM3UTest(unittest.TestCase):
pass pass