aboutsummaryrefslogtreecommitdiff
path: root/sgdfi/_dbs
diff options
context:
space:
mode:
authorThomas "Cakeisalie5" Touhey <thomas@touhey.fr>2018-10-10 13:20:03 +0200
committerThomas "Cakeisalie5" Touhey <thomas@touhey.fr>2018-10-10 13:20:03 +0200
commit105fd65703f780fbca81d6fd3e0957129b035f8b (patch)
tree13f689d99c38ee32b1c0408f0c1894221e287efa /sgdfi/_dbs
parent0ee8d13cd31de0b7c431a0d59013f0b70d57e2e5 (diff)
Added code, modifying how database is managed
Diffstat (limited to 'sgdfi/_dbs')
-rwxr-xr-xsgdfi/_dbs/__init__.py1
-rwxr-xr-xsgdfi/_dbs/_code.py179
-rwxr-xr-xsgdfi/_dbs/_st.py27
3 files changed, 204 insertions, 3 deletions
diff --git a/sgdfi/_dbs/__init__.py b/sgdfi/_dbs/__init__.py
index a4ff203..65de2d6 100755
--- a/sgdfi/_dbs/__init__.py
+++ b/sgdfi/_dbs/__init__.py
@@ -10,6 +10,7 @@ from ._st import StructureType, StructureTypeData
from ._ststatus import StructureStatus, StructureStatusData
from ._funcs import Function, FunctionData, FunctionRawData
from ._event import EventType, EventTypeData
+from ._code import Code, CodeType
from ._regime import AllocationsRegime
diff --git a/sgdfi/_dbs/_code.py b/sgdfi/_dbs/_code.py
new file mode 100755
index 0000000..5a9abfb
--- /dev/null
+++ b/sgdfi/_dbs/_code.py
@@ -0,0 +1,179 @@
+#!/usr/bin/env python3
+#******************************************************************************
+# Copyright (C) 2018 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
+# This file is part of the sgdfi project, which is MIT-licensed.
+#******************************************************************************
+""" Code decoding for SGDFi. """
+
+from enum import Enum as _Enum, unique as _unique
+
+from ._st import StructureType as _StructureType
+
+__all__ = ["Code", "CodeType"]
+
+@_unique
+class CodeType(_Enum):
+ """ Unknown code type. """
+ UNKNOWN = 0
+
+ """ Structure. """
+ STRUCTURE = 1
+
+ """ Adherent. """
+ ADHERENT = 2
+
+# If a code starts with:
+# - 0, then it is the top (000000000).
+# - X0 or X1, then it is a structure (see below for subtypes).
+# - X2 to X6, then it is an adherent.
+# - 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.
+
+def _strcode(value):
+ """ Normalize the code and validate it. """
+
+ try:
+ value = int(value)
+ assert value >= 0 and value <= 999999999
+ except (ValueError, AssertionError):
+ raise ValueError("Invalid code.") from None
+
+ return f"{value:09d}"
+
+class Code:
+ """ A structure code, which is a 9-digit number. """
+
+ def __init__(self, value):
+ self.__code = _strcode(value)
+
+ # Try to deduce things of this code.
+
+ typ = CodeType.UNKNOWN
+ styp = None
+ dep = None
+ depn = None
+ grpn = None
+
+ if value[0] == '0':
+ typ = CodeType.STRUCTURE
+ styp = _StructureType.SOMMET
+ elif value[1] in '01':
+ dep = int(value[2:4])
+ depn = int(value[4]) + (0, 10)[value[1] == '1']
+
+ typ = CodeType.STRUCTURE
+
+ if value[5:7] == '00':
+ # That's related to a territory!
+
+ if value[7:9] == '00':
+ styp = _StructureType.TERRITOIRE
+ else:
+ styp = _StructureType.ASSOCIES_T
+ else:
+ # That's related to a group!
+
+ grpn = int(value[5:7]) - 1
+
+ if value[7:9] == '00':
+ styp = _StructureType.GROUPE
+ else:
+ # TODO: units and stuff?
+
+ styp = _StructureType.ASSOCIES_L
+ elif value[1] in '234567':
+ # Adherent. X7 is unused for now, but is probably reserved.
+
+ typ = CodeType.ADHERENT
+ elif value[1:3] in ('80', '86', '88'):
+ if value[7] == '0' or value[7:9] == '10':
+ typ = CodeType.STRUCTURE
+ styp = _StructureType.CENTRE_NATIONAL
+ elif value[7] in '1234567' and value[8] == '8':
+ typ = CodeType.STRUCTURE
+ styp = _StructureType.ASSOCIES_N
+ elif value[7] == '8':
+ typ = CodeType.STRUCTURE
+ styp = _StructureType.ASSOCIES_N
+
+ self.__type = typ
+ self.__subtype = styp
+ self.__dep = dep
+ self.__depn = depn
+ self.__grpn = grpn
+
+ def __repr__(self):
+ p = [f"code = {self.__code}"]
+ if self.__type != CodeType.UNKNOWN:
+ p.append(f"type = {self.__type}")
+ if self.__type == CodeType.STRUCTURE:
+ p.append(f"subtype = {self.__subtype}")
+ if self.__dep is not None:
+ p.append(f"dep = {self.__dep}")
+ if self.__depn is not None:
+ p.append(f"depn = {self.__depn}")
+ if self.__grpn is not None:
+ p.append(f"grpn = {self.__grpn}")
+
+ return f"{self.__class__.__name__}({', '.join(p)})"
+
+ def __str__(self):
+ return self.__code
+
+ def __int__(self):
+ return int(self.__code)
+
+ def __eq__(self, value):
+ return self.__code == _strcode(value)
+
+ @property
+ def type(self):
+ """ The code type. """
+
+ return self.__type
+
+ @property
+ def subtype(self):
+ """ The code subtype. """
+
+ return self.__subtype
+
+ @property
+ def department(self):
+ """ The code of the associated administrative department. """
+
+ return self.__dep
+
+ @property
+ def territory_index(self):
+ """ The number of the territory in the administrative department. """
+
+ return self.__depn
+
+ @property
+ def group_index(self):
+ """ The index of the group in the territory. """
+
+ return self.__grpn
+
+# End of file.
diff --git a/sgdfi/_dbs/_st.py b/sgdfi/_dbs/_st.py
index e9c47a9..74e7cc6 100755
--- a/sgdfi/_dbs/_st.py
+++ b/sgdfi/_dbs/_st.py
@@ -14,14 +14,35 @@ class StructureType(_Enum):
""" The default type (unknown). """
UNKNOWN = 0
+ """ Sommet. """
+ SOMMET = 1
+
+ """ Territoire. """
+ TERRITOIRE = 2
+
+ """ Groupe. """
+ GROUPE = 3
+
+ """ Membres associés national. """
+ ASSOCIES_N = 10
+
+ """ Membres associés territorial. """
+ ASSOCIES_T = 11
+
+ """ Membres associés local. """
+ ASSOCIES_L = 12
+
+ """ Centre national. """
+ CENTRE_NATIONAL = 15
+
""" Unité louveteaux-jeanettes. """
- UNITE_8_11_ANS = 1
+ UNITE_8_11_ANS = 30
""" Unité pionniers-caravelles. """
- UNITE_14_17_ANS = 2
+ UNITE_14_17_ANS = 32
_StructureTypeData = {
- StructureType.UNITE_8_11_ANS: "Unité 8-11 ans",
+ StructureType.UNITE_8_11_ANS: "Unité 8-11 ans",
StructureType.UNITE_14_17_ANS: "Unité 14-17 ans",
}