-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathparse_util.py
More file actions
344 lines (269 loc) · 8.76 KB
/
parse_util.py
File metadata and controls
344 lines (269 loc) · 8.76 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
from __future__ import annotations
import re
from datetime import date, datetime
from decimal import Decimal, DecimalException
try:
import ftfy
import phonenumbers
from dateutil import parser as date_parser
from MailChecker import MailChecker
from phonenumbers import PhoneNumberFormat, phonenumberutil
parse_installed = True
except ModuleNotFoundError: # pragma: no cover
parse_installed = False
from collections.abc import Callable
from typing import Any, cast
from slugify import slugify
from typing_extensions import TypeIs, overload
from benedict.extras import require_parse
from benedict.serializers import JSONSerializer
from benedict.utils import type_util
CallableIsBool = Callable[[Any], TypeIs[bool]]
CallableIsDecimal = Callable[[Any], TypeIs[Decimal]]
CallableIsDict = Callable[[Any], TypeIs[dict[str, Any]]]
CallableIsListOrTuple = Callable[[Any], TypeIs[list[Any] | tuple[Any, ...]]]
CallableIsFloat = Callable[[Any], TypeIs[float]]
CallableIsInt = Callable[[Any], TypeIs[int]]
CallableIsStr = Callable[[Any], TypeIs[str]]
ParseWithTypeChecker = (
CallableIsBool
| CallableIsDecimal
| CallableIsDict
| CallableIsFloat
| CallableIsInt
| CallableIsListOrTuple
| CallableIsStr
)
ParseWithParser = (
Callable[..., bool | None]
| Callable[..., Decimal | None]
| Callable[..., dict[str, Any] | None]
| Callable[..., float | None]
| Callable[..., int | None]
| Callable[..., list[Any] | tuple[Any, ...] | None]
| Callable[..., str | None]
)
ParseWithInputOutput = (
bool | Decimal | float | dict[str, Any] | list[Any] | str | tuple[Any, ...] | None
)
@overload
def _parse_with(
val: bool | str,
type_checker: CallableIsBool | None,
parser: Callable[..., bool | None],
**kwargs: Any,
) -> bool | None: ...
@overload
def _parse_with(
val: Decimal | str,
type_checker: CallableIsDecimal | None,
parser: Callable[..., Decimal | None],
**kwargs: Any,
) -> Decimal | None: ...
@overload
def _parse_with(
val: str | dict[str, Any],
type_checker: CallableIsDict | None,
parser: Callable[..., dict[str, Any] | None],
**kwargs: Any,
) -> dict[str, Any] | None: ...
@overload
def _parse_with(
val: int | str,
type_checker: CallableIsInt | None,
parser: Callable[..., int | None],
**kwargs: Any,
) -> int | None: ...
@overload
def _parse_with(
val: float | str,
type_checker: CallableIsFloat | None,
parser: Callable[..., float | None],
**kwargs: Any,
) -> float | None: ...
@overload
def _parse_with(
val: list[Any] | tuple[Any, ...] | str,
type_checker: CallableIsListOrTuple | None,
parser: Callable[..., list[Any] | tuple[Any, ...] | None],
**kwargs: Any,
) -> list[Any] | tuple[Any, ...] | None: ...
@overload
def _parse_with(
val: str,
type_checker: CallableIsStr | None,
parser: Callable[..., str | None],
**kwargs: Any,
) -> str | None: ...
def _parse_with( # type: ignore[misc]
val: ParseWithInputOutput,
type_checker: ParseWithTypeChecker,
parser: ParseWithParser,
**kwargs: Any,
) -> ParseWithInputOutput:
if val is None:
return None
if callable(type_checker) and type_checker(val):
return val
s = str(val)
if not len(s):
return None
return parser(s, **kwargs)
def _parse_bool(val: str) -> bool | None:
val = val.lower()
if val in ["1", "true", "yes", "ok", "on"]:
return True
elif val in ["0", "false", "no", "ko", "off"]:
return False
return None
def parse_bool(val: Any) -> bool | None:
return _parse_with(val, type_util.is_bool, _parse_bool)
def parse_date(val: str | datetime, format: str | None = None) -> date | None:
v = parse_datetime(val, format)
if v:
return v.date()
return None
def _parse_datetime_with_format(val: str, format: str | None) -> datetime | None:
try:
return datetime.strptime(val, format or "")
except Exception:
return None
def _parse_datetime_without_format(val: str) -> datetime | None:
try:
return date_parser.parse(val)
except Exception:
return _parse_datetime_from_timestamp(val)
def _parse_datetime_from_timestamp(val: str | int | float) -> datetime | None:
try:
return datetime.fromtimestamp(float(val))
except Exception:
return None
def parse_datetime(val: str | datetime, format: str | None = None) -> datetime | None:
require_parse(installed=parse_installed)
if type_util.is_datetime(val):
return val
s = str(val)
if format:
return _parse_datetime_with_format(s, format)
else:
return _parse_datetime_without_format(s)
def _parse_decimal(val: str) -> Decimal | None:
try:
return Decimal(val)
except (ValueError, DecimalException):
return None
def parse_decimal(val: str) -> Decimal | None:
return _parse_with(val, type_util.is_decimal, _parse_decimal)
def _parse_dict(val: str) -> dict[Any, Any] | None:
serializer = JSONSerializer()
try:
d = serializer.decode(val)
if type_util.is_dict(d):
return d
return None
except Exception:
return None
def parse_dict(val: str | dict[str, Any]) -> dict[Any, Any] | None:
return _parse_with(val, type_util.is_dict, _parse_dict)
def _parse_float(val: str) -> float | None:
try:
return float(val)
except ValueError:
return None
def parse_float(val: str) -> float | None:
return _parse_with(val, type_util.is_float, _parse_float)
def _parse_email(val: str, check_blacklist: bool = True) -> str | None:
val = val.lower()
if check_blacklist:
if not MailChecker.is_valid(val):
return None
else:
if not MailChecker.is_valid_email_format(val):
return None
return val
def parse_email(val: str, check_blacklist: bool = True) -> str | None:
require_parse(installed=parse_installed)
return _parse_with(val, None, _parse_email, check_blacklist=check_blacklist)
def _parse_int(val: str) -> int | None:
try:
return int(val)
except ValueError:
return None
def parse_int(val: int | str) -> int | None:
return _parse_with(val, type_util.is_integer, _parse_int)
def _parse_list(val: str, separator: str | None = None) -> list[Any] | None:
if (
val.startswith("{")
and val.endswith("}")
or val.startswith("[")
and val.endswith("]")
):
try:
serializer = JSONSerializer()
ls: list[Any] | None = serializer.decode(val)
if type_util.is_list(ls):
return ls
return None
except Exception:
pass
if separator:
ls = list(val.split(separator))
return ls
return None
def parse_list(
val: str | tuple[Any, ...] | list[Any], separator: str | None = None
) -> list[Any] | None:
v = _parse_with(val, type_util.is_list_or_tuple, _parse_list, separator=separator)
if type_util.is_list_or_tuple(v):
return list(v)
return v # type: ignore[return-value]
def _parse_phonenumber(
val: str, country_code: str | None = None
) -> dict[str, str] | None:
try:
phone_obj = phonenumbers.parse(val, country_code)
if phonenumbers.is_valid_number(phone_obj):
return {
"e164": phonenumbers.format_number(phone_obj, PhoneNumberFormat.E164),
"international": phonenumbers.format_number(
phone_obj, PhoneNumberFormat.INTERNATIONAL
),
"national": phonenumbers.format_number(
phone_obj, PhoneNumberFormat.NATIONAL
),
}
return None
except phonenumberutil.NumberParseException:
return None
def parse_phonenumber(
val: str, country_code: str | None = None
) -> dict[str, str] | None:
require_parse(installed=parse_installed)
s = parse_str(val)
if not s:
return None
phone_raw = re.sub(r"[^0-9\+]", " ", s)
phone_raw = phone_raw.strip()
if phone_raw.startswith("00"):
phone_raw = "+" + phone_raw[2:]
if country_code and len(country_code) >= 2:
country_code = country_code[0:2].upper()
return _parse_with(phone_raw, None, _parse_phonenumber, country_code=country_code)
def _parse_slug(val: str) -> str:
result: str = slugify(val)
return result
def parse_slug(val: Any) -> str:
s = parse_str(val)
return _parse_slug(s)
def parse_str(val: Any) -> str:
require_parse(installed=parse_installed)
if type_util.is_string(val):
val = ftfy.fix_text(val)
else:
val = str(val)
val = val.strip()
val = " ".join(val.split())
return cast("str", val)
def parse_uuid(val: str) -> str | None:
s = parse_str(val)
return s if type_util.is_uuid(s) else None