processors.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # -*- coding: utf-8 -*-
  2. """
  3. Module for API-related calls.
  4. """
  5. import urlparse
  6. def hostname_tld_migration(hostname):
  7. """
  8. Migrate transifex.net to transifex.com.
  9. :param hostname: The hostname to migrate (if needed).
  10. :returns: A hostname with the transifex.com domain (if needed).
  11. """
  12. parts = urlparse.urlparse(hostname)
  13. if parts.hostname.endswith('transifex.net'):
  14. hostname = hostname.replace('transifex.net', 'transifex.com', 1)
  15. return hostname
  16. def hostname_ssl_migration(hostname):
  17. """
  18. Migrate Transifex hostnames to use HTTPS.
  19. :param hostname: The hostname to migrate (if needed).
  20. :returns: A https hostname (if needed).
  21. """
  22. parts = urlparse.urlparse(hostname)
  23. is_transifex = (
  24. parts.hostname[-14:-3] == '.transifex.' or
  25. parts.hostname == 'transifex.net' or
  26. parts.hostname == 'transifex.com'
  27. )
  28. is_https = parts.scheme == 'https'
  29. if is_transifex and not is_https:
  30. if not parts.scheme:
  31. hostname = 'https:' + hostname
  32. else:
  33. hostname = hostname.replace(parts.scheme, 'https', 1)
  34. return hostname
  35. def visit_hostname(hostname):
  36. """
  37. Have a chance to visit a hostname before actually using it.
  38. :param hostname: The original hostname.
  39. :returns: The hostname with the necessary changes.
  40. """
  41. for processor in [hostname_ssl_migration, hostname_tld_migration, ]:
  42. hostname = processor(hostname)
  43. return hostname