Completed
Push — master ( 7c0f11...307a39 )
by Fabio
03:52
created

benedict.serializers   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 44
dl 0
loc 56
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A get_serializer_by_format() 0 2 1
A get_format_by_path() 0 6 3
A get_serializers_extensions() 0 2 1
1
# -*- coding: utf-8 -*-
2
3
from benedict.serializers.abstract import AbstractSerializer
4
from benedict.serializers.base64 import Base64Serializer
5
from benedict.serializers.csv import CSVSerializer
6
from benedict.serializers.json import JSONSerializer
7
from benedict.serializers.pickle import PickleSerializer
8
from benedict.serializers.query_string import QueryStringSerializer
9
from benedict.serializers.toml import TOMLSerializer
10
from benedict.serializers.xml import XMLSerializer
11
from benedict.serializers.yaml import YAMLSerializer
12
13
_BASE64_SERIALIZER = Base64Serializer()
14
_CSV_SERIALIZER = CSVSerializer()
15
_JSON_SERIALIZER = JSONSerializer()
16
_PICKLE_SERIALIZER = PickleSerializer()
17
_QUERY_STRING_SERIALIZER = QueryStringSerializer()
18
_TOML_SERIALIZER = TOMLSerializer()
19
_YAML_SERIALIZER = YAMLSerializer()
20
_XML_SERIALIZER = XMLSerializer()
21
22
_SERIALIZERS = {
23
    'b64': _BASE64_SERIALIZER,
24
    'base64': _BASE64_SERIALIZER,
25
    'csv': _CSV_SERIALIZER,
26
    'json': _JSON_SERIALIZER,
27
    'pickle': _PICKLE_SERIALIZER,
28
    'qs': _QUERY_STRING_SERIALIZER,
29
    'querystring': _QUERY_STRING_SERIALIZER,
30
    'query-string': _QUERY_STRING_SERIALIZER,
31
    'query_string': _QUERY_STRING_SERIALIZER,
32
    'toml': _TOML_SERIALIZER,
33
    'yaml': _YAML_SERIALIZER,
34
    'yml': _YAML_SERIALIZER,
35
    'xml': _XML_SERIALIZER,
36
}
37
38
_SERIALIZERS_EXTENSIONS = [
39
    '.{}'.format(extension) for extension in _SERIALIZERS.keys()]
40
41
42
def get_format_by_path(path):
43
    path = path.lower()
44
    for extension in _SERIALIZERS_EXTENSIONS:
45
        if path.endswith(extension):
46
            return extension[1:]
47
    return None
48
49
50
def get_serializer_by_format(format):
51
    return _SERIALIZERS.get((format or '').lower().replace(' ', '_'))
52
53
54
def get_serializers_extensions():
55
    return list(_SERIALIZERS_EXTENSIONS)
56