OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 """ | 3 """ |
4 Copyright 2014 Google Inc. | 4 Copyright 2014 Google Inc. |
5 | 5 |
6 Use of this source code is governed by a BSD-style license that can be | 6 Use of this source code is governed by a BSD-style license that can be |
7 found in the LICENSE file. | 7 found in the LICENSE file. |
8 | 8 |
9 ImagePair class (see class docstring for details) | 9 ImagePair class (see class docstring for details) |
10 """ | 10 """ |
(...skipping 30 matching lines...) Expand all Loading... |
41 extra_columns: optional dictionary containing more metadata (test name, | 41 extra_columns: optional dictionary containing more metadata (test name, |
42 builder name, etc.) | 42 builder name, etc.) |
43 """ | 43 """ |
44 self.base_url = base_url | 44 self.base_url = base_url |
45 self.imageA_relative_url = imageA_relative_url | 45 self.imageA_relative_url = imageA_relative_url |
46 self.imageB_relative_url = imageB_relative_url | 46 self.imageB_relative_url = imageB_relative_url |
47 self.expectations_dict = expectations | 47 self.expectations_dict = expectations |
48 self.extra_columns_dict = extra_columns | 48 self.extra_columns_dict = extra_columns |
49 if not imageA_relative_url or not imageB_relative_url: | 49 if not imageA_relative_url or not imageB_relative_url: |
50 self._is_different = True | 50 self._is_different = True |
51 self._diff_record = None | 51 self.diff_record = None |
52 self._diff_record_set = True | |
53 elif imageA_relative_url == imageB_relative_url: | 52 elif imageA_relative_url == imageB_relative_url: |
54 self._is_different = False | 53 self._is_different = False |
55 self._diff_record = None | 54 self.diff_record = None |
56 self._diff_record_set = True | |
57 else: | 55 else: |
58 # Tell image_diff_db to add this ImagePair. | 56 # TODO(epoger): Rather than blocking until image_diff_db can read in |
59 # It will do so in a separate thread so as not to block this one; | 57 # the image pair and generate diffs, it would be better to do it |
60 # when you call self.get_diff_record(), it will block until the results | 58 # asynchronously: tell image_diff_db to download a bunch of file pairs, |
61 # are ready. | 59 # and only block later if we're still waiting for diff_records to come |
62 image_diff_db.add_image_pair_async( | 60 # back. |
| 61 self._is_different = True |
| 62 image_diff_db.add_image_pair( |
63 expected_image_locator=imageA_relative_url, | 63 expected_image_locator=imageA_relative_url, |
64 expected_image_url=posixpath.join(base_url, imageA_relative_url), | 64 expected_image_url=posixpath.join(base_url, imageA_relative_url), |
65 actual_image_locator=imageB_relative_url, | 65 actual_image_locator=imageB_relative_url, |
66 actual_image_url=posixpath.join(base_url, imageB_relative_url)) | 66 actual_image_url=posixpath.join(base_url, imageB_relative_url)) |
67 self._image_diff_db = image_diff_db | 67 self.diff_record = image_diff_db.get_diff_record( |
68 self._diff_record_set = False | 68 expected_image_locator=imageA_relative_url, |
69 | 69 actual_image_locator=imageB_relative_url) |
70 def get_diff_record(self): | 70 if self.diff_record and self.diff_record.get_num_pixels_differing() == 0: |
71 """Returns the DiffRecord associated with this ImagePair. | |
72 | |
73 Returns None if the images are identical, or one is missing. | |
74 This method will block until the DiffRecord is available. | |
75 """ | |
76 if not self._diff_record_set: | |
77 self._diff_record = self._image_diff_db.get_diff_record( | |
78 expected_image_locator=self.imageA_relative_url, | |
79 actual_image_locator=self.imageB_relative_url) | |
80 self._image_diff_db = None # release reference, no longer needed | |
81 if (self._diff_record and | |
82 self._diff_record.get_num_pixels_differing() == 0): | |
83 self._is_different = False | 71 self._is_different = False |
84 else: | |
85 self._is_different = True | |
86 self._diff_record_set = True | |
87 return self._diff_record | |
88 | 72 |
89 def as_dict(self): | 73 def as_dict(self): |
90 """Returns a dictionary describing this ImagePair. | 74 """Returns a dictionary describing this ImagePair. |
91 | 75 |
92 Uses the KEY__* constants as keys. | 76 Uses the KEY__* constants as keys. |
93 """ | 77 """ |
94 asdict = { | 78 asdict = { |
95 KEY__IMAGE_A_URL: self.imageA_relative_url, | 79 KEY__IMAGE_A_URL: self.imageA_relative_url, |
96 KEY__IMAGE_B_URL: self.imageB_relative_url, | 80 KEY__IMAGE_B_URL: self.imageB_relative_url, |
97 } | 81 } |
| 82 asdict[KEY__IS_DIFFERENT] = self._is_different |
98 if self.expectations_dict: | 83 if self.expectations_dict: |
99 asdict[KEY__EXPECTATIONS_DATA] = self.expectations_dict | 84 asdict[KEY__EXPECTATIONS_DATA] = self.expectations_dict |
100 if self.extra_columns_dict: | 85 if self.extra_columns_dict: |
101 asdict[KEY__EXTRA_COLUMN_VALUES] = self.extra_columns_dict | 86 asdict[KEY__EXTRA_COLUMN_VALUES] = self.extra_columns_dict |
102 diff_record = self.get_diff_record() | 87 if self.diff_record and (self.diff_record.get_num_pixels_differing() > 0): |
103 if diff_record and (diff_record.get_num_pixels_differing() > 0): | 88 asdict[KEY__DIFFERENCE_DATA] = self.diff_record.as_dict() |
104 asdict[KEY__DIFFERENCE_DATA] = diff_record.as_dict() | |
105 asdict[KEY__IS_DIFFERENT] = self._is_different | |
106 return asdict | 89 return asdict |
OLD | NEW |