comparison lib/python3.8/site-packages/pip/_internal/utils/urls.py @ 0:9e54283cc701 draft

"planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
author guerler
date Mon, 27 Jul 2020 03:47:31 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:9e54283cc701
1 import os
2 import sys
3
4 from pip._vendor.six.moves.urllib import parse as urllib_parse
5 from pip._vendor.six.moves.urllib import request as urllib_request
6
7 from pip._internal.utils.typing import MYPY_CHECK_RUNNING
8
9 if MYPY_CHECK_RUNNING:
10 from typing import Optional, Text, Union
11
12
13 def get_url_scheme(url):
14 # type: (Union[str, Text]) -> Optional[Text]
15 if ':' not in url:
16 return None
17 return url.split(':', 1)[0].lower()
18
19
20 def path_to_url(path):
21 # type: (Union[str, Text]) -> str
22 """
23 Convert a path to a file: URL. The path will be made absolute and have
24 quoted path parts.
25 """
26 path = os.path.normpath(os.path.abspath(path))
27 url = urllib_parse.urljoin('file:', urllib_request.pathname2url(path))
28 return url
29
30
31 def url_to_path(url):
32 # type: (str) -> str
33 """
34 Convert a file: URL to a path.
35 """
36 assert url.startswith('file:'), (
37 "You can only turn file: urls into filenames (not %r)" % url)
38
39 _, netloc, path, _, _ = urllib_parse.urlsplit(url)
40
41 if not netloc or netloc == 'localhost':
42 # According to RFC 8089, same as empty authority.
43 netloc = ''
44 elif sys.platform == 'win32':
45 # If we have a UNC path, prepend UNC share notation.
46 netloc = '\\\\' + netloc
47 else:
48 raise ValueError(
49 'non-local file URIs are not supported on this platform: %r'
50 % url
51 )
52
53 path = urllib_request.url2pathname(netloc + path)
54 return path