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

Python code coverage for Lib/nturl2path.py

#countcontent
1n/a"""Convert a NT pathname to a file URL and vice versa."""
2n/a
3n/adef url2pathname(url):
4n/a """OS-specific conversion from a relative URL of the 'file' scheme
5n/a to a file system path; not recommended for general use."""
6n/a # e.g.
7n/a # ///C|/foo/bar/spam.foo
8n/a # and
9n/a # ///C:/foo/bar/spam.foo
10n/a # become
11n/a # C:\foo\bar\spam.foo
12n/a import string, urllib.parse
13n/a # Windows itself uses ":" even in URLs.
14n/a url = url.replace(':', '|')
15n/a if not '|' in url:
16n/a # No drive specifier, just convert slashes
17n/a if url[:4] == '////':
18n/a # path is something like ////host/path/on/remote/host
19n/a # convert this to \\host\path\on\remote\host
20n/a # (notice halving of slashes at the start of the path)
21n/a url = url[2:]
22n/a components = url.split('/')
23n/a # make sure not to convert quoted slashes :-)
24n/a return urllib.parse.unquote('\\'.join(components))
25n/a comp = url.split('|')
26n/a if len(comp) != 2 or comp[0][-1] not in string.ascii_letters:
27n/a error = 'Bad URL: ' + url
28n/a raise OSError(error)
29n/a drive = comp[0][-1].upper()
30n/a components = comp[1].split('/')
31n/a path = drive + ':'
32n/a for comp in components:
33n/a if comp:
34n/a path = path + '\\' + urllib.parse.unquote(comp)
35n/a # Issue #11474 - handing url such as |c/|
36n/a if path.endswith(':') and url.endswith('/'):
37n/a path += '\\'
38n/a return path
39n/a
40n/adef pathname2url(p):
41n/a """OS-specific conversion from a file system path to a relative URL
42n/a of the 'file' scheme; not recommended for general use."""
43n/a # e.g.
44n/a # C:\foo\bar\spam.foo
45n/a # becomes
46n/a # ///C:/foo/bar/spam.foo
47n/a import urllib.parse
48n/a if not ':' in p:
49n/a # No drive specifier, just convert slashes and quote the name
50n/a if p[:2] == '\\\\':
51n/a # path is something like \\host\path\on\remote\host
52n/a # convert this to ////host/path/on/remote/host
53n/a # (notice doubling of slashes at the start of the path)
54n/a p = '\\\\' + p
55n/a components = p.split('\\')
56n/a return urllib.parse.quote('/'.join(components))
57n/a comp = p.split(':')
58n/a if len(comp) != 2 or len(comp[0]) > 1:
59n/a error = 'Bad path: ' + p
60n/a raise OSError(error)
61n/a
62n/a drive = urllib.parse.quote(comp[0].upper())
63n/a components = comp[1].split('\\')
64n/a path = '///' + drive + ':'
65n/a for comp in components:
66n/a if comp:
67n/a path = path + '/' + urllib.parse.quote(comp)
68n/a return path