|
1
|
|
|
from decimal import Decimal |
|
2
|
|
|
|
|
3
|
|
|
from benedict.dicts.base import BaseDict |
|
4
|
|
|
from benedict.dicts.parse import parse_util |
|
5
|
|
|
from benedict.utils import type_util |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class ParseDict(BaseDict): |
|
9
|
|
|
def __init__(self, *args, **kwargs): |
|
10
|
|
|
""" |
|
11
|
|
|
Constructs a new instance. |
|
12
|
|
|
""" |
|
13
|
|
|
super().__init__(*args, **kwargs) |
|
14
|
|
|
|
|
15
|
|
|
def _get_value(self, key, default, choices, parser_func, parser_kwargs=None): |
|
16
|
|
|
""" |
|
17
|
|
|
Get value by key, or keypath core method. |
|
18
|
|
|
If choices and value is in choices return value otherwise default. |
|
19
|
|
|
""" |
|
20
|
|
|
|
|
21
|
|
|
# Get raw value from self. |
|
22
|
|
|
value = self.get(key, None) |
|
23
|
|
|
# If value is None return default value. |
|
24
|
|
|
if value is None: |
|
25
|
|
|
return default |
|
26
|
|
|
|
|
27
|
|
|
# If not of the desired type, try to parse it using parser_func. |
|
28
|
|
|
value = parser_func(value, **(parser_kwargs or {})) |
|
29
|
|
|
# If value is None after parsing return default value. |
|
30
|
|
|
if value is None: |
|
31
|
|
|
return default |
|
32
|
|
|
|
|
33
|
|
|
# If choices and value in choices return value otherwise default. |
|
34
|
|
|
if ( |
|
35
|
|
|
type_util.is_list_or_tuple(choices) |
|
36
|
|
|
and len(choices) |
|
37
|
|
|
and value not in choices |
|
38
|
|
|
): |
|
39
|
|
|
return default |
|
40
|
|
|
|
|
41
|
|
|
return value |
|
42
|
|
|
|
|
43
|
|
|
def _get_values_list( |
|
44
|
|
|
self, key, default, separator, parser_func, parser_kwargs=None |
|
45
|
|
|
): |
|
46
|
|
|
""" |
|
47
|
|
|
Get value by key or keypath trying to return it as list of bool values. |
|
48
|
|
|
If separator is specified and value is a string it will be splitted. |
|
49
|
|
|
""" |
|
50
|
|
|
if key not in self: |
|
51
|
|
|
return default or [] |
|
52
|
|
|
values_list = self.get_list(key, [], separator) |
|
53
|
|
|
return [parser_func(value, **(parser_kwargs or {})) for value in values_list] |
|
54
|
|
|
|
|
55
|
|
|
def get_bool(self, key, default=False): |
|
56
|
|
|
""" |
|
57
|
|
|
Get value by key or keypath trying to return it as bool. |
|
58
|
|
|
Values like `1`, `true`, `yes`, `on` will be returned as `True`. |
|
59
|
|
|
""" |
|
60
|
|
|
return self._get_value(key, default, [True, False], parse_util.parse_bool) |
|
61
|
|
|
|
|
62
|
|
|
def get_bool_list(self, key, default=None, separator=","): |
|
63
|
|
|
""" |
|
64
|
|
|
Get value by key or keypath trying to return it as list of bool values. |
|
65
|
|
|
If separator is specified and value is a string it will be splitted. |
|
66
|
|
|
""" |
|
67
|
|
|
return self._get_values_list(key, default, separator, parse_util.parse_bool) |
|
68
|
|
|
|
|
69
|
|
|
def get_date(self, key, default=None, format=None, choices=None): |
|
70
|
|
|
""" |
|
71
|
|
|
Get value by key or keypath trying to return it as date. |
|
72
|
|
|
If format is not specified it will be autodetected. |
|
73
|
|
|
If choices and value is in choices return value otherwise default. |
|
74
|
|
|
""" |
|
75
|
|
|
return self._get_value( |
|
76
|
|
|
key, default, choices, parse_util.parse_date, {"format": format} |
|
77
|
|
|
) |
|
78
|
|
|
|
|
79
|
|
|
def get_date_list(self, key, default=None, format=None, separator=","): |
|
80
|
|
|
""" |
|
81
|
|
|
Get value by key or keypath trying to return it as list of date values. |
|
82
|
|
|
If separator is specified and value is a string it will be splitted. |
|
83
|
|
|
""" |
|
84
|
|
|
return self._get_values_list( |
|
85
|
|
|
key, default, separator, parse_util.parse_date, {"format": format} |
|
86
|
|
|
) |
|
87
|
|
|
|
|
88
|
|
|
def get_datetime(self, key, default=None, format=None, choices=None): |
|
89
|
|
|
""" |
|
90
|
|
|
Get value by key or keypath trying to return it as datetime. |
|
91
|
|
|
If format is not specified it will be autodetected. |
|
92
|
|
|
If choices and value is in choices return value otherwise default. |
|
93
|
|
|
""" |
|
94
|
|
|
return self._get_value( |
|
95
|
|
|
key, default, choices, parse_util.parse_datetime, {"format": format} |
|
96
|
|
|
) |
|
97
|
|
|
|
|
98
|
|
|
def get_datetime_list(self, key, default=None, format=None, separator=","): |
|
99
|
|
|
""" |
|
100
|
|
|
Get value by key or keypath trying to return it as list of datetime values. |
|
101
|
|
|
If separator is specified and value is a string it will be splitted. |
|
102
|
|
|
""" |
|
103
|
|
|
return self._get_values_list( |
|
104
|
|
|
key, default, separator, parse_util.parse_datetime, {"format": format} |
|
105
|
|
|
) |
|
106
|
|
|
|
|
107
|
|
|
def get_decimal(self, key, default=Decimal("0.0"), choices=None): |
|
108
|
|
|
""" |
|
109
|
|
|
Get value by key or keypath trying to return it as Decimal. |
|
110
|
|
|
If choices and value is in choices return value otherwise default. |
|
111
|
|
|
""" |
|
112
|
|
|
return self._get_value(key, default, choices, parse_util.parse_decimal) |
|
113
|
|
|
|
|
114
|
|
|
def get_decimal_list(self, key, default=None, separator=","): |
|
115
|
|
|
""" |
|
116
|
|
|
Get value by key or keypath trying to return it as list of Decimal values. |
|
117
|
|
|
If separator is specified and value is a string it will be splitted. |
|
118
|
|
|
""" |
|
119
|
|
|
return self._get_values_list(key, default, separator, parse_util.parse_decimal) |
|
120
|
|
|
|
|
121
|
|
|
def get_dict(self, key, default=None): |
|
122
|
|
|
""" |
|
123
|
|
|
Get value by key or keypath trying to return it as dict. |
|
124
|
|
|
If value is a json string it will be automatically decoded. |
|
125
|
|
|
""" |
|
126
|
|
|
return self._get_value(key, default or {}, None, parse_util.parse_dict) |
|
127
|
|
|
|
|
128
|
|
|
def get_email(self, key, default="", choices=None, check_blacklist=True): |
|
129
|
|
|
""" |
|
130
|
|
|
Get email by key or keypath and return it. |
|
131
|
|
|
If value is blacklisted it will be automatically ignored. |
|
132
|
|
|
If check_blacklist is False, it will be not ignored even if blacklisted. |
|
133
|
|
|
""" |
|
134
|
|
|
return self._get_value( |
|
135
|
|
|
key, |
|
136
|
|
|
default, |
|
137
|
|
|
choices, |
|
138
|
|
|
parse_util.parse_email, |
|
139
|
|
|
{"check_blacklist": check_blacklist}, |
|
140
|
|
|
) |
|
141
|
|
|
|
|
142
|
|
|
def get_float(self, key, default=0.0, choices=None): |
|
143
|
|
|
""" |
|
144
|
|
|
Get value by key or keypath trying to return it as float. |
|
145
|
|
|
If choices and value is in choices return value otherwise default. |
|
146
|
|
|
""" |
|
147
|
|
|
return self._get_value(key, default, choices, parse_util.parse_float) |
|
148
|
|
|
|
|
149
|
|
|
def get_float_list(self, key, default=None, separator=","): |
|
150
|
|
|
""" |
|
151
|
|
|
Get value by key or keypath trying to return it as list of float values. |
|
152
|
|
|
If separator is specified and value is a string it will be splitted. |
|
153
|
|
|
""" |
|
154
|
|
|
return self._get_values_list(key, default, separator, parse_util.parse_float) |
|
155
|
|
|
|
|
156
|
|
|
def get_int(self, key, default=0, choices=None): |
|
157
|
|
|
""" |
|
158
|
|
|
Get value by key or keypath trying to return it as int. |
|
159
|
|
|
If choices and value is in choices return value otherwise default. |
|
160
|
|
|
""" |
|
161
|
|
|
return self._get_value(key, default, choices, parse_util.parse_int) |
|
162
|
|
|
|
|
163
|
|
|
def get_int_list(self, key, default=None, separator=","): |
|
164
|
|
|
""" |
|
165
|
|
|
Get value by key or keypath trying to return it as list of int values. |
|
166
|
|
|
If separator is specified and value is a string it will be splitted. |
|
167
|
|
|
""" |
|
168
|
|
|
return self._get_values_list(key, default, separator, parse_util.parse_int) |
|
169
|
|
|
|
|
170
|
|
|
def get_list(self, key, default=None, separator=","): |
|
171
|
|
|
""" |
|
172
|
|
|
Get value by key or keypath trying to return it as list. |
|
173
|
|
|
If separator is specified and value is a string it will be splitted. |
|
174
|
|
|
""" |
|
175
|
|
|
return self._get_value( |
|
176
|
|
|
key, default or [], None, parse_util.parse_list, {"separator": separator} |
|
177
|
|
|
) |
|
178
|
|
|
|
|
179
|
|
|
def get_list_item(self, key, index=0, default=None, separator=","): |
|
180
|
|
|
""" |
|
181
|
|
|
Get list by key or keypath and return value at the specified index. |
|
182
|
|
|
If separator is specified and list value is a string it will be splitted. |
|
183
|
|
|
""" |
|
184
|
|
|
values = self.get_list(key, None, separator) |
|
185
|
|
|
if values: |
|
186
|
|
|
try: |
|
187
|
|
|
value = values[index] |
|
188
|
|
|
return value |
|
189
|
|
|
except IndexError: |
|
190
|
|
|
return default |
|
191
|
|
|
else: |
|
192
|
|
|
return default |
|
193
|
|
|
|
|
194
|
|
|
def get_phonenumber(self, key, country_code=None, default=None): |
|
195
|
|
|
""" |
|
196
|
|
|
Get phonenumber by key or keypath and return a dict |
|
197
|
|
|
with different formats (e164, international, national). |
|
198
|
|
|
If country code is specified (alpha 2 code), |
|
199
|
|
|
it will be used to parse phone number correctly. |
|
200
|
|
|
""" |
|
201
|
|
|
return self._get_value( |
|
202
|
|
|
key, |
|
203
|
|
|
default or {}, |
|
204
|
|
|
None, |
|
205
|
|
|
parse_util.parse_phonenumber, |
|
206
|
|
|
{"country_code": country_code}, |
|
207
|
|
|
) |
|
208
|
|
|
|
|
209
|
|
|
def get_slug(self, key, default="", choices=None): |
|
210
|
|
|
""" |
|
211
|
|
|
Get value by key or keypath trying to return it as slug. |
|
212
|
|
|
If choices and value is in choices return value otherwise default. |
|
213
|
|
|
""" |
|
214
|
|
|
return self._get_value(key, default, choices, parse_util.parse_slug) |
|
215
|
|
|
|
|
216
|
|
|
def get_slug_list(self, key, default=None, separator=","): |
|
217
|
|
|
""" |
|
218
|
|
|
Get value by key or keypath trying to return it as list of slug values. |
|
219
|
|
|
If separator is specified and value is a string it will be splitted. |
|
220
|
|
|
""" |
|
221
|
|
|
return self._get_values_list(key, default, separator, parse_util.parse_slug) |
|
222
|
|
|
|
|
223
|
|
|
def get_str(self, key, default="", choices=None): |
|
224
|
|
|
""" |
|
225
|
|
|
Get value by key or keypath trying to return it as string. |
|
226
|
|
|
Encoding issues will be automatically fixed. |
|
227
|
|
|
If choices and value is in choices return value otherwise default. |
|
228
|
|
|
""" |
|
229
|
|
|
return self._get_value(key, default, choices, parse_util.parse_str) |
|
230
|
|
|
|
|
231
|
|
|
def get_str_list(self, key, default=None, separator=","): |
|
232
|
|
|
""" |
|
233
|
|
|
Get value by key or keypath trying to return it as list of str values. |
|
234
|
|
|
If separator is specified and value is a string it will be splitted. |
|
235
|
|
|
""" |
|
236
|
|
|
return self._get_values_list(key, default, separator, parse_util.parse_str) |
|
237
|
|
|
|
|
238
|
|
|
def get_uuid(self, key, default="", choices=None): |
|
239
|
|
|
""" |
|
240
|
|
|
Get value by key or keypath trying to return it as valid uuid. |
|
241
|
|
|
If choices and value is in choices return value otherwise default. |
|
242
|
|
|
""" |
|
243
|
|
|
return self._get_value(key, default, choices, parse_util.parse_uuid) |
|
244
|
|
|
|
|
245
|
|
|
def get_uuid_list(self, key, default=None, separator=","): |
|
246
|
|
|
""" |
|
247
|
|
|
Get value by key or keypath trying to return it as list of valid uuid values. |
|
248
|
|
|
If separator is specified and value is a string it will be splitted. |
|
249
|
|
|
""" |
|
250
|
|
|
return self._get_values_list(key, default, separator, parse_util.parse_uuid) |
|
251
|
|
|
|