1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
from benedict.serializers import ( |
4
|
|
|
get_format_by_path, |
5
|
|
|
get_serializer_by_format, |
6
|
|
|
get_serializers_extensions, |
7
|
|
|
) |
8
|
|
|
|
9
|
|
|
import fsutil |
10
|
|
|
import tempfile |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
def autodetect_format(s): |
14
|
|
|
if is_url(s) or is_filepath(s): |
15
|
|
|
return get_format_by_path(s) |
16
|
|
|
return None |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
def decode(s, format, **kwargs): |
20
|
|
|
serializer = get_serializer_by_format(format) |
21
|
|
|
if not serializer: |
22
|
|
|
raise ValueError(f"Invalid format: {format}.") |
23
|
|
|
decode_opts = kwargs.copy() |
24
|
|
|
if format in ["b64", "base64"]: |
25
|
|
|
decode_opts.setdefault("subformat", "json") |
26
|
|
|
content = read_content(s, format) |
27
|
|
|
data = serializer.decode(content, **decode_opts) |
28
|
|
|
return data |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
def encode(d, format, **kwargs): |
32
|
|
|
serializer = get_serializer_by_format(format) |
33
|
|
|
if not serializer: |
34
|
|
|
raise ValueError(f"Invalid format: {format}.") |
35
|
|
|
s = serializer.encode(d, **kwargs) |
36
|
|
|
return s |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
def is_binary_format(format): |
40
|
|
|
return format in [ |
41
|
|
|
"xls", |
42
|
|
|
"xlsx", |
43
|
|
|
"xlsm", |
44
|
|
|
] |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
def is_data(s): |
48
|
|
|
return len(s.splitlines()) > 1 |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
def is_filepath(s): |
52
|
|
|
if any([s.endswith(ext) for ext in get_serializers_extensions()]): |
53
|
|
|
return True |
54
|
|
|
return fsutil.is_file(s) |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
def is_url(s): |
58
|
|
|
return any([s.startswith(protocol) for protocol in ["http://", "https://"]]) |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
def read_content(s, format): |
62
|
|
|
# s -> filepath or url or data |
63
|
|
|
s = s.strip() |
64
|
|
|
if is_data(s): |
65
|
|
|
return s |
66
|
|
|
elif is_url(s): |
67
|
|
|
return read_content_from_url(s, format) |
68
|
|
|
elif is_filepath(s): |
69
|
|
|
return read_content_from_file(s, format) |
70
|
|
|
# one-line data?! |
71
|
|
|
return s |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
def read_content_from_file(filepath, format): |
75
|
|
|
binary_format = is_binary_format(format) |
76
|
|
|
if binary_format: |
77
|
|
|
return filepath |
78
|
|
|
return read_file(filepath) |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
def read_content_from_url(url, format, **options): |
82
|
|
|
binary_format = is_binary_format(format) |
83
|
|
|
if binary_format: |
84
|
|
|
dirpath = tempfile.gettempdir() |
85
|
|
|
filepath = fsutil.download_file(url, dirpath, **options) |
86
|
|
|
return filepath |
87
|
|
|
return read_url(url, **options) |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
def read_file(filepath, **options): |
91
|
|
|
return fsutil.read_file(filepath, **options) |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
def read_url(url, **options): |
95
|
|
|
return fsutil.read_file_from_url(url, **options) |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
def write_file(filepath, content, **options): |
99
|
|
|
fsutil.write_file(filepath, content, **options) |
100
|
|
|
|