aboutsummaryrefslogtreecommitdiff
path: root/sgdfi/_repr.py
diff options
context:
space:
mode:
Diffstat (limited to 'sgdfi/_repr.py')
-rwxr-xr-xsgdfi/_repr.py210
1 files changed, 140 insertions, 70 deletions
diff --git a/sgdfi/_repr.py b/sgdfi/_repr.py
index aa9d97f..3d73312 100755
--- a/sgdfi/_repr.py
+++ b/sgdfi/_repr.py
@@ -11,80 +11,137 @@ from ._util import IID, Enum as _Enum, \
Base as _Base, Property as _Property, \
IIDProperty as _IIDProperty, DateProperty as _DateProperty, \
BoolProperty as _BoolProperty, EnumProperty as _EnumProperty, \
- ArrayProperty as _ArrayProperty, TextProperty as _TextProperty
+ ArrayProperty as _ArrayProperty, TextProperty as _TextProperty, \
+ ObjectProperty as _ObjectProperty, ValueProperty as _ValueProperty
from ._dbs import OperationType, OperationTypeData as _OperationTypeData, \
Function, FunctionRawData, FunctionData as _FunctionData, StructureType, \
StructureTypeData as _StructureTypeData, StructureStatus, \
StructureStatusData as _StructureStatusData, \
AllocationsRegime
-__all__ = ["IID", "Structure", "Adherent", "RallyRegistration", "Camp",
- "Place", "Operation", "OperationType", "Function", "StructureType",
- "StructureStatus", "AllocationsRegime"]
+__all__ = ["IID", "Title", "Structure", "Adherent", "RallyRegistration",
+ "Camp", "Place", "Operation", "OperationType", "Function",
+ "StructureType", "StructureStatus", "AllocationsRegime"]
# ---
-# Specific properties.
+# Internal classes.
# ---
-class _FunctionProperty(_Property):
- """ Function. """
+class Title(_Enum):
+ """ Unknown. """
+ UNKNOWN = 0
- def init(self):
- self.__data = None
+ """ Mister (monsieur). """
+ MISTER = 1
- def get(self):
- return self.__data
+ """ Miss (madame / mademoiselle). """
+ MISS = 2
+
+class _Name:
+ """ A person's name. """
+
+ def __init__(self, value = None):
+ self.__title = None
+ self.__first = None
+ self.__common = None
+ self.__last = None
- def set(self, value):
if value is None:
- self.__data = None
return
+ if type(value) != str:
+ raise ValueError("Expected a string.")
- self.__data = _FunctionData(value)
+ value = value.split()
- def delete(self):
- self.__data = None
+ # Try to decode the title (if there is one).
-class _OperationTypeProperty(_Property):
- """ Operation type. """
+ fst = value.pop(0)
+ title = fst.upper().replace('.', '')
+ if title == 'M':
+ self.__title = Title.MISTER
+ elif title == 'MME':
+ self.__title = Title.MISS
+ else:
+ self.__title = Title.UNKNOWN
+ value.insert(0, fst)
- def init(self):
- self.__data = None
+ # Get the common name.
- def get(self):
- return self.__data
+ self.__common = ' '.join(value[:-1])
+ self.__first = value[-1]
- def set(self, value):
- if value is None:
- self.__data = None
- return
+ def __repr__(self):
+ p = []
+ ful = str(self)
+ if ful:
+ p.append(f"full = {repr(ful)}")
+ if self.__common is not None and self.__last is not None and \
+ self.__last != self.__common:
+ p.append(f"last = {repr(self.__last)}")
- self.__data = _OperationTypeData(value)
+ return f"{self.__class__.__name__}({', '.join(p)})"
- def delete(self):
- self.__data = None
+ def __str__(self):
+ ops = []
-class _StructureTypeProperty(_Property):
- """ Structure type. """
+ titl = {Title.MISTER: "M.", Title.MISS: "Mme"}.get(self.__title, None)
+ if titl is not None:
+ ops.append(titl)
- def init(self):
- self.__data = None
+ if self.__common is not None:
+ ops.append(self.__common)
+ if self.__first is not None:
+ ops.append(self.__first)
- def get(self):
- return self.__data
+ return ' '.join(ops)
- def set(self, value):
+ @property
+ def title(self):
+ """ Title. """
+
+ return self.__title
+
+ @title.setter
+ def title(self, value):
+ try:
+ self.__title = Title(value)
+ except ValueError:
+ self.__title = Title.UNKNOWN
+
+ @property
+ def first(self):
+ """ First name (prénom). """
+
+ return self.__first
+
+ @property
+ def common(self):
+ """ Common name (nom de famille). """
+
+ return self.__common
+
+ @property
+ def last(self):
+ """ Last name (nom de jeune fille). """
+
+ return self.__last
+
+ @last.setter
+ def last(self, value):
if value is None:
- self.__data = None
+ self.__last = None
return
+ if type(value) != str:
+ raise ValueError("Expected a string.")
- self.__data = _StructureTypeData(value)
+ self.__last = value
- def delete(self):
- self.__data = None
+# ---
+# Specific properties.
+# ---
-class _StructureStatusProperty(_Property):
- """ Structure type. """
+class _FunctionProperty(_Property):
+ """ Function. """
def init(self):
self.__data = None
@@ -97,35 +154,24 @@ class _StructureStatusProperty(_Property):
self.__data = None
return
- self.__data = _StructureStatusData(value)
+ self.__data = _FunctionData(value)
def delete(self):
self.__data = None
-class _OperationFieldsProperty(_Property):
+class _OperationFields:
""" Operation fields. """
- def init(self, lastfield = None, sep = ','):
- pattern = f'([^{sep}\(]*(?P<par>\(([^\(\)]*(?&par)*)*\))*)*'
-
- self.__value = None
- self.__reg = _re.compile(pattern, _re.S)
- self.__sep = sep
-
- def get(self):
- return self.__value
-
- def set(self, value):
- if value is None:
- self.__value = None
- return
-
+ def __init__(self, value, sep = ','):
if type(value) != str:
raise ValueError("fields should be string")
+ pattern = f'([^{sep}\(]*(?P<par>\(([^\(\)]*(?&par)*)*\))*)*'
+ reg = _re.compile(pattern, _re.S)
+
fields = {}
lastfield = None
- for k in self.__reg.finditer(value):
+ for k in reg.finditer(value):
ma = k.group(0)
if not ma:
continue
@@ -143,7 +189,7 @@ class _OperationFieldsProperty(_Property):
if not lastkey:
continue
- fields[lastkey] += self.__sep + ma
+ fields[lastkey] += sep + ma
continue
# New group!
@@ -151,7 +197,33 @@ class _OperationFieldsProperty(_Property):
lastkey = g[0].strip()
fields[lastkey] = ':'.join(g[1:]).lstrip()
- self.__value = fields
+ self.__fields = fields
+
+ def __repr__(self):
+ return f"OperationFields({repr(self.__fields)})"
+
+ def __iter__(self):
+ return iter(self.__fields)
+
+ def __getitem__(self, key):
+ return self.__fields[key]
+
+class _OperationFieldsProperty(_Property):
+ """ Operation fields. """
+
+ def init(self, sep = ','):
+ self.__value = None
+ self.__sep = sep
+
+ def get(self):
+ return self.__value
+
+ def set(self, value):
+ if value is None:
+ self.__value = None
+ return
+
+ self.__value = _OperationFields(value, self.__sep)
def delete(self):
self.__value = None
@@ -164,12 +236,12 @@ class Structure(_Base):
""" A structure (unit, group, territory, …). """
iid = _IIDProperty()
- type = _StructureTypeProperty()
+ type = _ValueProperty(cls = _StructureTypeData)
name = _TextProperty()
code = _TextProperty() # TODO: public code thingy?
# TODO: private code thingy?
description = _TextProperty(not_empty = True)
- status = _StructureStatusProperty()
+ status = _ValueProperty(cls = _StructureStatusData)
hemisphere = _TextProperty()
address = _TextProperty(not_empty = True)
postal_code = _TextProperty()
@@ -189,14 +261,12 @@ class Adherent(_Base):
""" A person who is part of the organization. """
iid = _IIDProperty()
- common_name = _TextProperty()
- last_name = _TextProperty()
- first_name = _TextProperty()
+ name = _ValueProperty(cls = _Name, default = _Name())
# TODO: inscription (code adhérent, structure, fonction, fct 2ndaires,
# statut, début de mandat, fin de mandat).
- function = _FunctionProperty()
+ function = _ValueProperty(cls = _FunctionData)
address = _TextProperty()
postal_code = _TextProperty()
@@ -270,8 +340,8 @@ class Operation(_Base):
""" An event on the website.. """
time = _DateProperty()
- type = _OperationTypeProperty()
- author = _TextProperty()
+ type = _ValueProperty(cls = _OperationTypeData)
+ author = _ObjectProperty(types = (Adherent,), default = Adherent())
fields = _OperationFieldsProperty()
related = _ArrayProperty(types = (Structure, Adherent,
RallyRegistration, Camp, Place))