Completed
Push — master ( 9d3bc0...b059cb )
by Oleksandr
01:07
created

DeviceLink.from_ini()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 1
cts 2
cp 0.5
rs 10
cc 1
crap 1.125
1
# coding: utf-8
2
3 1
import zope.interface
4
5 1
from schematics.models import Model
6 1
from schematics.types import StringType, IntType
7 1
from schematics.types.compound import ListType, ModelType
8
9 1
from .interfaces import INISerializable, DefaultProvider
10 1
from .helpers import field_from_ini
11
12
13 1
@zope.interface.implementer(INISerializable)
14 1
@zope.interface.implementer(DefaultProvider)
15 1
class Connection(Model):
16 1
    host = StringType(
17
        default="",
18
        required=True,
19
    )
20 1
    port = IntType(
21
        min_value=0,
22
        max_value=65000,
23
        default=0,
24
        required=True,
25
    )
26 1
    allowed_hosts = ListType(
27
        field=StringType,
28
        min_size=0,
29
        required=True,
30
        min_length=1,
31
    )
32
33 1
    @classmethod
34
    def from_ini(cls, ini):
35
        return cls({
36
            'host': field_from_ini(
37
                cls.host, ini,
38
                'DeviceLink', 'host',
39
            ),
40
            'port': field_from_ini(
41
                cls.port, ini,
42
                'DeviceLink', 'port',
43
            ),
44
            'allowed_hosts': (
45
                field_from_ini(
46
                    cls.allowed_hosts, ini,
47
                    'DeviceLink', 'IPS', default="",
48
                )
49
                .split()
50
            ),
51
        })
52
53 1
    @classmethod
54
    def default(cls):
55
        return cls({
56
            'host': cls.host.default,
57
            'port': cls.port.default,
58
            'allowed_hosts': [],
59
        })
60
61
62 1
@zope.interface.implementer(INISerializable)
63 1
@zope.interface.implementer(DefaultProvider)
64 1
class DeviceLink(Model):
65 1
    connection = ModelType(
66
        model_spec=Connection,
67
        required=True,
68
    )
69
70 1
    @classmethod
71
    def from_ini(cls, ini):
72
        return cls({
73
            'connection': Connection.from_ini(ini),
74
        })
75
76 1
    @classmethod
77
    def default(cls):
78
        return cls({
79
            'connection': Connection.default(),
80
        })
81