aboutsummaryrefslogtreecommitdiff
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
parent0ee8d13cd31de0b7c431a0d59013f0b70d57e2e5 (diff)
Added code, modifying how database is managed
-rw-r--r--README.rst2
-rwxr-xr-xsgdfi/_db.py52
-rwxr-xr-xsgdfi/_dbs/__init__.py1
-rwxr-xr-xsgdfi/_dbs/_code.py179
-rwxr-xr-xsgdfi/_dbs/_st.py27
-rwxr-xr-xsgdfi/_manager.py3
-rwxr-xr-xsgdfi/_repr.py10
7 files changed, 211 insertions, 63 deletions
diff --git a/README.rst b/README.rst
index 4c16b8d..6fc13a8 100644
--- a/README.rst
+++ b/README.rst
@@ -60,6 +60,8 @@ There are plenty of things to do:
- use MIME types instead of random types for ``Manager.feed()``, and check
what we can send to ``Manager.feed()`` instead of text to also manage
binary file formats such as XLS.
+- attendance register
+- accidents
- maybe delegate?
- translate everything from the SDY project.
- isolate the database?
diff --git a/sgdfi/_db.py b/sgdfi/_db.py
deleted file mode 100755
index 4d33f4a..0000000
--- a/sgdfi/_db.py
+++ /dev/null
@@ -1,52 +0,0 @@
-#!/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.
-#******************************************************************************
-""" This submodule defines the local database (or data manager) for managing
- our knowledge of the intranet. """
-
-from ._repr import Structure as _Structure, Adherent as _Adherent, \
- RallyRegistration as _RallyRegistration, Camp as _Camp, \
- Place as _Place, Operation as _Operation, Event as _Event
-
-__all__ = ["Database"]
-
-class Database:
- """ The internal class to manage all data. """
-
- def __init__(self, cache_dir):
- self.__cache_dir = cache_dir
-
- # ---
- # Save and load the database.
- # ---
-
- def save(self):
- """ Save the database. """
-
- # TODO
- pass
-
- def load(self):
- """ Load the saved database. """
-
- # TODO
- pass
-
- # ---
- # Feed the database with fetched (and possibly incomplete) data.
- # ---
-
- def add(self, obj):
- """ Add or update an object to the database. """
-
- accepted = (_Structure, _Adherent, _RallyRegistration, _Camp, _Place,
- _Operation, _Event)
- if all(not isinstance(obj, x) for x in accepted):
- raise TypeError("Unaccepted object in the database.")
-
- # TODO
- pass
-
-# End of file.
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",
}
diff --git a/sgdfi/_manager.py b/sgdfi/_manager.py
index 67efe05..e7846e2 100755
--- a/sgdfi/_manager.py
+++ b/sgdfi/_manager.py
@@ -19,7 +19,6 @@ from appdirs import user_cache_dir as _user_cache_dir
from ._repr import Structure as _Structure, Adherent as _Adherent, \
Place as _Place, RallyRegistration as _RallyRegistration, Camp as _Camp, \
Operation as _Operation, FunctionRawData as _FunctionRawData
-from ._db import Database as _Database
from ._intranet import IntranetSession as _Session
from ._decode import Decoder as _Decoder
@@ -195,8 +194,6 @@ class Manager(_Decoder):
# Make the data manager.
- self.__db = _Database(cache_dir = cache_dir)
-
self.__sts = _StructureManager(self)
self.__ads = _AdherentManager(self)
self.__rrs = _RallyRegistrationManager(self)
diff --git a/sgdfi/_repr.py b/sgdfi/_repr.py
index d91b331..359cfa7 100755
--- a/sgdfi/_repr.py
+++ b/sgdfi/_repr.py
@@ -17,7 +17,7 @@ from ._dbs import OperationType, OperationTypeData as _OperationTypeData, \
Function, FunctionRawData, FunctionData as _FunctionData, StructureType, \
StructureTypeData as _StructureTypeData, StructureStatus, \
StructureStatusData as _StructureStatusData, \
- AllocationsRegime, EventTypeData as _EventTypeData
+ AllocationsRegime, EventTypeData as _EventTypeData, Code as _Code
__all__ = ["IID", "Title", "Structure", "Adherent", "RallyRegistration",
"Camp", "Place", "Operation", "OperationType", "Function", "Event",
@@ -243,7 +243,7 @@ class Structure(_Base):
iid = _IIDProperty()
type = _ValueProperty(cls = _StructureTypeData)
name = _TextProperty()
- code = _TextProperty() # TODO: public code thingy?
+ code = _ValueProperty(cls = _Code)
# TODO: private code thingy?
description = _TextProperty(not_empty = True)
status = _ValueProperty(cls = _StructureStatusData)
@@ -267,12 +267,12 @@ class Adherent(_Base):
iid = _IIDProperty()
name = _ValueProperty(cls = _Name, default = _Name())
+ code = _ValueProperty(cls = _Code)
+ function = _ValueProperty(cls = _FunctionData)
- # TODO: inscription (code adhérent, structure, fonction, fct 2ndaires,
+ # TODO: inscription (structure, fct 2ndaires,
# statut, début de mandat, fin de mandat).
- function = _ValueProperty(cls = _FunctionData)
-
address = _TextProperty()
postal_code = _TextProperty()
town = _TextProperty()