OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
2 | |
3 """ | |
4 Copyright 2014 Google Inc. | |
5 | |
6 Use of this source code is governed by a BSD-style license that can be | |
7 found in the LICENSE file. | |
8 | |
9 ImagePair class (see class docstring for details) | |
10 """ | |
11 | |
12 import posixpath | |
13 | |
14 class ImagePair(object): | |
15 """ | |
16 Describes a pair of images, along with optional metadata (pixel difference | |
17 metrics, whether to ignore mismatches, etc.) | |
18 """ | |
19 | |
20 def __init__(self, image_diff_db, | |
21 base_url, imageA_relative_url, imageB_relative_url, | |
22 expectations=None, extra_columns=None): | |
23 """ | |
24 Args: | |
25 image_diff_db: ImageDiffDB instance we use to generate/store image diffs | |
26 base_url: base of all image URLs | |
27 imageA_relative_url: URL pointing at an image, relative to base_url | |
28 imageB_relative_url: URL pointing at an image, relative to base_url | |
29 expectations: optional dictionary containing expectations-specific | |
30 metadata (ignore-failure, bug numbers, etc.) | |
31 extra_columns: optional dictionary containing more metadata (test name, | |
32 builder name, etc.) | |
33 """ | |
34 self.base_url = base_url | |
35 self.imageA_relative_url = imageA_relative_url | |
36 self.imageB_relative_url = imageB_relative_url | |
37 self.expectations_dict = expectations | |
38 self.extra_columns_dict = extra_columns | |
39 if imageA_relative_url == imageB_relative_url: | |
40 self.diff_record = None | |
41 else: | |
42 # TODO(epoger): Rather than blocking until image_diff_db can read in | |
43 # the image pair and generate diffs, it would be better to do it | |
44 # asynchronously: tell image_diff_db to download a bunch of file pairs, | |
45 # and only block later if we're still waiting for diff_records to come | |
46 # back. | |
47 image_diff_db.add_image_pair( | |
48 expected_image_locator=imageA_relative_url, | |
49 expected_image_url=posixpath.join(base_url, imageA_relative_url), | |
50 actual_image_locator=imageB_relative_url, | |
51 actual_image_url=posixpath.join(base_url, imageB_relative_url)) | |
52 self.diff_record = image_diff_db.get_diff_record( | |
53 expected_image_locator=imageA_relative_url, | |
54 actual_image_locator=imageB_relative_url) | |
55 | |
56 def as_dict(self): | |
57 """ | |
58 Return a dictionary describing this ImagePair, as needed when constructing | |
59 the JSON representation. | |
60 """ | |
61 asdict = { | |
62 'imageAUrl': self.imageA_relative_url, | |
63 'imageBUrl': self.imageB_relative_url, | |
64 } | |
65 if self.expectations_dict: | |
66 asdict['expectationsData'] = self.expectations_dict | |
rmistry
2014/02/10 17:34:54
Keep the names of the keys as top-level constants?
epoger
2014/02/10 18:01:16
Done.
| |
67 if self.extra_columns_dict: | |
68 asdict['extraColumnValues'] = self.extra_columns_dict | |
69 if self.diff_record and (self.diff_record.get_num_pixels_differing() > 0): | |
70 asdict['isDifferent'] = True | |
71 asdict['differenceData'] = self.diff_record.as_dict() | |
72 else: | |
73 asdict['isDifferent'] = False | |
74 return asdict | |
OLD | NEW |