yorm.bases.converter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 52
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A Container.create_default() 0 3 1
A Container.format_data() 0 3 1
A Container.to_value() 0 5 1
A Converter.to_data() 0 4 1
A Converter.to_value() 0 4 1
A Converter.create_default() 0 4 1
A Container.update_value() 0 4 1
1
"""Converter classes."""
2
3 1
from abc import ABCMeta, abstractclassmethod, abstractmethod
4 1
import logging
5
6 1
from .. import common
7 1
from . import Mappable
8
9 1
log = logging.getLogger(__name__)
10
11
12 1
class Converter(metaclass=ABCMeta):
13
    """Base class for attribute converters."""
14
15 1
    @abstractclassmethod
16
    def create_default(cls):
17
        """Create a default value for an attribute."""
18 1
        raise NotImplementedError(common.OVERRIDE_MESSAGE)
19
20 1
    @abstractclassmethod
21
    def to_value(cls, data):
22
        """Convert parsed data to an attribute's value."""
23 1
        raise NotImplementedError(common.OVERRIDE_MESSAGE)
24
25 1
    @abstractclassmethod
26
    def to_data(cls, value):
27
        """Convert an attribute to data optimized for dumping."""
28 1
        raise NotImplementedError(common.OVERRIDE_MESSAGE)
29
30
31 1
class Container(Mappable, Converter, metaclass=ABCMeta):
32
    """Base class for mutable attribute converters."""
33
34 1
    @classmethod
35
    def create_default(cls):
36 1
        return cls.__new__(cls)
37
38 1
    @classmethod
39
    def to_value(cls, data):
40 1
        value = cls.create_default()
41 1
        value.update_value(data, auto_track=True)
42 1
        return value
43
44 1
    @abstractmethod
45
    def update_value(self, data, *, auto_track):  # pragma: no cover (abstract method)
46
        """Update the attribute's value from parsed data."""
47
        raise NotImplementedError(common.OVERRIDE_MESSAGE)
48
49 1
    def format_data(self):
50
        """Format the attribute to data optimized for dumping."""
51
        return self.to_data(self)
52