OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Schema of the JSON summary file written out by the GM tool. | 6 """Schema of the JSON summary file written out by the GM tool. |
7 | 7 |
8 This must be kept in sync with the kJsonKey_ constants in gm_expectations.cpp ! | 8 This must be kept in sync with the kJsonKey_ constants in gm_expectations.cpp ! |
9 """ | 9 """ |
10 | 10 |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 url: a URL generated with CreateGmRelativeUrl(). | 156 url: a URL generated with CreateGmRelativeUrl(). |
157 | 157 |
158 Returns: (test_name, hash_type, hash_digest) tuple. | 158 Returns: (test_name, hash_type, hash_digest) tuple. |
159 """ | 159 """ |
160 hash_type, test_name, hash_digest = GM_RELATIVE_URL_RE.match(url).groups() | 160 hash_type, test_name, hash_digest = GM_RELATIVE_URL_RE.match(url).groups() |
161 return (test_name, hash_type, hash_digest) | 161 return (test_name, hash_type, hash_digest) |
162 | 162 |
163 | 163 |
164 def LoadFromString(file_contents): | 164 def LoadFromString(file_contents): |
165 """Loads the JSON summary written out by the GM tool. | 165 """Loads the JSON summary written out by the GM tool. |
| 166 |
166 Returns a dictionary keyed by the values listed as JSONKEY_ constants | 167 Returns a dictionary keyed by the values listed as JSONKEY_ constants |
167 above.""" | 168 above; if file_contents is empty, returns None.""" |
168 # TODO(epoger): we should add a version number to the JSON file to ensure | 169 # TODO(epoger): we should add a version number to the JSON file to ensure |
169 # that the writer and reader agree on the schema (raising an exception | 170 # that the writer and reader agree on the schema (raising an exception |
170 # otherwise). | 171 # otherwise). |
| 172 if not file_contents: |
| 173 return None |
171 json_dict = json.loads(file_contents) | 174 json_dict = json.loads(file_contents) |
172 return json_dict | 175 return json_dict |
173 | 176 |
174 | 177 |
175 def LoadFromFile(file_path): | 178 def LoadFromFile(file_path): |
176 """Loads the JSON summary written out by the GM tool. | 179 """Loads the JSON summary written out by the GM tool. |
177 Returns a dictionary keyed by the values listed as JSONKEY_ constants | 180 Returns a dictionary keyed by the values listed as JSONKEY_ constants |
178 above.""" | 181 above.""" |
179 file_contents = open(file_path, 'r').read() | 182 file_contents = open(file_path, 'r').read() |
180 return LoadFromString(file_contents) | 183 return LoadFromString(file_contents) |
181 | 184 |
182 | 185 |
183 def WriteToFile(json_dict, file_path): | 186 def WriteToFile(json_dict, file_path): |
184 """Writes the JSON summary in json_dict out to file_path. | 187 """Writes the JSON summary in json_dict out to file_path. |
185 | 188 |
186 The file is written Unix-style (each line ends with just LF, not CRLF); | 189 The file is written Unix-style (each line ends with just LF, not CRLF); |
187 see https://code.google.com/p/skia/issues/detail?id=1815 for reasons.""" | 190 see https://code.google.com/p/skia/issues/detail?id=1815 for reasons.""" |
188 with io.open(file_path, mode='w', newline='', encoding='utf-8') as outfile: | 191 with io.open(file_path, mode='w', newline='', encoding='utf-8') as outfile: |
189 outfile.write(unicode(json.dumps(json_dict, outfile, sort_keys=True, | 192 outfile.write(unicode(json.dumps(json_dict, outfile, sort_keys=True, |
190 indent=2))) | 193 indent=2))) |
OLD | NEW |