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.
11 def __init__(self, url, page_set, attributes=None, base_dir=None):
13 self.page_set = page_set
14 self._base_dir = base_dir
16 # These attributes can be set dynamically by the page.
17 self.credentials = None
20 self.script_to_evaluate_on_commit = None
23 for k, v in attributes.iteritems():
27 raise ValueError('Must prepend the URL with scheme (e.g. file://)')
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)
34 '%r object has no attribute %r' % (self.__class__, name))
41 return urlparse.urlparse(self.url).scheme
45 """Returns True iff this URL points to a file."""
46 return self._scheme == 'file'
50 """Returns True iff this URL is local. This includes chrome:// URLs."""
51 return self._scheme == 'file' or self._scheme == 'chrome'
55 """Returns the path of the file, stripping the scheme and query string."""
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))
64 def file_path_url(self):
65 """Returns the file path, including the params, query, and fragment."""
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
75 def serving_dir(self):
76 file_path = os.path.realpath(self.file_path)
77 if os.path.isdir(file_path):
80 return os.path.dirname(file_path)
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)
89 def display_name(self):
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('/')
99 def archive_path(self):
100 return self.page_set.WprFilePathForPage(self)