[diffoscope] 01/03: Reflow indentation for visual differences.

Maria Glukhova siamezzze-guest at moszumanska.debian.org
Mon Apr 24 08:18:42 CEST 2017


This is an automated email from the git hooks/post-receive script.

siamezzze-guest pushed a commit to branch siamezzze/image-visual-comparison
in repository diffoscope.

commit dcf74cb7568c657b29c1a6d62b38dda745e86eb9
Author: Maria Glukhova <siamezzze at gmail.com>
Date:   Sun Apr 23 18:58:24 2017 +0300

    Reflow indentation for visual differences.
    
    Use AssertionError to reduce identation inside try block;
    Nicer indentation for lists and arguments.
---
 diffoscope/comparators/gif.py   | 36 +++++++++++------
 diffoscope/comparators/image.py | 90 ++++++++++++++++++++++++++++-------------
 diffoscope/comparators/png.py   | 36 +++++++++++------
 3 files changed, 109 insertions(+), 53 deletions(-)

diff --git a/diffoscope/comparators/gif.py b/diffoscope/comparators/gif.py
index fb60416..e4d5df7 100644
--- a/diffoscope/comparators/gif.py
+++ b/diffoscope/comparators/gif.py
@@ -66,18 +66,30 @@ class GifFile(File):
         if (gifbuild_diff is not None) and Config().html_output and \
                 (get_image_size(self.path) == get_image_size(other.path)):
             try:
-                own_size = get_image_size(self.path)
-                other_size = get_image_size(other.path)
-                self_static = is_image_static(self.path)
-                other_static = is_image_static(other.path)
-                if (own_size == other_size) and self_static and other_static:
-                    logger.debug('Generating visual difference for %s and %s',
-                                 self.path, other.path)
-                    content_diff = Difference(None, self.path, other.path,
-                                              source='Image content')
-                    content_diff.add_visuals([pixel_difference(self.path, other.path),
-                                             flicker_difference(self.path, other.path)])
-                    differences.append(content_diff)
+                if get_image_size(self.path) != get_image_size(other.path):
+                    raise AssertionError
+                if not is_image_static(self.path):
+                    raise AssertionError
+                if not is_image_static(other.path):
+                    raise AssertionError
+                logger.debug(
+                    'Generating visual difference for %s and %s',
+                    self.path,
+                    other.path,
+                )
+                content_diff = Difference(
+                    None,
+                    self.path,
+                    other.path,
+                    source='Image content',
+                )
+                content_diff.add_visuals([
+                    pixel_difference(self.path, other.path),
+                    flicker_difference(self.path, other.path),
+                ])
+                differences.append(content_diff)
             except subprocess.CalledProcessError:  #noqa
                 pass
+            except AssertionError:  #noqa
+                pass
         return differences
diff --git a/diffoscope/comparators/image.py b/diffoscope/comparators/image.py
index 6ba7353..af1d951 100644
--- a/diffoscope/comparators/image.py
+++ b/diffoscope/comparators/image.py
@@ -86,8 +86,13 @@ class Identify(Command):
 def pixel_difference(image1_path, image2_path):
     compared_filename = get_named_temporary_file(suffix='.png').name
     try:
-        subprocess.check_call(('compare', image1_path, image2_path,
-                               '-compose', 'src', compared_filename))
+        subprocess.check_call((
+            'compare',
+            image1_path,
+            image2_path,
+            '-compose', 'src',
+            compared_filename,
+        ))
     except subprocess.CalledProcessError as e:
         # ImageMagick's `compare` will return 1 if images are different
         if e.returncode == 1:
@@ -101,9 +106,15 @@ def pixel_difference(image1_path, image2_path):
 @tool_required('convert')
 def flicker_difference(image1_path, image2_path):
     compared_filename = get_named_temporary_file(suffix='.gif').name
-    subprocess.check_call(
-        ('convert', '-delay', '50', image1_path, image2_path,
-         '-loop', '0', '-compose', 'difference', compared_filename))
+    subprocess.check_call((
+        'convert',
+        '-delay', '50',
+        image1_path,
+        image2_path,
+        '-loop', '0',
+        '-compose', 'difference',
+        compared_filename,
+    ))
     content = base64.b64encode(open(compared_filename, 'rb').read())
     content = content.decode('utf8')
     datatype = 'image/gif;base64'
@@ -112,28 +123,41 @@ def flicker_difference(image1_path, image2_path):
 
 @tool_required('identify')
 def get_image_size(image_path):
-    return subprocess.check_output(('identify', '-format',
-                                    '%[h]x%[w]', image_path))
+    return subprocess.check_output((
+        'identify',
+        '-format', '%[h]x%[w]',
+        image_path,
+    ))
 
 class JPEGImageFile(File):
     RE_FILE_TYPE = re.compile(r'\bJPEG image data\b')
 
     def compare_details(self, other, source=None):
-        content_diff = Difference.from_command(Img2Txt, self.path, other.path,
-                                               source='Image content')
-        if (content_diff is not None) and Config().html_output:
+        content_diff = Difference.from_command(
+            Img2Txt,
+            self.path,
+            other.path,
+            source="Image content",
+        )
+        if content_diff is not None and Config().html_output:
             try:
                 own_size = get_image_size(self.path)
                 other_size = get_image_size(other.path)
-                if own_size == other_size:
-                    logger.debug('Generating visual difference for %s and %s',
-                                 self.path, other.path)
-                    content_diff.add_visuals([
-                        pixel_difference(self.path, other.path),
-                        flicker_difference(self.path, other.path)
-                    ])
+                if own_size != other_size:
+                    raise AssertionError
+                logger.debug(
+                    'Generating visual difference for %s and %s',
+                    self.path,
+                    other.path,
+                )
+                content_diff.add_visuals([
+                    pixel_difference(self.path, other.path),
+                    flicker_difference(self.path, other.path),
+                ])
             except subprocess.CalledProcessError:  # noqa
                 pass
+            except AssertionError:  # noqa
+                pass
         return [
             content_diff,
             Difference.from_command(
@@ -156,21 +180,29 @@ class ICOImageFile(File):
         except subprocess.CalledProcessError:  # noqa
             pass
         else:
-            content_diff = Difference.from_command(Img2Txt, png_a, png_b,
-                                                   source='Image content')
-            if (content_diff is not None) and Config().html_output:
+            content_diff = Difference.from_command(
+                Img2Txt,
+                png_a,
+                png_b,
+                source="Image content",
+            )
+            if content_diff is not None and Config().html_output:
                 try:
-                    own_size = get_image_size(self.path)
-                    other_size = get_image_size(other.path)
-                    if own_size == other_size:
-                        logger.debug('Generating visual difference for %s and %s',
-                                     self.path, other.path)
-                        content_diff.add_visuals([
-                            pixel_difference(self.path, other.path),
-                            flicker_difference(self.path, other.path)
-                        ])
+                    if get_image_size(self.path) != get_image_size(other.path):
+                        raise AssertionError
+                    logger.debug(
+                        'Generating visual difference for %s and %s',
+                        self.path,
+                        other.path,
+                    )
+                    content_diff.add_visuals([
+                        pixel_difference(self.path, other.path),
+                        flicker_difference(self.path, other.path),
+                    ])
                 except subprocess.CalledProcessError:  # noqa
                     pass
+                except AssertionError:  # noqa
+                    pass
             differences.append(content_diff)
 
         differences.append(Difference.from_command(
diff --git a/diffoscope/comparators/png.py b/diffoscope/comparators/png.py
index a24a8f4..a674489 100644
--- a/diffoscope/comparators/png.py
+++ b/diffoscope/comparators/png.py
@@ -48,20 +48,32 @@ class PngFile(File):
     RE_FILE_TYPE = re.compile(r'^PNG image data\b')
 
     def compare_details(self, other, source=None):
-        sng_diff = Difference.from_command(Sng, self.path, other.path, source='sng')
+        sng_diff = Difference.from_command(
+            Sng, self.path, other.path, source='sng',
+        )
         differences = [sng_diff]
-        if (sng_diff is not None) and Config().html_output:
+        if sng_diff is not None and Config().html_output:
             try:
-                own_size = get_image_size(self.path)
-                other_size = get_image_size(other.path)
-                if own_size == other_size:
-                    logger.debug('Generating visual difference for %s and %s',
-                                 self.path, other.path)
-                    content_diff = Difference(None, self.path, other.path,
-                                              source='Image content')
-                    content_diff.add_visuals([pixel_difference(self.path, other.path),
-                                             flicker_difference(self.path, other.path)])
-                    differences.append(content_diff)
+                if get_image_size(self.path) != get_image_size(other.path):
+                    raise AssertionError
+                logger.debug(
+                    'Generating visual difference for %s and %s',
+                    self.path,
+                    other.path,
+                )
+                content_diff = Difference(
+                    None,
+                    self.path,
+                    other.path,
+                    source="Image content",
+                )
+                content_diff.add_visuals([
+                    pixel_difference(self.path, other.path),
+                    flicker_difference(self.path, other.path),
+                ])
+                differences.append(content_diff)
             except subprocess.CalledProcessError:  #noqa
                 pass
+            except AssertionError:  # noqa
+                pass
         return differences

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list