Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hhhrrrttt222111
GitHub Repository: hhhrrrttt222111/Dorkify
Path: blob/master/venv/Lib/site-packages/pip/_internal/utils/urls.py
811 views
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 {url!r})"
38
.format(**locals()))
39
40
_, netloc, path, _, _ = urllib_parse.urlsplit(url)
41
42
if not netloc or netloc == 'localhost':
43
# According to RFC 8089, same as empty authority.
44
netloc = ''
45
elif sys.platform == 'win32':
46
# If we have a UNC path, prepend UNC share notation.
47
netloc = '\\\\' + netloc
48
else:
49
raise ValueError(
50
'non-local file URIs are not supported on this platform: {url!r}'
51
.format(**locals())
52
)
53
54
path = urllib_request.url2pathname(netloc + path)
55
return path
56
57