-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathparse_dict.py
More file actions
390 lines (354 loc) · 12.8 KB
/
parse_dict.py
File metadata and controls
390 lines (354 loc) · 12.8 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
from __future__ import annotations
from collections.abc import Callable, Mapping, Sequence
from datetime import date, datetime
from decimal import Decimal
from typing import Any, cast
from typing_extensions import TypeVar
from benedict.dicts.base import BaseDict
from benedict.dicts.parse import parse_util
from benedict.utils import type_util
_K = TypeVar("_K", default=str)
_V = TypeVar("_V", default=Any)
ParserFunc = Callable[..., Any]
class ParseDict(BaseDict[_K, _V]):
def __init__(self, *args: Any, **kwargs: Any) -> None:
"""
Constructs a new instance.
"""
super().__init__(*args, **kwargs)
def _get_value(
self,
key: Any,
default: Any,
choices: Any,
parser_func: ParserFunc,
parserstrwargs: Any = None,
) -> Any:
"""
Get value by key, or keypath core method.
If choices and value is in choices return value otherwise default.
"""
# Get raw value from self.
value = self.get(key, None)
# If value is None return default value.
if value is None:
return default
# If not of the desired type, try to parse it using parser_func.
value = parser_func(value, **(parserstrwargs or {}))
# If value is None after parsing return default value.
if value is None:
return default
# If choices and value in choices return value otherwise default.
if (
type_util.is_list_or_tuple(choices)
and len(choices)
and value not in choices
):
return default
return value
def _get_values_list(
self,
key: _K,
default: list[Any] | None,
separator: str | None,
parser_func: ParserFunc,
parserstrwargs: Any = None,
) -> list[Any]:
"""
Get value by key or keypath trying to return it as list of bool values.
If separator is specified and value is a string it will be splitted.
"""
if key not in self:
return default or []
values_list = self.get_list(key, [], separator)
return [parser_func(value, **(parserstrwargs or {})) for value in values_list]
def get_bool(self, key: _K, default: bool = False) -> bool:
"""
Get value by key or keypath trying to return it as bool.
Values like `1`, `true`, `yes`, `on` will be returned as `True`.
"""
return cast(
"bool", self._get_value(key, default, [True, False], parse_util.parse_bool)
)
def get_bool_list(
self, key: _K, default: list[bool] | None = None, separator: str = ","
) -> list[bool]:
"""
Get value by key or keypath trying to return it as list of bool values.
If separator is specified and value is a string it will be splitted.
"""
return self._get_values_list(key, default, separator, parse_util.parse_bool)
def get_date(
self,
key: _K,
default: date | None = None,
format: str | None = None,
choices: Sequence[date] | None = None,
) -> date | None:
"""
Get value by key or keypath trying to return it as date.
If format is not specified it will be autodetected.
If choices and value is in choices return value otherwise default.
"""
return cast(
"date | None",
self._get_value(
key, default, choices, parse_util.parse_date, {"format": format}
),
)
def get_date_list(
self,
key: _K,
default: Any = None,
format: str | None = None,
separator: str = ",",
) -> list[date]:
"""
Get value by key or keypath trying to return it as list of date values.
If separator is specified and value is a string it will be splitted.
"""
return cast(
"list[date]",
self._get_values_list(
key, default, separator, parse_util.parse_date, {"format": format}
),
)
def get_datetime(
self,
key: _K,
default: datetime | None = None,
format: str | None = None,
choices: Sequence[datetime] | None = None,
) -> datetime | None:
"""
Get value by key or keypath trying to return it as datetime.
If format is not specified it will be autodetected.
If choices and value is in choices return value otherwise default.
"""
return cast(
"datetime | None",
self._get_value(
key, default, choices, parse_util.parse_datetime, {"format": format}
),
)
def get_datetime_list(
self,
key: _K,
default: list[datetime] | None = None,
format: str | None = None,
separator: str = ",",
) -> list[datetime]:
"""
Get value by key or keypath trying to return it as list of datetime values.
If separator is specified and value is a string it will be splitted.
"""
return self._get_values_list(
key, default, separator, parse_util.parse_datetime, {"format": format}
)
def get_decimal(
self,
key: _V,
default: Decimal | None = Decimal("0.0"),
choices: Sequence[Decimal] | None = None,
) -> Decimal: # noqa: B008
"""
Get value by key or keypath trying to return it as Decimal.
If choices and value is in choices return value otherwise default.
"""
return cast(
"Decimal",
self._get_value(key, default, choices, parse_util.parse_decimal),
)
def get_decimal_list(
self, key: _K, default: list[Decimal] | None = None, separator: str = ","
) -> list[Decimal]:
"""
Get value by key or keypath trying to return it as list of Decimal values.
If separator is specified and value is a string it will be splitted.
"""
return cast(
"list[Decimal]",
self._get_values_list(key, default, separator, parse_util.parse_decimal),
)
def get_dict(
self, key: _K, default: Mapping[str, Any] | None = None
) -> dict[_K, Any]:
"""
Get value by key or keypath trying to return it as dict.
If value is a json string it will be automatically decoded.
"""
return cast(
"dict[_K, Any]",
self._get_value(key, default or {}, None, parse_util.parse_dict),
)
def get_email(
self,
key: _K,
default: str = "",
choices: Sequence[str] | None = None,
check_blacklist: bool = True,
) -> str:
"""
Get email by key or keypath and return it.
If value is blacklisted it will be automatically ignored.
If check_blacklist is False, it will be not ignored even if blacklisted.
"""
return cast(
"str",
self._get_value(
key,
default,
choices,
parse_util.parse_email,
{"check_blacklist": check_blacklist},
),
)
def get_float(
self, key: _K, default: float = 0.0, choices: Sequence[_V] | None = None
) -> float:
"""
Get value by key or keypath trying to return it as float.
If choices and value is in choices return value otherwise default.
"""
return cast(
"float",
self._get_value(key, default, choices, parse_util.parse_float),
)
def get_float_list(
self, key: _K, default: list[float] | None = None, separator: str = ","
) -> list[float]:
"""
Get value by key or keypath trying to return it as list of float values.
If separator is specified and value is a string it will be splitted.
"""
return self._get_values_list(key, default, separator, parse_util.parse_float)
def get_int(
self, key: _K, default: int = 0, choices: Sequence[int] | None = None
) -> int:
"""
Get value by key or keypath trying to return it as int.
If choices and value is in choices return value otherwise default.
"""
return cast("int", self._get_value(key, default, choices, parse_util.parse_int))
def get_int_list(
self, key: _K, default: list[int] | None = None, separator: str = ","
) -> list[int] | None:
"""
Get value by key or keypath trying to return it as list of int values.
If separator is specified and value is a string it will be splitted.
"""
return cast(
"list[int]",
self._get_values_list(key, default, separator, parse_util.parse_int),
)
def get_list(
self,
key: _K,
default: Sequence[Any] | None = None,
separator: str | None = ",",
) -> list[Any]:
"""
Get value by key or keypath trying to return it as list.
If separator is specified and value is a string it will be splitted.
"""
return cast(
"list[Any]",
self._get_value(
key,
default or [],
None,
parse_util.parse_list,
{"separator": separator},
),
)
def get_list_item(
self, key: _K, index: int = 0, default: Any = None, separator: str = ","
) -> Any:
"""
Get list by key or keypath and return value at the specified index.
If separator is specified and list value is a string it will be splitted.
"""
values = self.get_list(key, None, separator)
if values:
try:
value = values[index]
return value
except IndexError:
return default
else:
return default
def get_phonenumber(
self,
key: _K,
country_code: str | None = None,
default: dict[str, str] | None = None,
) -> dict[str, str] | None:
"""
Get phonenumber by key or keypath and return a dict
with different formats (e164, international, national).
If country code is specified (alpha 2 code),
it will be used to parse phone number correctly.
"""
return cast(
"dict[str, str]",
self._get_value(
key,
default or {},
None,
parse_util.parse_phonenumber,
{"country_code": country_code},
),
)
def get_slug(
self, key: _K, default: str = "", choices: Sequence[str] | None = None
) -> str:
"""
Get value by key or keypath trying to return it as slug.
If choices and value is in choices return value otherwise default.
"""
return cast(
"str", self._get_value(key, default, choices, parse_util.parse_slug)
)
def get_slug_list(
self, key: _K, default: list[str] | None = None, separator: str = ","
) -> list[str]:
"""
Get value by key or keypath trying to return it as list of slug values.
If separator is specified and value is a string it will be splitted.
"""
return self._get_values_list(key, default, separator, parse_util.parse_slug)
def get_str(
self, key: _K, default: str = "", choices: Sequence[str] | None = None
) -> str:
"""
Get value by key or keypath trying to return it as string.
Encoding issues will be automatically fixed.
If choices and value is in choices return value otherwise default.
"""
return cast("str", self._get_value(key, default, choices, parse_util.parse_str))
def get_str_list(
self, key: _K, default: list[str] | None = None, separator: str = ","
) -> list[str]:
"""
Get value by key or keypath trying to return it as list of str values.
If separator is specified and value is a string it will be splitted.
"""
return self._get_values_list(key, default, separator, parse_util.parse_str)
def get_uuid(
self, key: _K, default: str = "", choices: Sequence[str] | None = None
) -> str:
"""
Get value by key or keypath trying to return it as valid uuid.
If choices and value is in choices return value otherwise default.
"""
return cast(
"str", self._get_value(key, default, choices, parse_util.parse_uuid)
)
def get_uuid_list(
self, key: _K, default: list[str] | None = None, separator: str = ","
) -> list[str]:
"""
Get value by key or keypath trying to return it as list of valid uuid values.
If separator is specified and value is a string it will be splitted.
"""
return self._get_values_list(key, default, separator, parse_util.parse_uuid)