Add --pretend option to splupdate command
from beets.plugins import BeetsPlugin
from beets import ui
from beets.util import (mkdirall, normpath, sanitize_path, syspath,
- bytestring_path, path_as_posix)
+ bytestring_path, path_as_posix, displayable_path)
from beets.library import Item, Album, parse_query_string
from beets.dbcore import OrQuery
from beets.dbcore.query import MultipleSort, ParsingError
@@ -44,6 +44,7 @@ class SmartPlaylistPlugin(BeetsPlugin):
'forward_slash': False,
'prefix': '',
'urlencode': False,
+ 'pretend_paths': False,
})
self.config['prefix'].redact = True # May contain username/password.
@@ -59,6 +60,10 @@ class SmartPlaylistPlugin(BeetsPlugin):
help='update the smart playlists. Playlist names may be '
'passed as arguments.'
)
+ spl_update.parser.add_option(
+ '-p', '--pretend', action='store_true',
+ help="display query results but don't write playlist files."
+ )
spl_update.func = self.update_cmd
return [spl_update]
@@ -84,7 +89,7 @@ class SmartPlaylistPlugin(BeetsPlugin):
else:
self._matched_playlists = self._unmatched_playlists
- self.update_playlists(lib)
+ self.update_playlists(lib, opts.pretend)
def build_queries(self):
"""
@@ -170,7 +175,11 @@ class SmartPlaylistPlugin(BeetsPlugin):
self._unmatched_playlists -= self._matched_playlists
- def update_playlists(self, lib):
+ def update_playlists(self, lib, pretend=False):
+ if pretend:
+ self._log.info("Showing query results for {0} smart playlists...",
+ len(self._matched_playlists))
+ else:
self._log.info("Updating {0} smart playlists...",
len(self._matched_playlists))
@@ -185,6 +194,9 @@ class SmartPlaylistPlugin(BeetsPlugin):
for playlist in self._matched_playlists:
name, (query, q_sort), (album_query, a_q_sort) = playlist
+ if pretend:
+ self._log.info('Results for playlist {}:', name)
+ else:
self._log.debug("Creating playlist {0}", name)
items = []
@@ -206,7 +218,12 @@ class SmartPlaylistPlugin(BeetsPlugin):
item_path = os.path.relpath(item.path, relative_to)
if item_path not in m3us[m3u_name]:
m3us[m3u_name].append(item_path)
+ if pretend and self.config['pretend_paths']:
+ print(displayable_path(item_path))
+ elif pretend:
+ print(item)
+ if not pretend:
prefix = bytestring_path(self.config['prefix'].as_str())
# Write all of the accumulated track lists to files.
for m3u in m3us:
@@ -221,4 +238,9 @@ class SmartPlaylistPlugin(BeetsPlugin):
path = bytestring_path(pathname2url(path))
f.write(prefix + path + b'\n')
- self._log.info("{0} playlists updated", len(self._matched_playlists))
+ if pretend:
+ self._log.info("Displayed results for {0} playlists",
+ len(self._matched_playlists))
+ else:
+ self._log.info("{0} playlists updated",
+ len(self._matched_playlists))
