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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
from __future__ import annotations
import random
import secrets
import string
import uuid
from abc import abstractmethod, ABCMeta
from collections.abc import Mapping, Callable
from typing import Any, ClassVar, Literal, TypeVar, Generic, ParamSpec
from .error import CruInternalError, CruException
def _str_case_in(s: str, case: bool, l: list[str]) -> bool:
if case:
return s in l
else:
return s.lower() in [s.lower() for s in l]
T = TypeVar("T")
class CruValueError(CruException):
def __init__(
self,
message: str,
value: Any,
value_type: ValueType | None,
*args,
**kwargs,
):
super().__init__(
message,
*args,
**kwargs,
)
self._value = value
self._value_type = value_type
@property
def value(self) -> Any:
return self._value
@property
def value_type(self) -> ValueType | None:
return self._value_type
class CruValueValidationError(CruValueError):
pass
class CruValueStringConversionError(CruValueError):
pass
# TODO: Continue here tomorrow!
class ValueType(Generic[T], metaclass=ABCMeta):
def __init__(self, name: str, _type: type[T]) -> None:
self._name = name
self._type = _type
@property
def name(self) -> str:
return self._name
@property
def type(self) -> type[T]:
return self._type
def check_value_type(self, value: Any) -> bool:
return isinstance(value, self.type)
def _do_check_value(self, value: Any) -> T:
return value
def check_value(self, value: Any) -> T:
if not isinstance(value, self.type):
raise CruValueValidationError("Value type is wrong.", value, self)
return self._do_check_value(value)
def _do_check_str_format(self, s: str) -> bool | tuple[bool, str]:
raise NotImplementedError()
def check_str_format(self, s: str) -> None:
ok, err = self._do_check_str_format(s)
if ok is None:
raise CruInternalLogicError("_do_check_str_format should not return None.")
if ok:
return
if err is None:
err = "Invalid value str format."
raise ValueStringConvertionError(err, s, value_type=self)
@abstractmethod
def _do_convert_value_to_str(self, value: T) -> str:
raise NotImplementedError()
def convert_value_to_str(self, value: T) -> str:
self.check_value(value)
return self._do_convert_value_to_str(value)
@abstractmethod
def _do_convert_str_to_value(self, s: str) -> T:
raise NotImplementedError()
def convert_str_to_value(self, s: str) -> T:
self.check_str_format(s)
return self._do_convert_str_to_value(s)
def check_value_or_try_convert_from_str(self, value_or_str: Any) -> T:
try:
return self.check_value(value_or_str)
except ValidationError as e:
if isinstance(value_or_str, str):
return self.convert_str_to_value(value_or_str)
else:
raise ValidationError(
"Value is not valid and is not a str.", value_or_str, self, inner=e
)
class TextValueType(ValueType[str]):
def __init__(self) -> None:
super().__init__("text")
def _do_check_str_format(self, s):
return True
def _do_convert_value_to_str(self, value):
return value
def _do_convert_str_to_value(self, s):
return s
class IntegerValueType(ValueType[int]):
def __init__(self) -> None:
super().__init__("integer")
def _do_check_str_format(self, s):
try:
int(s)
return True
except ValueError:
return False
def _do_convert_value_to_str(self, value):
return str(value)
def _do_convert_str_to_value(self, s):
return int(s)
class FloatValueType(ValueType[float]):
def __init__(self) -> None:
super().__init__("float")
def _do_check_str_format(self, s):
try:
float(s)
return True
except ValueError:
return False
def _do_convert_value_to_str(self, value):
return str(value)
def _do_convert_str_to_value(self, s):
return float(s)
class BooleanValueType(ValueType[bool]):
DEFAULT_TRUE_LIST: ClassVar[list[str]] = ["true", "yes", "y", "on", "1"]
DEFAULT_FALSE_LIST: ClassVar[list[str]] = ["false", "no", "n", "off", "0"]
def __init__(
self,
*,
case_sensitive=False,
true_list: None | list[str] = None,
false_list: None | list[str] = None,
) -> None:
super().__init__("boolean")
self._case_sensitive = case_sensitive
self._valid_true_strs: list[str] = (
true_list or BooleanValueType.DEFAULT_TRUE_LIST
)
self._valid_false_strs: list[str] = (
false_list or BooleanValueType.DEFAULT_FALSE_LIST
)
@property
def case_sensitive(self) -> bool:
return self._case_sensitive
@property
def valid_true_strs(self) -> list[str]:
return self._valid_true_strs
@property
def valid_false_strs(self) -> list[str]:
return self._valid_false_strs
@property
def valid_boolean_strs(self) -> list[str]:
return self._valid_true_strs + self._valid_false_strs
def _do_check_str_format(self, s):
if _str_case_in(s, self.case_sensitive, self.valid_boolean_strs):
return True
return (
False,
f"Not a valid boolean string ({ValueType.case_sensitive_to_str(self.case_sensitive)}). Valid string of true: {' '.join(self._valid_true_strs)}. Valid string of false: {' '.join(self._valid_false_strs)}. All is case insensitive.",
)
def _do_convert_value_to_str(self, value):
return "True" if value else "False"
def _do_convert_str_to_value(self, s):
return _str_case_in(s, self.case_sensitive, self._valid_true_strs)
class EnumValueType(ValueType[str]):
def __init__(self, valid_values: list[str], /, case_sensitive=False) -> None:
s = " | ".join([f'"{v}"' for v in valid_values])
self._valid_value_str = f"[ {s} ]"
super().__init__(f"enum{self._valid_value_str}")
self._case_sensitive = case_sensitive
self._valid_values = valid_values
@property
def case_sensitive(self) -> bool:
return self._case_sensitive
@property
def valid_values(self) -> list[str]:
return self._valid_values
def _do_check_value(self, value):
ok, err = self._do_check_str_format(value)
return ok, (value if ok else err)
def _do_check_str_format(self, s):
if _str_case_in(s, self.case_sensitive, self.valid_values):
return True
return (
False,
f"Value is not in valid values ({ValueType.case_sensitive_to_str(self.case_sensitive)}): {self._valid_value_str}",
)
def _do_convert_value_to_str(self, value):
return value
def _do_convert_str_to_value(self, s):
return s
TEXT_VALUE_TYPE = TextValueType()
INTEGER_VALUE_TYPE = IntegerValueType()
BOOLEAN_VALUE_TYPE = BooleanValueType()
P = ParamSpec("P")
class ValueGenerator(Generic[T, P]):
INTERACTIVE_KEY: ClassVar[Literal["interactive"]] = "interactive"
def __init__(
self, f: Callable[P, T], /, attributes: None | Mapping[str, Any] = None
) -> None:
self._f = f
self._attributes = attributes or {}
@property
def f(self) -> Callable[P, T]:
return self._f
@property
def attributes(self) -> Mapping[str, Any]:
return self._attributes
def generate(self, *args, **kwargs) -> T:
return self._f(*args, **kwargs)
def __call__(self, *args, **kwargs):
return self._f(*args, **kwargs)
@property
def interactive(self) -> bool:
return self._attributes.get(ValueGenerator.INTERACTIVE_KEY, False)
@staticmethod
def create_interactive(
f: Callable[P, T],
interactive: bool = True,
/,
attributes: None | Mapping[str, Any] = None,
) -> "ValueGenerator[T, P]":
return ValueGenerator(
f, dict({ValueGenerator.INTERACTIVE_KEY: interactive}, **(attributes or {}))
)
class UuidValueGenerator(ValueGenerator[str, []]):
def __init__(self) -> None:
super().__init__(lambda: str(uuid.uuid4()))
class RandomStringValueGenerator(ValueGenerator[str, []]):
@staticmethod
def _create_generate_ramdom_func(length: int, secure: bool) -> Callable[str, []]:
random_choice = secrets.choice if secure else random.choice
def generate_random_string():
characters = string.ascii_letters + string.digits
random_string = "".join(random_choice(characters) for _ in range(length))
return random_string
return generate_random_string
def __init__(self, length: int, secure: bool) -> None:
super().__init__(
RandomStringValueGenerator._create_generate_ramdom_func(length, secure)
)
UUID_VALUE_GENERATOR = UuidValueGenerator()
|