Skip to content

Commit

Permalink
Introduce CoverArtImageFromFile
Browse files Browse the repository at this point in the history
Suggested by Mineo : #408 (comment)
  • Loading branch information
zas committed Jun 25, 2015
1 parent 917641a commit b12c12e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
31 changes: 17 additions & 14 deletions picard/coverart/__init__.py
Expand Up @@ -163,20 +163,23 @@ def next_in_queue(self):
return

# local files
if coverartimage.from_file:
data = None
try:
with open(coverartimage.from_file, 'rb') as file:
self._set_metadata(coverartimage, file.read())
except IOError, (errnum, errmsg):
log.error("Failed to read %r: %s (%d)" %
(coverartimage.from_file, errmsg, errnum))
except CoverArtImageIOError:
# It doesn't make sense to store/download more images if we can't
# save them in the temporary folder, abort.
return
self.next_in_queue()
return
try:
if coverartimage.filepath:
data = None
try:
with open(coverartimage.filepath, 'rb') as file:
self._set_metadata(coverartimage, file.read())
except IOError, (errnum, errmsg):
log.error("Failed to read %r: %s (%d)" %
(coverartimage.from_file, errmsg, errnum))
except CoverArtImageIOError:
# It doesn't make sense to store/download more images if we can't
# save them in the temporary folder, abort.
return
self.next_in_queue()
return
except AttributeError:
pass

# on the web
self._message(
Expand Down
39 changes: 31 additions & 8 deletions picard/coverart/image.py
Expand Up @@ -117,14 +117,7 @@ class CoverArtImage:
is_front = None
sourceprefix = "URL"

def __init__(self, url=None, types=[], comment='', data=None,
from_file=None):
if from_file:
url = None
self.from_file = from_file
self.sourceprefix = 'LOCAL'
else:
self.from_file = None
def __init__(self, url=None, types=[], comment='', data=None):
if url is not None:
self.parse_url(url)
else:
Expand Down Expand Up @@ -401,3 +394,33 @@ def __repr__(self):
if self.comment:
p.append("comment=%r" % self.comment)
return "%s(%s)" % (self.__class__.__name__, ", ".join(p))


class CoverArtImageFromFile(CoverArtImage):

sourceprefix = 'LOCAL'

def __init__(self, filepath, tag=None, types=[], is_front=None,
support_types=False, comment='', data=None):
CoverArtImage.__init__(self, url=None, types=types, comment=comment,
data=data)
self.filepath = filepath
self.support_types = support_types
if is_front is not None:
self.is_front = is_front

@property
def source(self):
return u'%s %s' % (sourceprefix, self.filepath)

def __repr__(self):
p = []
p.append('%r' % self.filepath)
if self.types:
p.append("types=%r" % self.types)
if self.is_front is not None:
p.append("is_front=%r" % self.is_front)
p.append('support_types=%r' % self.support_types)
if self.comment:
p.append("comment=%r" % self.comment)
return "%s(%s)" % (self.__class__.__name__, ", ".join(p))
5 changes: 2 additions & 3 deletions picard/coverart/providers/local.py
Expand Up @@ -28,7 +28,7 @@

from picard import config, log
from picard.coverart.providers import CoverArtProvider
from picard.coverart.image import CoverArtImage
from picard.coverart.image import CoverArtImageFromFile


class CoverArtProviderLocal(CoverArtProvider):
Expand Down Expand Up @@ -57,8 +57,7 @@ def queue_images(self):
if self._match_filename(filename):
filepath = os.path.join(current_dir, root, filename)
if os.path.exists(filepath):
log.debug("Trying cover art file: %r", filepath)
self.queue_put(CoverArtImage(from_file=filepath))
self.queue_put(CoverArtImageFromFile(filepath))
return CoverArtProvider.FINISHED

def _match_filename(self, filename):
Expand Down

0 comments on commit b12c12e

Please sign in to comment.