Completed
Push — master ( b95acc...a5fbcd )
by Oleksandr
01:06
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
10
11
12 1
@zope.interface.implementer(INISerializable)
13 1
class Connection(Model):
14 1
    host = StringType(
15
        default="",
16
        required=True,
17
    )
18 1
    port = IntType(
19
        min_value=0,
20
        max_value=65000,
21
        default=0,
22
        required=True,
23
    )
24 1
    allowed_hosts = ListType(
25
        field=StringType,
26
        min_size=0,
27
        required=True,
28
        min_length=1,
29
    )
30
31 1
    @classmethod
32
    def from_ini(cls, ini):
33
        return cls({
34
            'host': ini.get(
35
                'DeviceLink', 'host',
36
                fallback=cls.host.default,
37
            ),
38
            'port': ini.getint(
39
                'DeviceLink', 'port',
40
                fallback=cls.port.default,
41
            ),
42
            'allowed_hosts': [
43
                x.strip()
44
                for x in
45
                (
46
                    ini
47
                    .get('DeviceLink', 'IPS', fallback="")
48
                    .split()
49
                )
50
            ],
51
        })
52
53
54 1
@zope.interface.implementer(INISerializable)
55 1
class DeviceLink(Model):
56 1
    connection = ModelType(
57
        model_spec=Connection,
58
        required=True,
59
    )
60
61 1
    @classmethod
62
    def from_ini(cls, ini):
63
        return cls({
64
            'connection': Connection.from_ini(ini),
65
        })
66