summaryrefslogtreecommitdiff
path: root/tools/generate-isocodes.py
blob: 3ccc9aea381d864cf5f25199ed1a68073b2143ef (plain)
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
#!/usr/bin/python3

# Generate ISO codes for use with e.g. locale subdir checks
# http://alioth.debian.org/projects/pkg-isocodes/

import codecs
import json
import os
from pprint import pprint
import sys
from urllib.request import urlopen


iso_3166_1_url = os.environ.get('ISO_3166_1_URL', 'https://salsa.debian.org/iso-codes-team/iso-codes/raw/main/data/iso_3166-1.json')
iso_639_3_url = os.environ.get('ISO_639_3_URL', 'https://salsa.debian.org/iso-codes-team/iso-codes/raw/main/data/iso_639-3.json')
iso_639_2_url = os.environ.get('ISO_639_2_URL', 'https://salsa.debian.org/iso-codes-team/iso-codes/raw/main/data/iso_639-2.json')
iso_15924_url = os.environ.get('ISO_15924_URL', 'https://salsa.debian.org/iso-codes-team/iso-codes/raw/main/data/iso_15924.json')

langs = set()
countries = set()

# country codes (2 letters)
with urlopen(iso_3166_1_url) as f:
    data = json.load(codecs.getreader('utf-8')(f))
    for entry in data['3166-1']:
        countries.add(entry['alpha_2'])

# language codes (2 or 3 letters, 3 only for ones we don't have 2-letter one)
with urlopen(iso_639_3_url) as f:
    data = json.load(codecs.getreader('utf-8')(f))
    for entry in data['639-3']:
        langs.add(entry.get('alpha_2') or entry['alpha_3'])
# Need to check iso-639-2 for collective language codes not in iso-639-3
with urlopen(iso_639_2_url) as f:
    data = json.load(codecs.getreader('utf-8')(f))
    for entry in data['639-2']:
        entry_code = entry.get('alpha_2') or entry['alpha_3']
        if entry_code not in langs:
            langs.add(entry_code)
# ISO 15924, Codes for the representation of names of scripts
with urlopen(iso_15924_url) as f:
    data = json.load(codecs.getreader('utf-8')(f))
    for entry in data['15924']:
        countries.add(entry['alpha_4'])

print(f'# Generated with {sys.argv[0]}')
print('')
print('LANGUAGES = \\')
pprint(langs)
print('')
print('COUNTRIES = \\')
pprint(countries)