aboutsummaryrefslogtreecommitdiff
path: root/sgdfi/_dbs/_ststatus.py
blob: 192c0fe0a7a22f8c50f6e6a51dc46dce0a77bc84 (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
#!/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 status reference for SGDFi. """

from enum import Enum as _Enum, unique as _unique

__all__ = ["StructureStatus", "StructureStatusData"]

@_unique
class StructureStatus(_Enum):
	""" The default status (unknown). """
	UNKNOWN = -1

	""" Ouverte. """
	OPEN = 0

	""" Fermée. """
	CLOSED = 1

	""" Suspendue. """
	SUSPENDED = 2

# Structure status data.
# - name.
# - internal identifier on the intranet.

_StructureStatusData = {
	StructureStatus.UNKNOWN:   ("Tous", -1),

	StructureStatus.OPEN:      ("Ouverte", 0),
	StructureStatus.CLOSED:    ("Fermée", 1),
	StructureStatus.SUSPENDED: ("Suspendue", 2)
}

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

class StructureStatusData:
	""" The structure status data. """

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

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

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

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

			try:
				self.__id = _StructureStatusLeads[lead]
				data = _StructureStatusData.get(self.__id, None)
			except KeyError:
				if type(value) == str:
					self.__name = value
				else:
					msg = "Could not determine a structure status."
					raise ValueError(msg) 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)}")

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

	@property
	def id(self):
		""" The status identifier, as one of the identifiers defined in
			the StructureStatus enumeration. """

		return self.__id

	@property
	def name(self):
		""" The status 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.