From 4227e3eabe307dc34ad466d818615478232af01a Mon Sep 17 00:00:00 2001 From: Bruno Cauet Date: Wed, 28 Jan 2015 16:24:05 +0100 Subject: [PATCH] beets.util.artresizer offers has_PIL() and has_IM() has_PIL() determines Python Imaging Library version (or None) and has_IM() does the same for Image Magick. ArtResizer._check_method() relies on those functions. It also does not accept a "method" parameter anymore, and neither does ArtResizer(). It was unused. --- beets/util/artresizer.py | 75 ++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/beets/util/artresizer.py b/beets/util/artresizer.py index ce6243647..e33b2decf 100644 --- a/beets/util/artresizer.py +++ b/beets/util/artresizer.py @@ -159,11 +159,10 @@ class ArtResizer(object): """ __metaclass__ = Shareable - def __init__(self, method=None): - """Create a resizer object for the given method or, if none is - specified, with an inferred method. + def __init__(self): + """Create a resizer object with an inferred method. """ - self.method = self._check_method(method) + self.method = self._check_method() log.debug(u"artresizer: method is {0}", self.method) self.can_compare = self._can_compare() @@ -211,41 +210,43 @@ class ArtResizer(object): return self.method[0] == IMAGEMAGICK and self.method[1] > (6, 8, 7) @staticmethod - def _check_method(method=None): - """A tuple indicating whether current method is available and its - version. If no method is given, it returns a supported one. - """ - # Guess available method - if not method: - for m in [IMAGEMAGICK, PIL]: - _, version = ArtResizer._check_method(m) - if version: - return (m, version) - return (WEBPROXY, (0)) + def _check_method(): + """Return a tuple indicating an available method and its version.""" + version = has_IM() + if version: + return IMAGEMAGICK, version - if method == IMAGEMAGICK: + version = has_PIL() + if version: + return PIL, version - # Try invoking ImageMagick's "convert". - try: - out = util.command_output(['identify', '--version']) + return WEBPROXY, (0) - if 'imagemagick' in out.lower(): - pattern = r".+ (\d+)\.(\d+)\.(\d+).*" - match = re.search(pattern, out) - if match: - return (IMAGEMAGICK, - (int(match.group(1)), - int(match.group(2)), - int(match.group(3)))) - return (IMAGEMAGICK, (0)) - except (subprocess.CalledProcessError, OSError): - return (IMAGEMAGICK, None) +def has_IM(): + """Return Image Magick version or None if it is unavailable + Try invoking ImageMagick's "convert".""" + try: + out = util.command_output(['identify', '--version']) - if method == PIL: - # Try importing PIL. - try: - __import__('PIL', fromlist=['Image']) - return (PIL, (0)) - except ImportError: - return (PIL, None) + if 'imagemagick' in out.lower(): + pattern = r".+ (\d+)\.(\d+)\.(\d+).*" + match = re.search(pattern, out) + if match: + return (int(match.group(1)), + int(match.group(2)), + int(match.group(3))) + return (0,) + + except (subprocess.CalledProcessError, OSError): + return None + + +def has_PIL(): + """Return Image Magick version or None if it is unavailable + Try importing PIL.""" + try: + __import__('PIL', fromlist=['Image']) + return (0,) + except ImportError: + return None