ยปCore Development>Code coverage>Lib/macurl2path.py

Python code coverage for Lib/macurl2path.py

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