99
99
local_path_to_url = _posix_local_path_to_url
100
100
local_path_from_url = _posix_local_path_from_url
101
MIN_ABS_URLPATHLENGTH = len('file:///')
101
MIN_ABS_FILEURL_LENGTH = len('file:///')
103
103
if sys.platform == 'win32':
104
104
local_path_to_url = _win32_local_path_to_url
105
105
local_path_from_url = _win32_local_path_from_url
107
MIN_ABS_URLPATHLENGTH = len('file:///C|/')
107
MIN_ABS_FILEURL_LENGTH = len('file:///C|/')
110
def basename(url, exclude_trailing_slash=True):
111
"""Return the last component of a URL.
113
:param url: The URL in question
114
:param exclude_trailing_slash: If the url looks like "path/to/foo/"
115
ignore the final slash and return 'foo' rather than ''
117
if exclude_trailing_slash:
118
url = strip_trailing_slash(url)
110
120
def strip_trailing_slash(url):
111
121
"""Strip trailing slash, except for root paths.
113
123
The definition of 'root path' is platform-dependent.
114
But the passed in URL must be a file:/// url.
124
This assumes that all URLs are valid netloc urls, such that they
127
It searches for ://, and then refuses to remove the next '/'.
128
It can also handle relative paths
130
path/to/foo => path/to/foo
131
path/to/foo/ => path/to/foo
132
http://host/path/ => http://host/path
133
http://host/path => http://host/path
134
http://host/ => http://host/
136
file:///foo/ => file:///foo
137
# This is unique on win32 platforms, and is the only URL
138
# format which does it differently.
139
file:///C|/ => file:///C|/
116
assert url.startswith('file:///'), \
117
'strip_trailing_slash expects file:// urls (%s)' % url
118
if len(url) != MIN_ABS_URLPATHLENGTH and url[-1] == '/':
141
if not url.endswith('/'):
144
if sys.platform == 'win32' and url.startswith('file:///'):
145
# This gets handled specially, because the 'top-level'
146
# of a win32 path is actually the drive letter
147
if len(url) > MIN_ABS_FILEURL_LENGTH:
149
scheme_loc = url.find('://')
151
# This is a relative path, as it has no scheme
152
# so just chop off the last character
155
# Find the path separating slash
156
# (first slash after the ://)
157
first_path_slash = url.find('/', scheme_loc+3)
158
if first_path_slash == -1 or first_path_slash == len(url)-1:
159
# Don't chop off anything if the only slash is the path
124
166
def unescape(url):
125
167
"""Unescape relpath from url format.