/
core.py
327 lines (268 loc) · 10.2 KB
/
core.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
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
"""
pyserde core module.
"""
import dataclasses
import logging
import re
from dataclasses import dataclass, field
from typing import Any, Callable, Dict, Iterator, List, Optional, Type, Union
import stringcase
from .compat import (
SerdeError,
dataclass_fields,
is_bare_dict,
is_bare_list,
is_bare_set,
is_bare_tuple,
is_dict,
is_list,
is_opt,
is_set,
is_tuple,
is_union,
type_args,
typename,
)
__all__: List = []
logger = logging.getLogger('serde')
# name of the serde context key
SERDE_SCOPE = '__serde__'
# main function keys
FROM_ITER = 'from_iter'
FROM_DICT = 'from_dict'
TO_ITER = 'to_iter'
TO_DICT = 'to_dict'
# prefixes used to distinguish the direction of a union function
UNION_SE_PREFIX = "union_se"
UNION_DE_PREFIX = "union_de"
SETTINGS = dict(debug=False)
def init(debug: bool = False):
SETTINGS['debug'] = debug
@dataclass
class SerdeScope:
cls: Type # the exact class this scope is for (needed to distinguish scopes between inherited classes)
# generated serialize and deserialize functions
funcs: Dict[str, callable] = field(default_factory=dict)
# default values of the dataclass fields (factories & normal values)
defaults: Dict[str, Union[callable, Any]] = field(default_factory=dict)
# type references to all used types within the dataclass
types: Dict[str, Type] = field(default_factory=dict)
# generated source code (only filled when debug is True)
code: Dict[str, str] = field(default_factory=dict)
# the union serializing functions need references to their types
union_se_args: Dict[str, List[Type]] = field(default_factory=dict)
# default values for to_dict & from_dict arguments
reuse_instances_default: bool = True
convert_sets_default: bool = False
def __repr__(self) -> str:
res: List[str] = []
res.append('==================================================')
res.append(self._justify(self.cls.__name__))
res.append('==================================================')
res.append('')
if self.code:
res.append('--------------------------------------------------')
res.append(self._justify('Functions generated by pyserde'))
res.append('--------------------------------------------------')
res.extend([code for code in self.code.values()])
res.append('')
if self.funcs:
res.append('--------------------------------------------------')
res.append(self._justify('Function references in scope'))
res.append('--------------------------------------------------')
for k, v in self.funcs.items():
res.append(f'{k}: {v}')
res.append('')
if self.defaults:
res.append('--------------------------------------------------')
res.append(self._justify('Default values for the dataclass fields'))
res.append('--------------------------------------------------')
for k, v in self.defaults.items():
res.append(f'{k}: {v}')
res.append('')
if self.types:
res.append('--------------------------------------------------')
res.append(self._justify('Type references in scope'))
res.append('--------------------------------------------------')
for k, v in self.types.items():
res.append(f'{k}: {v}')
res.append('')
if self.union_se_args:
res.append('--------------------------------------------------')
res.append(self._justify('Type list by used for union serialize functions'))
res.append('--------------------------------------------------')
for k, lst in self.union_se_args.items():
res.append(f'{k}: {[v for v in lst]}')
res.append('')
return '\n'.join(res)
def _justify(self, s: str, length=50) -> str:
white_spaces = int((50 - len(s)) / 2)
return ' ' * (white_spaces if white_spaces > 0 else 0) + s
def raise_unsupported_type(obj):
# needed because we can not render a raise statement everywhere, e.g. as argument
raise SerdeError(f"Unsupported type: {typename(type(obj))}")
def gen(code: str, globals: Dict = None, locals: Dict = None) -> str:
"""
Customized `exec` function.
"""
try:
from black import FileMode, format_str
code = format_str(code, mode=FileMode(line_length=100))
except Exception:
pass
exec(code, globals, locals)
return code
def add_func(serde_scope: SerdeScope, func_name: str, func_code: str, globals: Dict) -> None:
"""
Generate a function and add it to a SerdeScope's `funcs` dictionary.
:param serde_scope: the SerdeScope instance to modify
:param func_name: the name of the function
:param func_code: the source code of the function
:param globals: global variables that should be accessible to the generated function
"""
code = gen(func_code, globals)
serde_scope.funcs[func_name] = globals[func_name]
if SETTINGS['debug']:
serde_scope.code[func_name] = code
def is_instance(obj: Any, typ: Type) -> bool:
if is_opt(typ):
if obj is None:
return True
opt_arg = type_args(typ)[0]
return is_instance(obj, opt_arg)
elif is_union(typ):
for arg in type_args(typ):
if is_instance(obj, arg):
return True
return False
elif is_list(typ):
if not isinstance(obj, list):
return False
if len(obj) == 0 or is_bare_list(typ):
return True
list_arg = type_args(typ)[0]
# for speed reasons we just check the type of the 1st element
return is_instance(obj[0], list_arg)
elif is_set(typ):
if not isinstance(obj, set):
return False
if len(obj) == 0 or is_bare_set(typ):
return True
set_arg = type_args(typ)[0]
# for speed reasons we just check the type of the 1st element
return is_instance(next(iter(obj)), set_arg)
elif is_tuple(typ):
if not isinstance(obj, tuple):
return False
if len(obj) == 0 or is_bare_tuple(typ):
return True
for i, arg in enumerate(type_args(typ)):
if not is_instance(obj[i], arg):
return False
return True
elif is_dict(typ):
if not isinstance(obj, dict):
return False
if len(obj) == 0 or is_bare_dict(typ):
return True
ktyp = type_args(typ)[0]
vtyp = type_args(typ)[1]
for k, v in obj.items():
# for speed reasons we just check the type of the 1st element
return is_instance(k, ktyp) and is_instance(v, vtyp)
return False
else:
return isinstance(obj, typ)
@dataclass
class Func:
"""
Function wrapper that has `mangled` optional field.
pyserde copies every function reference into global scope
for code generation. Mangling function names is needed in
order to avoid name conflict in the global scope when
multiple fields receives `skip_if` attribute.
"""
inner: Callable[[Any], bool]
mangeld: str = ""
def __call__(self, v) -> bool:
return self.inner(v) # type: ignore
@property
def name(self) -> str:
return self.mangeld
def skip_if_false(v):
return not bool(v)
@dataclass
class Field:
"""
Field in pyserde class.
"""
type: Type
name: Optional[str]
default: Any = field(default_factory=dataclasses._MISSING_TYPE)
default_factory: Any = field(default_factory=dataclasses._MISSING_TYPE)
case: Optional[str] = None
rename: Optional[str] = None
skip: Optional[bool] = None
skip_if: Optional[Func] = None
skip_if_false: Optional[bool] = None
@classmethod
def from_dataclass(cls, f: dataclasses.Field) -> 'Field':
skip_if_false_func: Optional[Func] = None
if f.metadata.get('serde_skip_if_false'):
skip_if_false_func = Func(skip_if_false, cls.mangle(f, 'skip_if'))
skip_if: Optional[Func] = None
if f.metadata.get('serde_skip_if'):
func = f.metadata.get('serde_skip_if')
if callable(func):
skip_if = Func(func, cls.mangle(f, 'skip_if'))
return cls(
f.type,
f.name,
default=f.default,
default_factory=f.default_factory, # type: ignore
rename=f.metadata.get('serde_rename'),
skip=f.metadata.get('serde_skip'),
skip_if=skip_if or skip_if_false_func,
)
@staticmethod
def mangle(field: dataclasses.Field, name: str) -> str:
"""
Get mangled name based on field name.
"""
return f'{field.name}_{name}'
@property
def conv_name(self) -> str:
"""
Get an actual field name which `rename` and `rename_all` conversions
are made. Use `name` property to get a field name before conversion.
"""
return conv(self, self.case)
def fields(FieldCls: Type, cls: Type) -> Iterator[Field]:
return iter(FieldCls.from_dataclass(f) for f in dataclass_fields(cls))
def conv(f: Field, case: Optional[str] = None) -> str:
"""
Convert field name.
"""
name = f.name
if case:
casef = getattr(stringcase, case, None)
if not casef:
raise SerdeError(f"Unkown case type: {f.case}. Pass the name of case supported by 'stringcase' package.")
name = casef(name)
if f.rename:
name = f.rename
if name is None:
raise SerdeError('Field name is None.')
return name
def union_func_name(prefix: str, union_args: List[Type]) -> str:
"""
Generate a function name that contains all union types
:param prefix: prefix to distinguish between serializing and deserializing
:param union_args: type arguments of a Union
:return: union function name
>>> from ipaddress import IPv4Address
>>> from typing import List
>>> union_func_name("union_se", [int, List[str], IPv4Address])
'union_se_int_List_str__IPv4Address'
"""
return re.sub(r"[ ,\[\]]+", "_", f"{prefix}_{'_'.join([typename(e) for e in union_args])}")