|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
from benedict.dicts.io import IODict |
|
4
|
|
|
from benedict.dicts.keypath import KeypathDict |
|
5
|
|
|
from benedict.dicts.parse import ParseDict |
|
6
|
|
|
from benedict.dicts.utility import UtilityDict |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
def benediction(method): |
|
10
|
|
|
def wrapper(*args, **kwargs): |
|
11
|
|
|
value = method(*args, **kwargs) |
|
12
|
|
|
return benedict.cast(value) |
|
13
|
|
|
return wrapper |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
class benedict(IODict, KeypathDict, ParseDict, UtilityDict): |
|
17
|
|
|
|
|
18
|
|
|
def __init__(self, *args, **kwargs): |
|
19
|
|
|
super(benedict, self).__init__(*args, **kwargs) |
|
20
|
|
|
|
|
21
|
|
|
@classmethod |
|
22
|
|
|
def cast(cls, value): |
|
23
|
|
|
if isinstance(value, dict) and not isinstance(value, cls): |
|
24
|
|
|
return cls(value) |
|
25
|
|
|
else: |
|
26
|
|
|
return value |
|
27
|
|
|
|
|
28
|
|
|
@benediction |
|
29
|
|
|
def clone(self): |
|
30
|
|
|
return super(benedict, self).clone() |
|
31
|
|
|
|
|
32
|
|
|
@benediction |
|
33
|
|
|
def copy(self): |
|
34
|
|
|
return super(benedict, self).copy() |
|
35
|
|
|
|
|
36
|
|
|
@benediction |
|
37
|
|
|
def deepcopy(self): |
|
38
|
|
|
return super(benedict, self).deepcopy() |
|
39
|
|
|
|
|
40
|
|
|
@benediction |
|
41
|
|
|
def filter(self, predicate): |
|
42
|
|
|
return super(benedict, self).filter(predicate) |
|
43
|
|
|
|
|
44
|
|
|
@benediction |
|
45
|
|
|
def flatten(self, separator='_'): |
|
46
|
|
|
return super(benedict, self).flatten(separator) |
|
47
|
|
|
|
|
48
|
|
|
@classmethod |
|
49
|
|
|
@benediction |
|
50
|
|
|
def fromkeys(cls, sequence, value=None): |
|
51
|
|
|
return KeypathDict.fromkeys(sequence, value) |
|
52
|
|
|
|
|
53
|
|
|
@staticmethod |
|
54
|
|
|
@benediction |
|
55
|
|
|
def from_json(s, **kwargs): |
|
56
|
|
|
return IODict.from_json(s, **kwargs) |
|
57
|
|
|
|
|
58
|
|
|
@benediction |
|
59
|
|
|
def __getitem__(self, key): |
|
60
|
|
|
return super(benedict, self).__getitem__(key) |
|
61
|
|
|
|
|
62
|
|
|
@benediction |
|
63
|
|
|
def get(self, key, default=None): |
|
64
|
|
|
return super(benedict, self).get(key, default) |
|
65
|
|
|
|
|
66
|
|
|
@benediction |
|
67
|
|
|
def get_dict(self, key, default=None): |
|
68
|
|
|
return super(benedict, self).get_dict(key, default) |
|
69
|
|
|
|
|
70
|
|
|
def get_list(self, key, default=None, separator=','): |
|
71
|
|
|
values = super(benedict, self).get_list(key, default, separator) |
|
72
|
|
|
values = [benedict.cast(value) for value in values] |
|
73
|
|
|
return values |
|
74
|
|
|
|
|
75
|
|
|
@benediction |
|
76
|
|
|
def get_list_item(self, key, index=0, default=None, separator=','): |
|
77
|
|
|
return super(benedict, self).get_list_item( |
|
78
|
|
|
key, index, default, separator) |
|
79
|
|
|
|
|
80
|
|
|
@benediction |
|
81
|
|
|
def get_phonenumber(self, key, country_code=None, default=''): |
|
82
|
|
|
return super(benedict, self).get_phonenumber( |
|
83
|
|
|
key, country_code, default) |
|
84
|
|
|
|
|
85
|
|
|
@benediction |
|
86
|
|
|
def pop(self, key, default=None): |
|
87
|
|
|
return super(benedict, self).pop(key, default) |
|
88
|
|
|
|
|
89
|
|
|
@benediction |
|
90
|
|
|
def setdefault(self, key, default=None): |
|
91
|
|
|
return super(benedict, self).setdefault(key, default) |
|
92
|
|
|
|
|
93
|
|
|
@benediction |
|
94
|
|
|
def subset(self, keys): |
|
95
|
|
|
return super(benedict, self).subset(keys) |
|
96
|
|
|
|