convert: playlist: M3U read as bytes

- M3UFile.read() method reads in rb mode.
- M3UFile.read() method handles removal of (platform specific) line endings.
- Playlist contents and EXTM3U header is handled as bytes.
- Fix test_playlist*read* tests to encode playlist UTF-8 assert strings to
  bytes using bytestring_path() before comparision.
  - Fixture playlist_windows.m3u8 is now actually Windows formatted (\r\n + BOM)
This commit is contained in:
J0J0 Todos 2023-03-24 08:38:59 +01:00
parent a4d03ef586
commit 99231160a7
3 changed files with 14 additions and 12 deletions

View file

@ -42,18 +42,18 @@ class M3UFile():
"""Reads the m3u file from disk and sets the object's attributes."""
pl_normpath = normpath(self.path)
try:
with open(syspath(pl_normpath), "r", encoding="utf-8") as pl_file:
with open(syspath(pl_normpath), "rb") as pl_file:
raw_contents = pl_file.readlines()
except OSError as exc:
raise FilesystemError(exc, 'read', (pl_normpath, ),
traceback.format_exc())
self.extm3u = True if raw_contents[0] == "#EXTM3U\n" else False
self.extm3u = True if raw_contents[0].rstrip() == b"#EXTM3U" else False
for line in raw_contents[1:]:
if line.startswith("#"):
# Some EXTM3U comment, do something. FIXME
if line.startswith(b"#"):
# Support for specific EXTM3U comments could be added here.
continue
self.media_list.append(syspath(line, prefix=False))
self.media_list.append(normpath(line.rstrip()))
if not self.media_list:
raise EmptyPlaylistError

View file

@ -1,3 +1,3 @@
#EXTM3U
x:\This\is\å\path\to_a_file.mp3
x:\This\is\another\path\tö_a_file.mp3
#EXTM3U
x:\This\is\å\path\to_a_file.mp3
x:\This\is\another\path\tö_a_file.mp3

View file

@ -90,13 +90,14 @@ class M3UFileTest(unittest.TestCase):
)
rmtree(tempdir)
@unittest.skipIf(sys.platform == 'win32', 'win32')
def test_playlist_load_ascii(self):
"""Test loading ascii paths from a playlist file."""
the_playlist_file = path.join(RSRC, b'playlist.m3u')
m3ufile = M3UFile(the_playlist_file)
m3ufile.load()
self.assertEqual(m3ufile.media_list[0],
'/This/is/a/path/to_a_file.mp3\n')
bytestring_path('/This/is/a/path/to_a_file.mp3'))
@unittest.skipIf(sys.platform == 'win32', 'win32')
def test_playlist_load_unicode(self):
@ -105,18 +106,19 @@ class M3UFileTest(unittest.TestCase):
m3ufile = M3UFile(the_playlist_file)
m3ufile.load()
self.assertEqual(m3ufile.media_list[0],
'/This/is/å/path/to_a_file.mp3\n')
bytestring_path('/This/is/å/path/to_a_file.mp3'))
@unittest.skipUnless(sys.platform == 'win32', 'win32')
def test_playlist_load_unicode_windows(self):
"""Test loading unicode paths from a playlist file."""
the_playlist_file = path.join(RSRC, b'playlist_windows.m3u8')
winpath = path.join('x:\\', 'This', 'is', 'å', 'path', 'to_a_file.mp3')
winpath = bytestring_path(path.join(
'x:\\', 'This', 'is', 'å', 'path', 'to_a_file.mp3'))
m3ufile = M3UFile(the_playlist_file)
m3ufile.load()
self.assertEqual(
m3ufile.media_list[0],
winpath + '\n'
winpath
)
def test_playlist_load_extm3u(self):