2019-09-10 14:58:26 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-01-20 15:19:53 +00:00
|
|
|
from benedict.serializers import (
|
2021-10-12 12:27:35 +00:00
|
|
|
get_format_by_path,
|
|
|
|
get_serializer_by_format,
|
|
|
|
get_serializers_extensions,
|
|
|
|
)
|
2019-10-14 12:43:35 +00:00
|
|
|
|
2020-12-24 15:37:16 +00:00
|
|
|
import fsutil
|
2019-10-14 12:43:35 +00:00
|
|
|
|
|
|
|
|
2020-08-12 10:46:52 +00:00
|
|
|
def autodetect_format(s):
|
|
|
|
if is_url(s) or is_filepath(s):
|
2020-03-13 12:54:53 +00:00
|
|
|
return get_format_by_path(s)
|
2020-08-12 10:46:52 +00:00
|
|
|
return None
|
2020-03-13 12:54:53 +00:00
|
|
|
|
|
|
|
|
2019-11-07 16:45:45 +00:00
|
|
|
def decode(s, format, **kwargs):
|
2020-01-20 15:19:53 +00:00
|
|
|
serializer = get_serializer_by_format(format)
|
2020-01-31 12:02:07 +00:00
|
|
|
if not serializer:
|
|
|
|
raise ValueError('Invalid format: {}.'.format(format))
|
|
|
|
decode_opts = kwargs.copy()
|
|
|
|
data = serializer.decode(s.strip(), **decode_opts)
|
|
|
|
return data
|
2019-11-07 16:45:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def encode(d, format, **kwargs):
|
2020-01-20 15:19:53 +00:00
|
|
|
serializer = get_serializer_by_format(format)
|
2020-01-31 12:02:07 +00:00
|
|
|
if not serializer:
|
|
|
|
raise ValueError('Invalid format: {}.'.format(format))
|
|
|
|
s = serializer.encode(d, **kwargs)
|
|
|
|
return s
|
2019-11-07 16:45:45 +00:00
|
|
|
|
|
|
|
|
2020-02-04 10:28:47 +00:00
|
|
|
def is_data(s):
|
2021-10-12 12:27:35 +00:00
|
|
|
return len(s.splitlines()) > 1
|
2020-02-04 10:28:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_filepath(s):
|
2020-08-12 10:46:52 +00:00
|
|
|
if any([s.endswith(ext) for ext in get_serializers_extensions()]):
|
|
|
|
return True
|
2020-12-24 15:37:16 +00:00
|
|
|
return fsutil.is_file(s)
|
2020-02-04 10:28:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_url(s):
|
2021-10-12 12:27:35 +00:00
|
|
|
return any([s.startswith(protocol) for protocol in ['http://', 'https://']])
|
2020-02-04 10:28:47 +00:00
|
|
|
|
|
|
|
|
2019-10-03 16:42:44 +00:00
|
|
|
def read_content(s):
|
|
|
|
# s -> filepath or url or data
|
2020-02-04 10:28:47 +00:00
|
|
|
if is_data(s):
|
2019-11-11 13:38:48 +00:00
|
|
|
# data
|
|
|
|
return s
|
2020-02-04 10:28:47 +00:00
|
|
|
elif is_url(s):
|
2019-11-11 13:38:48 +00:00
|
|
|
# url
|
|
|
|
return read_url(s)
|
2020-02-04 10:28:47 +00:00
|
|
|
elif is_filepath(s):
|
2019-11-11 13:38:48 +00:00
|
|
|
# filepath
|
2020-02-04 10:28:47 +00:00
|
|
|
return read_file(s)
|
|
|
|
# one-line data?!
|
2020-01-30 14:50:01 +00:00
|
|
|
return s
|
2019-10-03 16:42:44 +00:00
|
|
|
|
|
|
|
|
2020-12-24 15:37:16 +00:00
|
|
|
def read_file(filepath, **options):
|
|
|
|
if fsutil.is_file(filepath):
|
|
|
|
return fsutil.read_file(filepath, **options)
|
2020-02-04 10:28:47 +00:00
|
|
|
return None
|
2019-09-10 14:58:26 +00:00
|
|
|
|
|
|
|
|
2020-12-24 15:37:16 +00:00
|
|
|
def read_url(url, **options):
|
|
|
|
return fsutil.read_file_from_url(url, **options)
|
|
|
|
|
|
|
|
|
|
|
|
def write_file(filepath, content, **options):
|
|
|
|
fsutil.write_file(filepath, content, **options)
|