aboutsummaryrefslogtreecommitdiff
path: root/sgdfi/_dbs
diff options
context:
space:
mode:
authorThomas "Cakeisalie5" Touhey <thomas@touhey.fr>2018-10-18 10:29:55 +0200
committerThomas "Cakeisalie5" Touhey <thomas@touhey.fr>2018-10-18 10:29:55 +0200
commit5dbce6f9480c8a7d9b115f9b2b7fae97f5f3c4bb (patch)
treefd88b7895582ccd9c633736074587544bd823a29 /sgdfi/_dbs
parentd176763a25e042c0d35d9b2d6f2d50ea7c155519 (diff)
Latest modifications
Diffstat (limited to 'sgdfi/_dbs')
-rwxr-xr-xsgdfi/_dbs/_code.py49
1 files changed, 17 insertions, 32 deletions
diff --git a/sgdfi/_dbs/_code.py b/sgdfi/_dbs/_code.py
index 29ac9ef..b416e30 100755
--- a/sgdfi/_dbs/_code.py
+++ b/sgdfi/_dbs/_code.py
@@ -22,43 +22,24 @@ class CodeType(_Enum):
""" Adherent. """
ADHERENT = 2
-# If a code starts with:
-# - 000000000, then it is the top.
-# - X0 or X1, then it is a structure (see below for subtypes).
-# - X2 to X6, then it is an adherent.
-# - 0 or X8, then it is a structure related to the national.
-#
-# If it is a structure and its code ends with:
-# - 0000, then it is a territory (territoire).
-# - 0081, then it is a “membre associés territorial”,
-# usually a “Développement et réseaux.”.
-# - 00, then it is a group (groupe).
-# - 81, then it is a “membre associés local”.
-#
-# XXX: … (units? other types?)
-#
-# If it is related to the national, then
-# - X80 or X86, then it is a centre national.
-# - X88, then if the code ends with:
-# - 0X or 10, then it is a centre national.
-# - [1-7]8, then it is a “membres associés national” (développement et
-# réseau formation).
-# - 8X, then it is a “membre associés national”.
-#
-# For any substructure, after the 10 or 11, the code starts with two digits
-# representing the department (e.g. 78 or 91) and a digit representing the
-# territory number for this department.
+_strcodeallowed = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def _strcode(value):
""" Normalize the code and validate it. """
+ oldvalue = value
+
try:
- value = int(value)
- assert value >= 0 and value <= 999999999
- except (ValueError, AssertionError):
- raise ValueError("Invalid code.") from None
+ value = f"{int(value):09d}"
+ except ValueError:
+ pass
+
+ value = value.strip().upper()
+ value = value.rjust(9, '0')
- return f"{value:09d}"
+ if len(value) != 9 or any(c not in _strcodeallowed for c in value):
+ raise ValueError(f"Can't get a code out of {repr(oldvalue)}")
+ return value
class Code:
""" A structure code, which is a 9-digit number. """
@@ -155,7 +136,11 @@ class Code:
return self.__code
def __eq__(self, value):
- return self.__code == _strcode(value)
+ try:
+ code = _strcode(value)
+ return self.__code == code
+ except ValueError:
+ return False
@property
def type(self):