python-benedict/benedict/dicts/io/io_util.py

76 lines
1.6 KiB
Python
Raw Normal View History

2019-09-10 14:58:26 +00:00
# -*- coding: utf-8 -*-
from benedict.serializers import (
2021-10-12 12:27:35 +00:00
get_format_by_path,
get_serializer_by_format,
get_serializers_extensions,
)
import fsutil
def autodetect_format(s):
if is_url(s) or is_filepath(s):
return get_format_by_path(s)
return None
def decode(s, format, **kwargs):
serializer = get_serializer_by_format(format)
if not serializer:
raise ValueError('Invalid format: {}.'.format(format))
decode_opts = kwargs.copy()
data = serializer.decode(s.strip(), **decode_opts)
return data
def encode(d, format, **kwargs):
serializer = get_serializer_by_format(format)
if not serializer:
raise ValueError('Invalid format: {}.'.format(format))
s = serializer.encode(d, **kwargs)
return s
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):
if any([s.endswith(ext) for ext in get_serializers_extensions()]):
return True
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):
# data
return s
2020-02-04 10:28:47 +00:00
elif is_url(s):
# url
return read_url(s)
2020-02-04 10:28:47 +00:00
elif is_filepath(s):
# 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
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
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)