parse_registry.py (5951B)
1 #!/usr/bin/env python3 2 3 import csv 4 import json 5 import re 6 7 with open("registry.txt", encoding="utf-8-sig", newline="") as registry_file: 8 registry_rows = list(csv.reader(registry_file, delimiter="\t")) 9 10 STRUCTURE_PART_PATTERN = "[0-9]+![nca]" 11 STRUCTURE_PATTERN = f"^({STRUCTURE_PART_PATTERN})+$" 12 13 def parse_line(prefix): 14 cells = [cell.strip() for cell in registry_rows.pop(0)] 15 parts = [None if not cell or "N/A" in cell else cell for cell in cells] 16 first = parts.pop(0) 17 assert first == prefix, first 18 return parts 19 20 21 def parse_countries(prefix): 22 def parse_countries(encoded): 23 return [x.split(' ', 1)[0] for x in encoded.strip('"').split(", ")] 24 25 return [[] if x is None else parse_countries(x) for x in parse_line(prefix)] 26 27 28 def parse_bool_line(prefix): 29 return [x == "Yes" for x in parse_line(prefix)] 30 31 32 def parse_int_line(prefix): 33 return [int(x.split("!")[0]) for x in parse_line(prefix)] 34 35 36 def parse_pattern(encoded): 37 if encoded is None: 38 return (0, [], "") 39 assert re.match(STRUCTURE_PATTERN, encoded), f"{STRUCTURE_PATTERN} {encoded}" 40 pattern_len = 0 41 rules = [] 42 for match in re.finditer(STRUCTURE_PART_PATTERN, encoded): 43 [repetition, kind] = match.group().split("!", 1) 44 repetition = int(repetition) 45 pattern_len += repetition 46 if len(rules) == 0: 47 rules.append((repetition, kind)) 48 else: 49 last = rules[-1] 50 if last[1] == kind: 51 rules[-1] = (last[0] + repetition, kind) 52 else: 53 rules.append((repetition, kind)) 54 regex = "" 55 for [repetition, kind] in rules: 56 if kind == "n": 57 regex += f"[0-9]{{{repetition}}}" 58 elif kind == "a": 59 regex += f"[A-Z]{{{repetition}}}" 60 else: 61 regex += f"[0-9A-Z]{{{repetition}}}" 62 return (pattern_len, rules, regex) 63 64 65 def parse_range(range): 66 if range is None: 67 return None 68 (start, end) = [int(x) for x in range.split("-", 1)] 69 return (start - 1, end) 70 71 72 parse_line("Data element") 73 country_names = parse_line("Name of country") 74 country_code = parse_line("IBAN prefix country code (ISO 3166)") 75 country_code_include = parse_countries( 76 "Country code includes other countries/territories" 77 ) 78 sepa = parse_bool_line("SEPA country") 79 sepa_include = parse_countries("SEPA country also includes") 80 parse_line("Domestic account number example") 81 82 parse_line("BBAN") 83 bban_patterns = parse_line("BBAN structure") 84 bban_len = parse_int_line("BBAN length") 85 bank_range = parse_line("Bank identifier position within the BBAN") 86 bank_patterns = parse_line("Bank identifier pattern") 87 branch_range = parse_line("Branch identifier position within the BBAN") 88 branch_patterns = parse_line("Branch identifier pattern") 89 parse_line("Bank identifier example") 90 parse_line("Branch identifier example") 91 bban_example = parse_line("BBAN example") 92 93 parse_line("IBAN") 94 iban_pattern = parse_line("IBAN structure") 95 iban_len = parse_int_line("IBAN length") 96 parse_line("Effective date") 97 iban_example = parse_line("IBAN electronic format example") 98 iban_print_example = parse_line("IBAN print format example") 99 100 parse_line("Contact details") 101 # We ignore contact details 102 103 registry = [] 104 for i in range(len(country_names)): 105 code = country_code[i] 106 if code == "IQ": 107 if bank_range[i] == "42461": 108 bank_range[i] = "1-4" 109 if branch_range[i] == "42556": 110 branch_range[i] = "5-7" 111 elif code == "NO" and bban_patterns[i] == "NO4!n6!n1!n": 112 bban_patterns[i] = "4!n6!n1!n" 113 elif code == "AL" and bank_patterns[i] == "8!n": 114 bank_patterns[i] = "3!n" 115 elif code == "EG": 116 if bank_patterns[i] == "4!": 117 bank_patterns[i] = "4!n" 118 if branch_patterns[i] == "4!": 119 branch_patterns[i] = "4!n" 120 elif code == "FI" and bank_patterns[i] is None: 121 bank_patterns[i] = "3!n" 122 elif code == "BA" and bban_example[i] == "1990440001200279": 123 # Use the BBAN from the electronic IBAN example. 124 bban_example[i] = "1290079401028494" 125 elif code == "BR" and bban_example[i] == "00360305000010009795493P1": 126 # The BBAN differs by one letter. Fix. 127 bban_example[i] = "00360305000010009795493C1" 128 elif code == "CR" and bban_example[i] == "15202001026284066": 129 # The BBAN removes the leading '0'. Add it back. 130 bban_example[i] = "015202001026284066" 131 elif code == "IL" and bban_example[i] == "010800000099999999": 132 # This looks like a typo. There is one 0 missing in the BBAN. 133 bban_example[i] = "0108000000099999999" 134 135 print(code) 136 bban_pattern = bban_patterns[i] 137 (bban_length, bban_rules, bban_regex) = parse_pattern(bban_pattern) 138 assert bban_len[i] == bban_length == iban_len[i] - 4 139 140 (_, bank_rules, _) = parse_pattern(bank_patterns[i]) 141 (_, branch_rules, _) = parse_pattern(branch_patterns[i]) 142 143 # if bban_bank[i] is not None: 144 # assert range_len(bban_bank[i]) == structure_len(bban_bank_structure[i]) 145 # if bban_branch[i] is not None: 146 # assert range_len(bban_branch[i]) == structure_len(bban_branch_structure[i]) 147 registry.append( 148 { 149 "name": country_names[i], 150 "code": code, 151 "code_include": country_code_include[i], 152 "sepa": sepa[i], 153 "sepa_include": sepa_include[i], 154 "iban_len": iban_len[i], 155 "iban_example": iban_example[i], 156 "bban_len": bban_length, 157 "bban_pattern": bban_pattern, 158 "bban_rules": bban_rules, 159 "bban_regex": bban_regex, 160 "bban_example": bban_example[i], 161 "bank_range": parse_range(bank_range[i]), 162 "bank_rules": bank_rules, 163 "branch_range": parse_range(branch_range[i]), 164 "branch_rules": branch_rules, 165 } 166 ) 167 168 with open("registry.json", "w") as json_file: 169 json.dump(registry, json_file, indent=2)