| OLD | NEW |
| (Empty) |
| 1 # Copyright 2016 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 """Represents a database of on-disk traces.""" | |
| 6 | |
| 7 import json | |
| 8 | |
| 9 | |
| 10 class LoadingTraceDatabase(object): | |
| 11 def __init__(self, traces_dict): | |
| 12 """traces_dict is a dictionary mapping filenames of traces to metadata | |
| 13 about those traces.""" | |
| 14 self._traces_dict = traces_dict | |
| 15 | |
| 16 def SetTrace(self, filename, trace_dict): | |
| 17 """Sets a mapping from |filename| to |trace_dict| into the database. | |
| 18 If there is an existing mapping for filename, it is replaced. | |
| 19 """ | |
| 20 self._traces_dict[filename] = trace_dict | |
| 21 | |
| 22 def GetTraceFilesForURL(self, url): | |
| 23 """Given a URL, returns the set of filenames of traces that were generated | |
| 24 for this URL.""" | |
| 25 trace_files = [f for f in self._traces_dict.keys() | |
| 26 if self._traces_dict[f]["url"] == url] | |
| 27 return trace_files | |
| 28 | |
| 29 def ToJsonDict(self): | |
| 30 """Returns a dict representing this instance.""" | |
| 31 return self._traces_dict | |
| 32 | |
| 33 def ToJsonString(self): | |
| 34 """Returns a string representing this instance.""" | |
| 35 return json.dumps(self._traces_dict, indent=2) | |
| 36 | |
| 37 def ToJsonFile(self, json_path): | |
| 38 """Saves a json file representing this instance.""" | |
| 39 json_dict = self.ToJsonDict() | |
| 40 with open(json_path, 'w') as output_file: | |
| 41 json.dump(json_dict, output_file, indent=2) | |
| 42 | |
| 43 @classmethod | |
| 44 def FromJsonDict(cls, json_dict): | |
| 45 """Returns an instance from a dict returned by ToJsonDict().""" | |
| 46 return LoadingTraceDatabase(json_dict) | |
| 47 | |
| 48 @classmethod | |
| 49 def FromJsonString(cls, json_string): | |
| 50 """Returns an instance from a string returned by ToJsonString().""" | |
| 51 return LoadingTraceDatabase(json.loads(json_string)) | |
| 52 | |
| 53 @classmethod | |
| 54 def FromJsonFile(cls, json_path): | |
| 55 """Returns an instance from a json file saved by ToJsonFile().""" | |
| 56 with open(json_path) as input_file: | |
| 57 return cls.FromJsonDict(json.load(input_file)) | |
| OLD | NEW |