def GetAutotestMetricValue(self, report_file): """Gets metric value from autotest benchmark report. Report example: {"avg_fps_1000_fishes": { "summary": { "units": "fps", "type": "scalar", "value": 56.733810392225671, "improvement_direction": "up" }, ..., }, ..., } self.metric = "avg_fps_1000_fishes/summary/value" Args: report_file: Path to benchmark report. Returns: Metric value in benchmark report. None if self.metric is undefined or metric does not exist in the report. """ if not self.metric: return None report = json_lib.ParseJsonFileWithComments(report_file) metric_value = json_lib.GetNestedDictValue(report, self.metric.split('/')) if metric_value is None: logging.error('Cannot get metric %s from %s', self.metric, report_file) return None if self.metric_take_average: return float(sum(metric_value)) / len(metric_value) return metric_value
def AddAccountsFromDatabase(self, account_db_path): """Add accounts from the database at |account_db_path| to self. Overrides previously loaded accounts. Args: account_db_path: path to file containing an account database. """ raw_db = json_lib.ParseJsonFileWithComments(account_db_path) json_lib.AssertIsInstance(raw_db, dict, 'accounts database') # We don't mandate that an accounts database specify either field. raw_db.setdefault(USERS_KEY, []) raw_db.setdefault(GROUPS_KEY, []) user_list = json_lib.PopValueOfType(raw_db, USERS_KEY, list, 'list of users in accounts database') group_list = json_lib.PopValueOfType(raw_db, GROUPS_KEY, list, 'list of groups in accounts database') # We do mandate that the database contain only fields we know about. if raw_db: raise ValueError('Accounts database include unknown fields: %r' % raw_db.keys()) for user in user_list: json_lib.AssertIsInstance( user, dict, 'user specification in accounts database') self._AddUser(user) for group in group_list: json_lib.AssertIsInstance( group, dict, 'group specification in accounts database') self._AddGroup(group)
def testParseJsonFileWithComments(self): """Test that we can parse a JSON file with comments.""" JSON_WITH_COMMENTS = """ { # I am a comment. "foo": [] } """ self.PatchObject(osutils, 'ReadFile', return_value=JSON_WITH_COMMENTS) self.assertEqual({u'foo': []}, json_lib.ParseJsonFileWithComments('fake path')) self.PatchObject(osutils, 'ReadFile', return_value='') self.assertRaises(ValueError, json_lib.ParseJsonFileWithComments, 'fake path')