index.py 1.0 KB

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