File indexing completed on 2024-07-21 12:06:53

0001 #!/usr/bin/env python3
0002 # SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
0003 # SPDX-License-Identifier: LGPL-2.0-or-later
0004 
0005 import json
0006 import os
0007 import requests
0008 
0009 # converts libaddressinput data to our addressbookformatrc format
0010 # Note: This is not meant to just blindly overwrite our data, we have a number
0011 # of additions and fixes that libaddressinput doesn't have. It is merely meant
0012 # as an easier way to diff our data to that of libaddressinput.
0013 
0014 # get a list of all 3166-1 alpha 2 ISO codes
0015 isoCodesFile = open('/usr/share/iso-codes/json/iso_3166-1.json')
0016 isoCodesJson = json.loads(isoCodesFile.read())
0017 isoCodes = []
0018 for isoCode in isoCodesJson['3166-1']:
0019     isoCodes.append(isoCode['alpha_2'])
0020 isoCodes.sort()
0021 
0022 outFile = open('addressformatrc', 'w')
0023 
0024 # for each ISO code, see if libaddressinput has data
0025 for isoCode in isoCodes:
0026     req = requests.get(f"https://www.gstatic.com/chrome/autofill/libaddressinput/chromium-i18n/ssl-address/data/{isoCode}")
0027     if req.status_code == 404:
0028         print (f"{isoCode} has no format information")
0029         continue
0030     data = json.loads(req.text)
0031     if not 'fmt' in data:
0032         print(f"{isoCode} has no format")
0033         continue
0034 
0035     outFile.write(f"[{isoCode}]\n")
0036     outFile.write(f"AddressFormat={data['fmt']}\n")
0037     if 'lfmt' in data:
0038         outFile.write(f"LatinAddressFormat={data['lfmt']}\n")
0039     if 'upper' in data:
0040         outFile.write(f"Upper={data['upper']}\n")
0041     if 'require' in data:
0042         outFile.write(f"Required={data['require']}\n")
0043     if 'zip' in data:
0044         outFile.write(f"PostalCodeFormat={data['zip']}\n")
0045     outFile.write('\n')