|
| 1 | +# cc.py - functions for handling Portuguese Identity numbers |
| 2 | +# coding: utf-8 |
| 3 | +# |
| 4 | +# Copyright (C) 2021 David Vaz |
| 5 | +# |
| 6 | +# This library is free software; you can redistribute it and/or |
| 7 | +# modify it under the terms of the GNU Lesser General Public |
| 8 | +# License as published by the Free Software Foundation; either |
| 9 | +# version 2.1 of the License, or (at your option) any later version. |
| 10 | +# |
| 11 | +# This library is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | +# Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public |
| 17 | +# License along with this library; if not, write to the Free Software |
| 18 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 | +# 02110-1301 USA |
| 20 | + |
| 21 | +"""CC (Número de Cartão de Cidadão, Portuguese Identity number). |
| 22 | +
|
| 23 | +The Portuguese Identity Number is alphanumeric and consists of the numeric |
| 24 | +Número de Identificação Civil, a two-letter version and a check digit. |
| 25 | +
|
| 26 | +More information: |
| 27 | +
|
| 28 | +* https://pt.wikipedia.org/wiki/Cartão_de_cidadão |
| 29 | +* https://www.autenticacao.gov.pt/documents/20126/115760/Validação+de+Número+de+Documento+do+Cartão+de+Cidadão.pdf |
| 30 | +
|
| 31 | +>>> validate('00000000 0 ZZ4') |
| 32 | +'000000000ZZ4' |
| 33 | +>>> validate('00000000 A ZZ4') # invalid format |
| 34 | +Traceback (most recent call last): |
| 35 | + ... |
| 36 | +InvalidFormat: ... |
| 37 | +>>> validate('00000000 0 ZZ3') # invalid check digits |
| 38 | +Traceback (most recent call last): |
| 39 | + ... |
| 40 | +InvalidChecksum: ... |
| 41 | +>>> format('000000000ZZ4') |
| 42 | +'00000000 0 ZZ4' |
| 43 | +""" |
| 44 | + |
| 45 | +import re |
| 46 | + |
| 47 | +from stdnum.exceptions import * |
| 48 | +from stdnum.util import clean |
| 49 | + |
| 50 | + |
| 51 | +_cc_re = re.compile(r'^\d*[A-Z0-9]{2}\d$') |
| 52 | + |
| 53 | + |
| 54 | +def compact(number): |
| 55 | + """Convert the number to the minimal representation. This strips the |
| 56 | + number of any valid separators and removes surrounding whitespace.""" |
| 57 | + number = clean(number, ' ').upper().strip() |
| 58 | + return number |
| 59 | + |
| 60 | + |
| 61 | +def calc_check_digit(number): |
| 62 | + """Calculate the check digit for the number.""" |
| 63 | + alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 64 | + cutoff = lambda x: x - 9 if x > 9 else x |
| 65 | + s = sum( |
| 66 | + cutoff(alphabet.index(n) * 2) if i % 2 == 0 else alphabet.index(n) |
| 67 | + for i, n in enumerate(number[::-1])) |
| 68 | + return str((10 - s) % 10) |
| 69 | + |
| 70 | + |
| 71 | +def validate(number): |
| 72 | + """Check if the number is a valid cartao de cidadao number.""" |
| 73 | + number = compact(number) |
| 74 | + if not _cc_re.match(number): |
| 75 | + raise InvalidFormat() |
| 76 | + if calc_check_digit(number[:-1]) != number[-1]: |
| 77 | + raise InvalidChecksum() |
| 78 | + return number |
| 79 | + |
| 80 | + |
| 81 | +def is_valid(number): |
| 82 | + """Check if the number is a valid cartao de cidadao number.""" |
| 83 | + try: |
| 84 | + return bool(validate(number)) |
| 85 | + except ValidationError: |
| 86 | + return False |
| 87 | + |
| 88 | + |
| 89 | +def format(number): |
| 90 | + """Reformat the number to the standard presentation format.""" |
| 91 | + number = compact(number) |
| 92 | + return ' '.join([number[:-4], number[-4], number[-3:]]) |
0 commit comments