180
180
def _win32_local_path_from_url(url):
181
181
"""Convert a url like file:///C:/path/to/foo into C:/path/to/foo"""
182
if not url.startswith('file:///'):
183
raise errors.InvalidURL(url, 'local urls must start with file:///')
182
if not url.startswith('file://'):
183
raise errors.InvalidURL(url, 'local urls must start with file:///, '
184
'UNC path urls must start with file://')
184
185
# We strip off all 3 slashes
185
win32_url = url[len('file:///'):]
186
if (win32_url[0] not in ('abcdefghijklmnopqrstuvwxyz'
186
win32_url = url[len('file:'):]
187
# check for UNC path: //HOST/path
188
if not win32_url.startswith('///'):
189
if (win32_url[2] == '/'
190
or win32_url[3] in '|:'):
191
raise errors.InvalidURL(url, 'Win32 UNC path urls'
192
' have form file://HOST/path')
193
return unescape(win32_url)
194
# usual local path with drive letter
195
if (win32_url[3] not in ('abcdefghijklmnopqrstuvwxyz'
187
196
'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
188
or win32_url[1] not in '|:'
189
or win32_url[2] != '/'):
197
or win32_url[4] not in '|:'
198
or win32_url[5] != '/'):
190
199
raise errors.InvalidURL(url, 'Win32 file urls start with'
191
200
' file:///x:/, where x is a valid drive letter')
192
return win32_url[0].upper() + u':' + unescape(win32_url[2:])
201
return win32_url[3].upper() + u':' + unescape(win32_url[5:])
195
204
def _win32_local_path_to_url(path):
205
214
# semantics, since 'nt' is not an available module.
206
215
win32_path = osutils._nt_normpath(
207
216
osutils._win32_abspath(path)).replace('\\', '/')
217
# check for UNC path \\HOST\path
218
if win32_path.startswith('//'):
219
return 'file:' + escape(win32_path)
208
220
return 'file:///' + win32_path[0].upper() + ':' + escape(win32_path[2:])
259
271
if path[i] not in _url_safe_characters:
260
272
chars = path[i].encode('utf-8')
261
273
path[i] = ''.join(['%%%02X' % ord(c) for c in path[i].encode('utf-8')])
262
return scheme + '://' + ''.join(path)
274
return str(scheme + '://' + ''.join(path))
265
277
def relative_url(base, other):
446
458
_hex_display_map.update((hex,'%'+hex) for hex in _no_decode_hex)
448
460
# These characters should not be escaped
449
_url_safe_characters = set('abcdefghijklmnopqrstuvwxyz'
450
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
461
_url_safe_characters = set(
462
"abcdefghijklmnopqrstuvwxyz" # Lowercase alpha
463
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" # Uppercase alpha
464
"0123456789" # Numbers
465
"_.-!~*'()" # Unreserved characters
466
"/;?:@&=+$," # Reserved characters
467
"%#" # Extra reserved characters
455
470
def unescape_for_display(url, encoding):
456
471
"""Decode what you can for a URL, so that we get a nice looking path.