Path: blob/master/venv/Lib/site-packages/pip/_internal/utils/urls.py
811 views
import os1import sys23from pip._vendor.six.moves.urllib import parse as urllib_parse4from pip._vendor.six.moves.urllib import request as urllib_request56from pip._internal.utils.typing import MYPY_CHECK_RUNNING78if MYPY_CHECK_RUNNING:9from typing import Optional, Text, Union101112def get_url_scheme(url):13# type: (Union[str, Text]) -> Optional[Text]14if ':' not in url:15return None16return url.split(':', 1)[0].lower()171819def path_to_url(path):20# type: (Union[str, Text]) -> str21"""22Convert a path to a file: URL. The path will be made absolute and have23quoted path parts.24"""25path = os.path.normpath(os.path.abspath(path))26url = urllib_parse.urljoin('file:', urllib_request.pathname2url(path))27return url282930def url_to_path(url):31# type: (str) -> str32"""33Convert a file: URL to a path.34"""35assert url.startswith('file:'), (36"You can only turn file: urls into filenames (not {url!r})"37.format(**locals()))3839_, netloc, path, _, _ = urllib_parse.urlsplit(url)4041if not netloc or netloc == 'localhost':42# According to RFC 8089, same as empty authority.43netloc = ''44elif sys.platform == 'win32':45# If we have a UNC path, prepend UNC share notation.46netloc = '\\\\' + netloc47else:48raise ValueError(49'non-local file URIs are not supported on this platform: {url!r}'50.format(**locals())51)5253path = urllib_request.url2pathname(netloc + path)54return path555657