1 # Copyright 2015 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 from perf_insights.mre import failure as failure_module 6 7 class DuplicateKeyError(Exception): 8 """Raised if an attempt is made to set a key more than once.""" 9 10 11 class MreResult(object): 12 13 def __init__(self, failures=None, pairs=None): 14 if failures is None: 15 failures = [] 16 if pairs is None: 17 pairs = {} 18 self._failures = failures 19 self._pairs = pairs 20 21 @property 22 def failures(self): 23 return self._failures 24 25 @property 26 def pairs(self): 27 return self._pairs 28 29 def AsDict(self): 30 d = { 31 'pairs': self._pairs 32 } 33 34 if self.failures: 35 d['failures'] = [failure.AsDict() for failure in self._failures] 36 37 return d 38 39 def AddFailure(self, failure): 40 if not isinstance(failure, failure_module.Failure): 41 raise ValueError('Attempted to add %s as Failure', failure) 42 43 self._failures.append(failure) 44 45 def AddPair(self, key, value): 46 if key in self._pairs: 47 raise DuplicateKeyError('Key ' + key + 'already exists in result.') 48 self._pairs[key] = value 49