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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
from __future__ import annotations
import copy
from collections.abc import Callable, Iterable
from dataclasses import dataclass, field
from typing import Any
from .list import CruUniqueKeyList
from ._type import CruTypeSet
from ._const import CruNotFound, CruUseDefault, CruDontChange
from ._iter import CruIterator
@dataclass
class CruAttr:
name: str
value: Any
description: str | None
@staticmethod
def make(
name: str, value: Any = CruUseDefault.VALUE, description: str | None = None
) -> CruAttr:
return CruAttr(name, value, description)
CruAttrDefaultFactory = Callable[["CruAttrDef"], Any]
CruAttrTransformer = Callable[[Any, "CruAttrDef"], Any]
CruAttrValidator = Callable[[Any, "CruAttrDef"], None]
@dataclass
class CruAttrDef:
name: str
description: str
default_factory: CruAttrDefaultFactory
transformer: CruAttrTransformer
validator: CruAttrValidator
def __init__(
self,
name: str,
description: str,
default_factory: CruAttrDefaultFactory,
transformer: CruAttrTransformer,
validator: CruAttrValidator,
) -> None:
self.name = name
self.description = description
self.default_factory = default_factory
self.transformer = transformer
self.validator = validator
def transform(self, value: Any) -> Any:
if self.transformer is not None:
return self.transformer(value, self)
return value
def validate(self, value: Any, /, force_allow_none: bool = False) -> None:
if force_allow_none is value is None:
return
if self.validator is not None:
self.validator(value, self)
def transform_and_validate(
self, value: Any, /, force_allow_none: bool = False
) -> Any:
value = self.transform(value)
self.validate(value, force_allow_none)
return value
def make_default_value(self) -> Any:
return self.transform_and_validate(self.default_factory(self))
def adopt(self, attr: CruAttr) -> CruAttr:
attr = copy.deepcopy(attr)
if attr.name is None:
attr.name = self.name
elif attr.name != self.name:
raise ValueError(f"Attr name is not match: {attr.name} != {self.name}")
if attr.value is CruUseDefault.VALUE:
attr.value = self.make_default_value()
else:
attr.value = self.transform_and_validate(attr.value)
if attr.description is None:
attr.description = self.description
return attr
def make(
self, value: Any = CruUseDefault.VALUE, description: None | str = None
) -> CruAttr:
value = self.make_default_value() if value is CruUseDefault.VALUE else value
value = self.transform_and_validate(value)
return CruAttr(
self.name,
value,
description if description is not None else self.description,
)
@dataclass
class CruAttrDefBuilder:
name: str
description: str
types: list[type] | None = field(default=None)
allow_none: bool = field(default=False)
default: Any = field(default=CruUseDefault.VALUE)
default_factory: CruAttrDefaultFactory | None = field(default=None)
auto_list: bool = field(default=False)
transformers: list[CruAttrTransformer] = field(default_factory=list)
validators: list[CruAttrValidator] = field(default_factory=list)
override_transformer: CruAttrTransformer | None = field(default=None)
override_validator: CruAttrValidator | None = field(default=None)
build_hook: Callable[[CruAttrDef], None] | None = field(default=None)
def __init__(self, name: str, description: str) -> None:
super().__init__()
self.name = name
self.description = description
def auto_adjust_default(self) -> None:
if self.default is not CruUseDefault.VALUE and self.default is not None:
return
if self.allow_none and self.default is CruUseDefault.VALUE:
self.default = None
if not self.allow_none and self.default is None:
self.default = CruUseDefault.VALUE
if self.auto_list and not self.allow_none:
self.default = []
def with_name(self, name: str | CruDontChange) -> CruAttrDefBuilder:
if name is not CruDontChange.VALUE:
self.name = name
return self
def with_description(
self, default_description: str | CruDontChange
) -> CruAttrDefBuilder:
if default_description is not CruDontChange.VALUE:
self.description = default_description
return self
def with_default(self, default: Any) -> CruAttrDefBuilder:
if default is not CruDontChange.VALUE:
self.default = default
return self
def with_default_factory(
self,
default_factory: CruAttrDefaultFactory | CruDontChange,
) -> CruAttrDefBuilder:
if default_factory is not CruDontChange.VALUE:
self.default_factory = default_factory
return self
def with_types(
self,
types: Iterable[type] | None | CruDontChange,
) -> CruAttrDefBuilder:
if types is not CruDontChange.VALUE:
self.types = None if types is None else list(types)
return self
def with_allow_none(self, allow_none: bool | CruDontChange) -> CruAttrDefBuilder:
if allow_none is not CruDontChange.VALUE:
self.allow_none = allow_none
return self
def with_auto_list(
self, auto_list: bool | CruDontChange = True
) -> CruAttrDefBuilder:
if auto_list is not CruDontChange.VALUE:
self.auto_list = auto_list
return self
def with_constraint(
self,
/,
allow_none: bool | CruDontChange = CruDontChange.VALUE,
types: Iterable[type] | None | CruDontChange = CruDontChange.VALUE,
default: Any = CruDontChange.VALUE,
default_factory: CruAttrDefaultFactory | CruDontChange = CruDontChange.VALUE,
auto_list: bool | CruDontChange = CruDontChange.VALUE,
) -> CruAttrDefBuilder:
return (
self.with_allow_none(allow_none)
.with_types(types)
.with_default(default)
.with_default_factory(default_factory)
.with_auto_list(auto_list)
)
def add_transformer(self, transformer: CruAttrTransformer) -> CruAttrDefBuilder:
self.transformers.append(transformer)
return self
def clear_transformers(self) -> CruAttrDefBuilder:
self.transformers.clear()
return self
def add_validator(self, validator: CruAttrValidator) -> CruAttrDefBuilder:
self.validators.append(validator)
return self
def clear_validators(self) -> CruAttrDefBuilder:
self.validators.clear()
return self
def with_override_transformer(
self, override_transformer: CruAttrTransformer | None | CruDontChange
) -> CruAttrDefBuilder:
if override_transformer is not CruDontChange.VALUE:
self.override_transformer = override_transformer
return self
def with_override_validator(
self, override_validator: CruAttrValidator | None | CruDontChange
) -> CruAttrDefBuilder:
if override_validator is not CruDontChange.VALUE:
self.override_validator = override_validator
return self
def is_valid(self) -> tuple[bool, str]:
if not isinstance(self.name, str):
return False, "Name must be a string!"
if not isinstance(self.description, str):
return False, "Default description must be a string!"
if (
not self.allow_none
and self.default is None
and self.default_factory is None
):
return False, "Default must be set if allow_none is False!"
return True, ""
@staticmethod
def _build(
builder: CruAttrDefBuilder, auto_adjust_default: bool = True
) -> CruAttrDef:
if auto_adjust_default:
builder.auto_adjust_default()
valid, err = builder.is_valid()
if not valid:
raise ValueError(err)
def composed_transformer(value: Any, attr_def: CruAttrDef) -> Any:
def transform_value(single_value: Any) -> Any:
for transformer in builder.transformers:
single_value = transformer(single_value, attr_def)
return single_value
if builder.auto_list:
if not isinstance(value, list):
value = [value]
value = CruIterator(value).transform(transform_value).to_list()
else:
value = transform_value(value)
return value
type_set = None if builder.types is None else CruTypeSet(*builder.types)
def composed_validator(value: Any, attr_def: CruAttrDef):
def validate_value(single_value: Any) -> None:
if type_set is not None:
type_set.check_value(single_value, allow_none=builder.allow_none)
for validator in builder.validators:
validator(single_value, attr_def)
if builder.auto_list:
CruIterator(value).foreach(validate_value)
else:
validate_value(value)
real_transformer = builder.override_transformer or composed_transformer
real_validator = builder.override_validator or composed_validator
default_factory = builder.default_factory
if default_factory is None:
def default_factory(_d):
return copy.deepcopy(builder.default)
d = CruAttrDef(
builder.name,
builder.description,
default_factory,
real_transformer,
real_validator,
)
if builder.build_hook:
builder.build_hook(d)
return d
def build(self, auto_adjust_default=True) -> CruAttrDef:
c = copy.deepcopy(self)
self.build_hook = None
return CruAttrDefBuilder._build(c, auto_adjust_default)
class CruAttrDefRegistry(CruUniqueKeyList[CruAttrDef, str]):
def __init__(self) -> None:
super().__init__(lambda d: d.name)
def make_builder(self, name: str, default_description: str) -> CruAttrDefBuilder:
b = CruAttrDefBuilder(name, default_description)
b.build_hook = lambda a: self.add(a)
return b
def adopt(self, attr: CruAttr) -> CruAttr:
d = self.get(attr.name)
return d.adopt(attr)
class CruAttrTable(CruUniqueKeyList[CruAttr, str]):
def __init__(self, registry: CruAttrDefRegistry) -> None:
self._registry: CruAttrDefRegistry = registry
super().__init__(lambda a: a.name, before_add=registry.adopt)
@property
def registry(self) -> CruAttrDefRegistry:
return self._registry
def get_value_or(self, name: str, fallback: Any = CruNotFound.VALUE) -> Any:
a = self.get_or(name, CruNotFound.VALUE)
if a is CruNotFound.VALUE:
return fallback
return a.value
def get_value(self, name: str) -> Any:
a = self.get(name)
return a.value
def make_attr(
self,
name: str,
value: Any = CruUseDefault.VALUE,
/,
description: str | None = None,
) -> CruAttr:
d = self._registry.get(name)
return d.make(value, description or d.description)
def add_value(
self,
name: str,
value: Any = CruUseDefault.VALUE,
/,
description: str | None = None,
*,
replace: bool = False,
) -> CruAttr:
attr = self.make_attr(name, value, description)
self.add(attr, replace)
return attr
|