-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patharepl_custom_handlers.py
116 lines (95 loc) · 3.85 KB
/
arepl_custom_handlers.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
import re
import datetime
import decimal
from arepl_jsonpickle.handlers import BaseHandler
from io import TextIOWrapper
from types import CodeType, FrameType, GeneratorType
NOT_SERIALIZABLE_MESSAGE = "not serializable by arepl"
class BaseCustomHandler(BaseHandler):
"""wrapper around BaseHandler for convenient unit testing"""
def restore(self, obj):
"""just for unit testing"""
return obj
class DatetimeHandler(BaseCustomHandler):
### better represention of datetime, see https://github.com/jsonpickle/jsonpickle/issues/109 ###
def flatten(self, obj, data):
x = {"date/time": str(obj)}
return x
class DecimalHandler(BaseCustomHandler):
def flatten(self, obj, data):
x = float(obj)
return x
class RegexMatchHandler(BaseCustomHandler):
### better represention of datetime, see https://github.com/jsonpickle/jsonpickle/issues/109 ###
def flatten(self, obj, data):
return {
"py/object": "_sre.SRE_Match",
"match": obj.group(0),
"groups": obj.groups(),
"span": obj.span(),
}
class FrameHandler(BaseCustomHandler):
### better represention of frame, see https://github.com/Almenon/AREPL-backend/issues/26 ###
def flatten(self, obj, data):
if obj is None:
return None
return {
"py/object": "types.FrameType",
"f_back": self.flatten(obj.f_back, data),
"f_builtins": NOT_SERIALIZABLE_MESSAGE,
"f_code": CodeHandler(None).flatten(obj.f_code, data),
"f_globals": NOT_SERIALIZABLE_MESSAGE,
"f_lasti": obj.f_lasti,
"f_lineno": obj.f_lineno,
"f_locals": NOT_SERIALIZABLE_MESSAGE,
}
class CodeHandler(BaseCustomHandler):
### better represention of frame, see https://github.com/Almenon/AREPL-backend/issues/26 ###
def flatten(self, obj, data):
return {
"co_argcount": obj.co_argcount,
"co_code": NOT_SERIALIZABLE_MESSAGE,
"co_cellvars": obj.co_cellvars,
"co_consts": NOT_SERIALIZABLE_MESSAGE,
"co_filename": obj.co_filename,
"co_firstlineno": obj.co_firstlineno,
"co_flags": obj.co_flags,
"co_lnotab": NOT_SERIALIZABLE_MESSAGE,
"co_freevars": NOT_SERIALIZABLE_MESSAGE,
"co_kwonlyargcount": obj.co_kwonlyargcount,
"co_name": obj.co_name,
"co_names": obj.co_names,
"co_nlocals": NOT_SERIALIZABLE_MESSAGE,
"co_stacksize": obj.co_stacksize,
"co_varnames": obj.co_varnames,
}
class GeneratorHandler(BaseCustomHandler):
### to prevent freeze-up when picking infinite generators, see https://github.com/Almenon/AREPL-backend/issues/96 ###
def flatten(self, obj, data):
return {
"py/object": "builtins.generator",
}
class TextIOHandler(BaseHandler):
"""Serialize file descriptors as None because we cannot roundtrip"""
def flatten(self, obj, data):
obj_repr = {
"py/object": "_io.TextIOWrapper",
"write_through": obj.write_through,
"line_buffering": obj.line_buffering,
"errors": obj.errors,
"encoding": obj.encoding,
}
if hasattr(obj, "mode"):
obj_repr["mode"] = obj.mode
return obj_repr
handlers = [
{"type": datetime.date, "handler": DatetimeHandler},
{"type": datetime.time, "handler": DatetimeHandler},
{"type": datetime.datetime, "handler": DatetimeHandler},
{"type": type(re.search("", "")), "handler": RegexMatchHandler},
{"type": FrameType, "handler": FrameHandler},
{"type": CodeType, "handler": CodeHandler},
{"type": decimal.Decimal, "handler": DecimalHandler},
{"type": GeneratorType, "handler": GeneratorHandler},
{"type": TextIOWrapper, "handler": TextIOHandler},
]