-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathoptions.py
300 lines (227 loc) · 9.29 KB
/
options.py
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# vim: ts=4:sw=4:et:
# ----------------------------------------------------------------------
# Copyleft (K), Jose M. Rodriguez-Rosa (a.k.a. Boriel)
#
# This program is Free Software and is released under the terms of
# the GNU General License
# ----------------------------------------------------------------------
import enum
import json
from enum import StrEnum
from typing import Any, Final
from src.api.exception import Error
__all__: Final[tuple[str, ...]] = (
"ANYTYPE",
"Action",
"Option",
"Options",
)
class ANYTYPE:
"""Dummy class to signal any value"""
pass
# ----------------------------------------------------------------------
# Exception for duplicated Options
# ----------------------------------------------------------------------
class DuplicatedOptionError(Error):
def __init__(self, option_name):
self.option = option_name
def __str__(self):
return "Option '%s' already defined" % self.option
class UndefinedOptionError(Error):
def __init__(self, option_name):
self.option = option_name
def __str__(self):
return "Undefined option '%s'" % self.option
class OptionStackUnderflowError(Error):
def __init__(self, option_name):
self.option = option_name
def __str__(self):
return "Cannot pop option '%s'. Option stack is empty" % self.option
class InvalidValueError(ValueError, Error):
def __init__(self, option_name, _type, value):
self.option = option_name
self.value = value
self.type = _type
def __str__(self):
return "Invalid value '%s' for option '%s'. Value type must be '%s'" % (self.value, self.option, self.type)
class InvalidConfigInitialization(Error):
def __init__(self, invalid_value):
self.invalid_value = invalid_value
def __str__(self):
return "Invalid value for config initialization"
class InvalidActionParameterError(Error):
def __init__(self, action, invalid_parameter):
self.invalid_parameter = invalid_parameter
self.action = action
def __str__(self):
return f"Action '{self.action}' does not accept parameter '{self.invalid_parameter}'"
class InvalidActionMissingParameterError(Error):
def __init__(self, action, missing_parameter):
self.missing_parameter = missing_parameter
self.action = action
def __str__(self):
return f"Action '{self.action}' requires parameter '{self.missing_parameter}'"
# ----------------------------------------------------------------------
# This class interfaces an Options Container
# ----------------------------------------------------------------------
class Option:
"""A simple container for options with optional type checking
on vale assignation.
"""
def __init__(self, name: str, type_, value=None, ignore_none=False):
self.name = name
self.type = type_
self.ignore_none = ignore_none
self.__value = None
self.value = value
self.stack: list[Any] = [] # An option stack
@property
def value(self) -> Any:
return self.__value
@value.setter
def value(self, value):
if value is None and self.ignore_none:
return
if value is not None and self.type is not None and not isinstance(value, self.type):
try:
if isinstance(value, str) and self.type is bool:
value = {
"false": False,
"true": True,
"off": False,
"on": True,
"-": False,
"+": True,
"no": False,
"yes": True,
}[value.lower()]
else:
value = self.type(value)
except TypeError:
pass
except ValueError:
pass
except KeyError:
pass
if value is not None and not isinstance(value, self.type):
raise InvalidValueError(self.name, self.type, value)
self.__value = value
def push(self, value=None):
if value is None:
value = self.value
self.stack.append(self.value)
self.value = value
def pop(self) -> Any:
if not self.stack:
raise OptionStackUnderflowError(self.name)
result = self.value
self.value = self.stack.pop()
return result
# ----------------------------------------------------------------------
# Options commands
# ----------------------------------------------------------------------
@enum.unique
class Action(StrEnum):
ADD = "add"
ADD_IF_NOT_DEFINED = "add_if_not_defined"
CLEAR = "clear"
LIST = "list"
@classmethod
def valid(cls, action: str) -> bool:
return action in list(cls)
# ----------------------------------------------------------------------
# This class interfaces an Options Container
# ----------------------------------------------------------------------
class Options:
"""Class to store config options."""
def __init__(self, init_value=None):
self._options: dict[str, Option] = {}
if init_value is not None:
if isinstance(init_value, dict):
self._options = init_value
elif isinstance(init_value, str):
self._options = json.loads(init_value)
else:
raise InvalidConfigInitialization(invalid_value=init_value)
def __add_option(self, name, type_=None, default=None, ignore_none=False):
if name in self._options:
raise DuplicatedOptionError(name)
if type_ is None and default is not None:
type_ = type(default)
elif type_ is ANYTYPE:
type_ = None
self._options[name] = Option(name, type_, default, ignore_none)
def __add_option_if_not_defined(self, name, type_=None, default=None, ignore_none=False):
if name in self._options:
return
self.__add_option(name, type_, default, ignore_none)
def __delattr__(self, item: str):
del self[item]
def __getattr__(self, item: str):
return self[item].value
def __setattr__(self, key: str, value: Any):
if key == "_options":
self.__dict__[key] = value
return
self[key] = value
def __getitem__(self, item: str) -> Option:
if item not in self._options:
raise UndefinedOptionError(option_name=item)
return self._options[item]
def __delitem__(self, item):
if item not in self._options:
raise UndefinedOptionError(item)
del self._options[item]
def __setitem__(self, key: str, value: Any):
if key not in self._options:
raise UndefinedOptionError(option_name=key)
self._options[key].value = value
def __contains__(self, item: str):
return item in self._options
def __call__(self, *args, **kwargs):
"""Multipurpose function.
- With no parameters, returns a dictionary {'option' -> value}
- With a command:
'add', name='xxxx', type_=None, default_value=None <= Creates the option 'xxxx', if_not_defined=False
'reset', clears the container
"""
def check_allowed_args(action: str, kwargs_, allowed_args, required_args=None):
for option in kwargs_.keys():
if option not in allowed_args:
raise InvalidActionParameterError(action, option)
for required_option in required_args or []:
if required_option not in kwargs_:
raise InvalidActionMissingParameterError(action, required_option)
# With no parameters
if not kwargs:
if not args or args == (Action.LIST,):
return dict(self._options.items())
assert args, f"Missing one action of {', '.join(Action)}"
assert len(args) == 1 and Action.valid(args[0]), f"Only one action of {', '.join(Action)} can be specified"
# clear
if args[0] == Action.CLEAR:
check_allowed_args(Action.CLEAR, kwargs, {})
self._options.clear()
return None
# list
if args[0] == Action.LIST:
check_allowed_args(Action.LIST, kwargs, {"options"})
options = set(kwargs["options"])
return {x: y for x, y in self._options.items() if x in options}
if args[0] == Action.ADD:
kwargs["type"] = kwargs.get("type")
kwargs["default"] = kwargs.get("default")
kwargs["ignore_none"] = kwargs.get("ignore_none", False)
check_allowed_args(Action.ADD, kwargs, {"name", "type", "default", "ignore_none"}, ["name"])
kwargs["type_"] = kwargs["type"]
del kwargs["type"]
self.__add_option(**kwargs)
return None
if args[0] == Action.ADD_IF_NOT_DEFINED:
kwargs["type"] = kwargs.get("type")
kwargs["default"] = kwargs.get("default")
kwargs["ignore_none"] = kwargs.get("ignore_none", False)
check_allowed_args(Action.ADD, kwargs, {"name", "type", "default", "ignore_none"}, ["name"])
kwargs["type_"] = kwargs["type"]
del kwargs["type"]
self.__add_option_if_not_defined(**kwargs)