From 29de697a8df6ea09c02639bc50d87315683d3f15 Mon Sep 17 00:00:00 2001 From: David Logie Date: Sat, 24 Jan 2015 12:56:25 +0000 Subject: [PATCH] Fix bug where playlists were not created in subdirs. Playlist names containing path separators would cause beets to crash if the parent directories for the playlist didn't already exist. --- beetsplug/importfeeds.py | 3 ++- beetsplug/smartplaylist.py | 3 ++- test/test_importfeeds.py | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/beetsplug/importfeeds.py b/beetsplug/importfeeds.py index 40d036832..027532ff9 100644 --- a/beetsplug/importfeeds.py +++ b/beetsplug/importfeeds.py @@ -21,7 +21,7 @@ import os import re from beets.plugins import BeetsPlugin -from beets.util import normpath, syspath, bytestring_path +from beets.util import mkdirall, normpath, syspath, bytestring_path from beets import config M3U_DEFAULT_NAME = 'imported.m3u' @@ -57,6 +57,7 @@ def _build_m3u_filename(basename): def _write_m3u(m3u_path, items_paths): """Append relative paths to items into m3u file. """ + mkdirall(m3u_path) with open(syspath(m3u_path), 'a') as f: for path in items_paths: f.write(path + '\n') diff --git a/beetsplug/smartplaylist.py b/beetsplug/smartplaylist.py index 6563e60db..ad9eed2d5 100644 --- a/beetsplug/smartplaylist.py +++ b/beetsplug/smartplaylist.py @@ -18,7 +18,7 @@ from __future__ import print_function from beets.plugins import BeetsPlugin from beets import ui -from beets.util import normpath, syspath +from beets.util import mkdirall, normpath, syspath import os @@ -98,6 +98,7 @@ class SmartPlaylistPlugin(BeetsPlugin): # Now iterate through the m3us that we need to generate for m3u in m3us: m3u_path = normpath(os.path.join(playlist_dir, m3u)) + mkdirall(m3u_path) with open(syspath(m3u_path), 'w') as f: for path in m3us[m3u]: f.write(path + '\n') diff --git a/test/test_importfeeds.py b/test/test_importfeeds.py index f9646c175..464470ebd 100644 --- a/test/test_importfeeds.py +++ b/test/test_importfeeds.py @@ -37,6 +37,22 @@ class ImportfeedsTestTest(unittest.TestCase): with open(playlist_path) as playlist: self.assertIn(item_path, playlist.read()) + def test_playlist_in_subdir(self): + config['importfeeds']['formats'] = 'm3u' + config['importfeeds']['m3u_name'] = 'subdir/imported.m3u' + album = Album(album='album/name', id=1) + item_path = os.path.join('path', 'to', 'item') + item = Item(title='song', album_id=1, path=item_path) + self.lib.add(album) + self.lib.add(item) + + self.importfeeds.album_imported(self.lib, album) + playlist = os.path.join(self.feeds_dir, + config['importfeeds']['m3u_name'].get()) + playlist_subdir = os.path.dirname(playlist) + self.assertTrue(os.path.isdir(playlist_subdir)) + self.assertTrue(os.path.isfile(playlist)) + def suite(): return unittest.TestLoader().loadTestsFromName(__name__)