Total Complexity | 7 |
Total Lines | 52 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
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 |