Blame tests/tiff_test/test_tag_compare.py

Packit Service 21b5d1
# -*- coding: utf-8 -*-
Packit Service 21b5d1
Packit Service 21b5d1
import system_tests
Packit Service 21b5d1
Packit Service 21b5d1
Packit Service 21b5d1
class OutputTagExtract(metaclass=system_tests.CaseMeta):
Packit Service 21b5d1
    """
Packit Service 21b5d1
    Test whether exiv2 -pa $file and exiv2 -pS $file produces the same output.
Packit Service 21b5d1
    """
Packit Service 21b5d1
Packit Service 21b5d1
    def parse_pa(self, stdout):
Packit Service 21b5d1
        """
Packit Service 21b5d1
        Parse the output of exiv2 -pa $file, which looks like this:
Packit Service 21b5d1
Packit Service 21b5d1
        Exif.Image.NewSubfileType                    Long        1  Primary image
Packit Service 21b5d1
Packit Service 21b5d1
        into a list of dictionaries with the keys:
Packit Service 21b5d1
        tag: last word of the first column (here NewSubfileType)
Packit Service 21b5d1
        type: lowercase second column
Packit Service 21b5d1
        len: third column
Packit Service 21b5d1
        val: fourth column
Packit Service 21b5d1
Packit Service 21b5d1
        It is furthermore checked that the first column begins with 'Exif.Image'
Packit Service 21b5d1
        """
Packit Service 21b5d1
        data = []
Packit Service 21b5d1
Packit Service 21b5d1
        for line in stdout:
Packit Service 21b5d1
            tmp = line.split()
Packit Service 21b5d1
Packit Service 21b5d1
            exif, image, tag = tmp[0].split('.')
Packit Service 21b5d1
            self.assertEquals(exif, "Exif")
Packit Service 21b5d1
            self.assertEquals(image, "Image")
Packit Service 21b5d1
Packit Service 21b5d1
            data.append({
Packit Service 21b5d1
                "tag": tag,
Packit Service 21b5d1
                "type": tmp[1].lower(),
Packit Service 21b5d1
                "len": int(tmp[2]),
Packit Service 21b5d1
                "val": " ".join(tmp[3:])
Packit Service 21b5d1
            })
Packit Service 21b5d1
Packit Service 21b5d1
        return data
Packit Service 21b5d1
Packit Service 21b5d1
    def parse_pS(self, stdout):
Packit Service 21b5d1
        """
Packit Service 21b5d1
        Parse the output of exiv2 -pS $file, which looks like this:
Packit Service 21b5d1
Packit Service 21b5d1
        STRUCTURE OF TIFF FILE (II): $file
Packit Service 21b5d1
        address |    tag                              |      type |    count |    offset | value
Packit Service 21b5d1
        254 | 0x00fe NewSubfileType               |      LONG |        1 |           | 0
Packit Service 21b5d1
         ...
Packit Service 21b5d1
        END $file
Packit Service 21b5d1
Packit Service 21b5d1
        into a list of dictionaries with the following keys:
Packit Service 21b5d1
        tag: the string after the hex number in the second column
Packit Service 21b5d1
        type: lowercase third column
Packit Service 21b5d1
        len: fourth column
Packit Service 21b5d1
        val: fifth column
Packit Service 21b5d1
Packit Service 21b5d1
        The first two lines and the last line are ignored, as they contain
Packit Service 21b5d1
        explanatory output.
Packit Service 21b5d1
        """
Packit Service 21b5d1
        data = []
Packit Service 21b5d1
Packit Service 21b5d1
        for i, line in enumerate(stdout):
Packit Service 21b5d1
            if i < 2 or i == len(stdout) - 1:
Packit Service 21b5d1
                continue
Packit Service 21b5d1
Packit Service 21b5d1
            tmp = line.split(" | ")
Packit Service 21b5d1
            data.append({
Packit Service 21b5d1
                "tag": tmp[1].split()[1],
Packit Service 21b5d1
                "type": tmp[2].replace(' ', '').lower(),
Packit Service 21b5d1
                "len": int(tmp[3].replace(' ', '')),
Packit Service 21b5d1
                "val": tmp[5]
Packit Service 21b5d1
            })
Packit Service 21b5d1
Packit Service 21b5d1
        return data
Packit Service 21b5d1
Packit Service 21b5d1
    def compare_pS_pa(self):
Packit Service 21b5d1
        """
Packit Service 21b5d1
        Compares the output from self.parse_pa() and self.parse_pS() (saved in
Packit Service 21b5d1
        self.pa_data & self.pS_data respectively).
Packit Service 21b5d1
        All dictionaries in the lists are compared for equality for the keys
Packit Service 21b5d1
        tag, len and type but only some for val. This is due to differently
Packit Service 21b5d1
        processed output (exiv2 -pa produces more readable output,
Packit Service 21b5d1
        e.g. compression is written in words and not as a number as it is by
Packit Service 21b5d1
        exiv2 -pS)
Packit Service 21b5d1
        """
Packit Service 21b5d1
        for pa_elem, pS_elem in zip(self.pa_data, self.pS_data):
Packit Service 21b5d1
            for key in ["tag", "type", "len"]:
Packit Service 21b5d1
                self.assertEquals(pa_elem[key], pS_elem[key])
Packit Service 21b5d1
Packit Service 21b5d1
            if pa_elem["tag"] in [
Packit Service 21b5d1
                    "ImageWidth", "ImageLength", "BitsPerSample",
Packit Service 21b5d1
                    "DocumentName", "ImageDescription", "StripOffsets",
Packit Service 21b5d1
                    "SamplesPerPixel", "StripByteCounts", "PlanarConfiguration"]:
Packit Service 21b5d1
                self.assertEquals(pa_elem["val"], pS_elem["val"])
Packit Service 21b5d1
Packit Service 21b5d1
    def compare_stdout(self, i, command, got_stdout, expected_stdout):
Packit Service 21b5d1
        super().compare_stdout(i, command, got_stdout, expected_stdout)
Packit Service 21b5d1
Packit Service 21b5d1
        if '-pa' in command:
Packit Service 21b5d1
            self.pa_data = self.parse_pa(got_stdout.splitlines())
Packit Service 21b5d1
        if '-pS' in command:
Packit Service 21b5d1
            self.pS_data = self.parse_pS(got_stdout.splitlines())
Packit Service 21b5d1
Packit Service 21b5d1
        if i == 1:
Packit Service 21b5d1
            self.compare_pS_pa()
Packit Service 21b5d1
Packit Service 21b5d1
    commands = [
Packit Service 21b5d1
        "$exiv2 %s $data_path/mini9.tif" % (opt) for opt in ["-pa", "-pS"]
Packit Service 21b5d1
    ]
Packit Service 21b5d1
Packit Service 21b5d1
    stderr = [""] * 2
Packit Service 21b5d1
    retval = [0] * 2
Packit Service 21b5d1
    stdout = [
Packit Service 21b5d1
        """Exif.Image.NewSubfileType                    Long        1  Primary image
Packit Service 21b5d1
Exif.Image.ImageWidth                        Short       1  9
Packit Service 21b5d1
Exif.Image.ImageLength                       Short       1  9
Packit Service 21b5d1
Exif.Image.BitsPerSample                     Short       3  8 8 8
Packit Service 21b5d1
Exif.Image.Compression                       Short       1  Uncompressed
Packit Service 21b5d1
Exif.Image.PhotometricInterpretation         Short       1  RGB
Packit Service 21b5d1
Exif.Image.DocumentName                      Ascii      24  /home/ahuggel/mini9.tif
Packit Service 21b5d1
Exif.Image.ImageDescription                  Ascii      18  Created with GIMP
Packit Service 21b5d1
Exif.Image.StripOffsets                      Long        1  8
Packit Service 21b5d1
Exif.Image.Orientation                       Short       1  top, left
Packit Service 21b5d1
Exif.Image.SamplesPerPixel                   Short       1  3
Packit Service 21b5d1
Exif.Image.RowsPerStrip                      Short       1  64
Packit Service 21b5d1
Exif.Image.StripByteCounts                   Long        1  243
Packit Service 21b5d1
Exif.Image.XResolution                       Rational    1  72
Packit Service 21b5d1
Exif.Image.YResolution                       Rational    1  72
Packit Service 21b5d1
Exif.Image.PlanarConfiguration               Short       1  1
Packit Service 21b5d1
Exif.Image.ResolutionUnit                    Short       1  inch
Packit Service 21b5d1
""",
Packit Service 21b5d1
        """STRUCTURE OF TIFF FILE (II): $data_path/mini9.tif
Packit Service 21b5d1
 address |    tag                              |      type |    count |    offset | value
Packit Service 21b5d1
     254 | 0x00fe NewSubfileType               |      LONG |        1 |           | 0
Packit Service 21b5d1
     266 | 0x0100 ImageWidth                   |     SHORT |        1 |           | 9
Packit Service 21b5d1
     278 | 0x0101 ImageLength                  |     SHORT |        1 |           | 9
Packit Service 21b5d1
     290 | 0x0102 BitsPerSample                |     SHORT |        3 |       462 | 8 8 8
Packit Service 21b5d1
     302 | 0x0103 Compression                  |     SHORT |        1 |           | 1
Packit Service 21b5d1
     314 | 0x0106 PhotometricInterpretation    |     SHORT |        1 |           | 2
Packit Service 21b5d1
     326 | 0x010d DocumentName                 |     ASCII |       24 |       468 | /home/ahuggel/mini9.tif
Packit Service 21b5d1
     338 | 0x010e ImageDescription             |     ASCII |       18 |       492 | Created with GIMP
Packit Service 21b5d1
     350 | 0x0111 StripOffsets                 |      LONG |        1 |           | 8
Packit Service 21b5d1
     362 | 0x0112 Orientation                  |     SHORT |        1 |           | 1
Packit Service 21b5d1
     374 | 0x0115 SamplesPerPixel              |     SHORT |        1 |           | 3
Packit Service 21b5d1
     386 | 0x0116 RowsPerStrip                 |     SHORT |        1 |           | 64
Packit Service 21b5d1
     398 | 0x0117 StripByteCounts              |      LONG |        1 |           | 243
Packit Service 21b5d1
     410 | 0x011a XResolution                  |  RATIONAL |        1 |       510 | 1207959552/16777216
Packit Service 21b5d1
     422 | 0x011b YResolution                  |  RATIONAL |        1 |       518 | 1207959552/16777216
Packit Service 21b5d1
     434 | 0x011c PlanarConfiguration          |     SHORT |        1 |           | 1
Packit Service 21b5d1
     446 | 0x0128 ResolutionUnit               |     SHORT |        1 |           | 2
Packit Service 21b5d1
END $data_path/mini9.tif
Packit Service 21b5d1
"""]