OLD | NEW |
| (Empty) |
1 # Copyright (c) 2012 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 import csv | |
5 import json | |
6 import os | |
7 import urlparse | |
8 | |
9 from telemetry import page as page_module | |
10 from telemetry import page_set_archive_info | |
11 | |
12 class PageSet(object): | |
13 def __init__(self, file_path='', attributes=None): | |
14 self.description = '' | |
15 self.archive_data_file = '' | |
16 self.file_path = file_path | |
17 self.credentials_path = None | |
18 self.user_agent_type = None | |
19 | |
20 if attributes: | |
21 for k, v in attributes.iteritems(): | |
22 setattr(self, k, v) | |
23 | |
24 self.pages = [] | |
25 | |
26 if self.archive_data_file: | |
27 base_dir = os.path.dirname(file_path) | |
28 self.wpr_archive_info = page_set_archive_info.PageSetArchiveInfo.FromFile( | |
29 os.path.join(base_dir, self.archive_data_file), file_path) | |
30 else: | |
31 self.wpr_archive_info = None | |
32 | |
33 @classmethod | |
34 def FromFile(cls, file_path): | |
35 with open(file_path, 'r') as f: | |
36 contents = f.read() | |
37 data = json.loads(contents) | |
38 return cls.FromDict(data, file_path) | |
39 | |
40 @classmethod | |
41 def FromDict(cls, data, file_path=''): | |
42 page_set = cls(file_path, data) | |
43 for page_attributes in data['pages']: | |
44 url = page_attributes.pop('url') | |
45 page = page_module.Page(url, page_set, attributes=page_attributes, | |
46 base_dir=os.path.dirname(file_path)) | |
47 page_set.pages.append(page) | |
48 return page_set | |
49 | |
50 def ContainsOnlyFileURLs(self): | |
51 for page in self.pages: | |
52 parsed_url = urlparse.urlparse(page.url) | |
53 if parsed_url.scheme != 'file': | |
54 return False | |
55 return True | |
56 | |
57 def ReorderPageSet(self, results_file): | |
58 """Reorders this page set based on the results of a past run.""" | |
59 page_set_dict = {} | |
60 for page in self.pages: | |
61 page_set_dict[page.url] = page | |
62 | |
63 pages = [] | |
64 with open(results_file, 'rb') as csv_file: | |
65 csv_reader = csv.reader(csv_file) | |
66 csv_header = csv_reader.next() | |
67 | |
68 if 'url' not in csv_header: | |
69 raise Exception('Unusable results_file.') | |
70 | |
71 url_index = csv_header.index('url') | |
72 | |
73 for csv_row in csv_reader: | |
74 if csv_row[url_index] in page_set_dict: | |
75 pages.append(page_set_dict[csv_row[url_index]]) | |
76 else: | |
77 raise Exception('Unusable results_file.') | |
78 | |
79 return pages | |
80 | |
81 def WprFilePathForPage(self, page): | |
82 if not self.wpr_archive_info: | |
83 return None | |
84 return self.wpr_archive_info.WprFilePathForPage(page) | |
85 | |
86 def __iter__(self): | |
87 return self.pages.__iter__() | |
88 | |
89 def __len__(self): | |
90 return len(self.pages) | |
91 | |
92 def __getitem__(self, key): | |
93 return self.pages[key] | |
94 | |
95 def __setitem__(self, key, value): | |
96 self.pages[key] = value | |
OLD | NEW |