1 | n/a | """Macintosh-specific module for conversion between pathnames and URLs. |
---|
2 | n/a | |
---|
3 | n/a | Do not import directly; use urllib instead.""" |
---|
4 | n/a | |
---|
5 | n/a | import urllib.parse |
---|
6 | n/a | import os |
---|
7 | n/a | |
---|
8 | n/a | __all__ = ["url2pathname","pathname2url"] |
---|
9 | n/a | |
---|
10 | n/a | def url2pathname(pathname): |
---|
11 | n/a | """OS-specific conversion from a relative URL of the 'file' scheme |
---|
12 | n/a | to a file system path; not recommended for general use.""" |
---|
13 | n/a | # |
---|
14 | n/a | # XXXX The .. handling should be fixed... |
---|
15 | n/a | # |
---|
16 | n/a | tp = urllib.parse.splittype(pathname)[0] |
---|
17 | n/a | if tp and tp != 'file': |
---|
18 | n/a | raise RuntimeError('Cannot convert non-local URL to pathname') |
---|
19 | n/a | # Turn starting /// into /, an empty hostname means current host |
---|
20 | n/a | if pathname[:3] == '///': |
---|
21 | n/a | pathname = pathname[2:] |
---|
22 | n/a | elif pathname[:2] == '//': |
---|
23 | n/a | raise RuntimeError('Cannot convert non-local URL to pathname') |
---|
24 | n/a | components = pathname.split('/') |
---|
25 | n/a | # Remove . and embedded .. |
---|
26 | n/a | i = 0 |
---|
27 | n/a | while i < len(components): |
---|
28 | n/a | if components[i] == '.': |
---|
29 | n/a | del components[i] |
---|
30 | n/a | elif components[i] == '..' and i > 0 and \ |
---|
31 | n/a | components[i-1] not in ('', '..'): |
---|
32 | n/a | del components[i-1:i+1] |
---|
33 | n/a | i = i-1 |
---|
34 | n/a | elif components[i] == '' and i > 0 and components[i-1] != '': |
---|
35 | n/a | del components[i] |
---|
36 | n/a | else: |
---|
37 | n/a | i = i+1 |
---|
38 | n/a | if not components[0]: |
---|
39 | n/a | # Absolute unix path, don't start with colon |
---|
40 | n/a | rv = ':'.join(components[1:]) |
---|
41 | n/a | else: |
---|
42 | n/a | # relative unix path, start with colon. First replace |
---|
43 | n/a | # leading .. by empty strings (giving ::file) |
---|
44 | n/a | i = 0 |
---|
45 | n/a | while i < len(components) and components[i] == '..': |
---|
46 | n/a | components[i] = '' |
---|
47 | n/a | i = i + 1 |
---|
48 | n/a | rv = ':' + ':'.join(components) |
---|
49 | n/a | # and finally unquote slashes and other funny characters |
---|
50 | n/a | return urllib.parse.unquote(rv) |
---|
51 | n/a | |
---|
52 | n/a | def pathname2url(pathname): |
---|
53 | n/a | """OS-specific conversion from a file system path to a relative URL |
---|
54 | n/a | of the 'file' scheme; not recommended for general use.""" |
---|
55 | n/a | if '/' in pathname: |
---|
56 | n/a | raise RuntimeError("Cannot convert pathname containing slashes") |
---|
57 | n/a | components = pathname.split(':') |
---|
58 | n/a | # Remove empty first and/or last component |
---|
59 | n/a | if components[0] == '': |
---|
60 | n/a | del components[0] |
---|
61 | n/a | if components[-1] == '': |
---|
62 | n/a | del components[-1] |
---|
63 | n/a | # Replace empty string ('::') by .. (will result in '/../' later) |
---|
64 | n/a | for i in range(len(components)): |
---|
65 | n/a | if components[i] == '': |
---|
66 | n/a | components[i] = '..' |
---|
67 | n/a | # Truncate names longer than 31 bytes |
---|
68 | n/a | components = map(_pncomp2url, components) |
---|
69 | n/a | |
---|
70 | n/a | if os.path.isabs(pathname): |
---|
71 | n/a | return '/' + '/'.join(components) |
---|
72 | n/a | else: |
---|
73 | n/a | return '/'.join(components) |
---|
74 | n/a | |
---|
75 | n/a | def _pncomp2url(component): |
---|
76 | n/a | # We want to quote slashes |
---|
77 | n/a | return urllib.parse.quote(component[:31], safe='') |
---|