Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2015 The LUCI Authors. All rights reserved. | 1 # Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
| 4 | 4 |
| 5 """This module facilitates conversion from dictionaries to ProtoRPC messages. | 5 """This module facilitates conversion from dictionaries to ProtoRPC messages. |
| 6 | 6 |
| 7 Given a dictionary whose keys' names and values' types comport with the | 7 Given a dictionary whose keys' names and values' types comport with the |
| 8 fields defined for a protorpc.messages.Message subclass, this module tries to | 8 fields defined for a protorpc.messages.Message subclass, this module tries to |
| 9 generate a Message instance that corresponds to the provided dictionary. The | 9 generate a Message instance that corresponds to the provided dictionary. The |
| 10 "normal" use case is for ndb.Models which need to be represented as a | 10 "normal" use case is for ndb.Models which need to be represented as a |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 properties=properties) | 193 properties=properties) |
| 194 | 194 |
| 195 | 195 |
| 196 def task_result_to_rpc(entity, send_stats): | 196 def task_result_to_rpc(entity, send_stats): |
| 197 """"Returns a swarming_rpcs.TaskResult from a task_result.TaskResultSummary or | 197 """"Returns a swarming_rpcs.TaskResult from a task_result.TaskResultSummary or |
| 198 task_result.TaskRunResult. | 198 task_result.TaskRunResult. |
| 199 """ | 199 """ |
| 200 outputs_ref = ( | 200 outputs_ref = ( |
| 201 _ndb_to_rpc(swarming_rpcs.FilesRef, entity.outputs_ref) | 201 _ndb_to_rpc(swarming_rpcs.FilesRef, entity.outputs_ref) |
| 202 if entity.outputs_ref else None) | 202 if entity.outputs_ref else None) |
| 203 cipd_pins = [ | |
| 204 _ndb_to_rpc(swarming_rpcs.CipdPackage, pkg) | |
| 205 for pkg in (entity.cipd_pins or ()) | |
|
M-A Ruel
2016/08/24 02:40:23
I've always used "or []" and never realized that (
| |
| 206 ] | |
| 203 performance_stats = None | 207 performance_stats = None |
| 204 if send_stats and entity.performance_stats.is_valid: | 208 if send_stats and entity.performance_stats.is_valid: |
| 205 def op(entity): | 209 def op(entity): |
| 206 if entity: | 210 if entity: |
| 207 return _ndb_to_rpc(swarming_rpcs.OperationStats, entity) | 211 return _ndb_to_rpc(swarming_rpcs.OperationStats, entity) |
| 208 | 212 |
| 209 performance_stats = _ndb_to_rpc( | 213 performance_stats = _ndb_to_rpc( |
| 210 swarming_rpcs.PerformanceStats, | 214 swarming_rpcs.PerformanceStats, |
| 211 entity.performance_stats, | 215 entity.performance_stats, |
| 212 isolated_download=op(entity.performance_stats.isolated_download), | 216 isolated_download=op(entity.performance_stats.isolated_download), |
| 213 isolated_upload=op(entity.performance_stats.isolated_upload)) | 217 isolated_upload=op(entity.performance_stats.isolated_upload)) |
| 214 kwargs = { | 218 kwargs = { |
| 215 'bot_dimensions': _string_list_pairs_from_dict(entity.bot_dimensions or {}), | 219 'bot_dimensions': _string_list_pairs_from_dict(entity.bot_dimensions or {}), |
| 216 'performance_stats': performance_stats, | 220 'performance_stats': performance_stats, |
| 217 'outputs_ref': outputs_ref, | 221 'outputs_ref': outputs_ref, |
| 222 'cipd_pins': cipd_pins, | |
| 218 'state': swarming_rpcs.StateField(entity.state), | 223 'state': swarming_rpcs.StateField(entity.state), |
| 219 } | 224 } |
| 220 if entity.__class__ is task_result.TaskRunResult: | 225 if entity.__class__ is task_result.TaskRunResult: |
| 221 kwargs['costs_usd'] = [] | 226 kwargs['costs_usd'] = [] |
| 222 if entity.cost_usd is not None: | 227 if entity.cost_usd is not None: |
| 223 kwargs['costs_usd'].append(entity.cost_usd) | 228 kwargs['costs_usd'].append(entity.cost_usd) |
| 224 kwargs['properties_hash'] = None | 229 kwargs['properties_hash'] = None |
| 225 kwargs['tags'] = [] | 230 kwargs['tags'] = [] |
| 226 kwargs['user'] = None | 231 kwargs['user'] = None |
| 227 else: | 232 else: |
| 228 assert entity.__class__ is task_result.TaskResultSummary, entity | 233 assert entity.__class__ is task_result.TaskResultSummary, entity |
| 229 kwargs['properties_hash'] = ( | 234 kwargs['properties_hash'] = ( |
| 230 entity.properties_hash.encode('hex') | 235 entity.properties_hash.encode('hex') |
| 231 if entity.properties_hash else None) | 236 if entity.properties_hash else None) |
| 232 return _ndb_to_rpc(swarming_rpcs.TaskResult, entity, **kwargs) | 237 return _ndb_to_rpc(swarming_rpcs.TaskResult, entity, **kwargs) |
| OLD | NEW |