| 1 |
#!/usr/bin/env python3 |
| 2 |
# encoding=UTF-8 |
| 3 |
|
| 4 |
# Copyright © 2012-2025 Jakub Wilk <[email protected]> |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the “Software”), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in |
| 14 |
# all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 |
# SOFTWARE. |
| 23 |
|
| 24 |
import datetime |
| 25 |
import functools |
| 26 |
import json |
| 27 |
import os |
| 28 |
import subprocess as ipc |
| 29 |
import sys |
| 30 |
|
| 31 |
int(0_0) # Python >= 3.6 is required |
| 32 |
|
| 33 |
class Panic(ValueError): |
| 34 |
pass |
| 35 |
|
| 36 |
@functools.lru_cache() |
| 37 |
def get_iso_codes_version(): |
| 38 |
version = ipc.check_output(['pkg-config', 'iso-codes', '--modversion']) |
| 39 |
version = version.decode('ASCII').strip() |
| 40 |
return version |
| 41 |
|
| 42 |
@functools.lru_cache() |
| 43 |
def get_iso_codes_dir(): |
| 44 |
prefix = ipc.check_output(['pkg-config', 'iso-codes', '--variable=prefix']) |
| 45 |
prefix = prefix.decode('ASCII').strip() |
| 46 |
return f'{prefix}/share/iso-codes/json' |
| 47 |
|
| 48 |
def main(): |
| 49 |
basedir = os.path.join( |
| 50 |
os.path.dirname(__file__), |
| 51 |
os.pardir, |
| 52 |
) |
| 53 |
path = os.path.join(basedir, 'data', 'iso-codes') |
| 54 |
sys.stdout = open(path + '.tmp', 'wt', encoding='UTF-8') # pylint: disable=consider-using-with |
| 55 |
iso_codes_version = get_iso_codes_version() |
| 56 |
today = datetime.date.today() |
| 57 |
print(f'''\ |
| 58 |
# This file has been generated automatically by private/update-iso-codes. |
| 59 |
# Do not edit. |
| 60 |
# iso-codes version: {iso_codes_version} |
| 61 |
# Last update: {today} |
| 62 |
''') |
| 63 |
generate_iso_639() |
| 64 |
generate_iso_3166() |
| 65 |
sys.stdout.close() |
| 66 |
os.rename(path + '.tmp', path) |
| 67 |
|
| 68 |
def load_json(path): |
| 69 |
with open(path, 'rt', encoding='UTF-8') as file: |
| 70 |
return json.load(file) |
| 71 |
|
| 72 |
def generate_iso_639(): |
| 73 |
# ======================= |
| 74 |
# ISO 639: language codes |
| 75 |
# ======================= |
| 76 |
l2t_to_2b = {} |
| 77 |
iso_639 = {} |
| 78 |
data = load_json(get_iso_codes_dir() + '/iso_639-2.json') |
| 79 |
for item in data['639-2']: |
| 80 |
l2t = item['alpha_3'] |
| 81 |
l2b = item.get('bibliographic', l2t) |
| 82 |
if l2b == l2t == 'qaa-qtz': |
| 83 |
continue |
| 84 |
for l in l2b, l2t: |
| 85 |
if len(l) != 3: |
| 86 |
raise Panic(f'len({l!r}) != 3') |
| 87 |
if l2b != l2t: |
| 88 |
l2t_to_2b[l2t] = l2b |
| 89 |
iso_639 = {} |
| 90 |
data = load_json(get_iso_codes_dir() + '/iso_639-3.json') |
| 91 |
for item in data['639-3']: |
| 92 |
code = item['alpha_3'] |
| 93 |
if len(code) != 3: |
| 94 |
raise Panic(f'len({code!r}) != 3') |
| 95 |
code1 = item.get('alpha_2') |
| 96 |
code2 = item.get('terminology') |
| 97 |
if code2 is None: |
| 98 |
# We're not interested in languages that are not in 639-2 (yet?). |
| 99 |
continue |
| 100 |
if code2 != code: |
| 101 |
raise Panic(f'{code!r} != {code2!r}') |
| 102 |
scope = item['scope'] |
| 103 |
if scope == 'S': |
| 104 |
# Not a real language, ignore. |
| 105 |
continue |
| 106 |
elif scope in {'M', 'I'}: |
| 107 |
pass |
| 108 |
else: |
| 109 |
raise Panic(f'unknown scope: {scope!r}') |
| 110 |
reference_name = item['name'] |
| 111 |
if reference_name in iso_639: |
| 112 |
raise Panic(f'duplicate reference name: {reference_name!r}') |
| 113 |
if code1 is not None: |
| 114 |
if len(code1) == 2: |
| 115 |
codelist = [code1, code] |
| 116 |
else: |
| 117 |
raise Panic(f'len({code1!r}) != 2') |
| 118 |
else: |
| 119 |
codelist = [code] |
| 120 |
try: |
| 121 |
codelist += [l2t_to_2b[code]] |
| 122 |
except KeyError: |
| 123 |
pass |
| 124 |
iso_639[reference_name] = codelist |
| 125 |
print('[language-codes]') |
| 126 |
iso_639_rev = {} |
| 127 |
for code, *aliases in iso_639.values(): |
| 128 |
for alias in aliases: |
| 129 |
iso_639_rev[alias] = code |
| 130 |
if not aliases: |
| 131 |
iso_639_rev[code] = '' |
| 132 |
for alias, code in sorted(iso_639_rev.items()): |
| 133 |
print(f'{alias} = {code}'.rstrip()) |
| 134 |
print() |
| 135 |
|
| 136 |
def generate_iso_3166(): |
| 137 |
# ========================= |
| 138 |
# ISO 3166: territory codes |
| 139 |
# ========================= |
| 140 |
iso_3166 = set() |
| 141 |
data = load_json(get_iso_codes_dir() + '/iso_3166-1.json') |
| 142 |
for item in data['3166-1']: |
| 143 |
cc = item['alpha_2'] |
| 144 |
iso_3166.add(cc) |
| 145 |
print('[territory-codes]') |
| 146 |
for cc in sorted(iso_3166): |
| 147 |
print(f'{cc} =') |
| 148 |
print() |
| 149 |
print('# vim\72ft=dosini') |
| 150 |
|
| 151 |
if __name__ == '__main__': |
| 152 |
main() |
| 153 |
|
| 154 |
# vim:ts=4 sts=4 sw=4 et |