From 5a9ac67d47fe3baa860d5beb46626fe30621249f Mon Sep 17 00:00:00 2001 From: Laurent Monin Date: Thu, 14 Sep 2023 12:46:50 +0200 Subject: [PATCH 1/3] Fix CoverArtImage.__repr__() output for multi types --- picard/coverart/image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/picard/coverart/image.py b/picard/coverart/image.py index fcad8f3c42..7c45b65c98 100644 --- a/picard/coverart/image.py +++ b/picard/coverart/image.py @@ -221,7 +221,7 @@ def __repr__(self): if self.types: p.append("types=%r" % self.types) p.append('support_types=%r' % self.support_types) - p.append('support_multi_types=%r' % self.support_types) + p.append('support_multi_types=%r' % self.support_multi_types) if self.is_front is not None: p.append("is_front=%r" % self.is_front) if self.comment: From 06f5d179c022acd37bbebc9cb2f0e48cf11e47a5 Mon Sep 17 00:00:00 2001 From: Laurent Monin Date: Thu, 14 Sep 2023 12:51:05 +0200 Subject: [PATCH 2/3] CoverArtImage.__repr__()/__str__(): use generators and shorten lines --- picard/coverart/image.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/picard/coverart/image.py b/picard/coverart/image.py index 7c45b65c98..8686992c61 100644 --- a/picard/coverart/image.py +++ b/picard/coverart/image.py @@ -214,29 +214,32 @@ def imageinfo_as_string(self): self.datalength, self.tempfile_filename) - def __repr__(self): - p = [] + def _repr(self): if self.url is not None: - p.append("url=%r" % self.url.toString()) + yield "url=%r" % self.url.toString() if self.types: - p.append("types=%r" % self.types) - p.append('support_types=%r' % self.support_types) - p.append('support_multi_types=%r' % self.support_multi_types) + yield "types=%r" % self.types + yield 'support_types=%r' % self.support_types + yield 'support_multi_types=%r' % self.support_multi_types if self.is_front is not None: - p.append("is_front=%r" % self.is_front) + yield "is_front=%r" % self.is_front if self.comment: - p.append("comment=%r" % self.comment) - return "%s(%s)" % (self.__class__.__name__, ", ".join(p)) + yield "comment=%r" % self.comment - def __str__(self): - p = ['Image'] + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, ", ".join(self._repr())) + + def _str(self): + yield 'Image' if self.url is not None: - p.append("from %s" % self.url.toString()) + yield "from %s" % self.url.toString() if self.types: - p.append("of type %s" % ','.join(self.types)) + yield "of type %s" % ','.join(self.types) if self.comment: - p.append("and comment '%s'" % self.comment) - return ' '.join(p) + yield "and comment '%s'" % self.comment + + def __str__(self): + return ' '.join(self._str()) def __eq__(self, other): if self and other: From 9cbc2b47660c1708d3b34621d02e377a46d88251 Mon Sep 17 00:00:00 2001 From: Laurent Monin Date: Thu, 14 Sep 2023 13:20:26 +0200 Subject: [PATCH 3/3] Add tests for CoverArtImage __repr__() and __str__() --- test/test_coverart_image.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/test_coverart_image.py b/test/test_coverart_image.py index 952706f2bc..50573d87ee 100644 --- a/test/test_coverart_image.py +++ b/test/test_coverart_image.py @@ -55,6 +55,23 @@ def create_image(extra_data, types=None, support_types=False, class CoverArtImageTest(PicardTestCase): + def test_repr_str_1(self): + image = CoverArtImage( + url='url', types=["booklet", "front"], + comment='comment', support_types=True, + support_multi_types=True + ) + 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'" + 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" + self.assertEqual(expected, str(image)) def test_is_front_image_no_types(self): image = create_image(b'a')