|
| 1 | +import re |
| 2 | +from datetime import date |
| 3 | +from types import SimpleNamespace |
| 4 | +from typing import Optional, TypedDict |
| 5 | +from .util import CHECK_DIGIT, weighted_modulus_digit, validate_regexp |
| 6 | + |
| 7 | + |
| 8 | +class ParseResult(TypedDict): |
| 9 | + yyyymmdd: date |
| 10 | + """birthday of this ID""" |
| 11 | + sn: str |
| 12 | + """serial number""" |
| 13 | + checksum: CHECK_DIGIT |
| 14 | + """checksum""" |
| 15 | + |
| 16 | + |
| 17 | +class CivilNumber: |
| 18 | + """ |
| 19 | + Kuwait Civil Number, Arabic: الرقم المدني |
| 20 | + https://en.wikipedia.org/wiki/National_identification_number#Kuwait |
| 21 | + https://prakhar.me/articles/kuwait-civil-id-checksum/ |
| 22 | + """ |
| 23 | + METADATA = SimpleNamespace(**{ |
| 24 | + 'iso3166_alpha2': 'KW', |
| 25 | + 'min_length': 12, |
| 26 | + 'max_length': 12, |
| 27 | + 'parsable': True, |
| 28 | + 'checksum': True, |
| 29 | + 'regexp': re.compile(r'^(?P<century>\d)' |
| 30 | + r'(?P<yy>\d{2})' |
| 31 | + r'(?P<mm>\d{2})' |
| 32 | + r'(?P<dd>\d{2})' |
| 33 | + r'(?P<sn>\d{4})' |
| 34 | + r'(?P<checksum>\d)$') |
| 35 | + }) |
| 36 | + |
| 37 | + MULTIPLIER = [2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] |
| 38 | + |
| 39 | + @staticmethod |
| 40 | + def validate(id_number: str) -> bool: |
| 41 | + """ |
| 42 | + Validate the civil number |
| 43 | + """ |
| 44 | + if not id_number: |
| 45 | + return False |
| 46 | + |
| 47 | + if not isinstance(id_number, str): |
| 48 | + id_number = repr(id_number) |
| 49 | + return CivilNumber.parse(id_number) is not None |
| 50 | + |
| 51 | + @staticmethod |
| 52 | + def parse(id_number: str) -> Optional[ParseResult]: |
| 53 | + """ |
| 54 | + parse the id number |
| 55 | + """ |
| 56 | + match_obj = CivilNumber.METADATA.regexp.match(id_number) |
| 57 | + if not match_obj: |
| 58 | + return None |
| 59 | + |
| 60 | + checksum = CivilNumber.checksum(id_number) |
| 61 | + if checksum is None or str(checksum) != match_obj.group('checksum'): |
| 62 | + return None |
| 63 | + |
| 64 | + century = match_obj.group('century') |
| 65 | + if century == '2': |
| 66 | + year_base = 1900 |
| 67 | + elif century == '3': |
| 68 | + year_base = 2000 |
| 69 | + else: |
| 70 | + return None |
| 71 | + yy = int(match_obj.group('yy')) |
| 72 | + mm = int(match_obj.group('mm')) |
| 73 | + dd = int(match_obj.group('dd')) |
| 74 | + return { |
| 75 | + 'yyyymmdd': date(year_base + yy, mm, dd), |
| 76 | + 'sn': match_obj.group('sn'), |
| 77 | + 'checksum': checksum |
| 78 | + } |
| 79 | + |
| 80 | + @staticmethod |
| 81 | + def checksum(id_number) -> Optional[CHECK_DIGIT]: |
| 82 | + """ |
| 83 | + https://prakhar.me/articles/kuwait-civil-id-checksum/ |
| 84 | + """ |
| 85 | + if not validate_regexp(id_number, CivilNumber.METADATA.regexp): |
| 86 | + return None |
| 87 | + |
| 88 | + numbers = [int(char) for char in id_number] |
| 89 | + modulus = weighted_modulus_digit(numbers[:-1], CivilNumber.MULTIPLIER, 11) |
| 90 | + if modulus > 10: |
| 91 | + # according to the algorithm, it will not be greater than 10 |
| 92 | + return None |
| 93 | + return modulus |
| 94 | + |
| 95 | + |
| 96 | +NationalID = CivilNumber |
| 97 | +"""alias of CivilNumber""" |
0 commit comments