benedict.serializers.toml.TOMLSerializer.decode()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
import toml
2
3
try:
4
    # python >= 3.11
5
    import tomllib
6
7
    tomllib_available = True
8
except ImportError:
9
    tomllib_available = False
10
11
from benedict.serializers.abstract import AbstractSerializer
12
13
14
class TOMLSerializer(AbstractSerializer):
15
    """
16
    This class describes a toml serializer.
17
    """
18
19
    def __init__(self):
20
        super().__init__(
21
            extensions=[
22
                "toml",
23
            ],
24
        )
25
26
    def decode(self, s, **kwargs):
27
        if tomllib_available:
28
            data = tomllib.loads(s, **kwargs)
29
        else:
30
            data = toml.loads(s, **kwargs)
31
        return data
32
33
    def encode(self, d, **kwargs):
34
        data = toml.dumps(dict(d), **kwargs)
35
        return data
36