language-data/scripts/export-iso-aliases
2020-09-29 11:50:32 +02:00

66 lines
1.9 KiB
Python
Executable file

#! /usr/bin/env python3
#
# Copyright © 2018 - 2020 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <https://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
import csv
import json
names = {}
def parse_csv(name):
result = {}
with open(name, "r") as csvfile:
reader = csv.reader(csvfile, delimiter=";")
for data in reader:
if data[0] == "#":
continue
if data[0] in result:
raise Exception("Duplicate {} in {}!".format(data[0], name))
result[data[0]] = data
return result
def add_name(item):
if "alpha_2" not in item:
return
if "alpha_3" in item:
names[item["alpha_3"]] = item["alpha_2"]
if "bibliographic" in item:
names[item["bibliographic"]] = item["alpha_2"]
with open("modules/iso-codes/data/iso_639-2.json", "r") as handle:
for item in json.load(handle)["639-2"]:
add_name(item)
with open("modules/iso-codes/data/iso_639-3.json", "r") as handle:
for item in json.load(handle)["639-3"]:
add_name(item)
aliases = parse_csv("aliases.csv")
lines = []
for name, alias in sorted(names.items()):
if name not in aliases:
lines.append(f"{name};{alias}\n")
if lines:
with open("aliases.csv", "a") as handle:
handle.writelines(sorted(lines))