2020-01-20 15:19:53 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
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
|
|
|
|
from benedict.serializers.json import JSONSerializer
|
2020-02-19 17:14:23 +00:00
|
|
|
from benedict.serializers.pickle import PickleSerializer
|
2020-01-20 15:19:53 +00:00
|
|
|
from benedict.serializers.query_string import QueryStringSerializer
|
|
|
|
from benedict.serializers.toml import TOMLSerializer
|
|
|
|
from benedict.serializers.xml import XMLSerializer
|
|
|
|
from benedict.serializers.yaml import YAMLSerializer
|
|
|
|
|
2020-02-20 16:14:48 +00:00
|
|
|
_BASE64_SERIALIZER = Base64Serializer()
|
|
|
|
_CSV_SERIALIZER = CSVSerializer()
|
|
|
|
_JSON_SERIALIZER = JSONSerializer()
|
|
|
|
_PICKLE_SERIALIZER = PickleSerializer()
|
|
|
|
_QUERY_STRING_SERIALIZER = QueryStringSerializer()
|
|
|
|
_TOML_SERIALIZER = TOMLSerializer()
|
|
|
|
_YAML_SERIALIZER = YAMLSerializer()
|
|
|
|
_XML_SERIALIZER = XMLSerializer()
|
2020-01-20 15:19:53 +00:00
|
|
|
|
|
|
|
_SERIALIZERS = {
|
2020-02-20 16:14:48 +00:00
|
|
|
'b64': _BASE64_SERIALIZER,
|
|
|
|
'base64': _BASE64_SERIALIZER,
|
|
|
|
'csv': _CSV_SERIALIZER,
|
|
|
|
'json': _JSON_SERIALIZER,
|
|
|
|
'pickle': _PICKLE_SERIALIZER,
|
|
|
|
'qs': _QUERY_STRING_SERIALIZER,
|
|
|
|
'querystring': _QUERY_STRING_SERIALIZER,
|
|
|
|
'query-string': _QUERY_STRING_SERIALIZER,
|
|
|
|
'query_string': _QUERY_STRING_SERIALIZER,
|
|
|
|
'toml': _TOML_SERIALIZER,
|
|
|
|
'yaml': _YAML_SERIALIZER,
|
|
|
|
'yml': _YAML_SERIALIZER,
|
|
|
|
'xml': _XML_SERIALIZER,
|
2020-01-20 15:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_SERIALIZERS_EXTENSIONS = [
|
|
|
|
'.{}'.format(extension) for extension in _SERIALIZERS.keys()]
|
|
|
|
|
|
|
|
|
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):
|
2020-02-20 16:14:48 +00:00
|
|
|
return _SERIALIZERS.get((format or '').lower().replace(' ', '_'))
|
2020-01-20 15:19:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_serializers_extensions():
|
|
|
|
return list(_SERIALIZERS_EXTENSIONS)
|