_format.py 626 B

123456789101112131415161718192021222324252627
  1. from __future__ import annotations
  2. from typing import TYPE_CHECKING
  3. if TYPE_CHECKING:
  4. from mdurl._url import URL
  5. def format(url: URL) -> str: # noqa: A001
  6. result = ""
  7. result += url.protocol or ""
  8. result += "//" if url.slashes else ""
  9. result += url.auth + "@" if url.auth else ""
  10. if url.hostname and ":" in url.hostname:
  11. # ipv6 address
  12. result += "[" + url.hostname + "]"
  13. else:
  14. result += url.hostname or ""
  15. result += ":" + url.port if url.port else ""
  16. result += url.pathname or ""
  17. result += url.search or ""
  18. result += url.hash or ""
  19. return result