diff options
author | J0J0 T <jojo@peek-a-boo.at> | 2022-08-12 09:18:45 +0200 |
---|---|---|
committer | J0J0 Todos <jojo@peek-a-boo.at> | 2023-03-29 07:21:27 +0200 |
commit | 68240f6e03d91c622a53d02b90b0755673fd2e7f (patch) | |
tree | 21c74469090cc8dea32d35ec1a2246414602082a | |
parent | 0cbf91e4d8815f844cd560c7ab7e0e377ff8dd15 (diff) |
convert: playlist: Add EmptyPlaylistError and test
- Add and Exception class called EmptyPlaylistError ought to be raised
when playlists without files are loaded or saved.
- Add a test for it in test_m3ufile
- Fix media_files vs. media_list attribute name.
-rw-r--r-- | beets/util/__init__.py | 21 | ||||
-rw-r--r-- | test/test_m3ufile.py | 6 |
2 files changed, 19 insertions, 8 deletions
diff --git a/beets/util/__init__.py b/beets/util/__init__.py index 58eb47034..9f0460ce1 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -124,6 +124,13 @@ class FilesystemError(HumanReadableException): return f'{self._reasonstr()} {clause}' +class EmptyPlaylistError(Exception): + """An error that should be raised when a playlist file without media files + is saved or loaded. + """ + pass + + class MoveOperation(Enum): """The file operations that e.g. various move functions can carry out. """ @@ -161,25 +168,29 @@ class M3UFile(): # Some EXTM3U comment, do something. FIXME continue self.media_list.append(line) + if not self.media_list: + raise EmptyPlaylistError - def set_contents(self, media_files, extm3u=True): - """Sets self.media_files to a list of media file paths, + def set_contents(self, media_list, extm3u=True): + """Sets self.media_list to a list of media file paths, and sets additional flags, changing the final m3u-file's format. - ``media_files`` is a list of paths to media files that should be added + ``media_list`` is a list of paths to media files that should be added to the playlist (relative or absolute paths, that's the responsibility of the caller). By default the ``extm3u`` flag is set, to ensure a save-operation writes an m3u-extended playlist (comment "#EXTM3U" at the top of the file). """ - self.media_files = media_files + self.media_list = media_list self.extm3u = extm3u def write(self): """Writes the m3u file to disk.""" header = ["#EXTM3U"] if self.extm3u else [] - contents = header + self.media_files + if not self.media_list: + raise EmptyPlaylistError + contents = header + self.media_list with open(self.path, "w") as playlist_file: playlist_file.writelines('\n'.join(contents)) playlist_file.write('\n') # Final linefeed to prevent noeol file. diff --git a/test/test_m3ufile.py b/test/test_m3ufile.py index 4f5807b6b..e0ab80fa0 100644 --- a/test/test_m3ufile.py +++ b/test/test_m3ufile.py @@ -20,7 +20,7 @@ import unittest # from unittest.mock import Mock, MagicMock -from beets.util import M3UFile +from beets.util import M3UFile, EmptyPlaylistError from beets.util import syspath, bytestring_path, py3_path, CHAR_REPLACE from test._common import RSRC @@ -30,8 +30,8 @@ class M3UFileTest(unittest.TestCase): tempdir = bytestring_path(mkdtemp()) the_playlist_file = path.join(tempdir, b'playlist.m3u8') m3ufile = M3UFile(the_playlist_file) - m3ufile.write() - self.assertFalse(path.exists(the_playlist_file)) + with self.assertRaises(EmptyPlaylistError): + m3ufile.write() rmtree(tempdir) def test_playlist_write(self): |