Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/stdnum/fr/nir.py
blob: bf1206a793f3073f6a450563eff14f705da68515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# nir.py - functions for handling French NIR numbers
# coding: utf-8
#
# Copyright (C) 2016 Dimitri Papadopoulos
# Copyright (C) 2016 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

"""NIR (French personal identification number).

The NIR (Numero d'Inscription au Repertoire national d'identification des
personnes physiques) is used to identify persons in France. It is popularly
known as the "social security number" and sometimes referred to as an INSEE
number. All persons born in France are registered in the Repertoire national
d'identification des personnes physiques (RNIPP) and assigned a NIR.

The number consists of 15 digits: the first digit indicates the gender,
followed by 2 digits for the year or birth, 2 for the month of birth, 5 for
the location of birth (COG), 3 for a serial and 2 check digits.

More information:

* http://www.insee.fr/en/methodes/default.asp?page=definitions/nir.htm
* https://en.wikipedia.org/wiki/INSEE_code
* http://resoo.org/docs/_docs/regles-numero-insee.pdf
* https://fr.wikipedia.org/wiki/Numéro_de_sécurité_sociale_en_France
* http://xml.insee.fr/schema/nir.html

>>> validate('2 95 10 99 126 111 93')
'295109912611193'
>>> validate('295109912611199')  # invalid check digit
Traceback (most recent call last):
    ...
InvalidChecksum: ...
>>> validate('253072B07300470')
'253072B07300470'
>>> validate('253072A07300443')
'253072A07300443'
>>> format('295109912611193')
'2 95 10 99 126 111 93'
"""

from stdnum.exceptions import *
from stdnum.util import clean


def compact(number):
    """Convert the number to the minimal representation. This strips the
    number of any valid separators and removes surrounding whitespace."""
    return clean(number, ' .').strip().upper()


def calc_check_digits(number):
    """Calculate the check digits for the number."""
    department = number[5:7]
    if department == '2A':
        number = number[:5] + '19' + number[7:]
    elif department == '2B':
        number = number[:5] + '18' + number[7:]
    return '%02d' % (97 - (int(number[:13]) % 97))


def validate(number):
    """Checks to see if the number provided is valid. This checks the length
    and check digits."""
    number = compact(number)
    if not (number.isdigit() or (
                number[:5].isdigit() and number[7:].isdigit() and
                number[5:7] in ('2A', '2B'))):
        raise InvalidFormat()
    if len(number) != 15:
        raise InvalidLength()
    if calc_check_digits(number) != number[13:]:
        raise InvalidChecksum()
    return number


def is_valid(number):
    """Checks to see if the number provided is valid. This checks the length
    and check digits."""
    try:
        return bool(validate(number))
    except ValidationError:
        return False


def format(number, separator=' '):
    """Reformat the passed number to the standard format."""
    number = compact(number)
    return separator.join((number[:1], number[1:3], number[3:5], number[5:7],
                           number[7:10], number[10:13], number[13:]))