Blame bin/validate_json_data.py

rpm-build 393af8
#!/usr/bin/env python3
rpm-build 393af8
#
rpm-build 393af8
# Checks all JSON data files against their schema.
rpm-build 393af8
#
rpm-build 393af8
# Copyright © 2016 Dr. Tobias Quathamer <toddy@debian.org>
rpm-build 393af8
#
rpm-build 393af8
# This program is free software; you can redistribute it and/or
rpm-build 393af8
# modify it under the terms of the GNU Lesser General Public
rpm-build 393af8
# License as published by the Free Software Foundation; either
rpm-build 393af8
# version 2.1 of the License, or (at your option) any later version.
rpm-build 393af8
#
rpm-build 393af8
# This program is distributed in the hope that it will be useful,
rpm-build 393af8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 393af8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build 393af8
# Lesser General Public License for more details.
rpm-build 393af8
#
rpm-build 393af8
# You should have received a copy of the GNU Lesser General Public
rpm-build 393af8
# License along with this program; if not, write to the Free Software
rpm-build 393af8
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
rpm-build 393af8
rpm-build 393af8
import json
rpm-build 393af8
from jsonschema import validate
rpm-build 393af8
rpm-build 393af8
standards = [
rpm-build 393af8
    "639-2",
rpm-build 393af8
    "639-3",
rpm-build 393af8
    "639-5",
rpm-build 393af8
    "3166-1",
rpm-build 393af8
    "3166-2",
rpm-build 393af8
    "3166-3",
rpm-build 393af8
    "4217",
rpm-build 393af8
    "15924",
rpm-build 393af8
]
rpm-build 393af8
rpm-build 393af8
# Validate against schema
rpm-build 393af8
for standard in standards:
rpm-build 393af8
    with open("data/schema-" + standard + ".json", encoding="utf-8") as schema_file:
rpm-build 393af8
        schema = json.load(schema_file)
rpm-build 393af8
        with open("data/iso_" + standard + ".json", encoding="utf-8") as json_file:
rpm-build 393af8
            validate(json.load(json_file), schema)
rpm-build 393af8
rpm-build 393af8
# Ensure correct sorting order
rpm-build 393af8
for standard in standards:
rpm-build 393af8
    # Read in the JSON file
rpm-build 393af8
    with open("data/iso_" + standard + ".json", encoding="utf-8") as json_file:
rpm-build 393af8
        iso = json.load(json_file)
rpm-build 393af8
    sort_key = "alpha_3"
rpm-build 393af8
    if standard in ["3166-3", "15924"]:
rpm-build 393af8
        sort_key = "alpha_4"
rpm-build 393af8
    if standard == "3166-2":
rpm-build 393af8
        sort_key = "code"
rpm-build 393af8
    iso[standard].sort(key=lambda item: item[sort_key])
rpm-build 393af8
    # Write the sorted JSON file
rpm-build 393af8
    with open("data/iso_" + standard + ".json", "w", encoding="utf-8") as json_file:
rpm-build 393af8
        json.dump(iso, json_file, ensure_ascii=False, indent=2, sort_keys=True)
rpm-build 393af8
        # Add a final newline
rpm-build 393af8
        json_file.write("\n")