urls.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import os
  2. import string
  3. import urllib.parse
  4. import urllib.request
  5. from .compat import WINDOWS
  6. def path_to_url(path: str) -> str:
  7. """
  8. Convert a path to a file: URL. The path will be made absolute and have
  9. quoted path parts.
  10. """
  11. path = os.path.normpath(os.path.abspath(path))
  12. url = urllib.parse.urljoin("file:", urllib.request.pathname2url(path))
  13. return url
  14. def url_to_path(url: str) -> str:
  15. """
  16. Convert a file: URL to a path.
  17. """
  18. assert url.startswith(
  19. "file:"
  20. ), f"You can only turn file: urls into filenames (not {url!r})"
  21. _, netloc, path, _, _ = urllib.parse.urlsplit(url)
  22. if not netloc or netloc == "localhost":
  23. # According to RFC 8089, same as empty authority.
  24. netloc = ""
  25. elif WINDOWS:
  26. # If we have a UNC path, prepend UNC share notation.
  27. netloc = "\\\\" + netloc
  28. else:
  29. raise ValueError(
  30. f"non-local file URIs are not supported on this platform: {url!r}"
  31. )
  32. path = urllib.request.url2pathname(netloc + path)
  33. # On Windows, urlsplit parses the path as something like "/C:/Users/foo".
  34. # This creates issues for path-related functions like io.open(), so we try
  35. # to detect and strip the leading slash.
  36. if (
  37. WINDOWS
  38. and not netloc # Not UNC.
  39. and len(path) >= 3
  40. and path[0] == "/" # Leading slash to strip.
  41. and path[1] in string.ascii_letters # Drive letter.
  42. and path[2:4] in (":", ":/") # Colon + end of string, or colon + absolute path.
  43. ):
  44. path = path[1:]
  45. return path