|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
from benedict.serializers import ( |
|
4
|
|
|
get_serializer_by_format, get_serializers_extensions, ) |
|
5
|
|
|
|
|
6
|
|
|
import errno |
|
7
|
|
|
import os |
|
8
|
|
|
import requests |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
def decode(s, format, **kwargs): |
|
12
|
|
|
serializer = get_serializer_by_format(format) |
|
13
|
|
|
if not serializer: |
|
14
|
|
|
raise ValueError('Invalid format: {}.'.format(format)) |
|
15
|
|
|
decode_opts = kwargs.copy() |
|
16
|
|
|
data = serializer.decode(s.strip(), **decode_opts) |
|
17
|
|
|
return data |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
def encode(d, format, **kwargs): |
|
21
|
|
|
serializer = get_serializer_by_format(format) |
|
22
|
|
|
if not serializer: |
|
23
|
|
|
raise ValueError('Invalid format: {}.'.format(format)) |
|
24
|
|
|
s = serializer.encode(d, **kwargs) |
|
25
|
|
|
return s |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
def is_data(s): |
|
29
|
|
|
return (len(s.splitlines()) > 1) |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
def is_filepath(s): |
|
33
|
|
|
return any([s.endswith(extension) |
|
34
|
|
|
for extension in get_serializers_extensions()]) |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
def is_url(s): |
|
38
|
|
|
return any([s.startswith(protocol) |
|
39
|
|
|
for protocol in ['http://', 'https://']]) |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def read_content(s): |
|
43
|
|
|
# s -> filepath or url or data |
|
44
|
|
|
if is_data(s): |
|
45
|
|
|
# data |
|
46
|
|
|
return s |
|
47
|
|
|
elif is_url(s): |
|
48
|
|
|
# url |
|
49
|
|
|
return read_url(s) |
|
50
|
|
|
elif is_filepath(s): |
|
51
|
|
|
# filepath |
|
52
|
|
|
return read_file(s) |
|
53
|
|
|
# one-line data?! |
|
54
|
|
|
return s |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
def read_file(filepath): |
|
58
|
|
|
if os.path.isfile(filepath): |
|
59
|
|
|
handler = open(filepath, 'r') |
|
60
|
|
|
content = handler.read() |
|
61
|
|
|
handler.close() |
|
62
|
|
|
return content |
|
63
|
|
|
return None |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
def read_url(url, *args, **kwargs): |
|
67
|
|
|
response = requests.get(url, *args, **kwargs) |
|
68
|
|
|
if response.status_code == requests.codes.ok: |
|
69
|
|
|
content = response.text |
|
70
|
|
|
return content |
|
71
|
|
|
raise ValueError( |
|
72
|
|
|
'Invalid url response status code: {}.'.format( |
|
73
|
|
|
response.status_code)) |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
def write_file_dir(filepath): |
|
77
|
|
|
filedir = os.path.dirname(filepath) |
|
78
|
|
|
if os.path.exists(filedir): |
|
79
|
|
|
return |
|
80
|
|
|
try: |
|
81
|
|
|
os.makedirs(filedir) |
|
82
|
|
|
except OSError as e: |
|
83
|
|
|
# Guard against race condition |
|
84
|
|
|
if e.errno != errno.EEXIST: |
|
85
|
|
|
raise e |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
def write_file(filepath, content): |
|
89
|
|
|
# https://stackoverflow.com/questions/12517451/automatically-creating-directories-with-file-output |
|
90
|
|
|
write_file_dir(filepath) |
|
91
|
|
|
handler = open(filepath, 'w+') |
|
92
|
|
|
handler.write(content) |
|
93
|
|
|
handler.close() |
|
94
|
|
|
return True |
|
95
|
|
|
|