aboutsummaryrefslogtreecommitdiff
path: root/sgdfi/_dbs/_funcs.py
blob: d70e69a1876de786414d7c4bc9c5049a11be3afe (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env python3
#******************************************************************************
# Copyright (C) 2018 Thomas Touhey <thomas@touhey.fr>
# This file is part of the sgdfi project, which is MIT-licensed.
#******************************************************************************
""" Functions reference for SGDFi. """

from ._funcdata import Function, FunctionData as _FunctionData

__all__ = ["Function", "FunctionData", "FunctionRawData"]

def _isid(value):
	try:
		Function(value)
	except:
		return False
	return True

def _isint(value):
	try:
		int(value)
	except:
		return False
	return True

class FunctionRawData:
	""" Raw data from a function for importation of the autocomplete file
		from the intranet. """

	def __init__(self, iid, code, nm, nf, cnames, srcorder):
		self.__iid = iid
		self.__code = code
		self.__nm = nm
		self.__nf = nf
		self.__cnames = tuple(cnames)
		self.__srcorder = srcorder

	def __repr__(self):
		p = []
		p.append(f"cnames = {repr(self.__cnames)}")
		p.append(f"iid = {repr(self.__iid)}")
		p.append(f"code = {repr(self.__code)}")
		p.append(f"nm = {repr(self.__nm)}")
		if self.__nf is not None:
			p.append(f"nf = {repr(self.__nf)}")

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

	@property
	def cnames(self):
		""" The canonical/constant name for the function. """

		return self.__cnames

	@property
	def iid(self):
		""" The internal identifier (e.g. 8434). """

		return self.__iid

	@property
	def code(self):
		""" The code (e.g. "110", "110M" or "9XX"). """

		return self.__code

	@property
	def name(self):
		""" The full name. """

		p = [self.__nm]
		if self.__nf is not None:
			p.append(self.__nf)
		return " / ".join(p)

	@property
	def nm(self):
		""" The masculine name. """

		return self.__nm

	@property
	def nf(self):
		""" The feminine name. """

		return self.__nf

	def add_cname_suffix(self, suffix):
		cnames = [cn + suffix for cn in self.__cnames]
		return FunctionRawData(self.__iid, self.__code, self.__nm, self.__nf,
			cnames, self.__srcorder)

	@property
	def srcorder(self):
		""" Order in the original JSON. """

		return self.__srcorder

# Each entry in `_FunctionData` has the function identifier as the key, and
# a data composed of the following components:
# - `c`: the intranet code (e.g. "120", "110M", "9XX", …).
# - `ii`: the intranet internal identifier (e.g. "8434", "8437", …).
# - `nm`: the masculin name for the function.
# - `nf` (opt.): the feminine name for the function.
#
# Leads defined in `_FunctionLeads` are:
# - the masculine noun (e.g. "LOUVETEAU").
# - the feminine noun (e.g. "JEANETTE").
# - the integer internal identifier (e.g. 8434).
# - a string version of the last.
# - the code, e.g. "110".
# - integer versions of the last ones.
# - the full masculine string (e.g. "110 (LOUVETEAU)").
# - the full feminine string (e.g. "110 (JEANETTE)").
# - the full string (e.g. "110 (LOUVETEAU / JEANETTE)").

_FunctionLeads = {}
_FunctionLeads.update({nouns[0].strip().casefold(): i for i, (c, ii, *nouns) \
	in _FunctionData.items()})
_FunctionLeads.update({nouns[1].strip().casefold(): i for i, (c, ii, *nouns) \
	in _FunctionData.items() if len(nouns) > 1})
_FunctionLeads.update({ii: i for i, (c, ii, *nouns) in _FunctionData.items()})
_FunctionLeads.update({str(ii): i for i, (c, ii, *nouns) \
	in _FunctionData.items()})
_FunctionLeads.update({c.casefold(): i for i, (c, ii, *nouns) \
	in _FunctionData.items()})
_FunctionLeads.update({int(c): i for i, (c, ii, *nouns) \
	in _FunctionData.items() if _isint(c)})
_FunctionLeads.update({f"{c} ({nouns[0]})".strip().casefold(): i \
	for i, (c, ii, *nouns) in _FunctionData.items()})
_FunctionLeads.update({f"{c} ({nouns[1]})".strip().casefold(): i \
	for i, (c, ii, *nouns) in _FunctionData.items() if len(nouns) > 1})
_FunctionLeads.update({f"{c} ({nouns[0]} / {nouns[1]})".strip().casefold(): i \
	for i, (c, ii, *nouns) in _FunctionData.items() if len(nouns) > 1})

class FunctionData:
	""" Function data (code, id, name, other). """

	def __init__(self, value):
		self.__id   =  None
		self.__iid  = None
		self.__code = None
		self.__nm   = None
		self.__nf   = None
		data = None

		if   isinstance(value, FunctionData):
			self.__id   = value.id
			self.__iid  = value.iid
			self.__code = value.code
			self.__nm   = value.nm
			self.__nf   = value.nf

			if self.__nm == self.__nf:
				self.__nf = None
		elif _isid(value):
			value = Function(value)
			self.__id = value

			data = _FunctionData.get(self.__id, None)
		else:
			# We suppose it's an intranet code such as "110", a private
			# function code such as 8434, or a function name.

			lead = value
			if type(value) == str:
				lead = lead.strip().casefold()

			try:
				self.__id = _FunctionLeads[lead]
				data = _FunctionData.get(self.__id, None)
			except KeyError:
				raise ValueError("Expected a valid function identifier.") \
					from None

		if data is not None:
			self.__code = data[0]
			self.__iid = data[1]
			self.__nm = data[2]
			if len(data) > 2:
				self.__nf = data[3]

	def __repr__(self):
		p = [f"id = {repr(self.__id)}"]
		if self.__code is not None:
			p.append(f"code = {repr(self.__code)}")
		if self.__iid is not None:
			p.append(f"iid = {repr(self.__iid)}")
		if self.__nm is not None:
			p.append(f"nm = {repr(self.__nm)}")
		if self.__nf is not None:
			p.append(f"nf = {repr(self.__nf)}")

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

	@property
	def id(self):
		""" The function identifier, as one of the functions defined in the
			Function enumeration. """

		return self.__id

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

		return self.__iid

	@property
	def code(self):
		""" The code within the intranet. """

		return self.__code

	@property
	def nm(self):
		""" The masculine noun for the function. """

		return self.__nm

	@property
	def nf(self):
		""" The feminine noun for the function. """

		if self.__nf is None:
			return self.__nm
		return self.__nf

# End of file.