aboutsummaryrefslogtreecommitdiff
path: root/sgdfi/_dbs/_st.py
blob: 59020ae35a128e0a760154d2a69b2f100aa136f4 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/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.
#******************************************************************************
""" Structure type reference for SGDFi. """

from enum import Enum as _Enum, unique as _unique

__all__ = ["StructureType", "StructureTypeData"]

@_unique
class StructureType(_Enum):
	""" The default type (unknown). """
	UNKNOWN = 0

	""" Autres. """
	AUTRES = 1

	""" Sommet. """
	SOMMET = 10

	""" Territoire. """
	TERRITOIRE = 11

	""" Groupe. """
	GROUPE = 12

	""" Membres associés national. """
	ASSOCIES_N = 13

	""" Membres associés territorial. """
	ASSOCIES_T = 14

	""" Membres associés local. """
	ASSOCIES_L = 15

	""" Centre national. """
	CENTRE_NATIONAL = 16

	""" Unité farfadets. """
	UNITE_FARFADET = 30

	""" Unité louveteaux-jeanettes. """
	UNITE_8_11_ANS = 31

	""" Unité scouts-guides. """
	UNITE_11_14_ANS = 32

	""" Unité pionniers-caravelles. """
	UNITE_14_17_ANS = 33

	""" Unité compagnons. """
	UNITE_17_20_ANS = 34

	""" Unité Vent du Large. """
	UNITE_VENT_DU_LARGE = 35

# Structure type data:
# - name.
# - internal code.

_StructureTypeData = {
	StructureType.UNKNOWN:             ("Tous", -1),

	StructureType.AUTRES:              ("Autres", 1211),

	StructureType.SOMMET:              ("Sommet", 1207),
	StructureType.TERRITOIRE:          ("Territoire", 1203),
	StructureType.GROUPE:              ("Groupe", 1200),
	StructureType.ASSOCIES_N:          ("Membres associés National", 1206),
	StructureType.ASSOCIES_T:          ("Membres associés Territorial", 1204),
	StructureType.ASSOCIES_L:          ("Membres associés local", 1201),
	StructureType.CENTRE_NATIONAL:     ("Centre National", 1205),

	StructureType.UNITE_FARFADET:      ("Unité Farfadet", 1208),
	StructureType.UNITE_8_11_ANS:      ("Unité 8-11 ans", 1199),
	StructureType.UNITE_11_14_ANS:     ("Unité 11-14 ans", 1212),
	StructureType.UNITE_14_17_ANS:     ("Unité 14-17 ans", 1210),
	StructureType.UNITE_17_20_ANS:     ("Unité 17-20 ans", 1209),
	StructureType.UNITE_VENT_DU_LARGE: ("Unité Vent du Large", 1202),
}

_StructureTypeLeads = {}
_StructureTypeLeads.update({name.strip().casefold(): i for i, (name, ii) \
	in _StructureTypeData.items()})
_StructureTypeLeads.update({ii: i for i, (name, ii) \
	in _StructureTypeData.items()})
_StructureTypeLeads.update({str(ii): i for i, (name, ii) \
	in _StructureTypeData.items()})

class StructureTypeData:
	""" Structure type data (id, name, …). """

	def __init__(self, value):
		def isid(id):
			try:
				StructureType(value)
			except ValueError:
				return False
			return True

		self.__id = StructureType.UNKNOWN
		self.__name = None
		self.__ii = None
		data = None

		if   isinstance(value, StructureTypeData):
			self.__id = value.id
			self.__ii = value.iid
			self.__name = value.name
		elif isid(value):
			value = StructureType(value)
			self.__id = value

			data = _StructureTypeData.get(value, None)
		else:
			lead = value
			if type(lead) == str:
				lead = lead.strip().casefold()

			try:
				self.__id = _StructureTypeLeads[lead]
				data = _StructureTypeData.get(self.__id, None)
			except KeyError:
				if type(value) == str:
					self.__name = value
				else:
					raise ValueError("Could not determine a structure type.") \
						from None

		if data is not None:
			self.__name = data[0]
			self.__ii   = data[1]

	def __repr__(self):
		p = []
		if self.__id is not None:
			p.append(f"id = {repr(self.__id)}")
		if self.__name is not None:
			p.append(f"name = {repr(self.__name)}")
		if self.__ii is not None:
			p.append(f"iid = {repr(self.__ii)}")

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

	@property
	def id(self):
		""" The type identifier, as one of the structure types defined in
			the StructureType enumeration. """

		return self.__id

	@property
	def name(self):
		""" The type name as defined in the structure summary page. """

		return self.__name

	@property
	def iid(self):
		""" The internal identifier on the intranet. """

		return self.__ii

# End of file.