diff --git a/beets/library.py b/beets/library.py index 2bf291c44..8d8958201 100644 --- a/beets/library.py +++ b/beets/library.py @@ -1562,17 +1562,19 @@ class Album(BaseAlbum): items, so the album must contain at least one item or item_dir must be provided. """ - image = bytestring_path(image) item_dir = item_dir or self.item_dir() if not isinstance(self._library.art_filename,Template): self._library.art_filename = Template(self._library.art_filename) - sanitized_art_filename = util.sanitize_for_path(self.evaluate_template(self._library.art_filename),os.path) - _, ext = os.path.splitext(image) + subpath = util.sanitize_path(util.sanitize_for_path( + self.evaluate_template(self._library.art_filename) + )) + subpath = bytestring_path(subpath) - dest = os.path.join(item_dir, util.sanitize_path(sanitized_art_filename) + ext) + _, ext = os.path.splitext(image) + dest = os.path.join(item_dir, subpath + ext) return bytestring_path(dest) diff --git a/beets/ui/__init__.py b/beets/ui/__init__.py index 1dbe7056e..0140d8bd5 100644 --- a/beets/ui/__init__.py +++ b/beets/ui/__init__.py @@ -516,20 +516,6 @@ def _get_path_formats(config): return path_formats -def _get_art_filename(config): - """Returns a string of album art format; reflecting - the config's specified album art filename. - """ - legacy_art_filename = config_val(config, 'beets', 'art_filename', None) - if legacy_art_filename: - # Old art filename format override the default value. - art_filename = Template(legacy_art_filename) - else: - # If no legacy art filename format, use the default instead. - art_filename = DEFAULT_ART_FILENAME - - return art_filename - def _pick_format(config=None, album=False, fmt=None): """Pick a format string for printing Album or Item objects, falling back to config options and defaults. @@ -785,7 +771,8 @@ def _raw_main(args, configfh): directory = options.directory or \ config_val(config, 'beets', 'directory', default_dir) path_formats = _get_path_formats(config) - art_filename = _get_art_filename(config) + art_filename = Template(config_val(config, 'beets', 'art_filename', + DEFAULT_ART_FILENAME)) lib_timeout = config_val(config, 'beets', 'timeout', DEFAULT_TIMEOUT) replacements = _get_replacements(config) try: diff --git a/beets/util/__init__.py b/beets/util/__init__.py index 7bb64bd24..12a338439 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -476,11 +476,13 @@ def truncate_path(path, pathmod=None, length=MAX_FILENAME_LENGTH): return pathmod.join(*out) -def sanitize_for_path(value, pathmod, key=None): +def sanitize_for_path(value, pathmod=None, key=None): """Sanitize the value for inclusion in a path: replace separators with _, etc. Doesn't guarantee that the whole path will be valid; you should still call sanitize_path on the complete path. """ + pathmod = pathmod or os.path + if isinstance(value, basestring): for sep in (pathmod.sep, pathmod.altsep): if sep: @@ -500,6 +502,7 @@ def sanitize_for_path(value, pathmod, key=None): value = u'%ikHz' % ((value or 0) // 1000) else: value = unicode(value) + return value def str2bool(value): diff --git a/docs/changelog.rst b/docs/changelog.rst index a655cd39b..8b9544769 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -35,6 +35,8 @@ Changelog * The output of the :ref:`update-cmd`, :ref:`remove-cmd`, and :ref:`modify-cmd` commands now respects the :ref:`list_format_album` and :ref:`list_format_item` config options. Thanks to Mike Kazantsev. +* The :ref:`art-filename` option can now be a template rather than a simple + string. Thanks to Jarrod Beardwood. * Fix album queries for ``artpath`` and other non-item fields. * Null values in the database can now be matched with the empty-string regular expression, ``^$``. diff --git a/docs/reference/config.rst b/docs/reference/config.rst index eb1954a2d..102d7ef2b 100644 --- a/docs/reference/config.rst +++ b/docs/reference/config.rst @@ -157,9 +157,10 @@ art_filename ~~~~~~~~~~~~ When importing album art, the name of the file (without extension) where the -cover art image should be placed. Defaults to ``cover`` (i.e., images will -be named ``cover.jpg`` or ``cover.png`` and placed in the album's -directory). +cover art image should be placed. This is a template string, so you can use any +of the syntax available to :doc:`/reference/pathformat`. Defaults to ``cover`` +(i.e., images will be named ``cover.jpg`` or ``cover.png`` and placed in the +album's directory). plugins ~~~~~~~