File: gettlds.py

package info (click to toggle)
python3-openid 3.0.2%2Bgit20140828-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,672 kB
  • ctags: 2,679
  • sloc: python: 17,137; xml: 234; sh: 15; makefile: 4
file content (70 lines) | stat: -rw-r--r-- 1,744 bytes parent folder | download | duplicates (2)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
"""
Fetch the current TLD list from the IANA Web site, parse it, and print
an expression suitable for direct insertion into each library's trust
root validation module.

Usage:
  python gettlds.py (php|python|ruby)

Then cut-n-paste.
"""

import urllib.request
import sys

LANGS = {
    'php': (
        r"'/\.(",  # prefix
        "'",  # line prefix
        "|",  # separator
        "|' .",  # line suffix
        r")\.?$/'" # suffix
    ),
    'python': (
        "['",
        "'",
        "', '",
        "',",
        "']"
    ),
    'ruby': (
        "%w'",
        "",
        " ",
        "",
        "'"
    ),
}

if __name__ == '__main__':

    lang = sys.argv[1]
    prefix, line_prefix, separator, line_suffix, suffix = LANGS[lang]

    iana_url = 'http://data.iana.org/TLD/tlds-alpha-by-domain.txt'

    with urllib.request.urlopen(iana_url) as iana_resource:
        tlds = []
        output_line = ""  # initialize a line of output

        for input_line in iana_resource:
            if input_line.startswith(b'#'):  # skip comments
                continue

            tld = input_line.decode("utf-8").strip().lower()
            nxt_output_line = output_line + prefix + tld  # update current line

            if len(nxt_output_line) > 60:
                # Long enough -- print it and reinitialize to only hold the
                # most recent TLD
                print(output_line + line_suffix)
                output_line = line_prefix + tld
            else:
                # Not long enough, so update it to the concatenated version
                output_line = nxt_output_line

            prefix = separator

    # Print the final line of remaining output
    print(output_line + suffix)