blob: a68821d0b732706252edd66c4b8b808cb76c6b66 [file] [log] [blame]
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +00001"""Macintosh-specific module for conversion between pathnames and URLs.
2
3Do not import directly; use urllib instead."""
Guido van Rossum1acbffe1996-05-28 23:52:06 +00004
Jeremy Hylton1afc1692008-06-18 20:49:58 +00005import urllib.parse
Guido van Rossum1acbffe1996-05-28 23:52:06 +00006import os
7
Skip Montanaro17ab1232001-01-24 06:27:27 +00008__all__ = ["url2pathname","pathname2url"]
9
Guido van Rossum1acbffe1996-05-28 23:52:06 +000010def url2pathname(pathname):
Georg Brandlc0b24732005-12-26 22:53:56 +000011 """OS-specific conversion from a relative URL of the 'file' scheme
12 to a file system path; not recommended for general use."""
Guido van Rossum1acbffe1996-05-28 23:52:06 +000013 #
14 # XXXX The .. handling should be fixed...
15 #
Senthil Kumaranf70ce682009-08-16 06:43:50 +000016 tp = urllib.parse.splittype(pathname)[0]
Fred Drake8152d322000-12-12 23:20:45 +000017 if tp and tp != 'file':
Collin Winterce36ad82007-08-30 01:19:48 +000018 raise RuntimeError('Cannot convert non-local URL to pathname')
Guido van Rossum116b31b1999-06-01 14:36:56 +000019 # Turn starting /// into /, an empty hostname means current host
20 if pathname[:3] == '///':
Tim Peters07e99cb2001-01-14 23:47:14 +000021 pathname = pathname[2:]
Guido van Rossum116b31b1999-06-01 14:36:56 +000022 elif pathname[:2] == '//':
Collin Winterce36ad82007-08-30 01:19:48 +000023 raise RuntimeError('Cannot convert non-local URL to pathname')
Eric S. Raymonddb5ebc72001-02-09 09:48:45 +000024 components = pathname.split('/')
Guido van Rossum1acbffe1996-05-28 23:52:06 +000025 # Remove . and embedded ..
26 i = 0
27 while i < len(components):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000028 if components[i] == '.':
29 del components[i]
30 elif components[i] == '..' and i > 0 and \
31 components[i-1] not in ('', '..'):
32 del components[i-1:i+1]
33 i = i-1
Fred Drake8152d322000-12-12 23:20:45 +000034 elif components[i] == '' and i > 0 and components[i-1] != '':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000035 del components[i]
36 else:
37 i = i+1
Guido van Rossum1acbffe1996-05-28 23:52:06 +000038 if not components[0]:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000039 # Absolute unix path, don't start with colon
Eric S. Raymonddb5ebc72001-02-09 09:48:45 +000040 rv = ':'.join(components[1:])
Guido van Rossum1acbffe1996-05-28 23:52:06 +000041 else:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000042 # relative unix path, start with colon. First replace
43 # leading .. by empty strings (giving ::file)
44 i = 0
45 while i < len(components) and components[i] == '..':
46 components[i] = ''
47 i = i + 1
Eric S. Raymonddb5ebc72001-02-09 09:48:45 +000048 rv = ':' + ':'.join(components)
Guido van Rossum4ff6d271998-08-06 13:37:21 +000049 # and finally unquote slashes and other funny characters
Senthil Kumaranf70ce682009-08-16 06:43:50 +000050 return urllib.parse.unquote(rv)
Guido van Rossum1acbffe1996-05-28 23:52:06 +000051
52def pathname2url(pathname):
Georg Brandlc0b24732005-12-26 22:53:56 +000053 """OS-specific conversion from a file system path to a relative URL
54 of the 'file' scheme; not recommended for general use."""
Guido van Rossum1acbffe1996-05-28 23:52:06 +000055 if '/' in pathname:
Collin Winterce36ad82007-08-30 01:19:48 +000056 raise RuntimeError("Cannot convert pathname containing slashes")
Eric S. Raymonddb5ebc72001-02-09 09:48:45 +000057 components = pathname.split(':')
Guido van Rossum32f92ca1997-05-20 16:00:07 +000058 # Remove empty first and/or last component
59 if components[0] == '':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000060 del components[0]
Guido van Rossum32f92ca1997-05-20 16:00:07 +000061 if components[-1] == '':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000062 del components[-1]
Guido van Rossum1acbffe1996-05-28 23:52:06 +000063 # Replace empty string ('::') by .. (will result in '/../' later)
Guido van Rossum32f92ca1997-05-20 16:00:07 +000064 for i in range(len(components)):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000065 if components[i] == '':
66 components[i] = '..'
Guido van Rossum1acbffe1996-05-28 23:52:06 +000067 # Truncate names longer than 31 bytes
Guido van Rossum4ff6d271998-08-06 13:37:21 +000068 components = map(_pncomp2url, components)
Guido van Rossum1acbffe1996-05-28 23:52:06 +000069
70 if os.path.isabs(pathname):
Eric S. Raymonddb5ebc72001-02-09 09:48:45 +000071 return '/' + '/'.join(components)
Guido van Rossum1acbffe1996-05-28 23:52:06 +000072 else:
Eric S. Raymonddb5ebc72001-02-09 09:48:45 +000073 return '/'.join(components)
Tim Peters07e99cb2001-01-14 23:47:14 +000074
Guido van Rossum4ff6d271998-08-06 13:37:21 +000075def _pncomp2url(component):
Jeremy Hylton1afc1692008-06-18 20:49:58 +000076 # We want to quote slashes
Senthil Kumaranf70ce682009-08-16 06:43:50 +000077 return urllib.parse.quote(component[:31], safe='')