Skip to content

Commit

Permalink
Merge pull request #2319 from zas/tagcaa_str_repr
Browse files Browse the repository at this point in the history
TagCoverArtImage(): properly set _repr() and _str() for this subclass
  • Loading branch information
zas authored Sep 20, 2023
2 parents 3018d52 + 6e0054d commit b472bfe
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
27 changes: 13 additions & 14 deletions picard/coverart/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ def __repr__(self):
return "%s(%s)" % (self.__class__.__name__, ", ".join(self._repr()))

def _str(self):
yield 'Image'
if self.url is not None:
yield "from %s" % self.url.toString()
if self.types:
Expand All @@ -237,7 +236,11 @@ def _str(self):
yield "and comment '%s'" % self.comment

def __str__(self):
return ' '.join(self._str())
output = self.__class__.__name__
s = tuple(self._str())
if s:
output += ' ' + ' '.join(s)
return output

def __eq__(self, other):
if self and other:
Expand Down Expand Up @@ -469,19 +472,15 @@ def source(self):
else:
return 'File %s' % (self.sourcefile)

def __repr__(self):
p = []
p.append('%r' % self.sourcefile)
def _repr(self):
yield '%r' % self.sourcefile
if self.tag is not None:
p.append("tag=%r" % self.tag)
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))
yield 'tag=%r' % self.tag
yield from super()._repr()

def _str(self):
yield 'from %r' % self.sourcefile
yield from super()._str()


class LocalFileCoverArtImage(CoverArtImage):
Expand Down
29 changes: 26 additions & 3 deletions test/test_coverart_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@
from picard.coverart.image import (
CoverArtImage,
LocalFileCoverArtImage,
TagCoverArtImage,
)
from picard.coverart.utils import (
Id3ImageType,
types_from_id3,
)
from picard.coverart.utils import Id3ImageType
from picard.metadata import Metadata
from picard.util import encode_filename
from picard.util.filenaming import WinPathTooLong
Expand All @@ -54,6 +58,25 @@ def create_image(extra_data, types=None, support_types=False,
)


class TagCoverArtImageTest(PicardTestCase):
def test_repr_str_1(self):
image_type = Id3ImageType.COVER_FRONT
image = TagCoverArtImage(
file='testfilename',
tag='tag',
types=types_from_id3(image_type),
comment='description',
support_types=True,
data=None,
id3_type=image_type,
is_front=True,
)
expected = "TagCoverArtImage('testfilename', tag='tag', types=['front'], support_types=True, support_multi_types=False, is_front=True, comment='description')"
self.assertEqual(expected, repr(image))
expected = "TagCoverArtImage from 'testfilename' of type front and comment 'description'"
self.assertEqual(expected, str(image))


class CoverArtImageTest(PicardTestCase):
def test_repr_str_1(self):
image = CoverArtImage(
Expand All @@ -63,14 +86,14 @@ def test_repr_str_1(self):
)
expected = "CoverArtImage(url='url', types=['booklet', 'front'], support_types=True, support_multi_types=True, comment='comment')"
self.assertEqual(expected, repr(image))
expected = "Image from url of type booklet,front and comment 'comment'"
expected = "CoverArtImage from url of type booklet,front and comment 'comment'"
self.assertEqual(expected, str(image))

def test_repr_str_2(self):
image = CoverArtImage()
expected = "CoverArtImage(support_types=False, support_multi_types=False)"
self.assertEqual(expected, repr(image))
expected = "Image"
expected = "CoverArtImage"
self.assertEqual(expected, str(image))

def test_is_front_image_no_types(self):
Expand Down

0 comments on commit b472bfe

Please sign in to comment.