MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / url2pathname

Function url2pathname

tools/python-3.11.9-amd64/Lib/nturl2path.py:8–43  ·  view source on GitHub ↗

OS-specific conversion from a relative URL of the 'file' scheme to a file system path; not recommended for general use.

(url)

Source from the content-addressed store, hash-verified

6# Testing is done through test_urllib.
7
8def url2pathname(url):
9 """OS-specific conversion from a relative URL of the 'file' scheme
10 to a file system path; not recommended for general use."""
11 # e.g.
12 # ///C|/foo/bar/spam.foo
13 # and
14 # ///C:/foo/bar/spam.foo
15 # become
16 # C:\foo\bar\spam.foo
17 import string, urllib.parse
18 # Windows itself uses ":" even in URLs.
19 url = url.replace(':', '|')
20 if not '|' in url:
21 # No drive specifier, just convert slashes
22 if url[:4] == '////':
23 # path is something like ////host/path/on/remote/host
24 # convert this to \\host\path\on\remote\host
25 # (notice halving of slashes at the start of the path)
26 url = url[2:]
27 components = url.split('/')
28 # make sure not to convert quoted slashes :-)
29 return urllib.parse.unquote('\\'.join(components))
30 comp = url.split('|')
31 if len(comp) != 2 or comp[0][-1] not in string.ascii_letters:
32 error = 'Bad URL: ' + url
33 raise OSError(error)
34 drive = comp[0][-1].upper()
35 components = comp[1].split('/')
36 path = drive + ':'
37 for comp in components:
38 if comp:
39 path = path + '\\' + urllib.parse.unquote(comp)
40 # Issue #11474 - handing url such as |c/|
41 if path.endswith(':') and url.endswith('/'):
42 path += '\\'
43 return path
44
45def pathname2url(p):
46 """OS-specific conversion from a file system path to a relative URL

Callers 3

open_local_fileMethod · 0.90
retrieveMethod · 0.90
open_local_fileMethod · 0.90

Calls 5

upperMethod · 0.80
endswithMethod · 0.80
replaceMethod · 0.45
splitMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected