- add sources.
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / page / page.py
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
5 import os
6 import re
7 import urlparse
8
9
10 class Page(object):
11   def __init__(self, url, page_set, attributes=None, base_dir=None):
12     self.url = url
13     self.page_set = page_set
14     self._base_dir = base_dir
15
16     # These attributes can be set dynamically by the page.
17     self.credentials = None
18     self.disabled = False
19     self.name = None
20     self.script_to_evaluate_on_commit = None
21
22     if attributes:
23       for k, v in attributes.iteritems():
24         setattr(self, k, v)
25
26     if not self._scheme:
27       raise ValueError('Must prepend the URL with scheme (e.g. file://)')
28
29   def __getattr__(self, name):
30     # Inherit attributes from the page set.
31     if self.page_set and hasattr(self.page_set, name):
32       return getattr(self.page_set, name)
33     raise AttributeError(
34         '%r object has no attribute %r' % (self.__class__, name))
35
36   def __str__(self):
37     return self.url
38
39   @property
40   def _scheme(self):
41     return urlparse.urlparse(self.url).scheme
42
43   @property
44   def is_file(self):
45     """Returns True iff this URL points to a file."""
46     return self._scheme == 'file'
47
48   @property
49   def is_local(self):
50     """Returns True iff this URL is local. This includes chrome:// URLs."""
51     return self._scheme == 'file' or self._scheme == 'chrome'
52
53   @property
54   def file_path(self):
55     """Returns the path of the file, stripping the scheme and query string."""
56     assert self.is_file
57     # Because ? is a valid character in a filename,
58     # we have to treat the url as a non-file by removing the scheme.
59     parsed_url = urlparse.urlparse(self.url[7:])
60     return os.path.normpath(os.path.join(
61         self._base_dir, parsed_url.netloc + parsed_url.path))
62
63   @property
64   def file_path_url(self):
65     """Returns the file path, including the params, query, and fragment."""
66     assert self.is_file
67     file_path_url = os.path.normpath(os.path.join(self._base_dir, self.url[7:]))
68     # Preserve trailing slash or backslash.
69     # It doesn't matter in a file path, but it does matter in a URL.
70     if self.url.endswith('/'):
71       file_path_url += os.sep
72     return file_path_url
73
74   @property
75   def serving_dir(self):
76     file_path = os.path.realpath(self.file_path)
77     if os.path.isdir(file_path):
78       return file_path
79     else:
80       return os.path.dirname(file_path)
81
82   @property
83   def file_safe_name(self):
84     """A version of display_name that's safe to use as a filename."""
85     # Just replace all special characters in the url with underscore.
86     return re.sub('[^a-zA-Z0-9]', '_', self.display_name)
87
88   @property
89   def display_name(self):
90     if self.name:
91       return self.name
92     if not self.is_file:
93       return self.url
94     all_urls = [p.url.rstrip('/') for p in self.page_set if p.is_file]
95     common_prefix = os.path.dirname(os.path.commonprefix(all_urls))
96     return self.url[len(common_prefix):].strip('/')
97
98   @property
99   def archive_path(self):
100     return self.page_set.WprFilePathForPage(self)