codegen_ts.py (2148B)
1 #!/usr/bin/env python3 2 3 import json 4 5 with open("registry.json", "r") as json_file: 6 registry = json.load(json_file) 7 8 ts = """/* 9 This file is part of TALER 10 Copyright (C) 2025 Taler Systems SA 11 12 TALER is free software; you can redistribute it and/or modify it under the 13 terms of the GNU Affero General Public License as published by the Free Software 14 Foundation; either version 3, or (at your option) any later version. 15 16 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 17 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 18 A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. 19 20 You should have received a copy of the GNU Affero General Public License along with 21 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 22 */ 23 24 /** IBAN ASCII characters rules */ 25 export enum IbanC { 26 /** Digits (0-9) */ 27 n, 28 /** Uppercase (A-Z) */ 29 a, 30 /** Digits or uppercase (0-9 & A-Z) */ 31 c, 32 } 33 34 export type IbanRule = [number, IbanC]; 35 export type IbanPattern = IbanRule[]; 36 export interface IbanCountryInfo { 37 name: string; 38 ibanLen: number; 39 bbanLen: number; 40 bbanRules: IbanPattern; 41 bbanRegex: RegExp; 42 } 43 44 export const ibanCountryInfoTable: Record<string, IbanCountryInfo> = { 45 """ 46 47 48 def fmt_rule(rule): 49 fmt = "[" 50 for repetition, char in rule: 51 fmt += f"[{repetition}, IbanC.{char}]," 52 return fmt + "]" 53 54 55 for r in registry: 56 ts += f"""{r["code"]}: {{ 57 name: "{r["name"].strip('"')}", 58 ibanLen: {r["iban_len"]}, 59 bbanLen: {r["bban_len"]}, 60 bbanRules: {fmt_rule(r["bban_rules"])}, 61 bbanRegex: /^{r["bban_regex"]}$/ 62 }}, 63 """ 64 ts += "};" 65 66 with open("registry.ts", "w") as ts_file: 67 ts_file.write(ts) 68 69 test = """ 70 export const VALID_IBAN: [string, string?][] = [ 71 """ 72 for r in registry: 73 bban = r["bban_example"] 74 if bban is None: 75 test += f'["{r["iban_example"]}", undefined],\n' 76 else: 77 test += f'["{r["iban_example"]}", "{bban}"],\n' 78 ts += "};" 79 test += """ 80 ]; 81 """ 82 with open("registry.test.ts", "w") as test_file: 83 test_file.write(test)