|
| 1 | +import json |
| 2 | +import re |
| 3 | +from pathlib import Path |
| 4 | +from xml.etree import ElementTree |
| 5 | + |
| 6 | +project_root = Path(__file__).parent.parent.parent |
| 7 | +metadata_path = project_root / "resources" / "metadata.xml" |
| 8 | +patterns_path = project_root / "src" / "metadata" / "validations.json" |
| 9 | +countries_path = project_root / "src" / "metadata" / "countries.json" |
| 10 | + |
| 11 | +tree = ElementTree.parse(metadata_path) |
| 12 | +territories = tree.find("territories") |
| 13 | + |
| 14 | + |
| 15 | +def update_mask(mask, length): |
| 16 | + length_without_cc = len(re.findall(r"[\d.]", re.sub(r"\+\d+\s", "", mask))) |
| 17 | + if length_without_cc != length: |
| 18 | + cc_mask = re.match(r"(\+\d+)\s", mask).group(1) |
| 19 | + ac_mask = re.match(r"\+\d+\s(?:(\S+)\s)?", mask).group(1) or "" |
| 20 | + ac_mask_length = len(re.findall(r"[\d.]", ac_mask)) |
| 21 | + pn_mask_expected_length = length - ac_mask_length |
| 22 | + pn_mask_prefix = re.sub(r"\+\d+\s(?:\S+\s)?", "", mask).split()[0] |
| 23 | + pn_mask_parts = [pn_mask_prefix] |
| 24 | + while len("".join(pn_mask_parts)) < pn_mask_expected_length: |
| 25 | + pn_mask_parts.append("." * len(pn_mask_prefix)) |
| 26 | + pn_mask = " ".join(pn_mask_parts) |
| 27 | + while len(pn_mask.replace(" ", "")) > pn_mask_expected_length: |
| 28 | + pn_mask = pn_mask[:-1] |
| 29 | + mask = f"{cc_mask} {ac_mask} {pn_mask}" if ac_mask else f"{cc_mask} {pn_mask}" |
| 30 | + return re.sub(r"\s(\.{1,2})$", r"\1", mask) |
| 31 | + |
| 32 | + |
| 33 | +with open(patterns_path) as fp: |
| 34 | + patterns = json.load(fp) |
| 35 | + |
| 36 | +with open(countries_path) as fp: |
| 37 | + countries = json.load(fp) |
| 38 | + |
| 39 | +for territory in territories: |
| 40 | + # Regenerate masks based on possible maximum lengths |
| 41 | + possible_lengths = map(lambda e: territory.find(f"{e.tag}/possibleLengths"), territory.iter()) |
| 42 | + possible_lengths = map(lambda e: e.get("national"), filter(lambda e: e is not None, possible_lengths)) |
| 43 | + possible_lengths = list(map(int, re.findall(r"\d+", ",".join(possible_lengths)))) |
| 44 | + min_length, max_length = min(possible_lengths), max(possible_lengths) |
| 45 | + for country in [c for c in countries if c[0] == territory.get("id").lower()]: |
| 46 | + country[3] = update_mask(country[3], max_length) |
| 47 | + |
| 48 | + # Update phone validation patterns |
| 49 | + general_desc = territory.find("generalDesc") |
| 50 | + national_number_pattern = general_desc.find("nationalNumberPattern").text |
| 51 | + national_number_pattern = re.sub(r"[\s\n]", "", national_number_pattern) |
| 52 | + patterns[territory.get("id").lower()] = [ |
| 53 | + f"^\\d{{{min_length},{max_length}}}$" if min_length != max_length else f"^\\d{{{max_length}}}$", |
| 54 | + f"^{national_number_pattern}$" |
| 55 | + ] |
| 56 | + |
| 57 | +with open(patterns_path, "w") as fp: |
| 58 | + json.dump(patterns, fp, indent=2) |
| 59 | + |
| 60 | +with open(countries_path, "w") as fp: |
| 61 | + json.dump(countries, fp, indent=2) |
0 commit comments