-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfrappe.py
90 lines (68 loc) · 2.19 KB
/
frappe.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
import json
def as_unicode(text: str, encoding: str = "utf-8") -> str:
"""Convert to unicode if required"""
if isinstance(text, str):
return text
elif text is None:
return ""
elif isinstance(text, bytes):
return str(text, encoding)
else:
return str(text)
def cstr(s, encoding="utf-8"):
return as_unicode(s, encoding)
class _dict(dict):
"""dict like object that exposes keys as attributes"""
__slots__ = ()
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
__setstate__ = dict.update
def __getstate__(self):
return self
def update(self, *args, **kwargs):
"""update and return self -- the missing dict feature in python"""
super().update(*args, **kwargs)
return self
def copy(self):
return _dict(self)
def json_handler(obj):
"""serialize non-serializable data for json"""
from collections.abc import Iterable
from re import Match
if isinstance(obj, (datetime.date, datetime.datetime, datetime.time)):
return str(obj)
elif isinstance(obj, datetime.timedelta):
return format_timedelta(obj)
elif isinstance(obj, decimal.Decimal):
return float(obj)
elif isinstance(obj, LocalProxy):
return str(obj)
elif isinstance(obj, frappe.model.document.BaseDocument):
doc = obj.as_dict(no_nulls=True)
return doc
elif isinstance(obj, Iterable):
return list(obj)
elif isinstance(obj, Match):
return obj.string
elif type(obj) == type or isinstance(obj, Exception):
return repr(obj)
elif callable(obj):
return repr(obj)
else:
raise TypeError(
f"""Object of type {type(obj)} with value of {repr(obj)} is not JSON serializable"""
)
def as_json(obj, indent=1, separators=None) -> str:
# from frappe.utils.response import json_handler
if separators is None:
separators = (",", ": ")
try:
return json.dumps(
obj, indent=indent, sort_keys=True, default=json_handler, separators=separators
)
except TypeError:
# this would break in case the keys are not all os "str" type - as defined in the JSON
# adding this to ensure keys are sorted (expected behaviour)
sorted_obj = dict(sorted(obj.items(), key=lambda kv: str(kv[0])))
return json.dumps(sorted_obj, indent=indent, default=json_handler, separators=separators)