blob: bba36fa8c4b4e47ab8101e1bcb580cba6095dee6 [file] [log] [blame]
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +00001#!/usr/bin/python
2
3"""
4Copyright 2014 Google Inc.
5
6Use of this source code is governed by a BSD-style license that can be
7found in the LICENSE file.
8
9ImagePair class (see class docstring for details)
10"""
11
12import posixpath
13
14# Keys used within ImagePair dictionary representations.
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000015KEY__DIFFERENCE_DATA = 'differenceData'
16KEY__EXPECTATIONS_DATA = 'expectations'
17KEY__EXTRA_COLUMN_VALUES = 'extraColumns'
18KEY__IMAGE_A_URL = 'imageAUrl'
19KEY__IMAGE_B_URL = 'imageBUrl'
20KEY__IS_DIFFERENT = 'isDifferent'
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000021
22
23class ImagePair(object):
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000024 """Describes a pair of images, pixel difference info, and optional metadata.
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000025 """
26
27 def __init__(self, image_diff_db,
28 base_url, imageA_relative_url, imageB_relative_url,
29 expectations=None, extra_columns=None):
30 """
31 Args:
32 image_diff_db: ImageDiffDB instance we use to generate/store image diffs
33 base_url: base of all image URLs
34 imageA_relative_url: URL pointing at an image, relative to base_url
35 imageB_relative_url: URL pointing at an image, relative to base_url
36 expectations: optional dictionary containing expectations-specific
37 metadata (ignore-failure, bug numbers, etc.)
38 extra_columns: optional dictionary containing more metadata (test name,
39 builder name, etc.)
40 """
41 self.base_url = base_url
42 self.imageA_relative_url = imageA_relative_url
43 self.imageB_relative_url = imageB_relative_url
44 self.expectations_dict = expectations
45 self.extra_columns_dict = extra_columns
46 if imageA_relative_url == imageB_relative_url:
47 self.diff_record = None
48 else:
49 # TODO(epoger): Rather than blocking until image_diff_db can read in
50 # the image pair and generate diffs, it would be better to do it
51 # asynchronously: tell image_diff_db to download a bunch of file pairs,
52 # and only block later if we're still waiting for diff_records to come
53 # back.
54 image_diff_db.add_image_pair(
55 expected_image_locator=imageA_relative_url,
56 expected_image_url=posixpath.join(base_url, imageA_relative_url),
57 actual_image_locator=imageB_relative_url,
58 actual_image_url=posixpath.join(base_url, imageB_relative_url))
59 self.diff_record = image_diff_db.get_diff_record(
60 expected_image_locator=imageA_relative_url,
61 actual_image_locator=imageB_relative_url)
62
63 def as_dict(self):
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000064 """Returns a dictionary describing this ImagePair.
65
66 Uses the KEY__* constants as keys.
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000067 """
68 asdict = {
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000069 KEY__IMAGE_A_URL: self.imageA_relative_url,
70 KEY__IMAGE_B_URL: self.imageB_relative_url,
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000071 }
72 if self.expectations_dict:
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000073 asdict[KEY__EXPECTATIONS_DATA] = self.expectations_dict
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000074 if self.extra_columns_dict:
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000075 asdict[KEY__EXTRA_COLUMN_VALUES] = self.extra_columns_dict
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000076 if self.diff_record and (self.diff_record.get_num_pixels_differing() > 0):
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000077 asdict[KEY__IS_DIFFERENT] = True
78 asdict[KEY__DIFFERENCE_DATA] = self.diff_record.as_dict()
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000079 else:
commit-bot@chromium.org536b15f2014-02-13 17:17:05 +000080 asdict[KEY__IS_DIFFERENT] = False
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000081 return asdict