From 374e043c3c2d09c3a0422915f45a995eec1a86af Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sat, 27 Apr 2013 16:47:42 -0700 Subject: [PATCH] API/test enhancements for #257 Also, `cautious` defaults to False for backwards-compatibility. --- beetsplug/fetchart.py | 16 ++++++++-------- test/test_art.py | 24 ++++++++++++++++-------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/beetsplug/fetchart.py b/beetsplug/fetchart.py index 14a91d451..56e635164 100644 --- a/beetsplug/fetchart.py +++ b/beetsplug/fetchart.py @@ -28,7 +28,6 @@ from beets import util from beets import config IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg'] -COVER_NAMES = ['cover', 'front', 'art', 'album', 'folder'] CONTENT_TYPES = ('image/jpeg',) DOWNLOAD_EXTENSION = '.jpg' @@ -110,7 +109,8 @@ def aao_art(asin): # Art from the filesystem. -def art_in_path(path, cover_names=COVER_NAMES): + +def art_in_path(path, cover_names, cautious): """Look for album art files in a specified directory.""" if not os.path.isdir(path): return @@ -123,7 +123,7 @@ def art_in_path(path, cover_names=COVER_NAMES): images.append(fn) # Look for "preferred" filenames. - cover_pat = r"(\b|_)(%s)(\b|_)" % '|'.join(cover_names) + cover_pat = r"(\b|_)({0})(\b|_)".format('|'.join(cover_names)) for fn in images: if re.search(cover_pat, os.path.splitext(fn)[0], re.I): log.debug(u'fetchart: using well-named art file {0}'.format( @@ -132,8 +132,7 @@ def art_in_path(path, cover_names=COVER_NAMES): return os.path.join(path, fn) # Fall back to any image in the folder. - cautious = config['fetchart']['cautious'].get(bool) - if not cautious and images: + if images and not cautious: log.debug(u'fetchart: using fallback art file {0}'.format( util.displayable_path(images[0]) )) @@ -173,9 +172,10 @@ def art_for_album(album, paths, maxwidth=None, local_only=False): # Local art. cover_names = config['fetchart']['cover_names'].as_str_seq() + cautious = config['fetchart']['cautious'].get(bool) if paths: for path in paths: - out = art_in_path(path, cover_names) + out = art_in_path(path, cover_names, cautious) if out: break @@ -222,8 +222,8 @@ class FetchArtPlugin(BeetsPlugin): 'auto': True, 'maxwidth': 0, 'remote_priority': False, - 'cautious': True, - 'cover_names': COVER_NAMES + 'cautious': False, + 'cover_names': ['cover', 'front', 'art', 'album', 'folder'], }) # Holds paths to downloaded images between fetching them and diff --git a/test/test_art.py b/test/test_art.py index a20385655..25c0a4390 100644 --- a/test/test_art.py +++ b/test/test_art.py @@ -54,23 +54,31 @@ class FSArtTest(_common.TestCase): def setUp(self): super(FSArtTest, self).setUp() self.dpath = os.path.join(self.temp_dir, 'arttest') - fetchart.FetchArtPlugin() os.mkdir(self.dpath) def test_finds_jpg_in_directory(self): - _common.touch(os.path.join(self.dpath, 'art.jpg')) - fn = fetchart.art_in_path(self.dpath) - self.assertEqual(fn, os.path.join(self.dpath, 'art.jpg')) + _common.touch(os.path.join(self.dpath, 'a.jpg')) + fn = fetchart.art_in_path(self.dpath, ('art',), False) + self.assertEqual(fn, os.path.join(self.dpath, 'a.jpg')) def test_appropriately_named_file_takes_precedence(self): _common.touch(os.path.join(self.dpath, 'a.jpg')) - _common.touch(os.path.join(self.dpath, 'cover.jpg')) - fn = fetchart.art_in_path(self.dpath) - self.assertEqual(fn, os.path.join(self.dpath, 'cover.jpg')) + _common.touch(os.path.join(self.dpath, 'art.jpg')) + fn = fetchart.art_in_path(self.dpath, ('art',), False) + self.assertEqual(fn, os.path.join(self.dpath, 'art.jpg')) def test_non_image_file_not_identified(self): _common.touch(os.path.join(self.dpath, 'a.txt')) - fn = fetchart.art_in_path(self.dpath) + fn = fetchart.art_in_path(self.dpath, ('art',), False) + self.assertEqual(fn, None) + + def test_cautious_skips_fallback(self): + _common.touch(os.path.join(self.dpath, 'a.jpg')) + fn = fetchart.art_in_path(self.dpath, ('art',), True) + self.assertEqual(fn, None) + + def test_empty_dir(self): + fn = fetchart.art_in_path(self.dpath, ('art',), True) self.assertEqual(fn, None) class CombinedTest(_common.TestCase):