index.py 1.0 KB

12345678910111213141516171819202122232425262728
  1. import urllib.parse
  2. class PackageIndex:
  3. """Represents a Package Index and provides easier access to endpoints"""
  4. __slots__ = ["url", "netloc", "simple_url", "pypi_url", "file_storage_domain"]
  5. def __init__(self, url: str, file_storage_domain: str) -> None:
  6. super().__init__()
  7. self.url = url
  8. self.netloc = urllib.parse.urlsplit(url).netloc
  9. self.simple_url = self._url_for_path("simple")
  10. self.pypi_url = self._url_for_path("pypi")
  11. # This is part of a temporary hack used to block installs of PyPI
  12. # packages which depend on external urls only necessary until PyPI can
  13. # block such packages themselves
  14. self.file_storage_domain = file_storage_domain
  15. def _url_for_path(self, path: str) -> str:
  16. return urllib.parse.urljoin(self.url, path)
  17. PyPI = PackageIndex("https://pypi.org/", file_storage_domain="files.pythonhosted.org")
  18. TestPyPI = PackageIndex(
  19. "https://test.pypi.org/", file_storage_domain="test-files.pythonhosted.org"
  20. )