From 131b19188566108238225486380a565b25232773 Mon Sep 17 00:00:00 2001 From: jizz Date: Tue, 4 Dec 2012 16:24:15 +1100 Subject: [PATCH 1/5] Update beets/ui/__init__.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix for Issue 450: Expand templates in art_filename config option --- beets/ui/__init__.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/beets/ui/__init__.py b/beets/ui/__init__.py index a1dc8bd04..a338f456f 100644 --- a/beets/ui/__init__.py +++ b/beets/ui/__init__.py @@ -516,6 +516,20 @@ 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 path formats override the default values. + art_filename = Template(legacy_art_filename) + else: + # If no legacy path format, use the defaults 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. @@ -771,8 +785,7 @@ def _raw_main(args, configfh): directory = options.directory or \ config_val(config, 'beets', 'directory', default_dir) path_formats = _get_path_formats(config) - art_filename = \ - config_val(config, 'beets', 'art_filename', DEFAULT_ART_FILENAME) + art_filename = _get_art_filename(config) lib_timeout = config_val(config, 'beets', 'timeout', DEFAULT_TIMEOUT) replacements = _get_replacements(config) try: From 6b7b54062f7b6f2425c960d9e7e4f175d945c07d Mon Sep 17 00:00:00 2001 From: jizz Date: Tue, 4 Dec 2012 16:25:18 +1100 Subject: [PATCH 2/5] Update beets/library.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix for Issue 450: Expand templates in art_filename config option --- beets/library.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/beets/library.py b/beets/library.py index cbc1d6018..b57c51943 100644 --- a/beets/library.py +++ b/beets/library.py @@ -1002,7 +1002,7 @@ class Library(BaseLibrary): self.path = bytestring_path(normpath(path)) self.directory = bytestring_path(normpath(directory)) self.path_formats = path_formats - self.art_filename = bytestring_path(art_filename) + self.art_filename = art_filename self.replacements = replacements self._memotable = {} # Used for template substitution performance. @@ -1564,8 +1564,11 @@ class Album(BaseAlbum): """ image = bytestring_path(image) item_dir = item_dir or self.item_dir() + sanitized_art_filename = util.sanitize_for_path(self.evaluate_template(self._library.art_filename),os.path) _, ext = os.path.splitext(image) - dest = os.path.join(item_dir, self._library.art_filename + ext) + + dest = os.path.join(item_dir, util.sanitize_path(sanitized_art_filename) + ext) + return dest def set_art(self, path, copy=True): From c56c52c5d50ef7c1cdfebd985f1d131cdb2ac471 Mon Sep 17 00:00:00 2001 From: jizz Date: Wed, 5 Dec 2012 11:02:34 +1100 Subject: [PATCH 3/5] updated comments --- beets/ui/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/beets/ui/__init__.py b/beets/ui/__init__.py index a338f456f..1dbe7056e 100644 --- a/beets/ui/__init__.py +++ b/beets/ui/__init__.py @@ -522,10 +522,10 @@ def _get_art_filename(config): """ legacy_art_filename = config_val(config, 'beets', 'art_filename', None) if legacy_art_filename: - # Old path formats override the default values. + # Old art filename format override the default value. art_filename = Template(legacy_art_filename) else: - # If no legacy path format, use the defaults instead. + # If no legacy art filename format, use the default instead. art_filename = DEFAULT_ART_FILENAME return art_filename From 12aaa2ee34138a532eae94f6c4bc5e9c7980e32c Mon Sep 17 00:00:00 2001 From: jizz Date: Wed, 5 Dec 2012 11:05:12 +1100 Subject: [PATCH 4/5] updated art_destination art_filename now acceptable as a string or a Template --- beets/library.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/beets/library.py b/beets/library.py index b57c51943..5d9921d63 100644 --- a/beets/library.py +++ b/beets/library.py @@ -1562,14 +1562,21 @@ class Album(BaseAlbum): items, so the album must contain at least one item or item_dir must be provided. """ + + if isinstance(self._library.art_filename,Template): + art_filename_template = self._library.art_filename,Template + else: + art_filename_template = Template(self._library.art_filename) + + image = bytestring_path(image) item_dir = item_dir or self.item_dir() - sanitized_art_filename = util.sanitize_for_path(self.evaluate_template(self._library.art_filename),os.path) + sanitized_art_filename = util.sanitize_for_path(self.evaluate_template(art_filename_template),os.path) _, ext = os.path.splitext(image) dest = os.path.join(item_dir, util.sanitize_path(sanitized_art_filename) + ext) - return dest + return bytestring_path(dest) def set_art(self, path, copy=True): """Sets the album's cover art to the image at the given path. From fe1b40f7d27adb0a210002b4f6390a7edfb19fb8 Mon Sep 17 00:00:00 2001 From: jizz Date: Wed, 5 Dec 2012 11:58:10 +1100 Subject: [PATCH 5/5] minor tweak --- beets/library.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/beets/library.py b/beets/library.py index 5d9921d63..2bf291c44 100644 --- a/beets/library.py +++ b/beets/library.py @@ -1563,15 +1563,13 @@ class Album(BaseAlbum): item_dir must be provided. """ - if isinstance(self._library.art_filename,Template): - art_filename_template = self._library.art_filename,Template - else: - art_filename_template = Template(self._library.art_filename) - - image = bytestring_path(image) item_dir = item_dir or self.item_dir() - sanitized_art_filename = util.sanitize_for_path(self.evaluate_template(art_filename_template),os.path) + + 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) dest = os.path.join(item_dir, util.sanitize_path(sanitized_art_filename) + ext)