aboutsummaryrefslogtreecommitdiff
path: root/sgdfi/_dbs/_stspe.py
blob: 03bde1737e7c679312edcde9cd4ef7c74e2c1555 (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
#!/usr/bin/env python3
#******************************************************************************
# Copyright (C) 2018 Thomas Touhey <thomas@touhey.fr>
# This file is part of the sgdfi project, which is MIT-licensed.
#******************************************************************************
""" Structure speciality reference for SGDFi. """

from enum import Enum as _Enum, unique as _unique

__all__ = ["StructureSpeciality", "StructureSpecialityData"]

@_unique
class StructureSpeciality(_Enum):
	""" The default speciality (unknown). """
	UNKNOWN = 0

	""" Sans specialité. """
	WITHOUT = 1

	""" Marine. """
	MARINE = 2

	""" Vent du Large. """
	VENT_DU_LARGE = 3

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

_StructureSpecialityData = {
	StructureSpeciality.UNKNOWN:       ("Toutes", -1),

	StructureSpeciality.WITHOUT:       ("sans spécialité", 624),
	StructureSpeciality.MARINE:        ("Marine", 622),
	StructureSpeciality.VENT_DU_LARGE: ("Vent du Large", 623),
}

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

class StructureSpecialityData:
	""" Structure speciality data (id, name, …). """

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

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

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

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

			try:
				self.__id = _StructureSpecialityLeads[lead]
				data = _StructureSpecialityData.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.__iid)}")

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

	@property
	def id(self):
		""" The speciality identifier, as one of the structure types defined in
			the StructureSpeciality 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.