aboutsummaryrefslogtreecommitdiff
path: root/sgdfi/objects/adherentname.py
blob: 206b570f0c7d452ae579ae5a35e1e974de0d134e (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
#******************************************************************************
# Copyright (C) 2018-2021 Thomas Touhey <thomas@touhey.fr>
# This file is part of the sgdfi project, which is MIT-licensed.
#******************************************************************************
""" Représentation d'un nom de personne physique sur l'intranet SGDF. """

from enum import Enum as _Enum, unique as _unique

from .object import DataProperty as _DataProperty

__all__ = ['AdherentNameTitle', 'AdherentName', 'AdherentNameProperty']

@_unique
class AdherentTitle(_Enum):
	""" Titre de civilité de l'adhérent.

		.. data:: UNKNOWN

			Titre de civilité inconnu.

		.. data:: MISTER

			Monsieur.

		.. data:: MISS

			Madame. """

	UNKNOWN = 0
	MISTER = 1
	MISS = 2

class AdherentName:
	""" Représentation d'un nom pour un adhérent. """

	__slots__ = ('_title', '_first', '_common', '_last')

	def __init__(self, value):
		if value is None:
			return None
		if isinstance(value, AdherentName):
			return value
		if type(value) != str:
			raise ValueError("a string, an AdherentName or None is required")

		# Décodage.

		value = value.split()
		if not value:
			return

		title, fst, common, last = None, None, None, None

		fst = value.pop(0)
		title = fst.upper().replace('.', '')
		if title == 'M':
			title = AdherentTitle.MISTER
		elif title == 'MME':
			title = AdherentTitle.MISS
		else:
			title = AdherentTitle.UNKNOWN
			value.insert(0, fst)

		common = ' '.join(value[:-1])
		last = value[-1]

		self._title = title
		self._first = fst
		self._last = last
		self._common = common

	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)}")

		return f"{self.__class__.__name__}({', '.join(p)})"

	def __bool__(self):
		return bool(str(self))

	def __str__(self):
		ops = []

		titl = {
			AdherentTitle.MISTER: "M.",
			AdherentTitle.MISS: "Mme"
		}.get(self._title, None)

		if titl is not None:
			ops.append(titl)

		if self._common is not None:
			ops.append(self._common)
		if self._first is not None:
			ops.append(self._first)

		return ' '.join(ops)

	@property
	def title(self):
		""" Titre de civilité auprès de l'administration française,
			tel que présent sur la carte d'identité. """

		return self._title

	@property
	def first(self):
		""" Prénom de l'adhérent. """

		return self._first

	@property
	def common(self):
		""" Nom de famille de l'adhérent. """

		return self._common

	@property
	def last(self):
		""" Nom de jeune fille de l'adhérent. """

		return self._last

class AdherentNameProperty(_DataProperty):
	""" Propriété de nom. """

	__slots__ = ()
	cls = AdherentName

# End of file.