aboutsummaryrefslogtreecommitdiff
path: root/sgdfi/_dbs/_st.py
blob: bf01e2931b5983707d59612d8af6ab0712440501 (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
#!/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

	""" 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é farfadets. """
	UNITE_6_8_ANS = 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

_StructureTypeData = {
	StructureType.UNITE_8_11_ANS:  "Unité 8-11 ans",
	StructureType.UNITE_14_17_ANS: "Unité 14-17 ans",
}

_StructureTypeLeads = {}
_StructureTypeLeads.update({name.strip().casefold(): i for i, name \
	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
		data = None

		if   isinstance(value, StructureTypeData):
			self.__id = value.id
			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

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

		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

# End of file.