iban-tools

Tools / code generators for IBAN validation
Log | Files | Refs

codegen_kt.py (1803B)


      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 kt = """/*
      9  * This file is part of LibEuFin.
     10  * Copyright (C) 2025 Taler Systems S.A.
     11 
     12  * LibEuFin is free software; you can redistribute it and/or modify
     13  * it under the terms of the GNU Affero General Public License as
     14  * published by the Free Software Foundation; either version 3, or
     15  * (at your option) any later version.
     16 
     17  * LibEuFin is distributed in the hope that it will be useful, but
     18  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     19  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
     20  * Public License for more details.
     21 
     22  * You should have received a copy of the GNU Affero General Public
     23  * License along with LibEuFin; see the file COPYING.  If not, see
     24  * <http://www.gnu.org/licenses/>
     25  */
     26 
     27 package tech.libeufin.common
     28 
     29 /** IBAN ASCII characters rules */
     30 enum class IbanC {
     31     /** Digits (0-9) */
     32     n,
     33     /** Uppercase (A-Z) */
     34     a,
     35     /** Digits or uppercase (0-9 & A-Z) */
     36     c,
     37 }
     38 
     39 enum class Country(val ibanLen: Int, val rules: List<Pair<Int, IbanC>>, val bbanRegex: Regex) {
     40 """
     41 
     42 def fmt_rule(rule):
     43     fmt = "listOf("
     44     for repetition, char in rule:
     45         fmt += f"Pair({repetition}, IbanC.{char}),"
     46     return fmt + ")"
     47 
     48 
     49 for r in registry:
     50     kt += f'    {r["code"]}({r["iban_len"]}, {fmt_rule(r["bban_rules"])}, Regex("^{r["bban_regex"]}$")),\n'
     51 kt += """;
     52     val bbanLen get() = ibanLen - 4
     53 }
     54 val VALID_IBAN = listOf(
     55 """
     56 for r in registry:
     57     bban = r["bban_example"]
     58     if bban is None:
     59         kt += f'    Pair("{r["iban_example"]}", null),\n'
     60     else:
     61         kt += f'    Pair("{r["iban_example"]}", "{bban}"),\n'
     62 kt += ");"
     63 
     64 with open("registry.kt", "w") as kt_file:
     65     kt_file.write(kt)