2020-01-20 15:19:53 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-10-14 14:53:06 +00:00
|
|
|
import re
|
|
|
|
|
2020-02-03 17:25:54 +00:00
|
|
|
from benedict.serializers.abstract import AbstractSerializer
|
2020-01-20 15:19:53 +00:00
|
|
|
from benedict.serializers.base64 import Base64Serializer
|
|
|
|
from benedict.serializers.csv import CSVSerializer
|
2021-05-04 21:22:11 +00:00
|
|
|
from benedict.serializers.ini import INISerializer
|
2020-01-20 15:19:53 +00:00
|
|
|
from benedict.serializers.json import JSONSerializer
|
2020-02-19 17:14:23 +00:00
|
|
|
from benedict.serializers.pickle import PickleSerializer
|
2020-09-09 14:45:39 +00:00
|
|
|
from benedict.serializers.plist import PListSerializer
|
2020-01-20 15:19:53 +00:00
|
|
|
from benedict.serializers.query_string import QueryStringSerializer
|
|
|
|
from benedict.serializers.toml import TOMLSerializer
|
2022-10-05 14:43:46 +00:00
|
|
|
from benedict.serializers.xls import XLSSerializer
|
2020-01-20 15:19:53 +00:00
|
|
|
from benedict.serializers.xml import XMLSerializer
|
|
|
|
from benedict.serializers.yaml import YAMLSerializer
|
|
|
|
|
2022-03-24 12:26:18 +00:00
|
|
|
__all__ = [
|
|
|
|
"AbstractSerializer",
|
|
|
|
"Base64Serializer",
|
|
|
|
"CSVSerializer",
|
|
|
|
"INISerializer",
|
|
|
|
"JSONSerializer",
|
|
|
|
"PickleSerializer",
|
|
|
|
"PListSerializer",
|
|
|
|
"QueryStringSerializer",
|
|
|
|
"TOMLSerializer",
|
2022-10-05 14:43:46 +00:00
|
|
|
"XLSSerializer",
|
2022-03-24 12:26:18 +00:00
|
|
|
"XMLSerializer",
|
|
|
|
"YAMLSerializer",
|
|
|
|
]
|
|
|
|
|
2020-02-20 16:14:48 +00:00
|
|
|
_BASE64_SERIALIZER = Base64Serializer()
|
|
|
|
_CSV_SERIALIZER = CSVSerializer()
|
2021-05-04 21:22:11 +00:00
|
|
|
_INI_SERIALIZER = INISerializer()
|
2020-02-20 16:14:48 +00:00
|
|
|
_JSON_SERIALIZER = JSONSerializer()
|
|
|
|
_PICKLE_SERIALIZER = PickleSerializer()
|
2020-09-09 14:45:39 +00:00
|
|
|
_PLIST_SERIALIZER = PListSerializer()
|
2020-02-20 16:14:48 +00:00
|
|
|
_QUERY_STRING_SERIALIZER = QueryStringSerializer()
|
|
|
|
_TOML_SERIALIZER = TOMLSerializer()
|
2022-10-05 14:43:46 +00:00
|
|
|
_XLS_SERIALIZER = XLSSerializer()
|
2020-02-20 16:14:48 +00:00
|
|
|
_XML_SERIALIZER = XMLSerializer()
|
2022-10-05 14:43:46 +00:00
|
|
|
_YAML_SERIALIZER = YAMLSerializer()
|
2020-01-20 15:19:53 +00:00
|
|
|
|
2022-10-05 14:43:46 +00:00
|
|
|
_SERIALIZERS_LIST = [
|
|
|
|
_BASE64_SERIALIZER,
|
|
|
|
_CSV_SERIALIZER,
|
|
|
|
_INI_SERIALIZER,
|
|
|
|
_JSON_SERIALIZER,
|
|
|
|
_PICKLE_SERIALIZER,
|
|
|
|
_PLIST_SERIALIZER,
|
|
|
|
_QUERY_STRING_SERIALIZER,
|
|
|
|
_TOML_SERIALIZER,
|
|
|
|
_YAML_SERIALIZER,
|
|
|
|
_XLS_SERIALIZER,
|
|
|
|
_XML_SERIALIZER,
|
|
|
|
]
|
2020-01-20 15:19:53 +00:00
|
|
|
|
2022-10-05 14:43:46 +00:00
|
|
|
_SERIALIZERS_BY_EXTENSION = {}
|
|
|
|
for serializer in _SERIALIZERS_LIST:
|
|
|
|
for extension in serializer.extensions():
|
|
|
|
_SERIALIZERS_BY_EXTENSION[extension] = serializer
|
|
|
|
|
|
|
|
_SERIALIZERS_EXTENSIONS = [
|
|
|
|
f".{extension}" for extension in _SERIALIZERS_BY_EXTENSION.keys()
|
|
|
|
]
|
2020-01-20 15:19:53 +00:00
|
|
|
|
|
|
|
|
2020-03-13 12:54:53 +00:00
|
|
|
def get_format_by_path(path):
|
|
|
|
path = path.lower()
|
|
|
|
for extension in _SERIALIZERS_EXTENSIONS:
|
|
|
|
if path.endswith(extension):
|
|
|
|
return extension[1:]
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2020-01-20 15:19:53 +00:00
|
|
|
def get_serializer_by_format(format):
|
2022-02-13 10:35:43 +00:00
|
|
|
format_key = (format or "").lower().strip()
|
|
|
|
format_key = re.sub(r"[\s\-\_]*", "", format_key)
|
2022-10-05 14:43:46 +00:00
|
|
|
serializer = _SERIALIZERS_BY_EXTENSION.get(format_key, None)
|
|
|
|
return serializer
|