| 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 |
| 52 elif imageA_relative_url == imageB_relative_url: | 53 elif imageA_relative_url == imageB_relative_url: |
| 53 self._is_different = False | 54 self._is_different = False |
| 54 self.diff_record = None | 55 self._diff_record = None |
| 56 self._diff_record_set = True |
| 55 else: | 57 else: |
| 56 # TODO(epoger): Rather than blocking until image_diff_db can read in | 58 # Tell image_diff_db to add this ImagePair. |
| 57 # the image pair and generate diffs, it would be better to do it | 59 # It will do so in a separate thread so as not to block this one; |
| 58 # asynchronously: tell image_diff_db to download a bunch of file pairs, | 60 # when you call self.get_diff_record(), it will block until the results |
| 59 # and only block later if we're still waiting for diff_records to come | 61 # are ready. |
| 60 # back. | 62 image_diff_db.add_image_pair_async( |
| 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.diff_record = image_diff_db.get_diff_record( | 67 self._image_diff_db = image_diff_db |
| 68 expected_image_locator=imageA_relative_url, | 68 self._diff_record_set = False |
| 69 actual_image_locator=imageB_relative_url) | 69 |
| 70 if self.diff_record and self.diff_record.get_num_pixels_differing() == 0: | 70 def get_diff_record(self): |
| 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): |
| 71 self._is_different = False | 83 self._is_different = False |
| 84 else: |
| 85 self._is_different = True |
| 86 self._diff_record_set = True |
| 87 return self._diff_record |
| 72 | 88 |
| 73 def as_dict(self): | 89 def as_dict(self): |
| 74 """Returns a dictionary describing this ImagePair. | 90 """Returns a dictionary describing this ImagePair. |
| 75 | 91 |
| 76 Uses the KEY__* constants as keys. | 92 Uses the KEY__* constants as keys. |
| 77 """ | 93 """ |
| 78 asdict = { | 94 asdict = { |
| 79 KEY__IMAGE_A_URL: self.imageA_relative_url, | 95 KEY__IMAGE_A_URL: self.imageA_relative_url, |
| 80 KEY__IMAGE_B_URL: self.imageB_relative_url, | 96 KEY__IMAGE_B_URL: self.imageB_relative_url, |
| 81 } | 97 } |
| 82 asdict[KEY__IS_DIFFERENT] = self._is_different | |
| 83 if self.expectations_dict: | 98 if self.expectations_dict: |
| 84 asdict[KEY__EXPECTATIONS_DATA] = self.expectations_dict | 99 asdict[KEY__EXPECTATIONS_DATA] = self.expectations_dict |
| 85 if self.extra_columns_dict: | 100 if self.extra_columns_dict: |
| 86 asdict[KEY__EXTRA_COLUMN_VALUES] = self.extra_columns_dict | 101 asdict[KEY__EXTRA_COLUMN_VALUES] = self.extra_columns_dict |
| 87 if self.diff_record and (self.diff_record.get_num_pixels_differing() > 0): | 102 diff_record = self.get_diff_record() |
| 88 asdict[KEY__DIFFERENCE_DATA] = self.diff_record.as_dict() | 103 if diff_record and (diff_record.get_num_pixels_differing() > 0): |
| 104 asdict[KEY__DIFFERENCE_DATA] = diff_record.as_dict() |
| 105 asdict[KEY__IS_DIFFERENT] = self._is_different |
| 89 return asdict | 106 return asdict |
| OLD | NEW |