OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 import json | 4 import json |
5 import urlparse | 5 import urlparse |
6 | 6 |
7 class Page(object): | 7 class Page(object): |
8 def __init__(self, url, attributes={}): | 8 def __init__(self, url, attributes=None): |
9 self.url = url | 9 self.url = url |
10 parsed_url = urlparse.urlparse(url) | 10 parsed_url = urlparse.urlparse(url) |
11 if parsed_url.scheme == None: | 11 if parsed_url.scheme == None: # pylint: disable=E1101 |
12 raise Exception('urls must be fully qualified: %s' % url) | 12 raise Exception('urls must be fully qualified: %s' % url) |
13 self.interactions = 'scroll' | 13 self.interactions = 'scroll' |
14 self.credentials = None | 14 self.credentials = None |
15 self.wait_time_after_navigate = 2 | 15 self.wait_time_after_navigate = 2 |
16 self.scroll_is_infinite = False | 16 self.scroll_is_infinite = False |
17 | 17 |
18 for k, v in attributes.iteritems(): | 18 if attributes: |
19 setattr(self, k, v) | 19 for k, v in attributes.iteritems(): |
| 20 setattr(self, k, v) |
20 | 21 |
21 def __str__(self): | 22 def __str__(self): |
22 return self.url | 23 return self.url |
23 | 24 |
24 class PageSet(object): | 25 class PageSet(object): |
25 def __init__(self, description='', file_path=''): | 26 def __init__(self, description='', file_path=''): |
26 self.description = description | 27 self.description = description |
27 self.file_path = file_path | 28 self.file_path = file_path |
28 self.pages = [] | 29 self.pages = [] |
29 | 30 |
30 @classmethod | 31 @classmethod |
31 def FromFile(cls, file_path): | 32 def FromFile(cls, file_path): |
32 with open(file_path, 'r') as f: | 33 with open(file_path, 'r') as f: |
33 contents = f.read() | 34 contents = f.read() |
34 data = json.loads(contents) | 35 data = json.loads(contents) |
35 return cls.FromDict(data, file_path) | 36 return cls.FromDict(data, file_path) |
36 | 37 |
37 @classmethod | 38 @classmethod |
38 def FromDict(cls, data, file_path=''): | 39 def FromDict(cls, data, file_path=''): |
39 page_set = cls(data['description'], file_path) | 40 page_set = cls(data['description'], file_path) |
40 for page_attributes in data['pages']: | 41 for page_attributes in data['pages']: |
41 url = page_attributes.pop('url') | 42 url = page_attributes.pop('url') |
42 page = Page(url, page_attributes) | 43 page = Page(url, page_attributes) |
43 page_set.pages.append(page) | 44 page_set.pages.append(page) |
44 return page_set | 45 return page_set |
45 | 46 |
46 def __iter__(self): | 47 def __iter__(self): |
47 return self.pages.__iter__() | 48 return self.pages.__iter__() |
OLD | NEW |