File: processors.py

package info (click to toggle)
0ad 0.0.17-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 51,252 kB
  • sloc: cpp: 223,208; ansic: 31,240; python: 16,343; perl: 4,083; sh: 1,011; makefile: 914; xml: 733; java: 621; ruby: 229; erlang: 53; sql: 40
file content (54 lines) | stat: -rw-r--r-- 1,492 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -*- coding: utf-8 -*-

"""
Module for API-related calls.
"""

import urlparse


def hostname_tld_migration(hostname):
    """
    Migrate transifex.net to transifex.com.

    :param hostname: The hostname to migrate (if needed).
    :returns: A hostname with the transifex.com domain (if needed).
    """
    parts = urlparse.urlparse(hostname)
    if parts.hostname.endswith('transifex.net'):
        hostname = hostname.replace('transifex.net', 'transifex.com', 1)
    return hostname


def hostname_ssl_migration(hostname):
    """
    Migrate Transifex hostnames to use HTTPS.

    :param hostname: The hostname to migrate (if needed).
    :returns: A https hostname (if needed).
    """
    parts = urlparse.urlparse(hostname)
    is_transifex = (
        parts.hostname[-14:-3] == '.transifex.' or
        parts.hostname == 'transifex.net' or
        parts.hostname == 'transifex.com'
    )
    is_https = parts.scheme == 'https'
    if is_transifex and not is_https:
        if not parts.scheme:
            hostname = 'https:' + hostname
        else:
            hostname = hostname.replace(parts.scheme, 'https', 1)
    return hostname


def visit_hostname(hostname):
    """
    Have a chance to visit a hostname before actually using it.

    :param hostname: The original hostname.
    :returns: The hostname with the necessary changes.
    """
    for processor in [hostname_ssl_migration, hostname_tld_migration, ]:
        hostname = processor(hostname)
    return hostname